我今天向今天提出了类似的问题,但我需要进一步发展的帮助。
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);
}
接受/拒绝库存:
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>
时,他们会获得一个清单,其中包含一些可以给他们提供套件的物品,我已经做了正确的测试。然后,当他们选择一个工具包时,/duel
命令的目标将获得一个包含接受项目和拒绝项目的库存。
我得到了一切正常工作,目标是获得接受或拒绝的库存,因为我对HashMaps并不熟悉,而且我有点困惑。
答案 0 :(得分:1)
如果你想从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());
}
}
}
}
}