Trying to make a command that lets me apply potion effects to people

Discussion in 'Plugin Development' started by SeventhSandwich, Jul 16, 2012.

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

    SeventhSandwich

    This is my code thus far:
    Code:
    package com.gmail.seventhsandwich.PotionCommand;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public class PotionCommand extends JavaPlugin implements Listener{
        public void onEnable() {
            getLogger().info("PotionCommand V0.1 has been enabled!");
        }
        public void onDisable() {
            getLogger().info("PotionCommand V0.1 has been disabled!");
        }
        public boolean onCommand(
                CommandSender sender,
                Command cmd,
                String commandLabel,
                String[] args){
            int duration = Integer.parseInt(args[3])*20;
            int amplifier = Integer.parseInt(args[2]);
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            if(cmd.getName().equalsIgnoreCase("potion")) {
                if(args[1] == "FR") {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.FIRE_RESISTANCE,
                                        duration,
                                        amplifier));
                          getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1] == "HARM") {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HARM,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1] == "HEAL") {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HARM,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1] == "POISON") {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HARM,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1] == "SPEED") {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HARM,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                    }
                    return true;
                }
            }
       
    
    However for some reason, the if(args[1] == "... statements don't work.

    It can detect that cmd is potion, but it can't detect things like POISON or SPEED.

    This is my plugin.yml file
    Code:
    name: PotionCommand
    main: com.gmail.seventhsandwich.PotionCommand.PotionCommand
    version: 0.1
    commands:
      potion:
        description: Lets you apply a potion effect to a chosen player
        usage: /potion [player] [duration] [amplifier]
    Please help.

    FYI, I was using
    getLogger().info("A command has been executed!");

    To help debug my code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. args[1] == "HEAL"

    I think you mean to write args[1].equals("HEAL")

    because you comparing the memory places of 2 pointers, whits is almost never the same
     
    SeventhSandwich likes this.
  3. Offline

    SeventhSandwich

    Thank you, I will try that.

    Thank you, it worked perfectly!

    I appreciate the help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Use args[1].equalsIgnoreCase("command") because == doesn't compare strings properly, it compares primitive data mostly.

    Also, you'll gonna have alot of "array index out of bounds" there because you're not checking if args's size (args.length) is actually big enough to use the index you're asking for...
    If someone types at least one argument less, you'll gonna have some errors.

    EDIT: late xD but my 2nd advice still stands.

    And also, your usage information in the plugin.yml is not accurate.... and array indexes start from 0, so your args[1] is checking in the 2nd argument.
     
  5. Offline

    SeventhSandwich

    Yeah I was testing that and I was going to add that, but I couldn't even get the command to work lol.

    I'm trying to do it with if(args.length() == 3) but I'm getting a, "cannot find symbol" and when I tried to build it, Windows Explorer crashed ;-;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  6. replace
    args.length()
    by
    args.length
    simply?
     
  7. Offline

    SeventhSandwich

    why would it be length as opposed to length();?
     
  8. Because it's a variable.

    That's how primitive arrays have it.

    ArrayList, List, HashSet, HashMap, etc (collections) are objects (non-primitive) and have the .size() method.
     
  9. Offline

    SeventhSandwich

    I added an if statement to see if there's a correct number of arguments being entered but now my commands don't work at all.

    Code:
    package com.gmail.seventhsandwich.PotionCommand;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public class PotionCommand extends JavaPlugin implements Listener{
        public void onEnable() {
            getLogger().info("PotionCommand V0.1 has been enabled!");
        }
        public void onDisable() {
            getLogger().info("PotionCommand V0.1 has been disabled!");
        }
        public boolean onCommand(
                CommandSender sender,
                Command cmd,
                String commandLabel,
                String[] args){
            int duration = Integer.parseInt(args[3])*20;
            int amplifier = Integer.parseInt(args[2]);
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            if(args.length == 3) {
            if(cmd.getName().equalsIgnoreCase("potion")) {
                getLogger().info("Potion is worked");
                if(args[1].equalsIgnoreCase("FR")){
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.FIRE_RESISTANCE,
                                        duration,
                                        amplifier));
                          getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1].equalsIgnoreCase("HARM")) {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HARM,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1].equalsIgnoreCase("HEAL")) {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.HEAL,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1].equalsIgnoreCase("POISON")) {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.POISON,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                if(args[1].equalsIgnoreCase("SPEED")) {
                                target.addPotionEffect(new PotionEffect(
                                        PotionEffectType.SPEED,
                                        duration,
                                        amplifier));
                                getLogger().info("A command has been executed!");
                            return false;
                        }
                    }
                    return true;
                }
            return true;
            }
    }
     
  10. You added it after you've already used args[3] and args[2], so it would be kinda pointless, you need to move it earlier in the code... at the start of the command.

    Also, args[3] is the 4th argument, so you can't have 3 arguments...
    Use if(args.length > 3) to avoid confusion, that way you can know that you can use the index 3 as well... but use it at the begining !
     
  11. Offline

    SeventhSandwich

    I tested typing in a wrong name on the command and it caused a bunch of errors. Is there any way I can check whether the chosen player is actually online/exist?
     
  12. this line
    Player target = player.getServer().getPlayer(args[0]);
    gets an player, and null if it dont exist, that is what the javadoc says, I gues your next question will be: How can I check if its null?
    you can do this by
    if(target==null)
    {
    player.sendMessage("player not found");
    return true;
    }
     
  13. Offline

    Stumblinbear

    I know you might be adding this... But, there are a few potion effects that are not potions.
    Example: confusion Adds the wavy portal effect, and also another one is nausea. Hope you add these, I was going to make one of these myself. But i guess i don't have to :D
    Do you think you could make it so you can add effects for when you right-click an item? That would be awesome!
    Just a few suggestions :3
     
Thread Status:
Not open for further replies.

Share This Page