Solved Cancel chat Message?

Discussion in 'Plugin Development' started by shohouku, Jun 24, 2014.

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

    shohouku

    So I'm trying to cancel a word when a player types it, but it's not working.

    I have no errors from my console, all my events are registered and I have listeners.
    My event is not getting cancelled.


    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. final Player p = event.getPlayer();
    4. if(skill1.contains(p.getName())) {
    5. if(event.getMessage().contains("UpgradeSkill")) {
    6. event.setCancelled(true);
    7. userupgrade.set(p.getName(), null);
    8. skill1.set(p.getName(), null);
    9. Search(p,1);
    10. if(points.getInt(p.getName() + ".Skillpoints") > 0) {
    11. takeSkillPoint(p, 1);
    12. }
    13. p.sendMessage(ChatColor.BOLD + "Your new skill level on [Search] is now:" + points.getInt(p.getName() + ".Search"));
    14. p.sendMessage(ChatColor.BOLD + "Unused Skill Points: " + points.getInt(p.getName() + ".Skillpoints"));
    15. }
    16.  
    17. } else {
    18. //WORK!
    19. }
    20. }
     
  2. Offline

    Konkz

    Do checks to see what conditions pass, I'm assuming you're typing "Upgradeskill" if won't trigger as the S is not capital.
     
  3. Offline

    shohouku


    Everything passes, but the message isn't getting cancelled.

    I think it's the bukkit development build..
     
  4. Offline

    Konkz

    Can I see your debugged code? shohouku
     
  5. Offline

    shohouku


    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. System.out.println(1);
    4. final Player p = event.getPlayer();
    5. System.out.println(2);
    6. if(skill1.contains(p.getName())) {
    7. System.out.println(3);
    8. if(event.getMessage().contains("UpgradeSkill")) {
    9. System.out.println(4);
    10. event.setCancelled(true);
    11. System.out.println(5);
    12. userupgrade.set(p.getName(), null);
    13. System.out.println(6);
    14. skill1.set(p.getName(), null);
    15. System.out.println(7);
    16. Search(p,1);
    17. if(points.getInt(p.getName() + ".Skillpoints") > 0) {
    18. takeSkillPoint(p, 1);
    19. }
    20. p.sendMessage(ChatColor.BOLD + "Your new skill level on [Search] is now:" + points.getInt(p.getName() + ".Search"));
    21. p.sendMessage(ChatColor.BOLD + "Unused Skill Points: " + points.getInt(p.getName() + ".Skillpoints"));
    22. }


    All of the numbers are being debugged to my console..


    So my event is being cancelled but it's not? :\
     
  6. Offline

    Konkz

     
  7. Offline

    shohouku


    hmmh, this was un-cancelling my event:

    I tried checking if the word was "UpgradeSkill" and tried cancelling it but it still didn't work.
    Code:java
    1. @EventHandler
    2. public void playerChat(AsyncPlayerChatEvent event) {
    3. String rawMessage = event.getMessage();
    4. String message = "<" + event.getPlayer().getDisplayName() + "> " + rawMessage;
    5. final Player p = (Player) event.getPlayer();
    6. Location playerLocation = event.getPlayer().getLocation();
    7. for (Player pl : event.getRecipients()) {
    8. if (pl.getLocation().distance(playerLocation) <= 50) {
    9. pl.sendMessage(message);
    10. } else {
    11. Random random = new Random();
    12. int randomNum = random.nextInt(100) + 1;
    13. if (randomNum <= 60) {
    14. p.sendMessage(ChatColor.ITALIC + "The wind passes by...no one hears you...");
    15. }
    16. }
    17. event.getRecipients().clear();
    18. }
    19. if(message.contains("UpgradeSkill")) {
    20. event.setCancelled(true);
    21. }
    22. }
     
  8. Offline

    Konkz


    Comment out all the code inside a method and simply just do event.setCancelled(true); and see if it will work. If it won't than it's another plugin that sets it back to false.
     
    shohouku likes this.
  9. Offline

    shohouku


    Thanks, it worked :)
     
    Konkz likes this.
Thread Status:
Not open for further replies.

Share This Page