dimanche 22 février 2015

Saving and loading player inventories to/from HashMaps



I am creating a plugin that saves the players inventory when they right click a sign that says "[SaveKit]" then loading the inventory when they right click a sign that says "[LoadKit]". Here is what i have:


The HashMaps :



public static HashMap<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
public static HashMap<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();


The saveKit() and loadKit() :



public static void saveKit(Player player) {
armor.put(player.getName(), player.getInventory().getArmorContents());
inv.put(player.getName(), player.getInventory().getContents());
player.sendMessage(tag + ChatColor.GREEN + " You have successfully saved your kit!");
}

public static void loadKit(Player player) {
player.getInventory().setContents(inv.get(player.getName()));
player.getInventory().setArmorContents(armor.get(player.getName()));
player.sendMessage(tag + ChatColor.GREEN + " You have successfully loaded your kit!");
}


and finally, the onInteract(PlayerInteractEvent e)



@EventHandler
public void onInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();
Action a = e.getAction();
Block b = e.getClickedBlock();

if (b == null) {
return;
}

if (a == Action.RIGHT_CLICK_BLOCK && b != null && b.getType() == Material.WALL_SIGN) {
Sign sign = (Sign) b.getState();

if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("[SaveKit]")) {
saveKit(p);
return;
} else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("[LoadKit]")) {
loadKit(p);
return;
}
}
}


So everything here works so far, apart from loading and saving the inventories, the messages send which shows it is calling the load/saveKit methods, but it is not loading or saving the inventories. Please help! Thanks!




Aucun commentaire:

Enregistrer un commentaire