Subclass Turning Into Superclass?

Discussion in 'Plugin Development' started by The Fancy Whale, Feb 7, 2015.

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

    The Fancy Whale

    I have made a subclass of ItemStack:
    Code:
    public class SpecialStack extends ItemStack {
        public String warpName;
      
        public SpecialStack(Material m, String warp) {
            super (m, 1);
            warpName = warp;
        }
    }
    
    And then I create a SpecialStack and put it in an inventory:
    Code:
    SpecialStack example = SpecialStack(Material.STONE, "Test");
    inventory.setItem(0, example);
    However, when I call the Inventory Click Event and I run:
    Code:
    event.getCurrentItem() instanceof SpecialStack
    it returns false. If I print the class name of the current item, it turns back into the itemstack. Why isn't it the SpecialStack?
    Any help is greatly appreciated, thanks!
     
  2. Offline

    mythbusterma

    @The Fancy Whale

    Because ItemStacks aren't stored, they have their values generated and destroyed as needed. It generates a new ItemStack each time you call getItem().
     
  3. Offline

    adam753

    ItemStacks get cloned when added to inventories, so extending ItemStack won't work very well. Have you thought about setting the item's lore instead?
     
  4. Offline

    Konato_K

    @The Fancy Whale Because the internals of CraftBukkit will create a copy of the ItemStack to set it in the inventory (that's why setting an item, then changing something in the ItemStack reference that was set won't do anything in the inventory).

    In the case the ItemStack is not a instance of CraftItemStack the server will create a new nms ItemStack and set the values from the other object (so, your SpecialItemStack will be replicated with a new nms ItemStack).
     
  5. Offline

    The Fancy Whale

    Okay thanks for the info. How do you all suggest I go about keeping a certain variable with an itemstack? Also, lores won't work because I don't want them to see the string value I am giving.
     
  6. Offline

    mythbusterma

    @The Fancy Whale

    I guess you ignored my post.

    You can't. Attach ItemMeta if you need to store additional data.
     
  7. Offline

    The Fancy Whale

    Sorry I missed your post, but how would I attach data to ItemMeta without modifying the lore or display name?
     
  8. Offline

    mythbusterma

    @The Fancy Whale

    Nevermind, I don't see any clear way of doing this. My mistake.
     
  9. Offline

    Konato_K

    @The Fancy Whale I don't think there is an easy way to do this, anyway, I think you can put lores with just color codes, they won't render client side and may only look as an empty line
     
  10. Offline

    Lactem

    You can keep your variables with a map. For example have a map with Integer and String. The Integer would represent the slot number and the String would represent your warp. Then (assuming you're trying to do something when a slot is clicked in an inventory, otherwise ignore this) use the warp with the associated slot number when it's clicked.
     
  11. Offline

    Totom3

    @The Fancy Whale I had the same problem once. A cheaty way would be to make the lore black using ChatColor.BLACK. It will show if your lore is long though. The other way would be to use ProtocolLib or something such to not send the lore to the players, but that's getting complicated and it's only the beginning.
     
Thread Status:
Not open for further replies.

Share This Page