Solved Removing an Entity

Discussion in 'Spigot Plugin Development' started by tatemylove, Aug 17, 2018.

  1. I'm trying to remove the creeper entity after it's been spawned and I'm not quite sure why I can't get it to despawn after the player logs out. No errors in the console

    Code (Text):
     final Creeper creeper = p.getWorld().spawn(p.getLocation(), Creeper.class);
                        creeper.setCustomName("§el" + p.getName() + "'s §6Pet");
                        creeper.setCustomNameVisible(true);
                        main.followPlayer(p, creeper, 1.75D);

                       new BukkitRunnable(){

                           @Override
                           public void run() {
                               if((!p.isOnline())){
                                   creeper.remove();
                                   cancel();
                               }
                           }
                       }.runTaskTimer(main, 0, 10);
                    }
     
  2. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Entity.html#remove-- I dont think thats the right way to remove the entity. The remove just "marks" it.
     
  3. #setHealth() or #damage() could work
     
    • Agree Agree x 1
  4. Both of those don't work, I've tried that before as well.
     
  5. Ok, what this is for is a pets plugin for my server. That's why it needs to be removed when the player switches to a different server or logs out completely. I honestly don't know why it's not working because in my COD plugin I used wolves as attack dogs and did wolf.remove() and it completely removed them
     
  6. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    Instead of storing it into one final, store it in a hashmap that is initialized on onEnable()

    In thé HasmMap you store thé player UUID as key and the Entity as value.

    When player leaves, first check if the map contains your player, if so, get thé value (which is your pet) then remove that pet instead.

    Be aware that you need to make sure no other things may Destroy your pet and if so, implement a code to remove it from the player.

    I would even recommend creating a new class Pet containing 2 properties , Pet owner and Entity.

    Then store this into a list and use a lambda to filter out your result.

    Last option will be thé most clean

    Verstuurd vanaf mijn ONEPLUS A3003 met Tapatalk
     
  7. Yeah I basically did that yesterday where I created a different operation to monitor if the plater left. I basically just assigned the pet a metadata of the player and checked if the player left and of the server contained the entity of that meta and removed it.