onChat

Discussion in 'Plugin Development' started by BluePuppy00042, Jun 29, 2015.

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

    BluePuppy00042

    This isn't working, and I'm new. Anyone can help? :/
    Code:
         public void onChat(AsyncPlayerChatEvent e) {
                Player p = e.getPlayer();
        
                if(e.getMessage().equalsIgnoreCase("Ouch"));
                p.setHealth(20);
                Bukkit.broadcastMessage("Need some help?.");
     
  2. Offline

    glassbillen

    Have you put a @EventHandler tag?
    If not, just add @EventHandler
    public void onChat(AsyncPlayerChatEvent e) {
    And done ;)
     
    BluePuppy00042 likes this.
  3. Offline

    CrystallFTW

    @BluePuppy00042
    I think your code should look like this.
    Code:
    @EventHandler
    public void onChat(AsyncPlayerChatEvent e) {
                Player p = e.getPlayer();
       
                if(e.getMessage().equalsIgnoreCase("Ouch")){
                p.setHealth(20);
                Bukkit.broadcastMessage("Need some help?.");
      }
    }
    
    Also don't forget to register the events.
     
  4. Offline

    glassbillen

    Didnt you just do what i told him and added the part with: Also don't forget to register the events.?
    EDIT: Oh Didnt see the closing brackets

    @CrystallFTW
     
  5. Offline

    BluePuppy00042

    Alright, I changed it up some, and added @EventHandler and its still not working. This is what it looks like now

    Code:
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
           
            if (event.getMessage().toLowerCase().contains("ouch")){
                p.setHealth(20);
                p.setFoodLevel(20);
                Bukkit.broadcastMessage("Need some help?");
               
            }
                
         }
    }
     
  6. @BluePuppy00042
    1. Is your class implementing Listener?
    2. Are you registering events?
    3. If you're new, and don't know Java, I highly suggest learning it.
     
    PDKnight likes this.
  7. Offline

    BluePuppy00042

    I am new, thats what I am trying to do by making this plugin. Also, how would I register an event? Like, is that something like using @EventHandler?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    Zombie_Striker

    @BluePuppy00042
    • Don't use Aysnc, Use PlayerChatEvent
    • Why would you want to broadcast to the whole server
    • To learn about Java/Bukkit, Go Here
     
  10. @Zombie_Striker
    Yes use Async. In case you haven't noticed, PlayerChatEvent has been deprecated for a long time now because it slows down the chat because it posts the chat message to the server's main thread instead of keeping it async.
     
  11. Making a plugin isn't going to help you learn Java. Self teaching isn't an option here.
     
  12. Offline

    BluePuppy00042

    I'm not "self teaching" Ive been watching videos and stuff. Please dont tell me how to learn something, because im going to do it the way that I am most comfortable with.

    I don't see the message you're trying to get to me .-.

    EDIT by Timtower: merged posts
     
  13. Offline

    timtower Administrator Administrator Moderator

    I am talking about that, the link I posted is a tutorial on event handling, follow it and you will make a big step forward.
     
  14. Offline

    BluePuppy00042

    Oh, thank you.
     
  15. Even videos won't teach you the correct way to learn Java. They include some, if not many, bad practices. I'm not forcing you how to learn Java, just wanted to point you in the correct path.

    Anyway, @timtower gave you a link to a tutorial on event handling. I suggest reading it to see where you're going wrong.

    @timtower you ninja...
     
  16. Offline

    BluePuppy00042

    I went through all of that, and its still not working.
     
  17. Offline

    timtower Administrator Administrator Moderator

    @BluePuppy00042 Could you post all relevant code then? Your onEnable and your listener class
     
  18. Offline

    BluePuppy00042

    Main Class:
    Code:
    public class Main extends JavaPlugin{
        public void onEnable(){
            saveConfig();
            reloadConfig();
            getServer().getPluginManager().registerEvents((Listener) new Baymax(), this);
            }
        public void onDisable(){
            saveConfig();
            reloadConfig();
        }
    
    }
    
    Listener Class:
    Code:
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
           
            if (e.getMessage().toLowerCase().contains("ouch")){
                p.setHealth(20);
                p.setFoodLevel(20);
                Bukkit.broadcastMessage("Need some help?");
               
            }
        }
    }
    
     
  19. Offline

    timtower Administrator Administrator Moderator

  20. Offline

    BluePuppy00042

    Baymax is the Listener class
     
  21. Offline

    timtower Administrator Administrator Moderator

    Show that entire class please
     
  22. Offline

    BluePuppy00042

    Code:
    package me.auzzipuppy.baymax;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    import com.google.common.util.concurrent.Service.Listener;
    
    public class Baymax extends Listener {
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
           
            if (e.getMessage().toLowerCase().contains("ouch")){
                p.setHealth(20);
                p.setFoodLevel(20);
                Bukkit.broadcastMessage("Need some help?");
               
            }
        }
    }
    
     
  23. Offline

    timtower Administrator Administrator Moderator

    @BluePuppy00042 It is implements listener:
     
  24. Offline

    BluePuppy00042

    oooooh ._.
    Lets see if that works....YEP! Thank you so much <3 I knew it was implements listener, idk why i put extends -_-
     
  25. @BluePuppy00042 Please set this thread to Solved by going to Thread Tools (at the top) > Edit Title > Prefix > Solved.
     
Thread Status:
Not open for further replies.

Share This Page