Solved Item not getting added to inventory?

Discussion in 'Plugin Development' started by TheWolfBadger, Jan 14, 2017.

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

    TheWolfBadger

    Basically when I use the ReadItem() function, the item is made and I can print it in console, but when I add it to the inventory it doesn't get added?... The other items do though?
    Code:
    @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent evt) {
            evt.getPlayer().getInventory().clear();
            evt.getPlayer().getInventory().setArmorContents(null);
            evt.getPlayer().getInventory().setItem(0, new ItemStack(Material.WATCH));
            evt.getPlayer().getInventory().setItem(4, new ItemStack(Material.ENDER_PEARL, 16));
            ItemStack item = zHub.readItem("Items.Hide_Players");
            evt.getPlayer().getInventory().setItem(8, item);
        }
    ReadItem():
    Code:
    public ItemStack readItem(String path) {
            Material mat = Material.valueOf(this.getConfig().getString(path+".Item"));
            String displayName = ChatColor.translateAlternateColorCodes('&', this.getConfig().getString(path+".Display_Name"));
            List<String> loresNoColor = this.getConfig().getStringList(path+".Lores");
            List<String> lores = new ArrayList<>();
            for(String s : loresNoColor) {
                lores.add(ChatColor.translateAlternateColorCodes('&', s));
            }
            ItemStack item = new ItemStack(mat);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(displayName);
            meta.setLore(lores);
            item.setItemMeta(meta);
            return item;
        }
    YML:
    Code:
    #############
    ### Items ###
    #############
    Items:
      Hide_Players:
        Item: 'REDSTONE_TORCH_OFF'
        Display_Name: '&7Show players'
        Lores:
          - 'Test'
      Show_Players:
        Item: 'REDSTONE_TORCH_ON'
        Display_Name: '&8Hide players'
        Lores:
          - 'dad'
      WATCH:
        Display_Name: '&5Server Selector'
        Lores:
          - 'dasds'
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    TheWolfBadger

    That doesn't change anything oddly enough... :(
     
  4. Offline

    timtower Administrator Administrator Moderator

    @TheWolfBadger What do you get if you print slot 8 after setting it?
     
  5. Offline

    TheWolfBadger

    "null"
    Clearly because there is no item there in game.
     
  6. Offline

    JanTuck

    I had this issue too when trying to set an acacia door ;/ it did not want to read it from config. So i changed the way i did it. I just saved the object "ItemStack" which actually saves it in a kinda editable state.

    [​IMG]

    The Code used.

    You wont actually be able to use it.
    [​IMG]

    And the output:
    [​IMG]

    Im not sure if this is what you wanted.

    Try adding some debugging to the code.

    Sendt fra min ALE-L21 med Tapatalk

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 15, 2017
  7. Offline

    TheWolfBadger

    I've used code like mine before. This is the first time it's just not really working though... :/

    Code:
    [13:17:55 INFO]: ItemStack{REDSTONE_TORCH_OFF x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=ยง7Show players, lore=[Test]}}
    [13:17:55 INFO]: null
    My code:
    Code:
            ItemStack item = zHub.readItem("Items.Hide_Players");
            evt.getPlayer().getInventory().setItem(8, item);
            System.out.print(item.toString());
            evt.getPlayer().updateInventory();
            System.out.print(evt.getPlayer().getInventory().getItem(8));
     
    Last edited: Jan 15, 2017
  8. Offline

    JanTuck

    I have no clue sorry. From what i can see it should work.
     
  9. Offline

    TheWolfBadger

    @timtower
    Okay, so I got it working! All great! EXCEPT, when you put REDSTONE_TORCH_OFF as one of the items it shits itself and acts like it doesn't even exist... It does all the other items though?...
    Code:
    ItemStack selector = zHub.readItem("Items.Selector");
            evt.getPlayer().getInventory().setItem(zHub.getConfig().getInt("Items.Selector.Slot"), selector);
            evt.getPlayer().getInventory().setItem(zHub.getConfig().getInt("Items.Enderpearls.Slot"), new ItemStack(Material.ENDER_PEARL, 16));
            ItemStack item = zHub.readItem("Items.Hide_Players");
            evt.getPlayer().getInventory().setItem(zHub.getConfig().getInt("Items.Hide_Players.Slot"), item);
            evt.getPlayer().updateInventory();
    Code:
    public ItemStack toItemStack(String material) {
            if(material.contains(":")) {
                String data[] = material.split(":");
                return new ItemStack(Material.valueOf(data[0]), 1, Short.parseShort(data[1]));
            }
            return new ItemStack(Material.valueOf(material), 1);
        }
        public ItemStack readItem(String path) {
            System.out.print(path+".Item");
            System.out.print(this.getConfig().getString(path+".Item"));
            ItemStack item = toItemStack(this.getConfig().getString(path+".Item"));
            String displayName = ChatColor.translateAlternateColorCodes('&', this.getConfig().getString(path+".Display_Name"));
            List<String> loresNoColor = this.getConfig().getStringList(path+".Lores");
            List<String> lores = new ArrayList<>();
            for(String s : loresNoColor) {
                lores.add(ChatColor.translateAlternateColorCodes('&', s));
            }
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(displayName);
            meta.setLore(lores);
            item.setItemMeta(meta);
            return item;
        }
    Code:
    #############
    ### Items ###
    #############
    Items:
      Hide_Players:
        Slot: 8
        Item: 'REDSTONE_TORCH_ON'
        Display_Name: '&7Hide players'
        Lores:
          - 'Test'
      Show_Players:
        Slot: 8
        Item: 'REDSTONE_TORCH_OFF'
        Display_Name: '&8Show players'
        Lores:
          - 'dad'
     
  10. Offline

    JanTuck

    Ohh yeah i should have seen that. Redstone Torch off arent actually an item. So it wouldnt show up in the inventory.
     
  11. Offline

    TheWolfBadger

    So how would I get it to be a redstone torch that is off though?
     
  12. Offline

    JanTuck

    Custom textures.

    You can place a redstone torch that is off but you cant get it in your inventory.
     
  13. Offline

    TheWolfBadger

    Ahhh I see now, https://bukkit.org/threads/redstone_torch_off-not-working.255379/ ...
    I guess I'll just use a lever. Thanks for your help! I knew it couldn't be me, knew Bukkit had to do something with this xD
     
Thread Status:
Not open for further replies.

Share This Page