存储玩家的库存
Store player's inventory
我如何存储玩家的库存,清除它,然后 return 到存储的版本?
如果你想暂时保存玩家的物品栏,你可以获取他们物品栏的内容(包括常规内容和我假设的盔甲?)并将它们放在某种列表中。您可能还必须为每个 ItemStack/item 保存槽号,在这种情况下您可能需要使用 HashMap。要在保存内容后清除玩家的物品栏,请使用 player.getInventory().clear()
。然后您可以稍后使用 player.getInventory.setItem(slot, item)
.
再次添加项目
如果你想确保即使在插件重新加载或服务器关闭时也能保存库存,你需要将库存的内容转换为字符串(序列化)并将其保存到文件中(Bukkit 内置了相关内容,以便保留所有项目元数据)。如果您需要这方面的更多信息,请告诉我,我可以为这些方法中的任何一种编写更详细的代码。可能有一种更简单的方法可以临时复制整个清单的内容,然后使用 player.getInventory().setContents(contents)
.
您可以使用 HashMap
来存储玩家所有物品栏和盔甲的内容
Map<UUID, ItemStack[]> items = new HashMap<UUID, ItemStack[]>();
Map<UUID, ItemStack[]> armor = new HashMap<UUID, ItemStack[]>();
然后您可以使用
将玩家的库存内容存储在 HashMap 中
UUID uuid = player.getUniqueId();
ItemStack[] contents = player.getInventory().getContents();
ItemStack[] armorContents = player.getInventory().getArmorContents();
items.put(uuid, contents);
armor.put(uuid, armorContents);
然后你可以通过
清除玩家的物品栏和盔甲
player.getInventory().clear();
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
要恢复库存和装甲,您只需使用
UUID uuid = player.getUniqueId();
ItemStack[] contents = items.get(uuid);
ItemStack[] armorContents = armor.get(uuid);
player.getInventory().setContents(contents);
player.getInventory().setArmorContents(armorContents);
因此,您可以拥有类似这样的代码
Map<UUID, ItemStack[]> items = new HashMap<UUID, ItemStack[]>();
Map<UUID, ItemStack[]> armor = new HashMap<UUID, ItemStack[]>();
public void storeAndClearInventory(Player player){
UUID uuid = player.getUniqueId();
ItemStack[] contents = player.getInventory().getContents();
ItemStack[] armorContents = player.getInventory().getArmorContents();
items.put(uuid, contents);
armor.put(uuid, armorContents);
player.getInventory().clear();
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
}
public void restoreInventory(Player player){
UUID uuid = player.getUniqueId();
ItemStack[] contents = items.get(uuid);
ItemStack[] armorContents = armor.get(uuid);
if(contents != null){
player.getInventory().setContents(contents);
}
else{//if the player has no inventory contents, clear their inventory
player.getInventory().clear();
}
if(armorContents != null){
player.getInventory().setArmorContents(armorContents);
}
else{//if the player has no armor, set the armor to null
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
}
}
然后你可以只调用 storeAndClearInventory(Player)
当你想存储玩家的物品栏并清除它时,当你想将玩家的物品栏恢复到它的原始状态时调用 restoreInventory(Player)
。
我如何存储玩家的库存,清除它,然后 return 到存储的版本?
如果你想暂时保存玩家的物品栏,你可以获取他们物品栏的内容(包括常规内容和我假设的盔甲?)并将它们放在某种列表中。您可能还必须为每个 ItemStack/item 保存槽号,在这种情况下您可能需要使用 HashMap。要在保存内容后清除玩家的物品栏,请使用 player.getInventory().clear()
。然后您可以稍后使用 player.getInventory.setItem(slot, item)
.
如果你想确保即使在插件重新加载或服务器关闭时也能保存库存,你需要将库存的内容转换为字符串(序列化)并将其保存到文件中(Bukkit 内置了相关内容,以便保留所有项目元数据)。如果您需要这方面的更多信息,请告诉我,我可以为这些方法中的任何一种编写更详细的代码。可能有一种更简单的方法可以临时复制整个清单的内容,然后使用 player.getInventory().setContents(contents)
.
您可以使用 HashMap
来存储玩家所有物品栏和盔甲的内容
Map<UUID, ItemStack[]> items = new HashMap<UUID, ItemStack[]>();
Map<UUID, ItemStack[]> armor = new HashMap<UUID, ItemStack[]>();
然后您可以使用
将玩家的库存内容存储在 HashMap 中UUID uuid = player.getUniqueId();
ItemStack[] contents = player.getInventory().getContents();
ItemStack[] armorContents = player.getInventory().getArmorContents();
items.put(uuid, contents);
armor.put(uuid, armorContents);
然后你可以通过
清除玩家的物品栏和盔甲player.getInventory().clear();
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
要恢复库存和装甲,您只需使用
UUID uuid = player.getUniqueId();
ItemStack[] contents = items.get(uuid);
ItemStack[] armorContents = armor.get(uuid);
player.getInventory().setContents(contents);
player.getInventory().setArmorContents(armorContents);
因此,您可以拥有类似这样的代码
Map<UUID, ItemStack[]> items = new HashMap<UUID, ItemStack[]>();
Map<UUID, ItemStack[]> armor = new HashMap<UUID, ItemStack[]>();
public void storeAndClearInventory(Player player){
UUID uuid = player.getUniqueId();
ItemStack[] contents = player.getInventory().getContents();
ItemStack[] armorContents = player.getInventory().getArmorContents();
items.put(uuid, contents);
armor.put(uuid, armorContents);
player.getInventory().clear();
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
}
public void restoreInventory(Player player){
UUID uuid = player.getUniqueId();
ItemStack[] contents = items.get(uuid);
ItemStack[] armorContents = armor.get(uuid);
if(contents != null){
player.getInventory().setContents(contents);
}
else{//if the player has no inventory contents, clear their inventory
player.getInventory().clear();
}
if(armorContents != null){
player.getInventory().setArmorContents(armorContents);
}
else{//if the player has no armor, set the armor to null
player.getInventory().setHelmet(null);
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);
}
}
然后你可以只调用 storeAndClearInventory(Player)
当你想存储玩家的物品栏并清除它时,当你想将玩家的物品栏恢复到它的原始状态时调用 restoreInventory(Player)
。