How to save an entity to a database and then load it back it with all data, including nbt

Discussion in 'Plugin Development' started by jonathanpecany, Nov 14, 2020.

  1. Offline

    jonathanpecany

    I am making this plugin that would need to save an entity data to like a database (Which I use PhpMyAdmin as it is more convenient for most cases.) and I am trying to save a list of entities to it as string with NBT data and everything as when to respawn it, it will continue having all it's data.

    I do plan to just store just the Entity, but I don't know how I would transform it back to an entity once turning it into a string as it will first store it in an array and then turn that array into a string, separated by commas and then turn back into an array to then turn it back into an Entity object, except I don't know how I would do that. I also don't know if just storing the whole Entity would store the nbt data or not. I am still kind of new to Java.

    I know there are other ways like storing the name and other excess data separate and such but that would be way too much work to do and would most likely kill performance. So I hope to get any help as possible to solve this problem as I tried looking on the internet and found absolute nothing that would help me.

    Also, mind explaining more about Entity's UUID a bit more as well. Like how does it work, like can you delete an entity completely and summon one with the UUID and it will have all the NBT data or what. Also had a bit of trouble finding an answer for this but it isn't off-topic to what I am asking as I might be able to use that instead of getting NBT but don't want the hassle of testing it through Minecraft as that will slow down the program I am using (eclipse) and then I will have to close Minecraft then restart, and yeah, too much of a hassle.

    Edit/Update: So I still need help with this problem I am having. I was thinking about just creating a whole class to be used inside a json and such but don't wanna store too much data and don't know how much would fit. Anyway to serialize the Entity and then store it inside JDBC and then later retrieve it like new. I have tried to do it using NMS but to no success.

    I'll accept any alternatives, as long as it isn't making me too do much work where I might forget something important and will cause a bug. I also wanna try to not use any APIs as that might just slow server start up more. I really desperately need help and don't know what to do that would be fast and takes less time and makes it less-bug efficient, as in too much work where it will cause bugs/hidden bugs.

    Thanks again.
     
    Last edited: Nov 24, 2020 at 10:05 PM
  2. Online

    timtower Moderator Moderator

  3. Offline

    jonathanpecany

    @timtower What do you mean by that?

    But if I am understanding you correctly though. My end goal is to simply do something like a pokemon ball type deal, but not doing that but I am needing to do something like that. If that makes sense. Keeping all entity info in tact the most efficient way possible. The more complex it is the more likely my code will not be as efficient.

    Here is a simple tree as a better explanation to what I am needing help for. All names are unique as to prevent confusion.
    Selecting Entity (From any available means like clicking, attacking, right-clicking, etc) [As well as removing entity once data been grabbed from it.] > Entity Object (Including NBT data | Grabbing) > Sending String > Store to Database > Receiving String > Entity Object (Including NBT data | Getting) > Spawn Entity

    I have managed to reach up to the Receiving String (In theory as I don't wanna constant restart Minecraft for performance sake.), but I just can't figure out how to transform it back into an entity, and does the entity object even have the NBT data built into it or not? I know that part may seem off-topic, but it isn't as it is part of the same idea.
     
    Last edited: Nov 14, 2020
  4. Online

    timtower Moderator Moderator

    @jonathanpecany Well, depending on the thing that you are trying to do there could be alternatives.
    Not sure about this one then.
     
  5. Offline

    jonathanpecany

    @timtower May I hear the alternatives then please. Is sharing alternatives allowed on bukkit forums as I know most forums doesn't allow that sort of thing.
     
    Last edited by a moderator: Nov 15, 2020
  6. Online

    timtower Moderator Moderator

    @jonathanpecany I don't have alternatives for you. And was talking about alternative methods to do this, not software.l
     
  7. Online

    Kars

    Have you tried serializing, deserializing and using world.addEntity?
     
    jonathanpecany likes this.
  8. Offline

    jonathanpecany

    @timtower Software? Don't remember mentioning anything about software. But alright then, thought you would have any since you mentioned that there could be alternatives.

    @Kars Not completely sure what Serializing means, care to share some basic info about it please, with examples, if possible.
     
  9. Online

    timtower Moderator Moderator

    Was talking about this. Don't see why having multiple solutions would be forbidden :p
     
  10. Offline

    jonathanpecany

    @timtower Just most places doesn't. Stack overflow doesn't allow it. But anyways, I would still like some help please.
     
  11. Online

    Kars

    @jonathanpecany google is your friend mate...
    But in short, serialization means to create a string representation of a Java object and deserialization is the opposite.
     
    jonathanpecany likes this.
  12. Offline

    jonathanpecany

    @Kars Me and Google hasn't really been getting a long recently. I still use google, but I have a lot of questions and Google doesn't like answering them.

    But thanks, and I asked about the example thing as to make it a bit convenient. Google might just almost (Not all) know everything but that said, not all websites will have up-to-date info and might not tag what version of Java something would be a part of, and all that.

    But thanks for the help though in thinking of an idea, you were a lot more helpful than timtower, who said there could be alternatives but he doesn't have an alternative.

    But I will do more research on serializing and deserializing and if I happen to solve my problem with your solution, I'll mark this post of mind as solved, but until then, I am still open to more suggestions or alternatives.
     
  13. Online

    Kars

    jonathanpecany likes this.
  14. Offline

    jonathanpecany

    @Kars Okay thanks, when testing the code you provided and inserting the blanks. It for some reason does not work and send a "IllegalArgumentException" error. Full error "java.lang.IllegalArgumentException: class net.minecraft.server.v1_16_R3.IBlockData declares multiple JSON fields named b". What would I be doing wrong here, I hope the answer isn't too complex.
     
  15. Online

    Kars

    @jonathanpecany that is out of your control. IBlockData and one of its superclasses both have a private variable named b.

    Why are you serializing blockdata though, i thought you wanted to serialize an entity?
     
  16. Offline

    jonathanpecany

    @Kars I am though, I don't know why it is doing that. I know I am getting an Entity.

    I am getting an entity from the PlayerInteractEntityEvent event and then getting event.getRightClicked(). So I honestly have no clue why it is doing a block. If that is what you are saying.

    I'll put in a simple block of code of how I am getting an entity.
    Code:
    @EventHandler
    public void onInteract (PlayerInteractEntityEvent event) {
        Entity entity = event.getRightClicked();
    }
    Which is the code I am starting with. Which I also included gson in it as well.
     
  17. Online

    Kars

  18. Offline

    jonathanpecany

    @Kars Okay, please do. I ain't even trying to get a block. Even if I was though, I wouldn't of asked for help as I would of just got the block name and not the whole thing.

    When you do, tell me how it goes. Because I know I am getting an entity.

    Also, when ever I was testing the code to see what was what and when sending a message to a player with entity name, for some odd reason it was sending it twice. I know I could use a counter and an if statement in it but I mainly just wanna know why it does that. Figure I ask here since it is still kind of part of the question and I would only make a new post if it is completely different.
     
  19. Offline

    jonthe445

    @jonathanpecany I think your best option is to set up an EntityClass, ie Squirtle with constructors for things such as 'name','health','attack',... etc. Then from the data base you can read/write appropriate updates to a single tuple including say PlayerID, EnttityID, name, health, attack that way you can access the tuple with the unique key (PlayerID + EnttiyID) retrieve the data and construct the 'pokemon' using the appropriate constructor, and summon from a method.

    If I am understanding your 'Pokemon' idea I think this is the better route and it wouldn't cost much overhead.
     
  20. Offline

    jonathanpecany

    @jonthe445 It isn't actually a Pokemon type plugin, it was an example to help people know what I need help with as about almost everyone knows what Pokemon is and could easily guess what I am needing help with. But thanks though. I am just looking for a way to be able to store an entire Entity object and then get the entire Entity so it is a lot easier and I make sure I don't accidentally miss anything that could cause any sort of bugs.
     
  21. Offline

    jonthe445

    @jonathanpecany
    Ok, I understand. I'm interested to see what you come up with. I still do not see why the entity needs to be the Exact same entity. I would just call a method the makes an "Exact" copy of said entity. Although it would probably have a new ID.
    I guess what I don't understand is why not just store all the desired attributes in a class and just use a method to generate all the different entity versions. Are you going to allow players to create their own entity attributes from within a menu gui or something? Example, we both summon our own versions of a creeper using your plugin and you want to be able to recreate your version of the creeper at some later time without repeating the 'Inital Setup' you already did?

    Guess this all goes back to @timtower question... What is your end goal?
     
  22. Offline

    jonathanpecany

    @jonthe445 I have mentioned my end goal but if you didn't understand it then I guess it is a bit hard to explain my end goal then. Thanks for the help anyways.
     
  23. Offline

    jonthe445

  24. Offline

    jonathanpecany

    @jonthe445 It's okay. Hopefully I can figure this out soon.
     
  25. Offline

    jonathanpecany

    Don't know if me editing my question bumps it or not, so just in case. *Bump*, there, bumped it, and I know doing that wasn't necessary, just wanted to type that.
     

Share This Page