Inventory for each player?

Discussion in 'Plugin Development' started by jusjus112, Mar 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jusjus112

    I need help with make an inventory for each player! It did work for me when i created the inventory when they clicked on the item! But how do i save items to it for each player! Like the player can put there own items in it, and it dont appear! But make it so that not every player has te same, only for each player!
    This is the code for creating an iventory if they clicked on an item:
    Code:java
    1. @EventHandler
    2. public void onChecklistClick1(InventoryClickEvent e) {
    3. Inventory checkmenu = Bukkit.createInventory(null, 45, ChatColor.RED + e.getWhoClicked().getName() + "'s " + ChatColor.GOLD + "checklist");
    4. FlyingBlocksPlugin.toCheckListInventory(plugin.checkList, "Achievements");
    5.  
    6. Inventory inv = e.getInventory();
    7. ItemStack cl = e.getCurrentItem();
    8. if (inv.getName().equals(menu.getName())) {
    9. if (e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "" + ChatColor.BOLD + "CheckList")) {
    10. if (cl.getType() == Material.BEACON) {
    11. e.setCancelled(true);
    12. e.getWhoClicked().openInventory(checkmenu);
    13. }
    14. }
    15. }
    16.  
     
  2. Make a HashMap with String playerName and Inventory inventory.

    Code:java
    1. Inventory checkmenu = Bukkit.createInventory(null, 45, ChatColor.RED + e.getWhoClicked().getName() + "'s " + ChatColor.GOLD + "checklist");
    2. inventoryHashMap.put(e.getPlayer().getName(), checkMenu);


    Code:java
    1. e.getWhoClicked().openInventory(inventoryHashMap.get(e.getPlayer().getName());
     
  3. Offline

    jusjus112

    WeeziMonkey
    So, can i save the inventory with my way?
    Code:java
    1. saveInventory(Config, Inventory, "Check");

    If yes, can you show me how to get it
     
  4. jusjus112

    Well, i know it's possible to save Inventories in config files, but i never quit understood how.
     
  5. Offline

    jusjus112

    WeeziMonkey
    But the way that i have made! How do i get the inventory from the player, i the
    Code:java
    1. saveInventory(Config, Inventory, "Check");
    2. //Inventory [The inventory to save]
     
  6. plugin.getConfig().get("lines.with.inventory"); ?
     
  7. Offline

    jusjus112

    WeeziMonkey
    But when the server reloads, is the data from the hashmap gone?
     
  8. jusjus112

    I am not exactly sure. I am sure it's gone when server restarts though, and you should never /reload a server anyways. Just when you do saveInventory save it per-player and when you use get() to get it back from the config make sure you get it from the right player, and save the inventory every time it's used. This way it doesnt matter if the HashMap is gone, as it's already in the config once the inventory is closed. You can also save all hashmap inventories in the config in onDisable().
     
  9. Offline

    jusjus112

    WeeziMonkey
    Exactly what I want is an achievement inventory. So when a player opens the inventory, the item should go into the inventory he has achieved! So any inventory of a player is different!
    My code sofar: That doesnt work
    Code:java
    1. @EventHandler
    2. public void onChecklistClick1(InventoryClickEvent e) {
    3. Inventory checkMenu = Bukkit.createInventory(null, 45, ChatColor.RED + e.getWhoClicked().getName() + "'s " + ChatColor.GOLD + "checklist");
    4. checklist.checkhash.put(e.getWhoClicked().getName(), checkMenu);
    5. checkMenu.addItem(new ItemStack(Material.DIAMOND, 64));
    6. Inventory inv = e.getInventory();
    7. ItemStack cl = e.getCurrentItem();
    8. if (inv.getName().equals(menu.getName())) {
    9. if (e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "" + ChatColor.BOLD + "CheckList")) {
    10. if (cl.getType() == Material.BEACON) {
    11. e.setCancelled(true);
    12. e.getWhoClicked().openInventory(checklist.checkhash.get(e.getWhoClicked().getName()));
    13. }
    14. }
    15. }
    16. }
    17.  
    18. public static Inventory toCheckListInventory(FileConfiguration config, String path) {
    19. for (int i = 0; i<27;i++) {
    20. if (config.isItemStack(path + "." + i)) {
    21. checkMenu.setItem(i, config.getItemStack(path + "." + i));
    22. }
    23. }
    24. return checkMenu;
    25. }
     
  10. jusjus112
    In that case, i would do something completely different.

    Unfortunelately, my mind can't really concentrate now, so i can't give you full code. But i would put strings or booleans per-player in the config, then when the inventory needs to be opened, make a new inventory (like you already did), and set the contents to the items for every achievement the player has. This way when he opens it, it only adds in the items of the achievements the player has.
     
Thread Status:
Not open for further replies.

Share This Page