1.17.x Custom Entity registry from String

Discussion in 'Spigot Plugin Development' started by onEnable, Jun 26, 2021.

  1. First and foremost I apologize for the threads name, let me explain in depth the situation.

    Right now I store location data along with an entities name within JSON formatting, I convert this in to usable information ie; locations etc.. No issues.

    The situation I've come across is that I need to take a string, ie; the entities name, and load it in whenever a chunk is loaded or unloaded. What I'm trying to do is prevent a case where I end up doing:

    Code (Text):

    if(name.equals("entity")) then
       new Entity(loc)
    end
     
    Or a switch case, whatever it may be. I will have numerous custom entities and registry of them that way would be relatively, eh.

    So, how can I take a string, then using that string find out the custom entity class and load the entity in.

    I have considered using reflection, not sure if this is the best way of going about it though!
     
  2. You could assign an UUID or (integer) ID to each new entity, then creating a method like "loadEntity(UUID)" or "loadEntity(ID)"

    You could create your UUID for an entity like that: UUID#nameUUIDFromBytes(("Entity: " + entityObject").getBytes());
    To create the id, it would be easier, just store the latest generated ID and add 1