Creating an Entity without spawning it yet

Discussion in 'Plugin Development' started by SharpBit, Mar 15, 2020.

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

    SharpBit

    Whenever a mob is spawned, I want to create an Entity of the same type but I do not want to spawn it in yet since I want to add the entity to the original mob as a passenger. How do I do this?
     
  2. Offline

    KarimAKL

    @SharpBit Why don't you want to spawn it, if it's supposed to be a passenger? Do you need it to be a passenger after a delay or something?
     
  3. Offline

    SharpBit

    Well world.spawnEntity() requires a location to be passed in, and I don't want it to spawn in a certain location before being added as a passenger
     
  4. Offline

    KarimAKL

    @SharpBit Just spawn it at the location of the entity you wish to add the passenger to. You shouldn't be able to see any difference.
     
  5. Offline

    SharpBit

    Is there a way to set a reason for spawn? Since im spawning an entity inside the CreatureSpawnEvent, it will just call the event again and lag the server
     
  6. Offline

    KarimAKL

    @SharpBit I looked it up, and it seems that you need to use NMS for that.

    I'd probably just do something like this:
    Code:Java
    1. private final Set<UUID> entities = new HashSet<>();
    2.  
    3. @EventHandler
    4. private void onSpawn(CreatureSpawnEvent event) {
    5. if (entities.contains(event.getEntity().getUniqueId())) {
    6. entities.remove(event.getEntity().getUniqueId());
    7. return;
    8. }
    9.  
    10. // Your code
    11.  
    12. entities.add(passenger);
    13. }
     
  7. Offline

    SharpBit

  8. Offline

    KarimAKL

    @SharpBit Oh yeah, that's my bad. :7

    I don't think you can create an entity without spawning it with the Bukkit API. (I'm pretty sure it's possible with NMS though)

    Try printing the value of event.getSpawnReason(), if it's "CUSTOM" for your passenger, just return if the reason is "CUSTOM".
     
  9. Offline

    SharpBit

    It seems like i got "potentially dangerous stackoverflow" but when i reran the server, i didn't see that anymore, instead saw this: https://hasteb.in/educaguy.txt

    Again, the error repeats infinitely...

    I added this to the beginning of the function btw:

    if (event.getSpawnReason().equals("CUSTOM")) {
    return;
    }
     
  10. Offline

    vildaberper

    SpawnReason.CUSTOM is not a String.
    When I do thease "nested events" I simply flip a boolean to ignore the next event call. But I don't see any reason why comparing SpawnReason wouldn't work in this case.
     
  11. Offline

    KarimAKL

    @SharpBit As @vildaberper already mentioned, SpawnReason isn't a String, it's an Enum; compare it like this:
    Code:Java
    1. if (event.getSpawnReason() == SpawnReason.CUSTOM) {/*...*/}
     
  12. Offline

    SharpBit

    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page