How do I create a Chat Listener?

Discussion in 'Plugin Development' started by football70500, Jul 29, 2012.

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

    football70500

    Hello, I am in need of learning how to create a chat listener. I want to code a plugin that if a player says "Can I have op?" then a fake message gets sent to them saying "You are now Op!". This is all I need to know.
     
  2. Offline

    sd5

    Put that in your onEnable():
    Code:
    Bukkit.getPluginManager().registerEvents(new MyPluginListener(), this);
    Then create a new class which implements Listener:

    Code:
    public class MyPluginListener implements Listener {
     
        @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
            if(event.getMessage().equalsIgnoreCase("can i have op?"))
                event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
        }
     
    }
     
  3. Offline

    Sabersamus


    Code:java
    1.  
    2. @EventHandler
    3. public void onChat(PlayerChatEvent event){
    4. if(event.getMessage().equalsIgnoreCase("Can i have op?"){ //or something like that
    5. event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
    6. }
    7. }
     
  4. Offline

    football70500

    I get this error:
    PHP:
    2012-07-29 13:47:46 [SEVERECould not load 'plugins\FakeOpMe.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginExceptionjava.lang.ClassNotFoundExceptionme.football7050.FakeOp
        at org
    .bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:151)
        
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:214)
        
    at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:190)
        
    at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:55)
        
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:158)
        
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:424)
        
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused byjava.lang.ClassNotFoundExceptionme.football7050.FakeOp
        at java
    .net.URLClassLoader$1.run(Unknown Source)
        
    at java.security.AccessController.doPrivileged(Native Method)
        
    at java.net.URLClassLoader.findClass(Unknown Source)
        
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:41)
        
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29)
        
    at java.lang.ClassLoader.loadClass(Unknown Source)
        
    at java.lang.ClassLoader.loadClass(Unknown Source)
        
    at java.lang.Class.forName0(Native Method)
        
    at java.lang.Class.forName(Unknown Source)
        
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:140)
        ... 
    8 more
     
  5. Offline

    Firefly

    Look at your plugin.yml. The main part of it needs to read: yourpackagename.yourmainclass

    I'm suspecting you only have yourpackagename
     
  6. Offline

    football70500

    I get this error now:
    PHP:
    2012-07-29 13:56:36 [SEVERECould not load 'plugins\FakeOpMe.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginExceptionjava.lang.ClassNotFoundExceptionme.football7050.FakeOp.ChatMessage
        at org
    .bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:151)
        
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:214)
        
    at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:190)
        
    at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:55)
        
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:158)
        
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:424)
        
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused byjava.lang.ClassNotFoundExceptionme.football7050.FakeOp.ChatMessage
        at java
    .net.URLClassLoader$1.run(Unknown Source)
        
    at java.security.AccessController.doPrivileged(Native Method)
        
    at java.net.URLClassLoader.findClass(Unknown Source)
        
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:41)
        
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29)
        
    at java.lang.ClassLoader.loadClass(Unknown Source)
        
    at java.lang.ClassLoader.loadClass(Unknown Source)
        
    at java.lang.Class.forName0(Native Method)
        
    at java.lang.Class.forName(Unknown Source)
        
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:140)
        ... 
    8 more
    Heres my plugin. yml:
    Code:
    name: FakeOpMe
    main: me.football7050.FakeOp.ChatMessage
    version: 1.0
    
     
  7. Offline

    Firefly

    is ChatMessage the class that contains onEnable/onDisable?
     
  8. Offline

    football70500

    Yes

    I only have one class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    Firefly

    This is your package name where ChatMessage is contained?

    me.football7050.FakeOp
     
  10. Offline

    football70500

    I feel stupid, i forgot to add another 0 in football7050
     
  11. Offline

    Firefly

    That would be why :D
     
  12. Offline

    football70500

    I got it. :D

    Now, when I type can i have op? in the chat, nothing happens :( here is my code
    Code:
        @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
            if(event.getMessage().equalsIgnoreCase("can i have op?"))
                event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  13. Offline

    Firefly

    Did you register the event in your onEnable?
     
  14. Offline

    football70500

    So I put
    @EventHandler
    public void onPlayerChat(PlayerChatEvent event) {
    if(event.getMessage().equalsIgnoreCase("can i have op?"))
    event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
    }

    Only in MyPluginListener, right? not in ChatMessage
     
  15. Offline

    Firefly

    You can put it either place. You'll just have to change your registerEvents line accordingly.
     
  16. Offline

    football70500

    It works! There is a bug that when you type the message, You are now Op! appears before your message that you sent. How to fix? And Is there any way I could put like a 4-5 second delay after you type Can I have op?

    Also, I need to make it so that players can type more words than can I have op? So like Im from pmc, can i have op?
    I need it so it only reads the can I have op? message but players can still type something infront of it. How to do that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  17. Offline

    Firefly

    You could schedule a delayed task, and run the message after it's completed. http://wiki.bukkit.org/Scheduler_Programming

    For the more words thing. I figure something like this should work.

    if (msg.contains("can i have op?")) { //bla bla }
     
  18. Offline

    football70500

    what would msg be classifed in java?
     
  19. Offline

    Sabersamus

    event.getMessage()
     
  20. Offline

    football70500

    Wouldn't work thats what I currently have now
     
  21. Offline

    russjr08

    What is your current code? event.getMessage() should work. Don't forget to store it somewhere like


    String eventMessage = event.getMessage();
     
  22. Offline

    football70500

    I need it so players can type a message in front of can i have op, but atm, you have to type can i have op exactly, I need it so that they can type "Im from pmc, can I have op?"

    Here's my code:


    HTML:
    package me.football7500.FakeOp;
     
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class MyPluginListener implements Listener {
     
        @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
            if(event.getMessage().equalsIgnoreCase("can i have op?"))
                event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
        }
     
  23. Offline

    russjr08

    Try that :)
     
  24. Offline

    football70500

    I still have to type just can i have op? I could do Imcan I have op but there can't be a space where can starts. How to fix?

    Nvm I fixed it, how do I make a delay so like 5 seconds after they ask for op, they get that message

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  25. Offline

    Sabersamus


    no. you asked what msg was, in fireflys post, String msg = event.getMessage();

    Code:java
    1.  
    2. if(msg.contains("can i have op?"){
    3. event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
    4. }
     
  26. Offline

    Firefly

    I'm pretty sure I told you to change it to contains() Apparently you didn't listen to my suggestion D:
     
    stoneminer02 and Sabersamus like this.
Thread Status:
Not open for further replies.

Share This Page