我正在嘗試爲Mincraft Bukkit服務器創建一個插件,並且遇到了一些存儲在字符串數組中的命令的讀取參數問題。這是我的整個插件的代碼。 (這個插件的目的是爲了讓你在遊戲中改變你的名字,我知道這已經被製作出來了,但是我想重新制作插件以供練習)Bukkit字符串沒有正確讀取
Main。 java的
package me.DarkSun159;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin{
Permission makeNicks = new Permission("makeNicks");
@Override
public void onEnable(){
getServer().getPluginManager().registerEvents(new EventListener(this), this);
getServer().getPluginManager().addPermission(makeNicks);
this.getConfig().options().copyDefaults(true);
this.saveConfig();
getLogger().info("+------------------------------------+");
getLogger().info("|DSP's NickName Mod had been enabled.|");
getLogger().info("+------------------------------------+");
}
@Override
public void onDisable(){
this.saveConfig();
getLogger().info("+-------------------------------------+");
getLogger().info("|DSP's NickName Mod had been disabled.|");
getLogger().info("+-------------------------------------+");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
if(cmd.getName().equalsIgnoreCase("nick") && sender instanceof Player){
Player player = (Player)sender;
if(args == null || args.length == 0 || args.length > 1){
messagePlayer.error(player, "Usage:\n /nick <nickname>");
return false;
}
else if(args[0] == "Admin" || args[0] == "admin" || args[0] == "Administrator" || args[0] == "administrator" || args[0] == "Mod" || args[0] == "mod" || args[0] == "Moderator" || args[0] == "moderator"){
messagePlayer.warning(player, "You cannot use " + args[0] + " as you nickname...");
return false;
}
else if(args[0] == "normal"){
this.getConfig().getConfigurationSection(player.getName()).set("", null);
messagePlayer.success(player, "You name has been set back to " + player.getName() + "!");
this.saveConfig();
return true;
}
else{
this.getConfig().addDefault(player.getName(), args[0]);
messagePlayer.success(player, "You nickname has been changed to " + args[0] + "!");
this.saveConfig();
return true;
}
}
return false;
}
}
EventListener.java
package me.DarkSun159;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class EventListener implements Listener{
Main plugin;
public EventListener(Main plugin){
this.plugin = plugin;
}
@EventHandler
public void onSpeech(AsyncPlayerChatEvent e){
Player player = e.getPlayer();
String message;
if(plugin.getConfig().contains(player.getName())){
message = ChatColor.LIGHT_PURPLE + "[" + plugin.getConfig().get(player.getName()) + "] " + ChatColor.RESET + e.getMessage();
for(Player p : Bukkit.getOnlinePlayers()){
p.sendMessage(message);
}
e.setCancelled(true);
}
}
}
現在,大部分的它工作順利(我仍然在發展中,因此它可能看起來不完整),但是當你加載它在你的服務器,它會允許你改變你的名字到我告訴它不讓你的名字(Admin,admin,Mod,mod,Adminis trator,管理員,主持人,主持人,正常)。我已經看過我的代碼一千次了,但我仍然不明白爲什麼它會讓人們將他們的名字改爲禁止的名字。
是的,非常感謝!我來自c + +背景,所以我想這就是爲什麼我認爲這會起作用。謝謝! –
我來自C#背景以學習Java,並且我發現自己能夠使用該語言,但是像這樣的事情讓我感到沮喪。閱讀Oracle的基本教程可以解決這樣的問題,所以我建議你也這樣做。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html – Scruffy