Solved Set an item in a Player-Owned Inventory

Discussion in 'Plugin Development' started by ipodtouch0218, Jun 2, 2016.

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

    ipodtouch0218

    So I have this method for a Punishment plugin I'm making:

    Code:
          punishGUI_0 = Bukkit.createInventory(p, 27, ChatColor.RED + "Select a Punishment");
          punishGUI_1 = Bukkit.createInventory(p, 27, ChatColor.RED + "Select a Severity");
          punishGUI_2 = Bukkit.createInventory(p, 27, ChatColor.RED + "Confirm");
          punishGUI_3 = Bukkit.createInventory(p, 27, ChatColor.RED + "Confirm (Again)");
    And I made a hashmap containing the player's selection. That all works.
    But when I try to set an item, it sets it for every single inventory, not just for the owner's.

    So what I'm asking, is how to set an item in an inventory for one specific player using the same inventory name.
     
  2. Offline

    Xerox262

    @ipodtouch0218 Post more of the class, from what I am hearing you're trying to change an item for a single player in one of the inventories but it's not working, for that you need to create the inventories for each player.
     
  3. Offline

    ipodtouch0218

    @Xerox262

    No need for more of the class.

    I'm using:

    Code:
                            ItemStack exploiting = new ItemStack(Material.RAW_FISH, 1, (short) 3);
                            ItemMeta metaExpl = exploiting.getItemMeta();
                            metaExpl.setDisplayName(ChatColor.RED + "Exploiting");
                            ArrayList<String> LoreExpl = new ArrayList<String>();
                            LoreExpl.add(ChatColor.GRAY + "Player took part in abusing an unintended bug.");
                            metaExpl.setLore(LoreExpl);
                            exploiting.setItemMeta(metaExpl);
                            punishGUI_1.setItem(0, exploiting);
    Which sets it for everyone in punishGUI_1. That's intended as I've just learnt.
    So... how would I go about creating a new inventory for everyone?
     
  4. Offline

    Xerox262

    @ipodtouch0218 The reason I asked for more of the Class was so I could see how you're creating the inventories in the first place, it affects how you create some for each player, could be as simple as adding a HashMap
     
  5. Offline

    ipodtouch0218

    http://pastebin.com/nD6xY1JL

    I create the Inventory name and info to be empty, and then change the variable to add the player's name on a PlayerJoinEvent.
     
  6. Offline

    Xerox262

    @ipodtouch0218 Make a Hashmap for the PunishGUI_0 inventory and key as UUID, you can access the inventories from the UUID in other events.
     
  7. Offline

    ipodtouch0218

    I'm confused how that will work.
    Code:
    HashMap<UUID, Inventory> inventory = new HashMap<UUID, Inventory>();
    I've added this. Is this what you mean?
    Code:
    inventory.get(UUID).setItem(0, item);
     
  8. Offline

    Xerox262

    @ipodtouch0218 Yes, however you need to add the inventory when they do the command, and remove it when they close it.
     
  9. Offline

    ipodtouch0218

    @Xerox262

    During my command:
    Code:
    //after assigning variables etc
    UUID playerUUID = player.getUniqueId();
    inventory.put(playerUUID, punishGUI_0)
    p.openInventory(inventory.get(playerUUID));
    Will that work?
     
  10. Offline

    Xerox262

    @ipodtouch0218 Try it and see, it looks like it wouldn't since it'll always add punishGUI_0, however I could be wrong.
     
  11. Offline

    ipodtouch0218

    @Xerox262

    I was playing around and it worked.. I re-compiled now there is a NullPointerException on ln388
    Code:
    /*386*/ UUID playerUUID = player.getUniqueId();
    /*387*/ inventory.put(playerUUID, punishGUI_0);
    /*388*/ inventory.get(playerUUID).setItem(8, skull);
    Even though I just set the variables and it works sometimes, sometimes not >.<
     
  12. Offline

    Xerox262

  13. Offline

    ipodtouch0218

    The inventory creation:
    Code:
        Inventory punishGUI_0;
        Inventory punishGUI_1;
        Inventory punishGUI_2;
        Inventory punishGUI_3;
      
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
          Player p = event.getPlayer();
          punishGUI_0 = Bukkit.createInventory(p, 27, ChatColor.RED + "Select a Punishment");
          punishGUI_1 = Bukkit.createInventory(p, 27, ChatColor.RED + "Select a Severity");
          punishGUI_2 = Bukkit.createInventory(p, 27, ChatColor.RED + "Confirm");
          punishGUI_3 = Bukkit.createInventory(p, 27, ChatColor.RED + "Confirm (Again)");
               }
    The head:
    Code:
                            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                            SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
                            skullMeta.setDisplayName(ChatColor.RED + stringTarget);
                            skullMeta.setOwner(stringTarget);
                            ArrayList<String> LoreSkull = new ArrayList<String>();
                            LoreSkull.add(ChatColor.RED + "UUID: " + targetUUID);
                            skullMeta.setLore(LoreSkull);
                            skull.setItemMeta(skullMeta);
    Everything is set.
     
  14. Offline

    Xerox262

    When is skull created though, in what method? And are you sure it's called before you try setting the item?
     
  15. Offline

    ipodtouch0218

    It's called during my onCommand method

    Code:
    Player player = (Player) sender;
    String stringTarget = args[0];
    @SuppressWarnings("deprecation")
    Player onlineTarget = Bukkit.getPlayerExact(stringTarget);
    UUID targetUUID;
    if (onlineTarget == null) {
        @SuppressWarnings("deprecation")
        OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(stringTarget);
        targetUUID = offlineTarget.getUniqueId();
    } else {
        targetUUID = onlineTarget.getUniqueId();
    }
    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
    skullMeta.setDisplayName(ChatColor.RED + stringTarget);
    skullMeta.setOwner(stringTarget);
    ArrayList<String> LoreSkull = new ArrayList<String>();
    LoreSkull.add(ChatColor.RED + "UUID: " + targetUUID);
    skullMeta.setLore(LoreSkull);
    skull.setItemMeta(skullMeta);
                        
    UUID playerUUID = player.getUniqueId();
    inventory.put(playerUUID, punishGUI_0);
    inventory.get(playerUUID).setItem(8, skull);
    
    player.openInventory(inventory.get(playerUUID));
    return true;
    (This is all that is inside the check for the cmd.getName, sender = player, and permission check. I set it up properly.)
     
  16. Offline

    Xerox262

  17. Offline

    ipodtouch0218

    @Xerox262

    Restarted once, reloaded once both with the code, just in case it was that

    EDIT:
    Turned out it was the reload. I'mma add a "inventory.clear();" on onDisable now.
     
  18. Offline

    ipodtouch0218

    @Xerox262

    I just checked with two players doing it at once and the items still change for everyone in the GUI.

    Any ideas?
     
    Last edited: Jun 3, 2016
  19. Offline

    ipodtouch0218

    *bumped so hard it went to the top of the forums list*
     
  20. Offline

    Xerox262

    @ipodtouch0218 Create the inventory when the command is run, that way it'll be player specific.
     
    ipodtouch0218 likes this.
  21. Offline

    ipodtouch0218

    @Xerox262

    Finally got it to work. Thanks!
    Code:
    inventory.put(player.getUniqueId(), Bukkit.createInventory(player, 9, ChatColor.BLUE + "Inventory Name");
    To get and set items:
    Code:
    inventory.get(player.getUniqueId()).setItem //..etc..
     
    SP3NC3RXD and Xerox262 like this.
Thread Status:
Not open for further replies.

Share This Page