gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:53:47 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com