is this actually safe to do?

Discussion in 'Plugin Development' started by xize, Jul 9, 2014.

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

    xize

    Hello,

    so I was wondering this for some while if it could be dangerous or just not.

    for example I made a wrap object such like this, of course this won't make much sense its just a example:

    Code:
    public class Something extends ItemStack {
        private final UUID itemUUID;
     
        public Something(ItemStack stack) {
              super(stack);
              this.itemUUID = UUID.randomUUID();
        }
     
        public UUID getUniqueId() {
            return itemUUID;
        }
    }
    
    and then I would instance it as:

    Code:
    ItemStack item = new ItemStack(Material.WOOD, 1);
    Something some = new Something(item);
    
    but now I can use the same Something object in p.getInventory().addItem(); would this be safe since it extends the ItemStack object?, or are there some things in the background which could cause troubles such like memory leaks when doing this kind of practice?

    thanks for the responses already :)

    -edit-
    I know that all the fields in the Something object etc will be gone doing that though, but still I don't want to cause a unintentional memory leak :p
     
  2. Offline

    Zupsub

    Why do you want to do this? As mentioned, there's no warrenty, fields like itemUUID stay in the ItemStack. But, when you don't overide methods of the ItemStack (or override them correctly) there shouldn't any iusses.
     
  3. Offline

    xize

    Zupsub
    well I want to expiriment a bit its absolutely not good code to use though but I wonder what happens when I do this:

    from the Object Something which I put it in p.getInventory().addItem() in theory then it should cast back to a normal ItemStack but the fields ive had previously are gone so my wonderings are how did the jvm handled that situation or is it still somewhere floating in that specific case?

    in my own plugin I made a few of these objects based on invisible item lores by using chat colors in combination of what ive wrote on the orginal post but I don't know if just the way of extending ItemStacks is actually dangerous, its pretty much redurant to use this aprouch though, but I'm trying to make a sort of data container or smilliar like the Entity metadata which normally ItemStacks doesn't have atleast didn't saw it in the ItemMeta to store my own things in it:p
     
  4. Offline

    Zupsub

    Well, the fields won't gone, only because you call addItem(). They are kept in memory as long as the whole ItemStack is kept in memory.
    I don't know the actual code of Craftbukkit and how it stores the inventory. Sometime your data will get lost, not later then you restart your server, since bukkit will only store the information an ItemStack normally has. Or if you write this ItemStack to an config, your UUID think will get lost too.
     
Thread Status:
Not open for further replies.

Share This Page