My First Bukkit plugin (send command, get answer)

Discussion in 'Plugin Development' started by 92Garfield, Jan 31, 2012.

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

    92Garfield

    I wanted to start makeing bukkit plugins. I'm not completly new to Java and I've programmed Flash a lot before.

    My Main problem is compiling the plugin correctly. I doubt there are errors in the source code:
    http://pastebin.com/V7yv1WT2
    But the config.yml might be wrong pls check that:
    http://pastebin.com/EvYCHKZv

    Eclipse does not show any errors, here is a picture of the setup.
    The Bukkit Library is referenced and the javad docs are linked.

    [​IMG]

    The error is get is:
    Code:
    2012-01-31 19:30:59 [SEVERE] Could not load 'plugins\Example.jar' in folder 'plugins': 
    java.lang.ClassNotFoundException: me.Garfield.Example
        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:175)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:188)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:109)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:156)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:132)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:52)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:407)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    If possible it would be nice if someone could post the source files of an empty plugin, that does nothing besides beeing loaded without errors.
     
  2. Offline

    emikodo

    For the yml file, you need to specify the package.main class. You have "me.Garfield.Example" which is the name of your package, but you're not specifying what main is. In this case it would be "me.Garfield.Example.Example".
    Hope this helps.

    Example.java
    Code:
    package me.emikodo.Example;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Example extends JavaPlugin{
        public void onEnable(){
        //do stuff
        }
     
        public void onDisable(){
     
        }
    }
    
    plugin.yml
    Code:
    name: Example
    main: me.emikodo.Example.Example
    version: 1.0
    
     
  3. Offline

    92Garfield

    Nice thanks.
    No more error.

    But it's not responding to my command either, could you tell me why?
    EDIT: Nevermind, was an error in the .yml again.

    EDIT2:
    Now I got the command working, but how would I modify the chat or react on it?
    I tried just using the onPlayerChat function, but it seems not to work, it's also marked as deprecated on the docs.

    Code:
        public void onPlayerChat(PlayerChatEvent event) {
            event.setMessage(event.getMessage().replaceAll("something", "*****"));
        }
     
  4. Offline

    NinjaWAffles

    So, I am not exactly on the right path, I don't believe, but I brewed up a simple example that you take look at and use.
    It's resources are on Pastebin.
    In that example, its just a few things:
    1) When a player says "/hello", then the server sends Hello, Welcome to the Server!
    2) When someone cusses as in saying:
    "This is a damn good plugin"
    - The plugin would recognize "damn" as stated in the playerChatListener, replace it to **** and then spit out: "This is a **** good plugin."

    I hope this helps, but if it didn't then feel free to PM me and I will try to the best of my abilities to help you.

    Thank you!
    ~Ninjawaffles
     
  5. Offline

    92Garfield

    Thanks a lot. That's exactly what I needed!

    Edit: The class playerChatListener is causing errors. I also had to add missing parameters to the registerEvent function.

    Code:
    getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, new playerChatListener(), Event.Priority.Normal, this);
    as for the playerChatListener, after fixing the errors and removeing non existant unused imports
    Code:
    package me.Garfield.Example;
     
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class playerChatListener implements Listener{
     
        public void onPlayerChat (PlayerChatEvent event)
        {
                event.setMessage(event.getMessage().replaceAll("damn", "****"));
        }
    }
    The error I get whenever I send a chat message is:
    Code:
    2012-02-01 13:40:45 [SEVERE] Could not pass event org.bukkit.event.player.PlayerChatEvent to Example
    java.lang.ClassCastException: me.Garfield.Example.playerChatListener cannot be cast to org.bukkit.event.player.PlayerListener
        at org.bukkit.plugin.java.JavaPluginLoader$6.execute(JavaPluginLoader.java:307)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:327)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:742)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:725)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:536)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Alright got it, changed the playerChatEvent to:
    Code:
    package me.Garfield.Example;
     
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class playerChatListener extends PlayerListener{
     
        public void onPlayerChat (PlayerChatEvent event)
        {
                event.setMessage(event.getMessage().replaceAll("damn", "****"));
        }
    }
     
  6. Offline

    NinjaWAffles

    That codes works for me. The Event System has changed so my code may not fully work with the Bukkit version you are currently using. Sorry if that code kinda helped or did not, but either way, I suggest as the Bukkit Dev team may eventually remove that Event System, that you upgrade. :D
     
  7. Offline

    92Garfield

    I'm using the API from 12. january.
    Gonna get the latest now.

    EDIT: API from 2. February and your code is fine. Why did they have to change it in those few days :<
     
  8. Offline

    NinjaWAffles

    I don't know why they changed it, but this Event System is much easier.
     
Thread Status:
Not open for further replies.

Share This Page