消息发送两次

时间:2015-04-15 19:19:54

标签: java minecraft bukkit

在我的Minecraft服务器中,每当我输入命令togglepvp时,它都会发送两次消息。我看不出有什么问题,或者为什么要发两次。

这是我的代码:

public class Main extends JavaPlugin implements Listener {

public List<String> togglepvp = new ArrayList<String>();
public List<String> pvpLore = new ArrayList<String>();

public void onEnable() {
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    Bukkit.getServer().getPluginManager().registerEvents(new SpawnKit(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new NPC(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new Mobs(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new Mobdrops(this), this);
}

public boolean onCommand(CommandSender sender, Command cmd, String lavel, String args[]) {
    if (cmd.getName().equalsIgnoreCase("toggle")) {

        Player player = (Player) sender;

        Inventory inv = Bukkit.getServer().createInventory(null, 9, "Toggle Menu");
        ItemStack pvp = new ItemStack(Material.EMPTY_MAP);
        ItemMeta pvpMeta = pvp.getItemMeta();
        pvpMeta.setDisplayName(ChatColor.GRAY + "PvP!");
        pvpLore.add(ChatColor.WHITE + "Toggles your PvP so you cannot attack others, "
                + "but they can still attack you.");
        pvpMeta.setLore(pvpLore);
        pvp.setItemMeta(pvpMeta);
        inv.addItem(pvp);
        pvpLore.clear();
        player.openInventory(inv);

    }

    Player player = (Player) sender;

    if (cmd.getName().equalsIgnoreCase("togglepvp")) {

        Player p = (Player) sender;

        if (togglepvp.contains(p.getName())) {
            togglepvp.remove(p.getName());
            p.sendMessage(ChatColor.GREEN + "Your going damage has been disabled.");
        }
        if (!(togglepvp.contains(p.getName()))) {
            togglepvp.add(p.getName());
            p.sendMessage(ChatColor.RED + "Your outgoing damage has been enabled.");
        }

    }

    if (cmd.getName().equalsIgnoreCase("plugins")) {
        player.sendMessage(ChatColor.GRAY + "");
    }

    if (cmd.getName().equalsIgnoreCase("?")) {
        player.sendMessage(ChatColor.GRAY + "Ask an admin/the community to help you!");
    }

    if (cmd.getName().equalsIgnoreCase("help")) {
        player.sendMessage(ChatColor.GRAY + "Ask an admin/the community to help you!");
    }
    return true;
}

@EventHandler
public void onClick(InventoryClickEvent e) {

    Player player = (Player) e.getWhoClicked();

    if (e.getInventory().getTitle().equals("Toggle Menu")) {
        e.setCancelled(true);
        if (e.getCurrentItem().getItemMeta().getDisplayName()
                .equalsIgnoreCase(ChatColor.GRAY + "PvP!")) {
          player.performCommand("togglepvp");
          player.closeInventory();
          if (togglepvp.contains(e.getWhoClicked())) {
              e.getCurrentItem().getItemMeta().getDisplayName()
              .equalsIgnoreCase(ChatColor.GRAY + "PvP! - ENABLED");
        }     
          if (e.getCurrentItem().getItemMeta().getDisplayName()
                  .equalsIgnoreCase(ChatColor.GRAY + "PvP! - ENABLED")) {
              player.performCommand("togglepvp");
              player.closeInventory();
          }
           }
    }

}

@EventHandler
public void onPvP(EntityDamageByEntityEvent e) {
    if ((e.getDamager() instanceof Player) && e.getEntity() instanceof Player) {

        Player p = (Player) e.getEntity();

           if (e.getDamage() <= 0.0D) {
                return;
              }

           if (togglepvp.contains(p.getName()))  {
    e.setDamage(0.0D);
    e.setCancelled(true);
    p.sendMessage(ChatColor.RED + "You cannot damage others while you have your outgoing damage disabled.");
     }
}
  }
}

1 个答案:

答案 0 :(得分:2)

if (!(togglepvp.contains(p.getName()))) {

阻止玩家关闭PvP,因为它们会立即再次启用 该行应以else开头,如

else if (!(togglepvp.contains(p.getName()))) {

只在触发命令时才执行其中一个逻辑分支。