two commands one plugin?

Discussion in 'Plugin Development' started by Veritas, Dec 14, 2014.

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

    Veritas

    Ok so I'm making my 1st legit plugin and this is all my code

    Code:
    package me.Veritas025.god;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class God extends JavaPlugin { { }
    
        @Override
        public void onEnable() {
        getLogger().info("Plugin Working");
               
       
       
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("God") && sender instanceof Player) {
        Player player = (Player) sender;
       
        player.sendMessage("You Now have Gods power!");
        player.getWorld().strikeLightning(player.getLocation());
        player.setHealth(20F); player.setFoodLevel(20); player.setFireTicks(0);player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 50));
        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 999999, 999));player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 999999, 999));
        player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 999999, 999));player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999, 999));
        player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 999999, 999));player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999, 999));
        player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 999999, 50)); player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999999, 999));
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 999999, 999));player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999, 10));
        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 999999, 999));player.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, 999999, 999));
       
       
        if (cmd.getName().equalsIgnoreCase("Human"))
            for(PotionEffect effect : player.getActivePotionEffects())
            {
                player.removePotionEffect(effect.getType());
            }player.sendMessage("You Are Now Human!");
            player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 120, 50));
            ((Player) player).setFireTicks(20);
        return true;
        }
        return false;
        }
    }
    
    So when I run it what it does is when I do /God it combines the two commands. Then when I do /Human Nothing happens. It doesn't even say Unknown command literally nothing happens like I didn't type anything? How do I make two commands in one plugin? Would I have to make a seperate class? If I did How would I do that in the plugin.yml file?
     
  2. Offline

    adam753

    Your curly braces are all over the place. (these things { })
    Start by putting one after the "/god" section, which I believe would fix the current problem. Then fix your indentation. Keeping good indentation will usually let you know if you did something wrong with the braces. If you find yourself having to put a random one at the end, you've done something wrong.
     
  3. Offline

    Dragonphase

    @Veritas

    Use CommandExecutors.

    Main.java:

    Code:
    public void onEnable() {
        registerCommands();
    }
    
    public void registerCommands() {
        getCommand("first").setExecutor(new FirstCommandExecutor(this));
        getCommand("second").setExecutor(new SecondCommandExecutor(this));
    }
    FirstCommandExecutor.java:

    Code:
    public class FirstCommandExecutor implements CommandExecutor {
    
        private MyPlugin plugin;
    
        public FirstCommandExecutor(MyPlugin instance) {
            plugin = instance;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
            if (args.length < 1) {
                handleBaseCommand(sender);
                return false;
            }
    
            // "/first one"
            if (args[0].equalsIgnoreCase("one")) {
                // Utils.trim(String[]) removes the first element of an array. I do this so that my args index is set back to 0 for subcommands.
                handleSubCommandOne(sender, Utils.trim(args));
            else if (args[0].equalsIgnoreCase("two")) {
                handleSubCommandTwo(sender, Utils.trim(args));
            }
    
            return false;
        }
    
        public void handleBaseCommand(CommandSender sender) {
            sender.sendMessage("You typed /first");
        }
    
        public void handleSubCommandOne(CommandSender sender, String[] args) {
            sender.sendMessage(String.format("You typed /first one %s", StringUtils.join(args, ' ')));
        }
    
        public void handleSubCommandTwo(CommandSender sender, String[] args) {
            sender.sendMessage(String.format("You typed /first two %s", StringUtils.join(args, ' ')));
        }
    }
    SecondCommandExecutor.java:

    Code:
    public class SecondCommandExecutor implements CommandExecutor {
    
        private MyPlugin plugin;
    
        public SecondCommandExecutor(MyPlugin instance) {
            plugin = instance;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
            if (args.length < 1) {
                handleBaseCommand(sender);
                return false;
            }
    
            // "/second one"
            if (args[0].equalsIgnoreCase("one")) {
                handleSubCommandOne(sender, Utils.trim(args));
            else if (args[0].equalsIgnoreCase("two")) {
                handleSubCommandTwo(sender, Utils.trim(args));
            }
    
            return false;
        }
    
        public void handleBaseCommand(CommandSender sender) {
            sender.sendMessage("You typed /second");
        }
    
        public void handleSubCommandOne(CommandSender sender, String[] args) {
            sender.sendMessage(String.format("You typed /second one %s", StringUtils.join(args, ' ')));
        }
    
        public void handleSubCommandTwo(CommandSender sender, String[] args) {
            sender.sendMessage(String.format("You typed /second two %s", StringUtils.join(args, ' ')));
        }
    }
    Utils.java:

    Code:
    public final class Utils {
    
        private Utils() {}
    
        public static <T> T[] trim(T[] args) {
            return Arrays.copyOfRange(args, 1, args.length);
        }
    }
    You could also handle subcommands differently by creating an individual class for each, or a SubCommand interface:

    Code:
    public interface SubCommand {
        public boolean execute(CommandSender sender, String[] args);
    }
     
  4. Offline

    tcvs

    @Veritas you just need to register both commands in the plugin.yml. You don't have to seperate the commands but I would recommend it.
     
  5. Offline

    3ShotGod

    There are two possible reasons it might not be working.

    1.) You haven't registered the second command in your plugin.yml properly.
    2.) You use if in your first command, all other commands should no longer be more if statements, but else if statements unless you are branching another command from you first one.
     
  6. Offline

    thekillamon

    I'm pretty sure you need to return the first command
     
  7. Offline

    ChintziSecilMC

    You do realise when you put if(cmd.getName().equalsIgnoreCase("God")&& sender instanceof Player){ it links the other command, you need to put a bracket after your /god code.
    @Veritas
     
Thread Status:
Not open for further replies.

Share This Page