I started developing plugin for my server. I made main class and pleh commands but all stopped when I started with gamemode
command. I mean it works with no errors but gives no output. It could be a simple mistake but I can't find it.
Here is my code:
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class Gamemode implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Haha nice try not happening today.");
return true;
}
Player p = (Player) sender;
if (cmd.getName().equalsIgnoreCase("gm")) {
if ((!p.hasPermission("sutils.*")) &&
(!p.hasPermission("sutils.gamemode"))) {
p.sendMessage(ChatColor.RED + "Sorry no permission.");
return true;
}
if (args.equals("0")) {
p.setGameMode(GameMode.SURVIVAL);
p.sendMessage(ChatColor.RED + "Gamemode set to Survival");
}
if (args.equals("1")) {
p.setGameMode(GameMode.CREATIVE);
p.sendMessage(ChatColor.RED + "Gamemode set to Creative");
}
if (args.equals("2")) {
p.setGameMode(GameMode.ADVENTURE);
p.sendMessage(ChatColor.RED + "Gamemode set to Adventure");
}
if (args.equals("3")) {
p.setGameMode(GameMode.SPECTATOR);
p.sendMessage(ChatColor.RED + "Gamemode set to Spectator");
}
return true;
}
return true;
}
}
Actually, the only thing that happens is when I type command in console it sends me my not a player msg but as player nope does not work.
The situation you have here is the following: 'args' is not a single string, but an array of strings. That being said, you cannot check if an array of strings is equal to a string.
So what you must do is:
With args[0], we try to access the argument at position 0 of the command. Something like this:
/gm 1
GM is the commandlabel of your command
1 is argument 0 of your command
For that, we can do the following:
if (args == null) {
p.sendMessage(ChatColor.RED + "You must use an arg!");
return false;
}
if (args[0].equals("0") {
p.setGameMode(GameMode.SURVIVAL);
p.sendMessage(ChatColor.RED + "Gamemode set to survival");
return true;
} else if (args[0].equals("1") {
//Setgamemode to creative
//......
}
That should do the trick!