Solved Arrow/Effect Error

Discussion in 'Plugin Development' started by Coopah, May 29, 2015.

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

    Coopah

    So, what this code is doing is basically when a player shoots a arrow it will check if the arrow is still in the air and then if it is, play an effect.

    The issue is, if the previously shot arrows are still somewhere in the ground and a player shoots another arrow the effect will begin to play from all the previous arrows locations. Is there a way where I can make it only play the effect from the arrows that are still in the air and not the ones that have landed?

    Code:
    Code:
    @EventHandler
        public void ProjLand(ProjectileHitEvent e) {
           
            Entity ent = e.getEntity();
            @SuppressWarnings("deprecation")
            Entity shooterE = e.getEntity().getShooter();
           
            if (ent instanceof Arrow && shooterE instanceof Player) {
               
                Player shooter = (Player) shooterE;
               
                if (customArrows.contains(shooter.getName())) {
                   
                    customArrows.remove(shooter.getName());
                   
                }
            }
        }
    
        @EventHandler
        public void onProjShoot(ProjectileLaunchEvent e) {
    
            @SuppressWarnings("deprecation")
            Entity shooterE = e.getEntity().getShooter();
            Entity ent = e.getEntity();
    
            if (shooterE instanceof Player && ent instanceof Arrow) {
    
                final Player shooter = (Player) shooterE;
                final Arrow arw = (Arrow) ent;
    
                if (CripplingArrows.contains(shooter.getName())) {
                   
                    customArrows.add(shooter.getName());
    
                    Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
    
                        public void run() {
    
                            if(customArrows.contains(shooter.getName())) {
    
                                shooter.getWorld().playEffect(arw.getLocation(), Effect.SMOKE, 1);
    
                            }
                        }
                    }, 0, 0);
                }
            }
        }
     
  2. Offline

    Agentleader1

    In the task timer, check if the projectile is dead or has no velocity.
     
  3. Offline

    Coopah

    @Agentleader1
    Code:
    if (!arw.isDead() || arw.getVelocity().equals(null)) {
    I added this line but the same error occurs. I believe this line is only going to check the arrow that has just been shot? It's not actually going to check all of the arrows?
     
  4. Offline

    Agentleader1

    @Coopah First off, if it's giving errors, check for each x,y,z velocity ==0. If you want to check multiple arrows(*), get entities in a world.
     
    Last edited: May 29, 2015
  5. Offline

    AdamQpzm

    This is not the way to null check. You cannot execute methods on null objects.
     
    bwfcwalshy and KingFaris11 like this.
  6. Offline

    MaTaMoR_

    To check null things
    Code:
    if(object == null) { }
    
     
  7. Offline

    I Al Istannen

  8. Offline

    Coopah

    @Agentleader1
    @MaTaMoR_
    @AdamQpzm
    @I Al Istannen
    I was doing it a completely stupid way.
    Fixed it by doing this:
    Code:
    public ArrayList<Projectile> smokeArrows = new ArrayList<Projectile>();
        public ArrayList<Projectile> piercingArrows = new ArrayList<Projectile>();
    
        public void addSmokeParticleEffect(final Projectile entity) {
    
            Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
    
                public void run() {
    
                    if (smokeArrows.contains(entity)) {
    
                        entity.getWorld().playEffect(entity.getLocation(), Effect.SMOKE, 1);
    
                    }
                }
            }, 0, 0);
        }
    
        public void addPiercingParticleEffect(final Projectile entity) {
    
            Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
    
                public void run() {
    
                    if (piercingArrows.contains(entity)) {
    
                        entity.getWorld().playEffect(entity.getLocation(), Effect.STEP_SOUND, Material.GLASS);
    
                    }
                }
            }, 0, 0);
        }
    
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onArrowLand(EntityDamageByEntityEvent e) {
    
            Entity damagerE = e.getDamager();
            Entity damagedE = e.getEntity();
    
            if (damagerE instanceof Arrow && damagedE instanceof Player) {
    
                Player damaged = (Player) damagedE;
                final Arrow arw = (Arrow) damagerE;
    
                if (arw.getShooter() instanceof Player) {
    
                    final Player damager = (Player) arw.getShooter();
    
                    if (ArrowToggles.CripplingArrows.contains(damager.getName())) {
    
                        damaged.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 60, 1));
    
                    } else if (ArrowToggles.PiercingArrows.contains(damager.getName())) {
    
                        e.setDamage(0D);
                        damaged.damage(4D);
                    }
                }
            }
        }
    
        @EventHandler
        public void ProjLand(ProjectileHitEvent e) {
    
            Entity ent = e.getEntity();
    
            if (ent instanceof Arrow) {
    
                Arrow arrow = (Arrow) ent;
    
                if (smokeArrows.contains(arrow)) {
    
                    smokeArrows.remove(arrow);
    
                } else if (piercingArrows.contains(arrow)) {
    
                    piercingArrows.remove(arrow);
    
                }
            }
        }
    
        @EventHandler
        public void onProjShoot(ProjectileLaunchEvent e) {
    
            @SuppressWarnings("deprecation")
            Entity shooterE = e.getEntity().getShooter();
            Entity ent = e.getEntity();
    
            if (shooterE instanceof Player && ent instanceof Arrow) {
    
                final Player shooter = (Player) shooterE;
                final Arrow arw = (Arrow) ent;
    
                if (ArrowToggles.CripplingArrows.contains(shooter.getName())) {
    
                    smokeArrows.add(arw);
                    addSmokeParticleEffect(arw);
    
                } else if (ArrowToggles.PiercingArrows.contains(shooter.getName())) {
    
                    piercingArrows.add(arw);
                    addPiercingParticleEffect(arw);
    
                }
            }
        }
     
    Last edited: May 30, 2015
  9. Offline

    AdamQpzm

    @Coopah
    1) Program to an interface, not an implementation. You shouldn't be declaring as an ArrayList.
    2) Why is the field public?
    3) Checking if a list contains an object before removing it is pointless.
    4) CripplingArrows does not follow naming conventions
    5) CripplingArrows is a public static field - why?

    All in all, I still maintain that you do not know enough Java to be making plugins, because you continue to make these same basic mistakes. Please learn Java before trying to make plugins, otherwise you will always run into these problems, and won't know what you're doing wrong nor how to fix it. Bukkit is written in Java so learning it is compulsory. I recommend the Oracle tutorials or a Java book.
     
  10. Offline

    Coopah

    @AdamQpzm
    Stop making false accusations it's pathetic. Do you know that humans make mistakes? This code was made in minutes then I tested it and it worked fine. I don't stare at my code for hours on end, I actually have a life :)

    You do love to start arguments don't you? Because I've seen you on countless threads trying to infuriate the person requesting help. This forums is here to help people not to try and find flaws in the code then point them out and throw accusations at them without giving any help whatsoever. All you do is scare new comers off.
     
  11. Offline

    AdamQpzm

    @Coopah These forums are for help with bukkit, not with Java. My advice is never meant to scare people off, it is meant to tell them they need to know Java before learning Bukkit. I believe that's what's happened here. Of course people can make mistakes, I make mistakes myself.

    However what I don't believe is that all of what you posted was simply mistakes. People who know enough programming don't tend to 'accidentally' declare as ArrayList instead of List, declare as public instead of private at least twice, accidentally start a field with a capital name and then use it with a capital name, and accidentally make it static. You're telling me that you accidentally did all that and didn't realise it? I don't buy it. If you still insist it was a mistake, then you're still in the wrong: Did you not check your code for mistakes before posting here? You should be doing that.
     
    KingFaris11 likes this.
  12. Offline

    Coopah

    @AdamQpzm
    Lets take this to PM. I'm willing to have a civilized discussion with you.
     
  13. Offline

    xXMaTTHDXx

    @Coopah instead of assuming he is attacking you, re read what he is saying and take it into consideration. I, when I first started programming, went straight into the BukkitAPI as soon as I know how to compile code. This was a huge mistake and I ended up stopping and teaching myself Java, then came back.
     
    AdamQpzm likes this.
  14. Offline

    Rocoty

    @Coopah He does make some valid points. You should consider them all. I know for a fact that @AdamQpzm knows what he's talking about.
     
    AdamQpzm likes this.
  15. Offline

    KingFaris11

    THANK YOU.

    Going a bit off-topic here (sorry), but I've always wondered why I see that I'm like the only one who does:
    Code:
    List<String> x = new ArrayList<String>();
    Not:
    Code:
    ArrayList<String> x = new ArrayList<String>();
    When viewing other people's SRC.

    Guess that's how it's meant to be.
     
Thread Status:
Not open for further replies.

Share This Page