java - Duel 插件,使用 HashMap 发送请求和打开库存

标签 java hashmap bukkit

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

HashMap :

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);

}

接受/拒绝库存:

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> 时,他们将获得一份库存 list ,其中包含一系列可以为他们提供套件的元素,我已经正确完成并测试了这些套件。然后,当他们选择套件时,/duel 的目标命令将获取包含接受项目和拒绝项目的库存。

我已经完成了目标获取接受或拒绝 list 的所有工作,因为我对 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 中项目的顺序。使用发送者作为 key ,而不是使用目标作为 key :

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()); 
                }
            }
        }
    }
}

关于java - Duel 插件,使用 HashMap 发送请求和打开库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590419/

相关文章:

java - 我如何迭代 [[tokens]] 列表并将它们替换为文本框输入?

Java的HashMap冲突解决

java - 读取 HashMap<String, HashMap<Double, Integer>

java - 如何改变活塞方向?

java - 使用随机中心点的同心圆

java - 我可以从 POST 请求返回不同的资源吗?

java - 执行 ant 创建 jar 时出现 zip 大小错误

java - 将 HikariCP 阴影放入 jar 中将不起作用

java - 在 For 循环中等待 Bukkit 任务完成

java - System.console() 在 NetBeans 中给出 NullPointerException