Delay after a chat message

Discussion in 'Plugin Development' started by goga_m, May 15, 2016.

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

    goga_m

    So im trying to make a kind of respond plugin, like when a player says "Who is the owner" then it is gonna bordcast a message that says "The owners are *names*"
    yet it does not seem to work


    Code:
    public class PlayerChat implements Listener {
    
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            String message = event.getMessage().toLowerCase();
    
            if (message.contains("Hello"))
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
                        Bukkit.broadcastMessage(ChatColor.RED + "Hello there " + p.getDisplayName());
                    }
                }, 20L);
    
        }
    }
     
  2. Offline

    teej107

    @goga_m Check your if statement. You think a String of all lowercase characters is going to contain "Hello"?
     
  3. Offline

    goga_m

    Thank you @teej107, but that dident fix my problem :/
     
  4. Offline

    Zombie_Striker

    @goga_m
    1. Are you sure you registered this event?
    2. Have you debugged? Are you sure the if statement ever is true?
    3. Are you sure the player is final?
     
  5. Offline

    goga_m

    It all worked fine, when i dident have the delay in the code, so i guess the event is registered

    (i am very new to bukkit and java coding)

    Looks like this without the delay
    Code:
    public class PlayerChat implements Listener {
    
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            String message = event.getMessage().toLowerCase();
    
            if (message.contains("Hello"));
            Bukkit.broadcastMessage(ChatColor.RED + "Hello there " + p.getDisplayName());
    
        }
    
    }
    
     
  6. Offline

    I Al Istannen

    @goga_m
    "scheduleSyncDelayedTask(this".
    The PlayerChat class is NOT the Main one (extending JavaPlugin). You need to pass it an instance of the JavaPlugin class. (I might be wrong, but I really shouldn't)

    What exactly is the error you get?
     
  7. Offline

    teej107

    What did you try?
     
  8. Offline

    timtower Administrator Administrator Moderator

    Cleaned up thread.
    If I removed too much: repost it.
    Thank you for staying on-topic to the ones that did.
    For the ones that didn't: please go to offtopic and discuss it there.
     
  9. Offline

    goga_m

    @teej107 the plugin still works fine, if it is Hello, and also works if it is hello

    in eclipse, the only error i get in there is at .scheduleSyncDelayedTask
    Code:
       Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() { 
    -The error says
    So i tried to change "this" to PlayerChat (name of the class file) and PlayerChat says "PlayerChat cannot be resolved to a variable"

    If anyone needs the main class file, here it is:

    Code:
    package com.thegogabox.plugin;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.thegogabox.plugin.commands.DayCommand;
    import com.thegogabox.plugin.commands.Lag;
    import com.thegogabox.plugin.commands.Mode;
    import com.thegogabox.plugin.commands.NightCommand;
    import com.thegogabox.plugin.commands.Ping;
    import com.thegogabox.plugin.commands.Spawn;
    import com.thegogabox.plugin.commands.World;
    import com.thegogabox.plugin.playerEvent.PlayerChat;
    
    public class Main extends JavaPlugin {
    
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
           
            registerCommands();
            registerEvents();
           
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Lag(), 100L, 1L);
           
            logger.info("This message is shown to make sure that" + pdfFile.getName() + " which is created by " + pdfFile.getAuthors() + " and running version: " + pdfFile.getVersion() + " have been loaded correctly!");
        }
    
        private void registerCommands() {
            getCommand("spawn").setExecutor(new Spawn());
            getCommand("world").setExecutor(new World ());
            getCommand("ping").setExecutor(new Ping());
            getCommand("day").setExecutor(new DayCommand());
            getCommand("night").setExecutor(new NightCommand());
            getCommand("mode").setExecutor(new Mode());
           
        }
    
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents((Listener) new PlayerChat(), this);
        }
    }
    
     
    Last edited: May 16, 2016
  10. Offline

    I Al Istannen

    @goga_m
     
  11. Offline

    goga_m

  12. Offline

    I Al Istannen

    @goga_m
    There is no ultimate answer. You need to pass an instance of the main class. How you get that is your choice. You could pass it through the constructor or create a Plugin Singleton. You could get it via Bukkit#getPluginManager().getPlugin(String name / Class mainClass). There are hundreds of ways, some preferred, some not. The "cleanest" way I know is passing it through the constructor, but the others can also be viable choices.
     
Thread Status:
Not open for further replies.

Share This Page