Arrow Repaiting task

Discussion in 'Plugin Development' started by bronzzze, Mar 22, 2015.

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

    bronzzze

    I am just writing one code for fun and I have some problems this is my code:
    Code:
    package me.bronzzze.arrow;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import particlelib.ParticleEffect;
    
    public class Main extends JavaPlugin implements Listener {
    
        public int task1;
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onArrowHit(ProjectileLaunchEvent event) {
            if (!(event.getEntity().getShooter() instanceof Player)) {
                return;
            }
            Player p = (Player) event.getEntity().getShooter();
            if (event.getEntity() instanceof Arrow) {
                final Arrow arrow = (Arrow) event.getEntity();
    
                if (p.getItemInHand().getType() == Material.BOW){
                   
                    task1 = Bukkit.getServer().getScheduler()
                            .scheduleSyncRepeatingTask(this, new Runnable() {
    
                                 
                             
                           
    
                                public void run() {
                                ParticleEffect.HEART.display(0,0,0,5,5,arrow.getLocation(),30);                        
                                   
                            }
                               
                            }, 1, 1);
    
                   
                }
              
                }
         
                }
                @SuppressWarnings("deprecation")
                @EventHandler
                public void onArrowHit(ProjectileHitEvent event) {
                   
                    if (!(event.getEntity().getShooter() instanceof Player)) {
                        return;
                    }
                    if (event.getEntity() instanceof Arrow) {
                        Bukkit.getScheduler().cancelTask(task1);
                       
           
        }
                }
       
    }
    I set when arrow hit it cancel task. But if i shoot more arrow before they hit floor Its work for only one.

    Here is one picture Task is not canceled.
    [​IMG]
     
    Last edited by a moderator: Mar 22, 2015
  2. you need a HashMap<Entity,Integer> where you store the task ids.
    Dont't you think that 1 ticks (0.05 seconds) is pretty fast for the runnable?
     
  3. Offline

    bronzzze

    Oh ty.
    No 1 tick its cool. Is this causing laggs if its only 1 tick beacuse it looks nicer?
     
  4. Depends on your ram. But it shouldn't lag. Only when there are maaaaaany arrows
     
  5. Offline

    bronzzze

    Ok. Thaks for fast respond I will try to add Hashmap. I just hashmap.put() in Task and hashmap.remove in arrowhitEvent right or something like this?
     
  6. Offline

    bronzzze

    I make like this. I am sure i made something stupid.
    Code:
        public HashMap<Entity,Integer> arrows = new HashMap<Entity,Integer>();      
    
    I dont know what to put instance of integer so I put 1
    Code:
        
    final Arrow arrow = (Arrow) event.getEntity();                            
    arrows.put(arrow, 1);
    
    On hitEvent
    task1 Is BukkitTask
    Code:
                if(arrows.containsKey(arrow)){
            task1.cancel();
     
  7. No.
    A HashMap contains Key and Value.
    The Entity(arrow) is the key.
    And the Integer(id of the scheduler) is the value.
    We can get the id(value) from the arrow(key) with hashmap.get(key)

    Make something like this:
    Code:
    int currentTaskID = yourSchedulerHere();
    arrows.put(arrow, currentTaskID);
    and to remove it:
    Code:
    Bukkit.getScheduler().cancelTask(arrows.get(arrow));
    arrows.remove(arrow);
     
  8. Offline

    bronzzze

    I put arrow in Hashmap and task there
    Code:
    arrows.put(arrow, task1);
    And there on hit event I cancelled task and remove arrow:
    Code:
    Bukkit.getScheduler().cancelTask(arrows.get(arrow));
                arrows.remove(arrow);
    Its not working
     
  9. Show your full code.
     
  10. @bronzzze
    You need to create a new class that extends BukkitRunnable. That will be your task.
    You start it onEnable and never stop it. then create a list and store the arrows. On your task, if the list isn`t empty, send the effect to every arrow on that list.
     
  11. Offline

    bronzzze

    Full code:
    Code:
    package me.bronzzze.arrow;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import particlelib.ParticleEffect;
    
    public class Main extends JavaPlugin implements Listener {
    
        public int task1;
        public HashMap<Entity, Integer> arrows = new HashMap<Entity, Integer>();
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onArrowLaunch(final ProjectileLaunchEvent event) {
    
            if (!(event.getEntity().getShooter() instanceof Player)) {
                return;
            }
            final Player p = (Player) event.getEntity().getShooter();
            if (event.getEntity() instanceof Arrow) {
                final Arrow arrow = (Arrow) event.getEntity();
                final World w = p.getWorld();
    
                if (p.getItemInHand().getType() == Material.BOW) {
    
                    task1 = Bukkit.getServer().getScheduler()
                            .scheduleSyncRepeatingTask(this, new Runnable() {
                                private int time = 12;
    
                                @Override
                                public void run() {
    
                                    arrows.put(arrow, task1);
                                    final Location l = arrow.getLocation();
                                    final Location loc1 = new Location(w,
                                            l.getX() + 0.5D, l.getY(), l.getZ());
                                    final Location loc2 = new Location(w,
                                            l.getX() + 0.43D, l.getY(),
                                            l.getZ() + 0.25D);
                                    final Location loc3 = new Location(w,
                                            l.getX() + 0.25D, l.getY(),
                                            l.getZ() + 0.43D);
                                    final Location loc4 = new Location(w, l.getX(),
                                            l.getY(), l.getZ() + 0.5D);
                                    final Location loc5 = new Location(w,
                                            l.getX() - 0.25D, l.getY(),
                                            l.getZ() + 0.43D);
                                    final Location loc6 = new Location(w,
                                            l.getX() - 0.43D, l.getY(),
                                            l.getZ() + 0.25D);
                                    final Location loc7 = new Location(w,
                                            l.getX() - 0.5D, l.getY(), l.getZ());
                                    final Location loc8 = new Location(w,
                                            l.getX() - 0.43D, l.getY(),
                                            l.getZ() - 0.25D);
                                    final Location loc9 = new Location(w,
                                            l.getX() - 0.25D, l.getY(),
                                            l.getZ() - 0.43D);
                                    final Location loc10 = new Location(w,
                                            l.getX(), l.getY(), l.getZ() - 0.5D);
                                    final Location loc11 = new Location(w,
                                            l.getX() + 0.25D, l.getY(),
                                            l.getZ() - 0.43D);
                                    final Location loc12 = new Location(w,
                                            l.getX() + 0.43D, l.getY(),
                                            l.getZ() - 0.25D);
                                    if (time == 12) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc1, 50);
                                    }
                                    if (time == 11) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc2, 50);
    
                                    }
                                    if (time == 10) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc3, 15);
                                    }
                                    if (time == 9) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc4, 50);
                                    }
                                    if (time == 8) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc5, 50);
                                    }
                                    if (time == 7) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc6, 50);
                                    }
                                    if (time == 6) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc7, 50);
                                    }
                                    if (time == 5) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc8, 50);
                                    }
                                    if (time == 4) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc9, 50);
                                    }
                                    if (time == 3) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc10, 50);
                                    }
                                    if (time == 2) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc11, 50);
                                    }
                                    if (time == 1) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc12, 50);
                                    }
                                    if (time == 0) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc1, 50);
                                        time = time + 12;
                                    }
                                    time--;
                                }
    
                            }, 1, 1);
    
                }
    
            }
    
        }
    
        @EventHandler
        public void onArrowHit(ProjectileHitEvent event) {
            if (event.getEntity() instanceof Arrow) {
                final Arrow arrow = (Arrow) event.getEntity();
                Bukkit.getScheduler().cancelTask(arrows.get(arrow));
                arrows.remove(arrow);
    
            }
    
        }
    
    }
    Edit:
    I dont know how to work when you extend bukkitrunnable
     
  12. You never put the arrow & id in the HashMap
    You need to put it in after the Runnable

    @Juancomaster1998 Thats not true! Don't give an answer if youre not knowing what youre doing.
     
    Last edited by a moderator: Mar 23, 2015
    bronzzze likes this.
  13. @FisheyLP
    Don`t use a hashmap, you can use an ArrayList, which usses less resources.

    EDIT: oh, I see, you`ve an animation. then yes, use an hashmap.

    @FisheyLP
    What is not true? I`ve been using that for tons of games tested with 100+ users and it had always worked perfectly for me. If it doesn`t works to you is because you don`t know how to.
     
    Last edited by a moderator: Mar 23, 2015
  14. Offline

    bronzzze

    Code:
                    task1 = Bukkit.getServer().getScheduler()
                            .scheduleSyncRepeatingTask(this, new Runnable() {
                                private int time = 12;
    
                                @Override
                                public void run() {
    
                                    final Location l = arrow.getLocation();
                                    final Location loc1 = new Location(w,
                                            l.getX() + 0.5D, l.getY(), l.getZ());
                                    final Location loc2 = new Location(w,
                                            l.getX() + 0.43D, l.getY(),
                                            l.getZ() + 0.25D);
                                    final Location loc3 = new Location(w,
                                            l.getX() + 0.25D, l.getY(),
                                            l.getZ() + 0.43D);
                                    final Location loc4 = new Location(w, l.getX(),
                                            l.getY(), l.getZ() + 0.5D);
                                    final Location loc5 = new Location(w,
                                            l.getX() - 0.25D, l.getY(),
                                            l.getZ() + 0.43D);
                                    final Location loc6 = new Location(w,
                                            l.getX() - 0.43D, l.getY(),
                                            l.getZ() + 0.25D);
                                    final Location loc7 = new Location(w,
                                            l.getX() - 0.5D, l.getY(), l.getZ());
                                    final Location loc8 = new Location(w,
                                            l.getX() - 0.43D, l.getY(),
                                            l.getZ() - 0.25D);
                                    final Location loc9 = new Location(w,
                                            l.getX() - 0.25D, l.getY(),
                                            l.getZ() - 0.43D);
                                    final Location loc10 = new Location(w,
                                            l.getX(), l.getY(), l.getZ() - 0.5D);
                                    final Location loc11 = new Location(w,
                                            l.getX() + 0.25D, l.getY(),
                                            l.getZ() - 0.43D);
                                    final Location loc12 = new Location(w,
                                            l.getX() + 0.43D, l.getY(),
                                            l.getZ() - 0.25D);
                                    if (time == 12) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc1, 50);
                                    }
                                    if (time == 11) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc2, 50);
    
                                    }
                                    if (time == 10) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc3, 15);
                                    }
                                    if (time == 9) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc4, 50);
                                    }
                                    if (time == 8) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc5, 50);
                                    }
                                    if (time == 7) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc6, 50);
                                    }
                                    if (time == 6) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc7, 50);
                                    }
                                    if (time == 5) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc8, 50);
                                    }
                                    if (time == 4) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc9, 50);
                                    }
                                    if (time == 3) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc10, 50);
                                    }
                                    if (time == 2) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc11, 50);
                                    }
                                    if (time == 1) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc12, 50);
                                    }
                                    if (time == 0) {
                                        ParticleEffect.HEART.display(0, 0, 0, 4, 5,
                                                loc1, 50);
                                        time = time + 12;
                                    }
                                    time--;
                                }
                           
    
    
                            }, 1, 1);
                    arrows.put(arrow, task1);
    
    I put it like this and now is working
     
    FisheyLP likes this.
  15. @Juancomaster1998 He need to cancel the task with the ID. HashMap is the best because you can get the ID from the entity. ArrayList could only have either ID or Entity. That wouldnt work *facepalm* :p
     
    bronzzze likes this.
  16. Offline

    bronzzze

    Ty Fishey for help:)
     
  17. @FisheyLP
    well, so you`re creating one task for every arrow on the world? that`s the worst idea I`ve never seen.
     
  18. I'm not creating the task. @bronzzze is doing it :p Ask him why
     
  19. Offline

    bronzzze

    How should i do then?
     
  20. @FisheyLP
    because he doesn`t know best ways to do it.
     
  21. Offline

    bronzzze

    I am pretty new at Bukkit
     
  22. Then do it like @Juancomaster1998 said:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, runnable) is the method. Just use instead of new Runnable(){} your class which extends BukkitRunnable
     
    Last edited by a moderator: Mar 23, 2015
  23. @bronzzze
    Well, I`ll recommend something like:
    Code:
        public void onEnable() {
            new runnable().runTaskTimer(this, 20, 1);
        }
       
        @EventHandler
        public void launch(ProjectileLaunchEvent e) {
            if (e.getEntity() instanceof Arrow) runnable.arrows.put((Arrow)e.getEntity(), 0);
        }
       
        @EventHandler
        public void hit(ProjectileHitEvent e) {
            if (e.getEntity() instanceof Arrow) runnable.arrows.remove((Arrow)e.getEntity());
        }
       
        public static class runnable extends BukkitRunnable {
    
            public static HashMap<Arrow, Integer> arrows = new HashMap<Arrow, Integer>();
           
            public runnable() { }
           
            public void run() {
                if (!arrows.isEmpty()) {
                    for (Arrow arrow : arrows.keySet()) {
                        int time = arrows.get(arrow);
                        anim(arrow, time);
                        time++;
                        if (time > 12) time = 0;
                        arrows.put(arrow, time);
                    }
                }
            }
           
            public static void anim(Arrow arrow, int time) {
                final Location l = arrow.getLocation().clone();
                if (time == 1) l.setX(l.getX()+0.5D);
                else if (time == 2) {
                    l.setX(l.getX()+0.43D);
                    l.setY(l.getY()+0.25D);
                } else if (time == 3) {
                    l.setX(l.getX()+0.25D);
                    l.setZ(l.getZ()+0.43D);
                } else if (time == 4) l.setZ(l.getZ()+0.5D);
                else if (time == 5) {
                    l.setX(l.getX()-0.25D);
                    l.setZ(l.getZ()+0.43D);
                } else if (time == 6) {
                    l.setX(l.getX()-0.43D);
                    l.setZ(l.getZ()+0.25D);
                } else if (time == 7) l.setX(l.getX()-0.5D);
                else if (time == 8) {
                    l.setX(l.getX()-0.43D);
                    l.setZ(l.getZ()-0.25D);
                } else if (time == 9) {
                    l.setX(l.getX()-0.25D);
                    l.setZ(l.getZ()-0.43D);
                } else if (time == 10) l.setZ(l.getZ()-0.5D);
                else if (time == 11) {
                    l.setX(l.getX()+0.25D);
                    l.setZ(l.getZ()-0.43D);
                } else if (time == 12) {
                    l.setX(l.getX()+0.43D);
                    l.setZ(l.getZ()-0.25D);
                }
                ParticleEffect.HEART.display(0, 0, 0, 4, 5, l, 50);
            }
        }
     
  24. Online

    timtower Administrator Administrator Moderator

    Merged a couple posts.
    To all: please use the edit button instead of double posting.
     
Thread Status:
Not open for further replies.

Share This Page