1

I am trying to have a command /gm 1 to change the players gamemode to gamemode 1. I know there is a plugin called essentials out there that has that command but I am trying to practice building bukkit plugins.

So here is my Main.java:

if(cmd.getName().equalsIgnoreCase("gm 1")){
   String gm1 =  "gamemode 1 " + player.getName();
   Bukkit.dispatchCommand(Bukkit.getConsoleSender(), gm1);
   player.sendMessage(ChatColor.GRAY + "Your now in Creative Mode.");   
}


Plugin.yml:

commands:
   gm 1:
     description: Sets your game mode to creative.

I am pretty new to java can someone help?

4
  • Your problem is cmd.getName().equalsIgnoreCase("gm 1") is not true? Printout cmd.getName to check if it's really equals Commented Mar 23, 2014 at 19:47
  • To rule out a potential NPE, swap the order of your equalsIgnoreCase, so it reads "gm 1".equalsIgnoreCase(cmd.getName()).
    – Makoto
    Commented Mar 23, 2014 at 19:49
  • I tried yours Makoto and I get a syntax error
    – CHASE
    Commented Mar 23, 2014 at 20:09
  • Post the updated code, and what cmd.getName() returns Commented Mar 23, 2014 at 20:11

2 Answers 2

2

The problem is that cmd.getName() only returns the very first part of the command. (Here being gm). The 1 would be stored as args[0] because every word after the first is stored as a separate string in the args variable. You want something more like:

if(cmd.getName().equalsIgnoreCase("gm") && args[0] == "1"){
   String gm1 =  "gamemode 1 " + player.getName();
   Bukkit.dispatchCommand(Bukkit.getConsoleSender(), gm1);
   player.sendMessage(ChatColor.GRAY + "You're now in Creative Mode.");   
}

And

commands:
  gm:
    description: Sets your game mode to creative.

Because, as above, the command is just gm.

0
@Override
public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
    Player p = (Player)sender;
    if(cmd.getName().equalsIgnoreCase("build")) {
        if(p.hasPermission("server.build")) {
            if(args.length < 0) {
                p.sendMessage(Main.p + " §cBitte benutze /build [on] [off]");
            }else {
                if(args[0].equalsIgnoreCase("on")) {
                    p.setGameMode(GameMode.CREATIVE);
                    p.sendMessage(Main.p + " §aDu hast den §eBuild-Modus §aAktiviert");
                    p.playSound(p.getLocation(), Sound.BAT_DEATH, 10, 10);
                }else if(args[0].equalsIgnoreCase("off")) {
                    p.setGameMode(GameMode.SURVIVAL);
                    p.sendMessage(Main.p + " §cDu hast den §eBuild-Modus §cDeaktiviert");
                    p.setHealth(20);
                    p.setFoodLevel(20);
                    p.playSound(p.getLocation(), Sound.BAT_DEATH, 10, 10);
                }else {
                    p.sendMessage(Main.p + " §cBitte benutze /build [on] [off]");
                }
            }
        }else {
            p.sendMessage(Main.p + " §cDu hast keine Rechte §8| §aBewerbe dich doch auf ...");
        }
    }
    return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.