如何在布尔值中调用 InventoryClickEvent?
How do I call a InventoryClickEvent in a boolean?
我首先创建了将要打开的库存:
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;
}
}
所以,我想要发生的是..
当玩家键入 /duel 时,(目标)将在他们面前打开一个库存,可以选择接受或拒绝决斗请求。我已经对此进行了测试,它按预期工作。如果他们拒绝,他们将收到一条消息,告诉他们他们拒绝了,如果他们接受,他们将获得一个套件来决斗。这也按预期工作。但是,我需要能够为 /duel 命令的发送者提供相同的工具包,但我不知道该怎么做。所以...当(目标)接受决斗时,双方玩家都会收到一个工具包,这样他们就可以战斗了。
有没有办法在 onCommand
中调用 onClick
事件?
或者有没有办法从 onClick
事件中联系命令的发送者?
我的 Bukkit 编码一般,但显然还有很多东西要学,所以任何 help/constructive 批评总是有帮助的!
您可以使用 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
或存储他们的用户名。
我首先创建了将要打开的库存:
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;
}
}
所以,我想要发生的是..
当玩家键入 /duel 时,(目标)将在他们面前打开一个库存,可以选择接受或拒绝决斗请求。我已经对此进行了测试,它按预期工作。如果他们拒绝,他们将收到一条消息,告诉他们他们拒绝了,如果他们接受,他们将获得一个套件来决斗。这也按预期工作。但是,我需要能够为 /duel 命令的发送者提供相同的工具包,但我不知道该怎么做。所以...当(目标)接受决斗时,双方玩家都会收到一个工具包,这样他们就可以战斗了。
有没有办法在 onCommand
中调用 onClick
事件?
或者有没有办法从 onClick
事件中联系命令的发送者?
我的 Bukkit 编码一般,但显然还有很多东西要学,所以任何 help/constructive 批评总是有帮助的!
您可以使用 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
或存储他们的用户名。