p.isBlocking() not working

Discussion in 'Plugin Development' started by Hex_27, Apr 21, 2015.

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

    Hex_27

    This code is under PlayerInteractEvent
    Code:
                    for(String enchantname: enchantment.keySet()){
                        p.sendMessage("4");
                    String s = getConfig().getString(enchantname + ".special");
                    if(s.equals("RESISTANCE")){
                        p.sendMessage("5");
                    if(event.getPlayer().getItemInHand().getType().toString().endsWith("_SWORD")){
                        p.sendMessage("6");
                        while(p.isBlocking()){
                            p.sendMessage("7");
                            p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 1));
                           
                        }
                    }
                }
                }
    4,5 and 6 is sent to the player, but 7 is not, and the player doesn't have the potion effect. Why is this so?
     
  2. Offline

    teej107

    @Hex_27 You might be better off replacing the #isBlocking() method to using the PlayerInteractEvent and checking for a right click.
     
  3. Offline

    Hex_27

    @teej107 it is possible to use the while() loop to keep checking if the player is right-clicking?
     
  4. @Hex_27 Nope, that will freeze the server as soon as they're blocking. Since isBlocking() will be true until the client tells the server otherwise, and the server won't be accepting packets since it's too busy with your loop, it'll be an infinite loop.
     
  5. Offline

    EcMiner

    The while loop isn't working because the PlayerInteractEvent actually gets called just before it updates the blocking status, so you'd have to delay the while loop by 1 tick. But using the while loop is a really bad idea because it will actually freeze your server when someone right clicks with a sword in his hand, use a repeating task instead.
     
Thread Status:
Not open for further replies.

Share This Page