我可以在另一个 GUI 中单独单击项目时更新库存吗?
Can I make the inventory to be updated when clicking the item in another GUI, individually?
我正在为我的跑酷服务器制作 GUI,但我遇到了问题:
(YT link, 可以过期)
如您所见,当我点击下一页时,其他玩家的 GUI 也更新了。
我也试过将 Inventory 的所有者设置为不为 null(如此个别),但它只显示空的 GUI。
我该如何解决这个问题?我搜索了很多并尝试了很多东西,但找不到答案。
// the showed inventory
Inventory basicInv = Bukkit.createInventory(null, 54, "Basic Jumps");
@EventHandler
public void clickInv(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getClickedInventory() == null) return;
if (event.getCurrentItem() == null) return;
if (event.getCurrentItem().getItemMeta() == null) return;
if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(" ")) {
event.setCancelled(true);
}
switch (event.getClickedInventory().getName()) {
case "OneJump Menu":
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(basicName)
&& event.getCurrentItem().getItemMeta().getLore().equals(Collections.singletonList(ChatColor.GRAY + "Includes very historical and original jumps."))) {
player.openInventory(basicInv);
basicInventorySet1(player, basicInv);
event.setCancelled(true);
plugin.getPlayerConfig().set("Player." + player.getUniqueId() + ".PlayingJumpType", "basicjump");
plugin.savePlayerConfig();
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(DisplayName)
|| event.getCurrentItem().getItemMeta().getDisplayName().equals(basicName)) {
event.setCancelled(true);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(" ")) {
event.setCancelled(true);
} else {
event.setCancelled(true);
player.closeInventory();
sendMessage(player, "§cComing soon!", "현재 개발 중입니다!");
}
break;
case "Basic Jumps":
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg2Name)) {
event.setCancelled(true);
basicInventorySet2(player, basicInv);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg1Name)) {
event.setCancelled(true);
basicInventorySet1(player, basicInv);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg3Name)) {
event.setCancelled(true);
basicInventorySet3(player, basicInv);
}
break;
}
}
完整来源:BasicInv.java OjMenu.java
在您的情况下,您将创建一个库存,并将其用于每个人。所以每次玩家打开物品栏时,都会打开“共享”物品栏。
您正在这样做:
Inventory inv = Bukkit.createInventory(null, 54, "Inventory"); // create one instance
public void show(Player p) {
p.openInventory(inv); // everyone open same inventory instance
}
所以一切都将使用相同的实例。
在您的第一个代码示例中,这样更好:
public Inventory createInventory() {
return Bukkit.createInventory(null, 54, "Inventory"); // create a new inventory
}
public void showBasicJump(Player p) { // show an inventory to the given player
Inventory myInv = createInventory(); // create a new inv, for thi splayer
myInv.setItem(0, new ItemStack(Material.STICK); // set item
basicInventorySet1(player, myInv); // call your method that change all items
p.openInventory(myInv); // final inventory
}
public void updateBasicJumpInv(InventoryClickEvent e, Player player) { // before this, you made all check about inv
Inventory openInv = e.getClickedInventory(); // get the current opened inv. Can also work with player.getOpenInventory()
basicInventorySet1(player, openInv);
p.updateInventory(); // update player inv
}
这将在您每次打开新清单时创建一个新实例。
我正在为我的跑酷服务器制作 GUI,但我遇到了问题:
(YT link, 可以过期)
如您所见,当我点击下一页时,其他玩家的 GUI 也更新了。
我也试过将 Inventory 的所有者设置为不为 null(如此个别),但它只显示空的 GUI。
我该如何解决这个问题?我搜索了很多并尝试了很多东西,但找不到答案。
// the showed inventory
Inventory basicInv = Bukkit.createInventory(null, 54, "Basic Jumps");
@EventHandler
public void clickInv(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getClickedInventory() == null) return;
if (event.getCurrentItem() == null) return;
if (event.getCurrentItem().getItemMeta() == null) return;
if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(" ")) {
event.setCancelled(true);
}
switch (event.getClickedInventory().getName()) {
case "OneJump Menu":
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(basicName)
&& event.getCurrentItem().getItemMeta().getLore().equals(Collections.singletonList(ChatColor.GRAY + "Includes very historical and original jumps."))) {
player.openInventory(basicInv);
basicInventorySet1(player, basicInv);
event.setCancelled(true);
plugin.getPlayerConfig().set("Player." + player.getUniqueId() + ".PlayingJumpType", "basicjump");
plugin.savePlayerConfig();
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(DisplayName)
|| event.getCurrentItem().getItemMeta().getDisplayName().equals(basicName)) {
event.setCancelled(true);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(" ")) {
event.setCancelled(true);
} else {
event.setCancelled(true);
player.closeInventory();
sendMessage(player, "§cComing soon!", "현재 개발 중입니다!");
}
break;
case "Basic Jumps":
if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg2Name)) {
event.setCancelled(true);
basicInventorySet2(player, basicInv);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg1Name)) {
event.setCancelled(true);
basicInventorySet1(player, basicInv);
} else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(pg3Name)) {
event.setCancelled(true);
basicInventorySet3(player, basicInv);
}
break;
}
}
完整来源:BasicInv.java OjMenu.java
在您的情况下,您将创建一个库存,并将其用于每个人。所以每次玩家打开物品栏时,都会打开“共享”物品栏。
您正在这样做:
Inventory inv = Bukkit.createInventory(null, 54, "Inventory"); // create one instance
public void show(Player p) {
p.openInventory(inv); // everyone open same inventory instance
}
所以一切都将使用相同的实例。
在您的第一个代码示例中,这样更好:
public Inventory createInventory() {
return Bukkit.createInventory(null, 54, "Inventory"); // create a new inventory
}
public void showBasicJump(Player p) { // show an inventory to the given player
Inventory myInv = createInventory(); // create a new inv, for thi splayer
myInv.setItem(0, new ItemStack(Material.STICK); // set item
basicInventorySet1(player, myInv); // call your method that change all items
p.openInventory(myInv); // final inventory
}
public void updateBasicJumpInv(InventoryClickEvent e, Player player) { // before this, you made all check about inv
Inventory openInv = e.getClickedInventory(); // get the current opened inv. Can also work with player.getOpenInventory()
basicInventorySet1(player, openInv);
p.updateInventory(); // update player inv
}
这将在您每次打开新清单时创建一个新实例。