Need help, no errors, just won't work ingame.

Discussion in 'Plugin Development' started by TheSmallBones, May 9, 2012.

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

    TheSmallBones

    Okay, so I have made this tiny plugin (WIP) which let's you vote for my server's following week activity.
    Here's the code.

    Code:
    package me.Kyle.Macm;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class macm extends JavaPlugin{
     
        Logger log;
       
        public void onEnable(){
            log = this.getLogger();
            log.info("Macm V1.0 enabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("macm")){ // If the player typed /macm then do the following...
                if(args.length == 0){
                    player.sendMessage(ChatColor.GOLD + "Welcome to MacmCraft. Want to vote for next week's activity? Type /mvote");
                }
                if(cmd.getName().equalsIgnoreCase("mvote")){
                    if(args.length == 0){
                        player.sendMessage(ChatColor.GOLD + "Usage: /mvote [vote] example: /mvote hungergames");
                    }
                }
               
                return true;
            } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            return false;
        }
     
     
     
        public void onDisable(){
            log.info("Macm V1.0 disabled.");
        }
    }
    Plugin.yml file: (I think I have this wrong)
    Code:
    name: Macm
    version: 1.0
    main: me.Kyle.Macm.macm
    commands:
        macm:
        mvote:
    
     
  2. Offline

    jamietech

    Add this to your onEnable():
    Code:
    getCommand("macm").setExecutor(this);
    getCommand("mvote").setExecutor(this);
     
  3. Offline

    TheSmallBones

    Wow, I never saw that in any tutorials or anything. What does this do?

    Edit: btw I use your hardcore plugin on my server! Kind of buggy but it's great :)

    Still won't work. Where do I add it?

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

    jamietech

    You'll need to implement CommandExecutor too
     
  5. Offline

    TheSmallBones

    I just started Java 2 weeks ago. How?
     
  6. jamietech
    Those aren't required because he isn't using the separate class handler for command method, he's just hooking the main onCommand() triggered in the main class... notice he's filtering commands.

    TheSmallBones
    Maybe it doesn't work because of the plugin.yml's commands don't have any sub-nodes... add usage: /<command> to both of them, mind the spacing.
    Also, am I seeing things or are you checking if command is "macm" and if true you check if command is "mvote" ? That'll never trigger, because if command is "macm" it can't also be "mvote" =)... bad indentation leads to code made incorrectly by humans.
     
  7. jamietech
    macm extends JavaPlugin
    JavaPlugin extends PluginBase
    PluginBase implements Plugin
    Plugin extends CommandExecutor
    Long story short: No need for TheSmallBones to implement CommandExecutor.
    Also commands not registered with getCommand("cmd").setExecutor(something); are automatically registered to the JavaPlugin (in this case: macm) class, so no need for
    getCommand("macm").setExecutor(this);
    getCommand("mvote").setExecutor(this);

    TheSmallBones Try changing your plugin.yml to:
    Code:
    name: Macm
    version: 1.0
    main: me.Kyle.Macm.macm
    commands:
        macm:
            description: ...
            usage: /<command>
        mvote:
            description: ...
            usage: /<command>
    and if that doesn't work have a new look for errors in your server.log file (especially at startup).
     
  8. Offline

    jamietech

    Oops :x
    I don't really use commands in my class extending JavaPlugin so I didn't know. :oops:
     
  9. Offline

    Komak57

    Code:
    package me.Kyle.Macm;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.CommandExecutor;
     
    public class macm extends JavaPlugin implement CommandExecutor {
     
        Logger log;
     
        public void onEnable(){
            log = this.getLogger();
            getCommand("macm").setExecutor(this);
            getCommand("mvote").setExecutor(this);
            log.info("Macm V1.0 enabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            boolean IsPlyr = false;
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
                IsPlyr = true;
            }
            if (IsPlyr == false) { // console commands
            } else { // player commands
                if(cmd.getName().equalsIgnoreCase("macm")){ // If the player typed /macm then do the following...
                    if(args.length == 0){
                        player.sendMessage(ChatColor.GOLD + "Welcome to MacmCraft. Want to vote for next week's activity? Type /mvote");
                    }
                    if(cmd.getName().equalsIgnoreCase("mvote")){
                        if(args.length == 0){
                            player.sendMessage(ChatColor.GOLD + "Usage: /mvote [vote] example: /mvote hungergames");
                        }
                    }
                 
                    return true;
                } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            }
            return false;
        }
     
        public void onDisable(){
            log.info("Macm V1.0 disabled.");
        }
    }
    implements and exports go at the top (for the class declaration).
    getCommand(String command).setExecutor(this); - sends command args to the desired command handler (new class or same class)
    the IsPlyr boolean will help you handle commands when sender is actually the console, not a player. Can prevent some errors when trying to send messages to the player in question, or to a target player.

    Code:
    name: Macm
    version: 1.0
    main: me.Kyle.Macm.macm
    commands:
        macm:
            description: Request MOTD
            usage: /<command>
        mvote:
            description: Vote for a function
            usage: /<command> [hungergame|etc]
    -- insure there are NO tab characters and that they are all spaces, then ensure you refresh the listing in Eclipse to prevent a compile error.

    props to V10lator and jamietech

    for future reference, you will need to implement Listener and add "new myBlockListener(this);" on enable for a class named myBlockListener.java to add listeners such as block listeners, player listeners, or login listeners to your code.
     
  10. Komak57
    ...
     
  11. Offline

    remcodemah

    if(commandLable.equalignorecase("command"){
    command things
    }

    /*i know i made a typo*/
     
Thread Status:
Not open for further replies.

Share This Page