Elses and ifs help

Discussion in 'Plugin Development' started by TheMinecraftKnight, Dec 31, 2016.

Thread Status:
Not open for further replies.
  1. Hey, I've just started learning Bukkit and Java, and to get some practice I'm trying to make a plugin that activates a form of godmode.
    When you type /superheal, it should say "Usage: /superheal on/off"
    When typing /superheal on, it gives you all the potion effects in the code
    When typing /superheal off, it takes them away as you can see.
    When typing "/superheal anythingelse", it should say the usage message again.
    What actually happens is this:
    /superheal = Nothing. Nothing in the console, it doesn't do anything.
    /superheal on = Gives the effects BUT says the usage message
    Everything else works fine.
    Any idea why?
    Here's my code (ignore the stuff above the //Superheal Command, it's other commands)
    Code:
    package com.knightzmc.basics;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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 KnightzMCRandomTP extends JavaPlugin{
    
    public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)
        {
            Player player = (Player) theSender;
            if(commandLabel.equalsIgnoreCase("randomtp"))
            {
    
                int randomx = (int) (Math.random() * 100000);
                int randomz = (int) (Math.random() * 100000);
                int randomy = player.getWorld().getHighestBlockYAt(randomx, randomz);
    
                Location randomLocation = new Location(player.getWorld(), randomx, randomy, randomz);
                player.teleport(randomLocation);
                player.sendMessage(ChatColor.GOLD + "You were teleported to X: " + ChatColor.GREEN + randomx + ChatColor.GOLD + " Y: " +ChatColor.GREEN + randomy + ChatColor.GOLD + " Z: " + ChatColor.GREEN + randomz);
            }
            if(commandLabel.equalsIgnoreCase("heal"))
            {
                if(player.hasPermission("knightzmc.heal"))
                {
                    player.setHealth(20);
                }
                else
                {
                    player.sendMessage(ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN + "/heal");
                }
            }
    //Superheal Command
            if(commandLabel.equalsIgnoreCase("superheal"))
            {
                    if(player.hasPermission("knightzmc.superheal"))
                    {
                        if(args.length == 1)
                            {
                                if(args[0].equalsIgnoreCase("on"))
                                {
                                    player.setMaxHealth(1200);
                                    player.setHealth(1200);
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 255));
                                }
                                if(args[0].equalsIgnoreCase("off"))
                                    {
                                        player.setMaxHealth(20);
                                        player.setHealth(20);
                                        player.removePotionEffect(PotionEffectType.REGENERATION);
                                    }
                            
                                else
                                {
                                    if(args.length == 0)
                                    {
                                        player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN +"/superheal on/off");
                                    }
                                    else player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN +"/superheal on/off");
                                }
                            }
                    }
                    else player.sendMessage(ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN + commandLabel);
            }
            return true;
        }
    }
    
    Any help would be greatly appreciated :)
     
  2. Offline

    Zombie_Striker

    are you sure the sender will always be a player? You never check. Check if the sender is a player before casting.

    Use cmd.getName() instead of commandlabel. Commandlabel does not support aliases.

    Either return false after this section or use else cases for each command.

    Fix your formatting and indentation. Open brackets should be on the same as the if statement, and use CTRL-Shift-F to format your code.

    This is why formatting is important. You are checking if args[0] is off, if so, do stuff. If it is not equal to off, but does exist, check if the length is equal to 0 (meaning it does not exist). Format your code.
     
  3. Done.

    So instead of if (commandLabel.equalsIgnoreCase("heal")) { I would put if (cmd.getName("heal")){?
    It doesn't seem to work (the method is not applicable for the arguments String)

    Either return false after this section or use else cases for each command.
    Done

    Fix your formatting and indentation. Open brackets should be on the same as the if statement, and use CTRL-Shift-F to format your code.
    Also done.
    This is why formatting is important. You are checking if args[0] is off, if so, do stuff. If it is not equal to off, but does exist, check if the length is equal to 0 (meaning it does not exist). Format your code.
    I see where I've gone wrong now, thanks :)
    New Code:
    Code:
    package com.knightzmc.basics;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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 KnightzMCRandomTP extends JavaPlugin {
    
        public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) theSender;
            if (commandLabel.equalsIgnoreCase("randomtp")) {
                if (theSender instanceof Player) {
                    int randomx = (int) (Math.random() * 100000);
                    int randomz = (int) (Math.random() * 100000);
                    int randomy = player.getWorld().getHighestBlockYAt(randomx, randomz);
    
                    Location randomLocation = new Location(player.getWorld(), randomx, randomy, randomz);
                    player.teleport(randomLocation);
                    player.sendMessage(ChatColor.GOLD + "You were teleported to X: " + ChatColor.GREEN + randomx
                            + ChatColor.GOLD + " Y: " + ChatColor.GREEN + randomy + ChatColor.GOLD + " Z: "
                            + ChatColor.GREEN + randomz);
                } else
                    System.out.println("This command can only be used as a player!");
                return false;
            }
            if (cmd.getName("heal")) {
                if (theSender instanceof Player) {
    
                    if (player.hasPermission("knightzmc.heal")) {
                        player.setHealth(20);
                    } else {
                        player.sendMessage(
                                ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN + "/heal");
                    }
                } else
                    System.out.println("This command can only be used as a player!");
            }
            // Superheal Command
            if (commandLabel.equalsIgnoreCase("superheal")) {
                if (theSender instanceof Player) {
    
                    if (player.hasPermission("knightzmc.superheal")) {
                        if (args.length == 1) {
                            if (args[0].equalsIgnoreCase("on")) {
                                player.setMaxHealth(1200);
                                player.setHealth(1200);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 255));
                            }
                            if (args[0].equalsIgnoreCase("off")) {
                                player.setMaxHealth(20);
                                player.setHealth(20);
                                player.removePotionEffect(PotionEffectType.REGENERATION);
                            } else
                                player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN + "/superheal on/off");
                        }
    
                        else {
                            if (args.length == 0) {
                                player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN + "/superheal on/off");
                            }
                        }
                    } else
                        player.sendMessage(ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN
                                + commandLabel);
                }
            }
            return true;
        }
    }
    
    Is that any better?
     
  4. Offline

    Zombie_Striker

    No, only replace the CommandLabel with cmd.getName. It should look like

    if(cmd.gewtNamre().equalsIgnoreCase("Command"))

    You have to check if the sender is a player Before you cast. If the sender is not a player (if a console/plugin/command block sent any commands. Lets say its a console), the way you currently have it will cast the console to a player. Now, consoles are not players, so this will throw a ClassCastExeption.

    Since "sender" represents all cases, use "sender.sendmMessage("this command....")" to print out error messages.

    Remove the first open bracket. You can marge thoe two lines into
    Code:
    else if (args.length ==0 ){
     
  5. Ok.
    Should I put it below my public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args) { then? Or above each of my if(cmd.getName()....? Below the public boolean only makes the first command work.

    Since "sender" represents all cases, use "sender.sendmMessage("this command....")" to print out error messages.
    Done

    Remove the first open bracket. You can marge thoe two lines into
    Code:
    else if (args.length ==0 ){
    Also done.
    New code:
    Code:
    package com.knightzmc.basics;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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 KnightzMCRandomTP extends JavaPlugin {
    
        public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) theSender;
            if (theSender instanceof Player) {
                if (cmd.getName().equalsIgnoreCase("randomtp")) {
                    int randomx = (int) (Math.random() * 100000);
                    int randomz = (int) (Math.random() * 100000);
                    int randomy = player.getWorld().getHighestBlockYAt(randomx, randomz);
    
                    Location randomLocation = new Location(player.getWorld(), randomx, randomy, randomz);
                    player.teleport(randomLocation);
                    player.sendMessage(ChatColor.GOLD + "You were teleported to X: " + ChatColor.GREEN + randomx
                            + ChatColor.GOLD + " Y: " + ChatColor.GREEN + randomy + ChatColor.GOLD + " Z: "
                            + ChatColor.GREEN + randomz);
                } else
                    theSender.sendMessage("This command can only be used as a player!");
                return false;
            }
    
            if (cmd.getName().equalsIgnoreCase("heal")) {
    
                if (player.hasPermission("knightzmc.heal")) {
                    player.setHealth(20);
                } else {
                    player.sendMessage(
                            ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN + "/heal");
                }
            } else
                theSender.sendMessage("This command can only be used as a player!");
    
            // Superheal Command
            if (cmd.getName().equalsIgnoreCase("superheal"))
    
            {
                if (player.hasPermission("knightzmc.superheal")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("on")) {
                            player.setMaxHealth(1200);
                            player.setHealth(1200);
                            player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 255));
                        }
                        if (args[0].equalsIgnoreCase("off")) {
                            player.setMaxHealth(20);
                            player.setHealth(20);
                            player.removePotionEffect(PotionEffectType.REGENERATION);
                        } else
                            player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN + "/superheal on/off");
                    }
    
                    else if (args.length == 0) {
                        player.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.GREEN + "/superheal on/off");
                    }
                }
            } else
                player.sendMessage(
                        ChatColor.GOLD + "Sorry, you don't have permission to use " + ChatColor.GREEN + commandLabel);
            return true;
        }
    }
    
     
  6. Offline

    Zombie_Striker

    @TheMinecraftKnight
    The instanceof check should be above the player variable, so it should be
    BTW: I just found this. Your issue is that you forgot to add an else statement after the "on" check. It should be
     
Thread Status:
Not open for further replies.

Share This Page