cancelling event

Discussion in 'Plugin Development' started by DevManABCD, Jul 22, 2014.

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

    DevManABCD

    Code:java
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onChat(AsyncPlayerChatEvent e) {
    3. if(e.getMessage().startsWith("!")) {
    4. e.setCancelled(true);
    5. for(Player p : Bukkit.getOnlinePlayers()) {
    6. e.setCancelled(true);
    7. if(enabled.containsKey(p)) {
    8. IChatBaseComponent comp = ChatSerializer
    9. .a("{\"text\":\"" +
    10. "\",\"extra\":[{\"text\":\"" + "§8<§4Odpisz§8> §8[§2Handel§8] §a" + p.getName() + ChatColor.WHITE + ": §7" + ChatColor.ITALIC + e.getMessage().replaceAll("!", "") +
    11. "\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
    12. "/msg " +p.getName() + " Czesc! Jestem zainteresowany twoja oferta!" + "\"}}]}");
    13. PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
    14. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    15. e.setCancelled(true);
    16. e.setCancelled(true);
    17. }
    18. e.setCancelled(true);
    19. e.setCancelled(true);
    20. }
    21. e.setCancelled(true);
    22. }



    Why e.setCanceled(true); didnt work? When i chat !offer or !hey its trade channel i still se !hey its trade channel in main chat ;c
     
  2. Offline

    Niknea

    DevManABCD Try replacing:
    PHP:
    e.setCancelled(true)
    to
    PHP:
    e.setMessage("");
    Also, the amount of canceling your doing is completely unnecessary.
     
  3. Offline

    Konkz

    You cancel the event after checking the first time, yet you continue to cancel them x2 most of the time?
     
  4. I have to admit, the fact you have more event.setCancelled(true) in that method than other lines combined is pretty impressive - it appears 7 times while there are only 6 other calls.

    Debug the code to see which parts are executed.
     
  5. Offline

    JuicyDev

    Code:java
    1. enabled.containsKey(p)

    You appear to be testing if a Player object is in a map. If you are indeed storing Player objects in a map then you shouldn't because it causes memory leaks. If you aren't storing Player objects in a map then this is probably your mistake here.
     
  6. Offline

    DevManABCD

    I
    was testing disabling evrnt, thats reason why i put that amount of this function. I just set it up to null, now it works, thanks.
     
Thread Status:
Not open for further replies.

Share This Page