这是我的代码:
package meg.zach.d;
import org.apache.logging.log4j.core.jmx.Server;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class Main extends JavaPlugin {
public void onEnable() {
getLogger().info("Plugin Enabled");
}
public void onDisable() {
}
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
Player p = (Player) sender;
if (cmd.getName().equalsIgnoreCase("promote") && sender instanceof Player) {
if (args[0] == "mod") {
for (Player playerToPromote : Bukkit.getServer().getOnlinePlayers()) {
if (playerToPromote.getName().equalsIgnoreCase(args[1])) {
String modN = ChatColor.GRAY + "[Mod] ";
playerToPromote.setDisplayName(modN + playerToPromote.getDisplayName());
String modb = ChatColor.YELLOW + "has been promoted into a ";
String mod = ChatColor.GOLD + "Mod ";
Bukkit.getServer().broadcastMessage(playerToPromote + modb + mod);
}
else if(!(playerToPromote.isOnline())){
p.sendMessage(ChatColor.RED + "player not online");
}
}
}
}
return false;
}
所以我的问题是,当我执行/promote 和 mod 并命名时,它不会更改显示名称或任何内容,而且 getOnlinePlayers
由于某种原因已被弃用。
有谁知道如何解决这个问题?
最佳答案
好吧,我已经将代码重写为如下所示:
package meg.zach.d;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
// Got rid of onEnable(), simply sent a message to show the plugin was
// enabled, but is done automatically by the server
// Got rid of onDisable(), not necessary if empty
Logger log = Logger.getLogger("Minecraft");
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
// The below checks if the sender is a player
if (!(sender instanceof Player)) {
log.info("error message : sender isnt player");
return true;
}
// I can now safely cast sender to type Player
Player p;
if (cmd.getName().equalsIgnoreCase("commandnamehere")) {
// Checking if there are enough arguments
if (args.length != 1) {
// Message to send when there isn't enough arguments
p.sendMessage("Look! Not enough arguments!");
return true;
}
if (args[0] == "lookaspecialthingy") {
// Instead of looping through all the online players, I just try
// to cast the player name to a Player, and check if the object
// is null
Player target = Bukkit.getPlayer(args[0]);
if (target == null) {
// Player is offline
return true;
}
String modN = ChatColor.GRAY + "[Mod] ";
target.setDisplayName(modN + target.getDisplayName());
String modb = ChatColor.YELLOW + " has been promoted into a ";
String mod = ChatColor.GOLD + "Mod!";
Bukkit.getServer().broadcastMessage(target + modb + mod);
}
}
return false;
}
}
请记住,此代码尚未经过测试,但如果您稍微更改一下,它应该可以正常工作。
您还需要在您的plugin.yml 中定义该命令。您可以在此处查看如何创建plugin.yml:http://wiki.bukkit.org/Plugin_YAML
关于Java Minecraft Bukkit 推广插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976177/