Duel 插件,使用 HashMaps 发送请求和打开库存

Duel Plugin, sending requests and opening inventories with HashMaps

我今天问了一个类似的问题,但我需要进一步开发方面的帮助。

HashMaps:

Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
Map<UUID, UUID> selecting = new HashMap<UUID, UUID>();

命令:

if (cmd.getName().equalsIgnoreCase("duel")) {
        if (!(args.length == 1)) {
            sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
            return true;

        } else if (args.length == 1) {
            Player p = Bukkit.getServer().getPlayer(args[0]);
            if (p != null) {
                if (p.equals(sender)) {
                    sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
                    return true;
                } else {
                    if (duels.containsKey(p) || duels.containsKey(sender)) {
                        sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
                        return true;
                    } else

                        openKitSelector((Player) sender);
                        selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId());

KitSelector库存:

public void openKitSelector(Player p) {
    Inventory selector = Bukkit.createInventory(p, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "KitSelector" + ChatColor.DARK_RED + "]");

    ItemStack diamond = new ItemStack(Material.DIAMOND_SWORD);
    ItemMeta diamondMeta = diamond.getItemMeta();
    diamondMeta.setDisplayName(ChatColor.DARK_AQUA + "Diamond Kit");
    diamond.setItemMeta(diamondMeta);

    ItemStack iron = new ItemStack(Material.IRON_SWORD);
    ItemMeta ironMeta = iron.getItemMeta();
    ironMeta.setDisplayName(ChatColor.DARK_GREEN + "Iron Kit");
    iron.setItemMeta(ironMeta);

    selector.setItem(0, diamond);
    selector.setItem(1, iron);

    p.openInventory(selector);

}

Accept/Deny库存:

private void openGUI(Player player) {
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]");

    ItemStack accept = new ItemStack(Material.EMERALD_BLOCK);
    ItemMeta acceptMeta = accept.getItemMeta();

    ItemStack decline = new ItemStack(Material.REDSTONE_BLOCK);
    ItemMeta declineMeta = decline.getItemMeta();

    acceptMeta.setDisplayName(ChatColor.GREEN + "Accept!");
    accept.setItemMeta(acceptMeta);

    declineMeta.setDisplayName(ChatColor.RED + "Decline!");
    decline.setItemMeta(declineMeta);

    inv.setItem(3, accept);
    inv.setItem(5, decline);

    player.openInventory(inv);
}

所以我需要做的是,当有人输入 /duel <player> 时,他们会得到一份清单,其中包含 select 数量的物品,这些物品会为他们提供工具包,我已经正确地完成了这一点并且测试。然后当他们 select 一个工具包时,/duel 命令的目标将获得一个包含接受项目和拒绝项目的库存。

由于我对 HashMap 没有真正的经验,我有点困惑,因为目标获得了接受或拒绝的清单,所以我已经完成了所有工作。

如果你想从selecting HashMap中获取决斗请求的目标,你可以使用:

UUID id = selecting.get(player.getUniqueId());
Player target = Bukkit.getPlayer(id);

因此,如果您想打开目标的工具包选择器,您可以使用:

//get the UUID of the Player that is being targeted by the Player player
UUID id = selecting.get(player.getUniqueId());

//get the target Player from the UUID above
Player target = Bukkit.getPlayer(id);

//open the kit selector for the target Player above
openKitSelector(target);

要在决斗请求者选择他们的工具包后打开目标的物品栏,您必须监听 InventoryCloseEvent:

@EventHandler
public void inventoryClose(InventoryCloseEvent e){
  //called when a player closes their inventory
}

因此,您的代码可能如下所示:

@EventHandler
public void inventoryClose(InventoryCloseEvent e){
  //check if the inventory is the duel inventory
  if(e.getInventory().getName().equals(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]"){
    //get the player who opened the inventory
    Player p = (Player) e.getWhoClicked();

    //now, check if the selecting HashMap contains the Player above
    if(selecting.containsKey(p.getUniqueId())){
      //get the target of the duel
      Player target = Bukkit.getPlayer(selecting.get(p.getUniqueId()));

      //open the kit selector for the target Player above
      openKitSelector(target);

      //now, remove the target's, and the sender's UUID from the selecting
      //HashMap, to make sure that we don't accidentally open the kit
      //selector for a player who is currently in a duel.
      selecting.remove(p.getUniqueId());

      //make sure the selecting HashMap contains the target's UUID before
      //attempting to remove it
      if(selecting.containsKey(target.getUniqueId())){
        selecting.remove(target.getUniqueId());
      }
    }
  }
}

如果你想确保玩家点击物品栏中的东西,你可以使用 InventoryClickEvent:

@EventHandler
public void inventoryClick(InventoryClickEvent e){
  //called when a player clicks something in a inventory
}

此外,您还必须调换项目在 HashMap 中的顺序。不使用目标作为键,而是使用发送者作为键:

selecting.put(((Player) sender).getUniqueId(), p.getUniqueId());

因此,您的 onCommand 可能如下所示:

if(cmd.getName().equalsIgnoreCase("duel")){
    if(!(args.length == 1)){
        sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
        return true;
    }
    else if (args.length == 1){
        Player p = Bukkit.getServer().getPlayer(args[0]);
        if(p != null){
            if(p.equals(sender)){
               sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
                return true;
            }
            else{
                if(duels.containsKey(p) || duels.containsKey(sender)){
                    sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
                    return true;
                }
                else{
                    openKitSelector((Player) sender);

                    //this line was changed from
                    //selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId())
                    //to the new value, with the sender as the key,
                    //and the target as the value.
                    selecting.put(((Player) sender).getUniqueId(), p.getUniqueId()); 
                }
            }
        }
    }
}