arrayList没有保存名字?

时间:2014-09-27 07:56:35

标签: java bukkit

我正在为Bukkit创建一个插件。但我需要将PlayerName存储在List中,现在我使用的是ArrayList,但它似乎没有保存它们?

这是我的代码:

public class Menu implements Listener {
private static Menu instance = new Menu();

public static Menu getInstance() {
    return instance;
}

ArrayList<String> inMenu = new ArrayList<String>();

public void menuAdd(Player p) {
    if(inMenu.contains(p.getName())) {return;}
    inMenu.add(p.getName());
     [1]p.sendMessage(inMenu.toString());

}

@EventHandler
  public void onPlayerChat(AsyncPlayerChatEvent e) {
    [2]e.getPlayer().sendMessage(inMenu.toString());
    if(inMenu.contains(e.getPlayer().getName())) {
        if(e.getMessage().contains("appel")) {
            inMenu.remove(e.getPlayer().getName());
        }
        return;
    } else{
        e.getPlayer().sendMessage("Not in list");
    }

  }

在[1]它调试playerName在ArrayList中,但是当我在[2]调试时,列表中没有任何东西?

主:

public class Main extends JavaPlugin implements Listener {

private static Main instance;
private static Plugin plugin;

@Override
public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this, this);


        if(getServer().getPluginManager().getPlugin("Citizens") == null || getServer().getPluginManager().getPlugin("Citizens").isEnabled() == false) {
            System.out.print("Citizens 2.0 not found or not enabled");
            getServer().getPluginManager().disablePlugin(this); 
            return;
        }
        net.citizensnpcs.api.CitizensAPI.getTraitFactory().registerTrait(net.citizensnpcs.api.trait.TraitInfo.create(HorecaTrait.class).withName("Horeca"));

        instance = this;

        plugin = this;

        registerEvents(this, new Menu(), new HorecaTrait());
}

1 个答案:

答案 0 :(得分:0)

您使用menu.getInstance()方法错误。您在new menu()方法中创建了onEnable(),但我猜测(虽然没有证据证明这一点)您正在使用Menu.getInstance().menuAdd()添加它。要解决此问题,请将new menu()中的onEnable()更改为Menu.getInstance()

主要课程改为正常工作:

public class Main extends JavaPlugin implements Listener {

    private static Main instance;
    private static Plugin plugin;

    @Override
    public void onEnable() {
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(this, this);


        if(getServer().getPluginManager().getPlugin("Citizens") == null || getServer().getPluginManager().getPlugin("Citizens").isEnabled() == false) {
            System.out.print("Citizens 2.0 not found or not enabled");
            getServer().getPluginManager().disablePlugin(this);    
            return;
        }
        net.citizensnpcs.api.CitizensAPI.getTraitFactory().registerTrait(net.citizensnpcs.api.trait.TraitInfo.create(HorecaTrait.class).withName("Horeca"));

        instance = this;

        plugin = this;

        registerEvents(this, Menu.getInstance(), new HorecaTrait()); //CHANGED HERE.
    }

作为旁注,您可能不需要instanceplugin,因为Main实际上也是Plugin