我首先创建了将要打开的广告资源:
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);
}
然后我创建了将要运行的命令:
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 {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You sent a duel request to " + ChatColor.BLUE + p.getName());
p.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You recieved a duel request from " + ChatColor.BLUE + sender.getName());
openGUI(p);
}
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Player not found!");
}
}
return true;
我然后创建了一个在玩家点击他们的库存时调用的事件。
@SuppressWarnings("deprecation")
@EventHandler
private void onClick(InventoryClickEvent e) {
if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("[DuelRequest]"))
return;
Player player = (Player) e.getWhoClicked();
e.setCancelled(true);
switch (e.getCurrentItem().getType()) {
case EMERALD_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You accepted the request");
player.getInventory().setItem(0, new ItemStack(Material.DIAMOND_SWORD));
player.getInventory().setItem(1, new ItemStack(Material.BOW));
player.getInventory().setItem(2, new ItemStack(Material.GOLDEN_APPLE, 2));
player.getInventory().setItem(3, new ItemStack(Material.ARROW, 32));
player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
break;
case REDSTONE_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You denied the request!");
break;
default:
player.closeInventory();
break;
}
}
所以,我想要发生的是......
当玩家输入/决斗时,(目标)将在他们面前打开一个库存,并选择接受或拒绝决斗请求。我测试了这个,它按预期工作。如果他们拒绝,他们会收到一条消息,告诉他们拒绝了,如果他们接受,他们会得到一个Kit来与他们决斗。这也按预期工作。但是,我需要能够给/ duel命令的发送者提供相同的工具包,但我不知道如何操作。所以......当(目标)接受决斗时,两个玩家都会收到一个工具包,这样他们就可以战斗了。
有没有办法在onClick
内调用onCommand
事件?
或者有没有办法从onClick
事件中联系命令的发件人?
我对Bukkit编码很平均,但显然还有很多东西需要学习,所以任何帮助/建设性批评总是有用的!
答案 0 :(得分:3)
您可以使用HashMap
将发出决斗请求的玩家存储到玩家:
Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
HashMaps
用于存储另一个值的值,它实际上是一大组变量。
当发件人运行命令时,您可以使用:
duels.put(target.getUniqueId(), sender.getUniqueId());
因此,当目标接受决斗时,您可以通过以下方式获取请求的发件人:
duels.get(target.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 {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You sent a duel request to " + ChatColor.BLUE + p.getName());
p.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You recieved a duel request from " + ChatColor.BLUE + sender.getName());
openGUI(p);
//Put the player in the HashMap here
duels.put(p.getUniqueId(), ((Player) sender).getUniqueId());
}
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Player not found!");
}
}
return true;
您的onClick()
可能如下所示:
@SuppressWarnings("deprecation")
@EventHandler
//It's better if you make your events public
//and not private
public void onClick(InventoryClickEvent e) {
if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("[DuelRequest]"))
return;
Player player = (Player) e.getWhoClicked();
e.setCancelled(true);
//get the UUID stored in the duels HashMap for the player's UUID as a key
UUID uuid = duels.get(player.getUniqueId());
//Get the challenger from the UUID above
Player challenger = Bukkit.getPlayer(uuid);
switch (e.getCurrentItem().getType()) {
case EMERALD_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You accepted the request");
player.getInventory().setItem(0, new ItemStack(Material.DIAMOND_SWORD));
player.getInventory().setItem(1, new ItemStack(Material.BOW));
player.getInventory().setItem(2, new ItemStack(Material.GOLDEN_APPLE, 2));
player.getInventory().setItem(3, new ItemStack(Material.ARROW, 32));
player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
//give the kit to the challenger here
//Maybe make a method, giveKit(Player p), to clean up your code
//Then you could run giveKit(challenger) and giveKit(player)
break;
case REDSTONE_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You denied the request!");
//tell the challenger that player denied their duel request
challenger.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + player.getName + " denied your duel request");
break;
default:
player.closeInventory();
break;
}
}
如果您想检查HashMap
中是否存在UUID
,您可以使用duels.containsKey(uuid)
确保永远不会在HashMaps
内存储Player
个对象,因为如果播放器离开服务器,它可能会导致内存泄漏。存储玩家信息的最佳方式是存储他们的UUID
,或存储他们的用户名。