1.15.2 Custom items

Discussion in 'Spigot Plugin Development' started by Gerolmed, Mar 10, 2020.

  1. Currently I am doing the following: I have a custom class Item that is extended by all kinds of custom classes.
    I can serilize this to and from an itemstack. Specific slots in the player inventory are only allowed to contain specific item types.
    After the player interacts with the inventory. The whole inventory is serialized to an array of my custom item class. Now I check if any of my custom items is at the wrong place. If yes I cancel the inventory action. Picking up an item will cause it to serialize and deserialize again. (Item names/lore are translated to players language)

    Any ideas/recommendations how I can improve the process?
  2. Put all of these datas in RAM (like in a list or hashmap)
  3. They are already and I am quite unhappy with that. It forces me to constantly synchronize it on every user action additionally to validating the actions.
  4. I'm not 100% sure why you serialize / deserialize your items every time.
    If you just want to find out if the item is one of your custom items you can use the PersistenDataContainer API.
    Depends on what kind of info you need to store, but if you just need to tag your item in some way to determine if it is one of yours, you can add any value at all with your plugin key, and just check if the item's data container has that key.

    You can store pretty much everything in the data container, but it might require you to do some code re-writing to get all your old functionality working with the API.
    (For example you can make an item that gains XP, or counts mob kills, and it will store that right on the item. The server will know about it after restart, no separate HashMaps to keep track of anything required.)
    • Agree Agree x 1
  5. I am already using that to store serialized items (though I use json as I also use that for backend storage)
    The data I store is fairly complex. Lists sub classes etc. Pretty much everything a java object can store. The item data varies depending on the type of the item (I got it bound to my own serialization system).
    These custom item classes also have different implementations to define how the item behaves based on interfaces.
    So yeah that's sadly not a thing I can do also there are no default minecraft items just my custom ones. Specific inventory slots are bound to different item types e.g. amulets. other items (key items) may not be dropped
  6. It really depends on how your custom data is set up. I was hoping that you did not need to deserialze your objects every time, just check to see if they were "tagged" in some way. For example you don't need to check an amulets stats if you are just concerned about which inventory slot it is allowed to be move to, you could just check if it has been tagged as an amulet (the persistent data container key).

    If you want better performance out of your serialization process you need to ditch the JSON and use some binary format. JSON is really easy to serialize, but takes up many more bits than are necessary. For example storing an int is 4 bytes in binary format, but in JSON it is stored something like "myNum:1337" which is 10 characters long making this data 80 bytes in UTF-8, or 160 bytes in Unicode. The data size alone makes accessing that data at least 20-40 times slower using JSON instead of binary, and that is without the overhead of determining data types etc.

    This may not be a possibility for you at this point, as you can just store all of the items info in one String container. Doing this in some binary format most likely requires multiple Keys and data sets for each value type, and very well could be a complete re-write.

    That's really all I can suggest, "tag" the items some way as oppose to deserialize them if possible, and possibly only check the item slots that have moved, and not the entire inventory.
    • Like Like x 1
  7. The saving time is a good point though. Rn i am using json as i got that supported quite easily. I am using my own library Realm Drive to convert classes to storable data and back. So saving (onto items not for the backend part) it to some other format should work as long as I can convert it from RealmObjects to that type and back.
    The json part on the item was only a temporary thing though. Probably gonna store it to some kind of nbt format and attach that to the items.
    I might adapt your idea of storing the slot type and go for attaching that as an additional property to the persistence container for runtime speed.