Really confused about Simple Plugin Development!

Discussion in 'Plugin Development' started by Flafla2, May 2, 2011.

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

    Flafla2

    Hi, I want to become a plugin developer, and I don't really know Java. I however DO know many other object-oriented languages(Objective-C,Javascript) and decided to give it a shot. So I have this code, and it is probably terrible, and Eclipse throws 10 errors at me. So here is my code so far - I have 2 scripts:
    Commands.java
    Code:
    package com.Flafla2.Commands;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import org.bukkit.Server;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    public class Commands extends JavaPlugin {
        // This gets called only ONCE each time the server runs.  Almost no initialization should go here: most should be in onEnable.
        public Commands(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) throws IOException
        {
            super(pluginLoader, instance, desc, folder, plugin, cLoader);
            // Were we to require any initialization logic, we would put it here.  Configuration information goes in onEnable, not here, as we want the ability to refresh it.
        }
    
        // Hello, world!
        public void onEnable()
        {
            // Print some basic info about the command.  This won't appear in the server's log file, as that would be unnecessary.
            System.out.println("Loaded " + this.getDescription().getName() + " version " + this.getDescription().getVersion() + ".");
    
            // Now the meat of the class: register our listener class.  More about this later.
            // The priority is tricky to understand; it is vital to set it properly, or things will break.  This should be part of your planning.  See the sticky on this topic.
            getServer().getPluginManager().registerEvent(Type.PLAYER_COMMAND, new CommandPlayerListener(this), Priority.Low, this);
        }
    
        // Goodbye, world.
        public void onDisable()
        {
            // If we had any persistence, we might do a final save here.
        }
    }
    and CommandsPlayerListener.java
    Code:
    package com.Flafla2.Commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import org.bukkit.Server;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    public class CommandsPlayerListener extends PlayerListener {
        private final Server server; // Quick access to the server
        private final Commands parent; // The plugin core is our portal to the rest of the server.
    
        public CommandsPlayerListener(GeneReal parent)
        {
            // Initialize our final variables.
            this.parent = parent;
            this.server = parent.getServer();
        }
    
        @Override
        public void onPlayerCommand(PlayerChatEvent event)
        {
            if (event.isCancelled()) return; // IMPORTANT: Don't process any further if the command has already been cancelled!
    
            Player player = event.getPlayer(); // The player that issued the command
            String[] sects = event.getMessage().split(" +", 2); // This will contain two strings: the command, and everything else.
            String[] args = (sects.length > 1 ? sects[1].split(" +") : new String[0]); // This will contain all the arguments after the command, space-delimited.
            Commands cmd; // This will hold the command ID.  See the Commands enum at the bottom of this class.
            try
            {
                // Determine a matching command based on sects[0], but remove the leading "/".
                cmd = Commands.valueOf(sects[0].substring(1).toUpperCase());
            }
            catch (Exception ex)
            {
                // The command is either too short (Bukkit is misbehaving) or doesn't match one for which we are listening.
                return;
            }
    
            try
            {
                // We can use a switch because we converted the command string into an enum.
                switch (cmd)
                {
                case PING: // Command was /tree
                    player.sendMessage("Pong!");
                    break;
    
                case PINGY: // Command was /bigtree
                    player.sendMessage("Pongy!");
                    break;
    
                default:
                    return; // We forgot to implement a command: treat it as non-existent.
                }
            }
            catch (NoSuchMethodError ex)
            {
                // We are running an old version of CraftBukkit that does not support generateTree or generateBigTree.
                player.sendMessage("The server is not recent enough to support " + sects[0].toLowerCase() + ".");
            }
            catch (Exception ex)
            {
                // Unexpected error encountered.  Tell the user.  Can be thrown on purpose to notify the user of syntax errors and such.
                player.sendMessage("§cError: " + ex.getMessage());
            }
    
            // The command has been processed and should be prevented from bubbling.
            Logger.getLogger("Minecraft").log(Level.INFO, String.format("%1$s issued command: %2$s", player.getName(), event.getMessage())); // Log the use and interception of the command.
            event.setCancelled(true); // Prevent other plugins from processing the command.
        }
    
        // A list of all supported commands, case-insensitive.
        private enum Commands
        {
            PING,
            PINGY
        }
    }
    
    The code is supposed to be a simple ping-pong script.

    So, again, I don't know much java, and the documentation is terribly non-beginner friendly. Can anyone please give me a clear explanation on what is wrong?

    Thanks in advance,
    Flafla2
     
  2. Offline

    Jayjay110

  3. Offline

    DreadKyller

    onPlayerCommand() is really not that effective anymore, you should use onCommand(CommandSender sender, Command cmd, String label, String[] args)

    and then register that with:

    PHP:
    PluginManager pm getServer().getPluginManager();
    pm.getCommand("cmd").setExecutor(new commander(this));
    The tutorial posted above is a good place to get more info.
     
  4. Offline

    Flafla2

    I checked out the guide, and it worked excellently! Thanks guys!
     
Thread Status:
Not open for further replies.

Share This Page