Killing Entities

Discussion in 'Plugin Development' started by KaiBB, Dec 20, 2011.

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

    KaiBB

    What's the command for killing mobs? I know the one for spawning is (spawnEntity), and I already tried killEntity. Help? ^-^
     
  2. EntityListener.onEntityDeath(EntityDeathEvent) :)
     
  3. Offline

    KaiBB

    Can I run this event on a command? That's more of what I meant.
     
  4. What?
    Do you want to kill an entity? Or do you want the event when a entity is killed?
     
  5. Offline

    KaiBB

    I should probably rephrase it, eh? I want to kill the entity on command. This is my second post, nobody replied to the first.
     
  6. If you want to kill the player who typed a command, you should use:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. if (!(sender instanceof Player)) {
    4. // Tell them they have to be a player
    5. return true;
    6. }
    7. Player player = (Player) sender;
    8. player.damage(10000);
    9. return true;
    10. }
    11.  
     
  7. Offline

    KaiBB

    Well I want it to kill a spawned EnderDragon ^-^ . Does that I mean I would need a higher damage?
     
  8. No, it just means you would have to replace player.damage with the ender dragon instance, and you can remove the rest of the code :p
     
  9. Offline

    KaiBB

    Thanks :D

    Wait, would it kill every EnderDragon then? This is what I want, btw.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  10. No..but to do that its way easier :p
    This will kill all them in every world..To change that change the w variable to the world you want to kill all of them in
    Code:java
    1.  
    2. for (World w : Bukkit.getWorlds()) {
    3. for (Entity e : w.getEntities()) {
    4. if (e instanceof EnderDragon) {
    5. e.damage(100000000000000000);
    6. }
    7. }
    8. }
    9.  
     
  11. Offline

    Chiller

    Too many zeros........ Why not 1's?
     
  12. It doesn't matter, I just made it a high amount so that I know it will die ;)
     
  13. Offline

    KaiBB

    I have one error, on the damage. Should I add it to the cast 'e'? Sorry, handling mobs isn't exactly my best skill :p
     
  14. oh, my bad.... try this:
    Code:java
    1.  
    2. for (World w : Bukkit.getWorlds()) {
    3. for (Entity e : w.getEntities()) {
    4. if (e instanceof EnderDragon) {
    5. ((EnderDragon) e).damage(10000000000000);
    6. }
    7. }
    8. }
    9.  
     
  15. Offline

    KaiBB

    Would I just go back to where I spawned it and at player.getWorld() set that to w?
     
  16. That would work
     
  17. Offline

    Chiller

    But I don't want that many zeros...
     
  18. Sorry hun, I tried my hardest D:
     
  19. Offline

    KaiBB

    One line below where I spawned the dragon I did this:
    Code:
    player.getWorld(); World w = w;
    And it says "the local variable w may not have been initialized" .

    Yeah, it was too many, so it came up an error. Just two 0's too many ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  20. Offline

    Chiller

    I told him that, but he didn't listen...
     
  21. Offline

    KaiBB

    I'd prefer one of you just helped with the variable problem :p
     
  22. An error? Forrealz? :confused:
    I answered the variable problem! Replace for (World w : Bukkit.getWorlds()) with World w = ((Player) sender).getWorld(); after you make sure the sender is a player
     
  23. Offline

    Chiller

    Normally you would just change that at the very beginning of the code! Which would be: Player player = (Player) sender;
     
  24. Offline

    KaiBB

    @Chiller
    @tips48
    what? you've confused me. :confused:
    Here is my full code:
    Code:
    package me.kai.spawnenderdragon;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.CreatureType;
    import org.bukkit.entity.EnderDragon;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SpawnDragon extends JavaPlugin {
        public static SpawnDragon plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " is enabled.");
        }
        @Override
        public void onDisable() {
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " is disabled.");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            readCommand((Player) sender, commandLabel);
            return false;
        }
    
        public void readCommand(Player player, String command) {
            if(command.equalsIgnoreCase("spawndragon")) {
                player.getWorld().spawnCreature(player.getLocation(), CreatureType.ENDER_DRAGON);
                player.getWorld(); World w = w;
            }
            else if(command.equalsIgnoreCase("killdragon")) {
    
                for (World w : Bukkit.getWorlds()) {
                    for (Entity e : w.getEntities()) {
                    if (e instanceof EnderDragon) {
                    ((EnderDragon) e).damage(1000000000);
                    }
                    }
                    }
    
            }
        }
    }
     
  25. Offline

    Chiller

    Try returning true in onCommand
     
  26. NO! BAD! First you have to do if (sender instanceof Player) so the console doesn't get a error when the preform it

    Replace
    Code:java
    1.  
    2. [FONT=Consolas] else if(command.equalsIgnoreCase("killdragon")) { for (World w : Bukkit.getWorlds()) { for (Entity e : w.getEntities()) { if (e instanceof EnderDragon) { ((EnderDragon) e).damage(1000000000); } } } }[/FONT]
    3. [FONT=Consolas][/FONT]

    with
    Code:java
    1.  
    Code:java
    1.  
    2. [FONT=Consolas][FONT=Consolas] else if(command.equalsIgnoreCase("killdragon")) { World w = player.getWorld(); for (Entity e : w.getEntities()) { if (e instanceof EnderDragon) { ((EnderDragon) e).damage(1000000000); } } } }[/FONT][/FONT]
    3.  

    Wacky formatting, sorry

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  27. Offline

    Chiller

    Its still at the beginning though...
     
  28. But thats before it ;)
     
  29. Offline

    KaiBB

    Thanks, this seems to work. Now I just needa test it.
     
  30. Sure thing :)
     
Thread Status:
Not open for further replies.

Share This Page