Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Remove an NPC..

Comments in 'Plugin Development' started by Legoboy0215, Jul 15, 2015.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    I know I can do that via killing them, but I want it with code...
    I tried this: (Which obviously won't work)
    PHP:
    $name trim(implode(" "$args));
    $sender->getLevel()->removeEntity($name);
    Anyone can help me correct this code?
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    $entity $sender->getLevel()->getEntity($entityId);

    if (
    $entity !== null$entity->kill();
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    How do I know the id?
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Actually, after you get the entity object, you just need to do
    $entity->kill();

    Anyway, how do you want to select the entity?
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    With its name. My entity is an NPC.
  6. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Try this? rmb to use pocketmine\entity\Human
    PHP:
    foreach($sender->getLevel()->getEntities() as $e){
            if (
    $e instanceof Human && $e->getName() === "NPC"$e->kill(); //whoops, forgot a ';'
    }
    Legoboy0215 likes this.
  7. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    or use EntityDamageByEntityEvent maybe it is easier for you
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Nope.
    is much easier.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And if a player has the name called NPC?
    iksaku likes this.
  10. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    $e instanceof Human
    EDIT: Sorry. I forgot player IS a human.
  11. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Then this will work?
    PHP:
    foreach($sender->getLevel()->getEntities() as $e){
    if (
    $e instanceof Human && !($e instanceof Player) && $e->getName() === "NPC"$e->kill();
    }
    I always though Huamn is spawned "NPC"s and Player is real player :p
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Me too!
  13. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    NPC's are usually just player packets.
    jojoe77777 and ahoj_truhliku like this.
  14. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    but player NPC is not instance of player right?
  15. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    What is Player NPC? NPC stands for Non-Player Character, then how can it be an player?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In your case, "NPC" is only the spawned human's nametag. Yes, nametag. Human->getName() returns the nametag. But if your human is a player, it returns the player's login name. Either way, it doesn't identify your humans.
    Last edited: Jul 15, 2015
    iksaku likes this.
  17. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    If you're going to play with entities like creating, moving, editing and removing, then I will strongly recommend you to keep a track of all the entities you're creating, it's simple, just keep them in an array with any key of your choose, and when you need to interact with them, just access the array and you will get back the entity object ;)
    CrazedMiner likes this.
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And when the server restarts?
  19. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    Those are the advantages of NBT ;) use both ways of saving:
    - PocketMine-MP entities' NBT saving to keep the entity object saved
    - Custom plugin saving, for looking at certain NBT values of entities when enabled, so get back the class
  20. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    what is function to remove entity i tried close() and kill() but it doesnt remove any entity :/
    EDIT: how to remove entities in onEnable() method
    Last edited: Jul 15, 2015

Share This Page

Advertisement