Instanced Inventories

Discussion in 'Plugin Development' started by ImpaTheImpaler, Feb 10, 2015.

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

    ImpaTheImpaler

    I have been making a backpack plugin for my server, and everything works. The only problem I have is the backpack is public and everyone has the same backpacks. I am trying to make it so every player has their own.

    Here is my code :
    Code:
    package me.impatheimpaler.myplugins;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Storage extends JavaPlugin implements Listener {
    
        private HashMap<String, Inventory> backpack = new HashMap<String, Inventory>();
      
        public void onEnable() {
          Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
      
        public void onDisable() {
          
        }
      
         Inventory backpack = Bukkit.getServer().createInventory(null, 27, "Storage");
      
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
          
            Player p = e.getPlayer();
          
          
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
            if (!(e.getItem().getType() == Material.INK_SACK)) return;
            if (e.getItem().getType() == Material.INK_SACK) {
                storage.put(p.getName(), backpack);
                p.sendMessage(ChatColor.GREEN + "Storage opened.");
                p.openInventory(backpack);
            }
            
           }
        }
    
     
  2. Offline

    sirrus86

    Create a map, give each UUID their own backpack, have it pull up the appropriate backpack when they do your event .
     
  3. Offline

    pie_flavor

    @sirrus86 Clearly you haven't read his example code. He's done just that.
    @ImpaTheImpaler Don't make clone threads, bump the original.
     
  4. Offline

    Gater12

  5. Offline

    ResultStatic

    @ImpaTheImpaler create the backpack variable right before u put it in the map, ur putting the same inventory for each player in the map, you need to create a new one each time.
     
  6. Offline

    pie_flavor

    @Gater12
     
  7. Offline

    TehHypnoz

    I think you have to use:

    Code:
    p.openInventory(backpack.get(p.getName());
    
    Also keep in mind that the backpacks are gone when you reload/restart the server, so make sure to save the inventories to file (don't ask me how, I've also had trouble with this in the past).
     
  8. Offline

    Gater12

    @pie_flavor
    Take a closer look and backpack is not the one from the HashMap rather from another instance he created. They open the same instance therefore making it 'global'.
     
  9. Offline

    1Rogue

    Use either a serializable wrapper or make a class which copies the state
     
Thread Status:
Not open for further replies.

Share This Page