Custom Item Bug

  • English

2019-04-16: A new hotfix (0.9.5.7) is now available!

  • Custom Item Bug

    Hi @red51

    Custom items are bloody awesome addition <3 thank you! I'm sure the folk on Medieval Realms are going to love getting their gemstones in their inventory instead of instantly being converted to cash.

    One problem though, the stacks spit fine, but when you try to stack them back up they won't stack until you close the inventory down and re-open up. A wee bug I think?

    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • Hehe, this looks really promising! :thumbup:
    Unfortunately I wasn't able to reproduce this issue, but I've already made some changes to the custom item handling (maybe that bug was fixed as a side effect)^^ A new hotfix will be available tomorrow, if you still experience any custom item related issues with it, please let me know ;)
  • red51 wrote:

    A new hotfix will be available tomorrow

    Thanks for the heads up! Yep one gemstone down, 9 more to go. :thumbsup:


    Ryan wrote:

    People get minerals on your server?

    Yep, there have been gemstones for quite some time now but they were instantly sold. Red's new update makes it way more fun now as they go into your inventory instead. :saint:
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • Hi @red51 I'm using this code to add gems to a players inventory:


    Java Source Code

    1. Inventory inventory = player.getInventory();
    2. inventory.insertNewCustomItem(EMERALD, 1, 1, 1,
    3. Inventory.SlotType.Inventory);
    When a player finds a gemstone and they currently do not have one in their inventory the notification shows correct ( 1x Emerald * )



    However, the second one found shows up at ( 2x Emerald * ) even though only one is added to the inventory (meaning they now have 2).

    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • yahwho wrote:

    However, the second one found shows up at ( 2x Emerald * ) even though only one is added to the inventory (meaning they now have 2).
    If a 2nd item (of the same type, no matter if it's a vanilla or custom item) is added to the inventory within 1-2 seconds, the "1x item" label just gets updated ;) That's actually intended. The reason for this is that there are sometimes situations in which many items are added to the inventory in a short time, and an updated label showing the total amount of items is sometimes more useful in these situations than several "1x" labels ^^
  • Just to confirm, I tried again on a clean server restart (i.e. no reloadplugins command used). I found one Emerald then stopped (I actually went away and grabbed a coffee) I came back and continued mining and found a second Emerald. Although (correctly) I had one more added to my inventory the lower right hand corner notification said I had found 2x Emerald.

    This is the code snippet I am using to add custom item to inventory:

    Java Source Code

    1. Inventory inventory = player.getInventory();
    2. inventory.insertNewCustomItem(EMERALD, 1, 1, 1,
    3. Inventory.SlotType.Inventory);
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • Sorry @red51 yet another message!

    The method:

    Java Source Code

    1. //method 1
    2. inventory.insertNewCustomItem(EMERALD, 0, 1);

    Work well for adding new custom items to the inventory (in the first available free slot or stacks if item already exists), however, it does not generate the nice inventory addition notification/animation on the lower right hand corner of the screen.


    The following method does generate a nice notification/animation on the lower right hand corner;


    Java Source Code

    1. //method 2
    2. inventory.insertNewCustomItem(EMERALD, 0, 1, 0, Inventory.SlotType.Inventory);


    However, the above second method requires that I specify a slot (in my examples case - slot 0) the result of which is the item already occupying the slot(0) gets deleted.

    Now I have tried writing some code to analyse the inventory (so I can use method 2) so I can allocate the correct slot (or stack if needed), however using item.getName() I simply get "apiitem" which isn't going to work.

    The ideal solution would be a notification/animation on method 1.

    Or, a new method like method 1;


    Java Source Code

    1. //method 3
    2. inventory.insertNewCustomItemWithGuiFeedback(EMERALD, 0, 1);




    :D
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • yahwho wrote:

    however using item.getName() I simply get "apiitem"

    Hmm this is actually causing some roadblock for me. I can't work with the items I've added as I have no means to differentiate between them.

    The names, including definition name are all "apiitem" and the getTypeID are all the same at 890. =O

    Edit: I can't even hack around it using different max stack sizes as the event.getItem().getMaxStacksize() for all custom items return 1 even though that's not the set max stack size.
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • yahwho wrote:

    Work well for adding new custom items to the inventory (in the first available free slot or stacks if item already exists), however, it does not generate the nice inventory addition notification/animation on the lower right hand corner of the screen.
    Oh, thanks for letting me know! This seems to be a general issue with the "insertNewItem()" methods (not only affecting custom items) =O

    yahwho wrote:

    The names, including definition name are all "apiitem" and the getTypeID are all the same at 890
    Yes, that's intended. Internally there is a universal "apiitem" which is used by every custom item. This way the game makes sure there are no collision between item id's, and in addition to that, the game is still runnable if stop using a certain custom item plugin (while there are still custom items in your inventories or chests) ;)
    The game distinguishes individual custom items via the item attribute. Every custom item has a CustomItemAttribute (which can be received with Item.getAttribute()). You can add individual attributes to your CustomItemAttribute (this way you can store additional information per item), for example:

    Java Source Code

    1. //Store per-item data
    2. Item item = playerInventory.insertNewCustomItem(UUID, 0, 1);
    3. Item.CustomItemAttribute attribute = (Item.CustomItemAttribute) item.getAttribute();
    4. attribute.setAttribute("timestamp", String.valueOf(System.currentTimeMillis()));
    5. attribute.setAttribute("playername", player.getName());
    6. attribute.setAttribute("foundAtLocation", player.getPosition().toString());
    7. ...
    8. ...
    9. //Get info from attribute
    10. Item item = player.getEquippedItem();
    11. //Find out if this is actually a custom item
    12. if(item != null && item.getAttribute() instanceof Item.CustomItemAttribute){
    13. Item.CustomItemAttribute attribute = (Item.CustomItemAttribute) item.getAttribute();
    14. if(attribute.hasAttribute("playername")){
    15. String playername = attribute.getAttribute("playername");
    16. }
    17. }
    Display All

    You can also get the UUID from the CustomItemAttribute. This way you can get the actual CustomItem definition from the server:

    Java Source Code

    1. Item.CustomItemAttribute attribute = (Item.CustomItemAttribute) item.getAttribute();
    2. CustomItem customItemDef = getServer().getCustomItem(attribute.getUUID());
    3. //Probably it's always a good idea to do a null check
    4. if(customItemDef != null){
    5. String itemname = customItemDef.getName();
    6. int maxStackSize = customItemDef.getMaxStacksize();
    7. }