Strange Bug/Error

Discussion in 'Plugin Development' started by regice202, Apr 16, 2014.

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

    regice202

    I've been recently starting to learn about developing plugins and it was going pretty smooth I was ironing out the bugs by myself for the most part but.. I changed 1 line of code that should really have very little relation with what I was trying to do. It was spawning an arrow depending on where the player's direction was (worked fine). But then I seemed to have a problem with a line of code that stopped all types of damages and I only wanted it to stop arrow damage. And ever since I made it so it only focuses on arrow damage the code spawning the arrow won't work :/
    here's the code:

    Code:
    @EventHandler
        public void onRightCick(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action a = event.getAction();
            if (a == (Action.RIGHT_CLICK_AIR) || a == (Action.RIGHT_CLICK_BLOCK)){
                player.sendMessage("test1");
                if (player.getItemInHand().getType() == (Material.TRIPWIRE_HOOK)){
                    player.sendMessage("test2");
                    if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <=225 ){
                        Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, -1), Arrow.class);
                        arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                        player.sendMessage("test3");
                    }
                    if (player.getLocation().getYaw() >= 225 && player.getLocation().getYaw() <=270 ){
                        Entity arrow = player.getWorld().spawn(player.getLocation().add(1, 1.7, 0), Arrow.class);
                        arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                        player.sendMessage("test3");
                    }
                    if (player.getLocation().getYaw() >= 0 && player.getLocation().getYaw() <=45 ){
                        Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, 1), Arrow.class);
                        arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                        player.sendMessage("test3");
                    }
                    if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <=135 ){
                        Entity arrow = player.getWorld().spawn(player.getLocation().add(-1, 1.7, 0), Arrow.class);
                        arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                        player.sendMessage("test3");
                    }
                    if (player.getLocation().getYaw() >= 315 && player.getLocation().getYaw() <=360 ){
                        Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, 1), Arrow.class);
                        arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                        player.sendMessage("test3");
                    }
                    else{
                        player.sendMessage("test4");
                    }
                }
            }
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
            if (event.getCause() == DamageCause.PROJECTILE){
                    event.setDamage(-1);
            }
        }
    and yes it is a registered event cause it worked before and I never touched the register for the event. I attached a picture of the current result. thanks for reading and hope you can help!

    -regice202
     

    Attached Files:

  2. Offline

    rfsantos1996

    Set damage to 0 or cancel event
     
  3. Offline

    Maurdekye

    regice202 Add some debug messages; perhaps some events are being called when you don't expect them to be, or the if statement doesn't cover every aspect you're expecting. Also, don't set the damage to 0; just cancel the event. It's much more smooth from a code point of view.
     
  4. Offline

    regice202

    Maurdekye I changed it so that the event is now cancelled instead of setting the damage. But after I added some debug messages it still won't work everything is right, i have no errors anywhere, and it worked before. I added some messages to see if the yaw thing had anything to do with it or the && and the yaw part is the only thing stopping it for some reason. new code:
    Code:
    @EventHandler
        public void onRightCick(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action a = event.getAction();
            if (a == (Action.RIGHT_CLICK_AIR) || a == (Action.RIGHT_CLICK_BLOCK)){
                player.sendMessage("test1");
                if (player.getItemInHand().getType() == (Material.TRIPWIRE_HOOK)){
                    player.sendMessage("test2");
                    if (player.getLocation().getYaw() >= 135){
                        if (player.getLocation().getYaw() <=225 ){
                            Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, -1), Arrow.class);
                            arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                            player.sendMessage("test3");
                        }
                    }
                    if (player.getLocation().getYaw() >= 225){
                        player.sendMessage("test5");
                        if (player.getLocation().getYaw() <=270 ){
                            Entity arrow = player.getWorld().spawn(player.getLocation().add(1, 1.7, 0), Arrow.class);
                            arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                            player.sendMessage("test3");
                        }
                    }
                    if (player.getLocation().getYaw() >= 0){
                        player.sendMessage("test6");
                        if (player.getLocation().getYaw() <=45 ){
                            Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, 1), Arrow.class);
                            arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                            player.sendMessage("test3");
                        }
                    }
                    if (player.getLocation().getYaw() >= 45){
                        player.sendMessage("test7");
                        if (player.getLocation().getYaw() <=135 ){
                            Entity arrow = player.getWorld().spawn(player.getLocation().add(-1, 1.7, 0), Arrow.class);
                            arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                            player.sendMessage("test3");
                        }
                    }
                    if (player.getLocation().getYaw() >= 315){
                        player.sendMessage("test8");
                        if (player.getLocation().getYaw() <=360 ){
                            Entity arrow = player.getWorld().spawn(player.getLocation().add(0, 1.7, 1), Arrow.class);
                            arrow.setVelocity(player.getLocation().getDirection().multiply(6));
                            player.sendMessage("test3");
                        }
                    }
                    else{
                        player.sendMessage("test4");
                    }
                }
            }
        }
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
            if (event.getCause() == DamageCause.PROJECTILE){
                    event.setCancelled(true);
            }
        }
    when I try to execute it the only messages i get are test1, test2, and test4. I'm almost positive this is a bug. please continue to help! :D thanks for the suggestions!
    -regice202
     
  5. Offline

    Maurdekye

    regice202 Add a System.out.print() to the EntityDamageByEntityEvent, inside the if statement. Check to see when it's being called, and with what kind of information. Maybe it isn't reaching that part of the code at all, or maybe it's getting there more often then you'd like.
     
Thread Status:
Not open for further replies.

Share This Page