Solved extends JavaPlugin implements Listener

Discussion in 'Spigot Plugin Development' started by Dori_mon, Jun 22, 2016.

  1. I can't get it!!!
    What is the problem here?
    Code (Text):
    package me.Dori_mon.PluginHider;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            System.out.println(">> PluginHider has been Enabled! <<");
            System.out.println(">> Plugin by Dori_mon <<");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable() {
            System.out.println("<< PluginHider has been Disabled! >>");
            System.out.println("<< Plugin by Dori_mon >>");
        }
       
        @EventHandler
        public void onPlayerExecuteCommand(PlayerCommandPreprocessEvent event) {
            if (event.getMessage().equalsIgnoreCase("/pl")) {
                String prefix = getConfig().getString("prefix");
                prefix = ChatColor.translateAlternateColorCodes('&', prefix);
                String message = getConfig().getString("message");
                message = ChatColor.translateAlternateColorCodes('&', message);
                Player p = event.getPlayer();
                event.setCancelled(true);
                p.sendMessage(prefix + " " + message);
            }
        }

    }
     

    There are no errors and it creats folder with config so the problem is with the registering of extends JavaPlugin implements Listener on the same class. But I need them both to use getConfig().
    Can someone help me?
     
  2. Register your listener on enable
     
  3. How can I register event on my onEnable()?
     
  4. And use:
    publicboolean onCommand(CommandSender sender, Command cmd, String label, String[] args){

    instead of the Event
     
  5. I prefer to use the event, I just need help with register events
     
  6. getServer().getPluginManager().registerEvents(this, this);
     
  7. After reading over your actually even though. It seems like you are trying to make a command. Using a PlayerCommandPreprocessEvent is not what you want in this case. Just use the regular Command Executor. Using the PreprocessEvent is for dealing with special symbols like @a, @p, <players>, etc.
     
  8. No just no. The way he is doing it right now is fine. He needs to add this to his onEnable()
    Code (Text):
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
     
  9. What do you mean? It can be used to execute things like what he is doing
     
  10. Code (Text):
    package me.Dori_mon.PluginHider;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            this.getCommand("pl").setExecutor(this);
            System.out.println(">> PluginHider has been Enabled! <<");
            System.out.println(">> Plugin by Dori_mon <<");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable() {
            System.out.println("<< PluginHider has been Disabled! >>");
            System.out.println("<< Plugin by Dori_mon >>");
        }
       
        @EventHandler
        public void onPlayerExecuteCommand(PlayerCommandPreprocessEvent event) {
            if (event.getMessage().equalsIgnoreCase("/pl")) {
                String prefix = getConfig().getString("prefix");
                prefix = ChatColor.translateAlternateColorCodes('&', prefix);
                String message = getConfig().getString("message");
                message = ChatColor.translateAlternateColorCodes('&', message);
                Player p = event.getPlayer();
                event.setCancelled(true);
                p.sendMessage(prefix + " " + message);
            }
        }

    }
     

    What's the problem here?
     
  11. Thanks Bear, you are right, but in my opinion the Commandexecutor would be the finer way, but dealing it with the Event is also fine
     
  12. replace:
    this.getCommand("pl").setExecutor(this);
    with
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    and it should work fine
     
  13. Code (Text):
    package me.Dori_mon.PluginHider;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            System.out.println(">> PluginHider has been Enabled! <<");
            System.out.println(">> Plugin by Dori_mon <<");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable() {
            System.out.println("<< PluginHider has been Disabled! >>");
            System.out.println("<< Plugin by Dori_mon >>");
        }
       
        @EventHandler
        public void onPlayerExecuteCommand(PlayerCommandPreprocessEvent event) {
            if (event.getMessage().equalsIgnoreCase("/pl")) {
                String prefix = getConfig().getString("prefix");
                prefix = ChatColor.translateAlternateColorCodes('&', prefix);
                String message = getConfig().getString("message");
                message = ChatColor.translateAlternateColorCodes('&', message);
                Player p = event.getPlayer();
                event.setCancelled(true);
                p.sendMessage(prefix + " " + message);
            }
        }

    }
     


    And what is the problem here? bear?
     
  14. Look at my post. I spooned you the code because I want you to get it and I understand the docs may be a little hard for new people to read
     
  15. This is the error:
    Code (Text):
    [20:50:44 ERROR]: Could not pass event PlayerCommandPreprocessEvent to PluginHider v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1290) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1157) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:683) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:639) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:543) [craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    Caused by: java.lang.NullPointerException
            at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:210) ~[craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            at me.Dori_mon.PluginHider.Main.onPlayerExecuteCommand(Main.java:30) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_72]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_72]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_72]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_72]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.9.4.jar:git-Bukkit-c5e9a16]
            ... 16 more
     
  16. Nothing, looks good to me as long as your plugin.yml is correct it should work
     

Share This Page