Solved Despawn custom entity

Discussion in 'Plugin Development' started by Burnsalan18, May 29, 2014.

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

    Burnsalan18

    After I spawn a entity I want a timer to start for maybe 10 seconds. When the counter reaches zero I want the entity that I spawn to despawn. Any suggestions to this that wont lag my server?
     
  2. Offline

    RawCode

    post your own coding attemmpts or visit plugin request section.
     
  3. Offline

    XvBaseballkidvX

    Code:java
    1. public void removeEntity(final Entity e, int seconds){
    2. new BukkitRunnable(){
    3. public void run(){
    4. e.remove();
    5. }
    6. }.runTaskLater(Main.getInstance(), 20*seconds);
    7. }
     
  4. Offline

    Burnsalan18

    Oh sorry about that, forgot.
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            World world = p.getWorld();
        //    ItemStack itemInHand = p.getItemInHand();       
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Master Bane " + ChatColor.RED + "[HellHound]")) {
            if (COOLDOWN.containsKey(p.getName())){
            long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
            if (diff < COOLDOWN_TIME){
            p.sendMessage(ChatColor.DARK_AQUA + "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
            return;
            } // CoolDown
            } // CoolDown Key
           
     
           
            world.playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
            world.playSound(p.getLocation(), Sound.AMBIENCE_THUNDER, 2, 1);
            spawnMinion(p);
            event.getPlayer().sendMessage(ChatColor.GOLD + "You stab the sword into the ground summoning a " + ChatColor.RED + "HellHound");
            COOLDOWN.put(p.getName(), System.currentTimeMillis());   
            } // Action
        } // Listener
    } // Class
    Im not asking for you to do my code for me, all im asking is for suggestions on how I can go about doing this.
     
  5. Burnsalan18 if its extended entity, you can create integer in the class of the entity, and override h() to increase that integer by 1 every 1 tick, and then simply if(ticks amount > your despawn time)... this.die();
     
  6. Offline

    Burnsalan18

    Someone_Like_You Im not sure what you mean, are you saying to use a runnable?
     
  7. Burnsalan18 runnable would be your best option, is your entity is custom one ?(extended entity?)
     
  8. Offline

    Burnsalan18

    Someone_Like_You Ill post the code for it
    Code:
        public void spawnMinion(Player player){
            Wolf Wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
            Wolf.setCustomName(ChatColor.BLUE + "HellHound");
            Wolf.setCustomNameVisible(true);
            Wolf.setTamed(true);
            Wolf.setOwner(player);
            Wolf.setSitting(false);
            Wolf.setCollarColor(DyeColor.GREEN);
            Wolf.setMaxHealth(400);
            Wolf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
            Wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 10));
            Wolf.setFireTicks(Integer.MAX_VALUE);
        }
    
     
  9. Burnsalan18 okay, you might want to use the code of XvBaseballkidvX then.
    for your entity, if youd like to put it inside the spawnMinion() mehtod, youll probably do something like this :
    Code:java
    1. public void spawnMinion(Player player){
    2. final Wolf Wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
    3. Wolf.setCustomName(ChatColor.BLUE + "HellHound");
    4. Wolf.setCustomNameVisible(true);
    5. Wolf.setTamed(true);
    6. Wolf.setOwner(player);
    7. Wolf.setSitting(false);
    8. Wolf.setCollarColor(DyeColor.GREEN);
    9. Wolf.setMaxHealth(400);
    10. Wolf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
    11. Wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 10));
    12. Wolf.setFireTicks(Integer.MAX_VALUE);
    13. new BukkitRunnable(){
    14. public void run(){
    15. if(wolf.isVaild()){
    16. wolf.remove();
    17. }
    18. }
    19. }.runTaskLater(YOUR_PLUGIN, TICKS);
    20. //20 ticks = 1 second,
    21. note: I wrote it in here, means youll have to fix it
    22. }
     
  10. Offline

    Burnsalan18

  11. Burnsalan18 I wrote wolf instead of Wolf (as you called your wolf entity, Wolf Wolf = .....)
    edit, whats the error saying?.. eclipse error I geuss?
     
  12. Offline

    jimuskin

    Burnsalan18 Doesn't look like it's capitalized like the rest of your code. Maybe that's the error?

    Ninja'd
     
    Someone_Like_You likes this.
  13. Offline

    Burnsalan18

    Someone_Like_You I know, I saw before I typed it in that it was lower case. Only Wolf is errored, is it because I typed it in wrong?

    Code:
        public static Hellhoundsword plugin;
        public void spawnMinion(Player player){
            Wolf Wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
            Wolf.setCustomName(ChatColor.BLUE + "HellHound");
            Wolf.setCustomNameVisible(true);
            Wolf.setTamed(true);
            Wolf.setOwner(player);
            Wolf.setSitting(false);
            Wolf.setCollarColor(DyeColor.GREEN);
            Wolf.setMaxHealth(400);
            Wolf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
            Wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 10));
            Wolf.setFireTicks(Integer.MAX_VALUE);
              new BukkitRunnable(){
                    public void run(){
                        Wolf.remove();
                    }
                }.runTaskLater(plugin, 20*60);
        }
    
     
  14. Burnsalan18 try changing Wolf Wolf = ... to something like Wolf w = .... and set all methods to w, its might be cause youre calling your entity same name as its class
    edit: you dont have to call it w, just not the same name.. so wolf (with W) will be fine
     
  15. Offline

    Burnsalan18

    Someone_Like_You Changed all Wolf to w, still errored, could it be because I cant have a runnable there?

    Also, when I hover over it, it says to change the modifier of w to final
     
  16. Offline

    jimuskin

    Burnsalan18 Change

    Code:
    Wolf wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
    to
    Code:
    final Wolf wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
     
    Someone_Like_You likes this.
  17. Burnsalan18 add final infront of the Wolf
    final Wolf Wolf = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
     
  18. Offline

    jimuskin

    Someone_Like_You likes this.
  19. Offline

    Burnsalan18

    jimuskin Someone_Like_You That seems to have drop kicked the error, now im going to compile this and see if it all works :)

    Edit: Okay so im not getting any console errors but the entity is no longer spawning on rightclick, I think it might have to do with these two lines

    Code:
        public static Hellhoundsword plugin;
     
     
                }.runTaskLater(plugin, 20*60);
    The class that all of this is in is not the main class
     
  20. Burnsalan18 you can create instance of your plugin by doing this in your main class:
    private Plugin plugin; (put this on top)
    plugin = this; (add this to onEnable())
    and create a method like that:
    public static Plugin getPlugin(){
    return this.plugin;
    }
    after that, in your other classes, you can simply do MAIN_CLASS.getPlugin() :)

    jimuskin [​IMG]
     
  21. Offline

    Burnsalan18

    *Facepalms* I might have just figured it out after looking in my console, there was an error saying plugin already initialized, so I looked at my listener class main method and sure enough, it was extending javaplugin like the mainclass.
     
  22. Burnsalan18 haha... :) anything else? mind mark as Solved? (if it is)
     
  23. Offline

    Burnsalan18

    Someone_Like_You Lol fix one problem a new one arrises XD

    For some reason both the timers are cancelling each other out. When I spawn the wolf the despawn timer doesnt work and the delay for spawning another one also disables. Heres my full code
    Code:
    package me.burnsalan18.weapons;
     
    import java.util.HashMap;
     
     
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Effect;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Wolf;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
     
     
     
    public class Hellhoundsword implements Listener {
        private HashMap<String, Long> COOLDOWN = new HashMap<String, Long>();
        public int COOLDOWN_TIME = 180;   
        public void spawnMinion(Player player){
        final Wolf w = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
            w.setCustomName(ChatColor.BLUE + "HellHound");
            w.setCustomNameVisible(true);
            w.setTamed(true);
            w.setOwner(player);
            w.setSitting(false);
            w.setCollarColor(DyeColor.GREEN);
            w.setMaxHealth(400);
            w.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
            w.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 10));
            w.setFireTicks(Integer.MAX_VALUE);
              new BukkitRunnable(){
                    public void run(){
                        w.remove();
                    }
                }.runTaskLater(weapons.plugin, 20*2);
        }
       
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            World world = p.getWorld();       
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Master Bane " + ChatColor.RED + "[HellHound]")) {
            if (COOLDOWN.containsKey(p.getName())){
            long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
            if (diff < COOLDOWN_TIME){
            p.sendMessage(ChatColor.DARK_AQUA + "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
            return;
            } // CoolDown
            } // CoolDown Key
            world.playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
            world.playSound(p.getLocation(), Sound.AMBIENCE_THUNDER, 2, 1);
            spawnMinion(p);
            event.getPlayer().sendMessage(ChatColor.GOLD + "You stab the sword into the ground summoning a " + ChatColor.RED + "HellHound");
            COOLDOWN.put(p.getName(), System.currentTimeMillis());
            } // Action
        } // Listener
    } // Class
    
     
  24. Burnsalan18 Thats programming, solve one, get another :) , why dont you use same schedule to set the cooldown for the player?.. let me open eclipse for secnod

    Code:java
    1. package me.burnsalan18.weapons;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.UUID;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.DyeColor;
    8. import org.bukkit.Effect;
    9. import org.bukkit.Sound;
    10. import org.bukkit.World;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.entity.Wolf;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19. import org.bukkit.scheduler.BukkitRunnable;
    20.  
    21.  
    22.  
    23. public class Hellhoundsword implements Listener {
    24. ArrayList<UUID> cooldown = new ArrayList<UUID>();
    25.  
    26. public void spawnMinion(final Player player){
    27. final Wolf w = (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
    28. w.setCustomName(ChatColor.BLUE + "HellHound");
    29. w.setCustomNameVisible(true);
    30. w.setTamed(true);
    31. w.setOwner(player);
    32. w.setSitting(false);
    33. w.setCollarColor(DyeColor.GREEN);
    34. w.setMaxHealth(400);
    35. w.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
    36. w.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 10));
    37. w.setFireTicks(Integer.MAX_VALUE);
    38. //Starting the task to remove the wolf.
    39. Integer seconds_wolf = 10; //Seconds to remove wolf;
    40. new BukkitRunnable(){
    41. public void run(){
    42. w.remove();
    43. }
    44. }.runTaskLater(weapons.plugin, seconds_wolf);
    45. //finish wolf task;
    46.  
    47. //starting another task to remove the player from cooldown;
    48. Integer seconds_cooldown = 20;//Seconds to remove player from cooldown;
    49. new BukkitRunnable(){
    50. public void run(){
    51. if(cooldown.contains(player.getUniqueId())){
    52. cooldown.remove(player.getUniqueId());
    53. }
    54. }
    55. }.runTaskLater(weapons.plugin, seconds_cooldown);
    56. }
    57.  
    58.  
    59. @EventHandler
    60. public void onPlayerInteract(PlayerInteractEvent event) {
    61. Player p = event.getPlayer();
    62. World world = p.getWorld();
    63. if (event.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Master Bane " + ChatColor.RED + "[HellHound]")) {
    64. if (cooldown.contains(p.getUniqueId())){
    65. p.sendMessage(ChatColor.DARK_AQUA + "You must wait before you can do that again!");
    66. }else{
    67. world.playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    68. world.playSound(p.getLocation(), Sound.AMBIENCE_THUNDER, 2, 1);
    69. spawnMinion(p);
    70. event.getPlayer().sendMessage(ChatColor.GOLD + "You stab the sword into the ground summoning a " + ChatColor.RED + "HellHound");
    71. cooldown.add(p.getUniqueId());
    72. } // Action
    73. } // Listener
    74. }
    75. } // Class

    enjoy, its wont tell the player excact time, but it should work :)
     
  25. Offline

    Burnsalan18

    Actually...That might just work :D
     
    Someone_Like_You likes this.
  26. Burnsalan18 btw, Il suggest you to check if the item have display name (item...getItemMeta().hasDisplayName()
     
  27. Offline

    Burnsalan18

    Thank you so much for the help! Marking the topic solved now.
     
Thread Status:
Not open for further replies.

Share This Page