as I said in the title I want to make the following 2 commands: /event pvp on
and /event pop off it is not working. Help me pls. btw I'm new in Coding Java
this is my code:
public boolean onCommand(CommandSender sender, Command command, String label, String[] args, String arg2) {
Player player = (Player) sender;
if(label.equalsIgnoreCase("event")) {
if(args.length == 0) {
player.sendMessage("Syntax: /event (Event) on/off");
return true;
} else if(args.length == 1) {
String mode = args[0];
if(mode.equalsIgnoreCase("pvp")) {
if(arg2.length() == 1) {
String Modus = arg2;
if(Modus.equalsIgnoreCase("on")) {
new PlayerRDEvent(this);
for(Player onps : Bukkit.getOnlinePlayers()){
onps.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event hat begonnen!");
onps.playSound(onps.getLocation(), Sound.ARROW_HIT, 2, 0);
}}}
return true;
} else if(mode.equalsIgnoreCase("off")) {
for(Player onps2 : Bukkit.getOnlinePlayers()){
onps2.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event ist zu ende!");
onps2.playSound(onps2.getLocation(), Sound.ARROW_HIT, 2, 0);}
HandlerList.unregisterAll();
return true;
} else {
player.sendMessage("Use: /event (event) on/off");
}
}
}
return false;
}
You were using mode as a variable for args[0]
and didn't use it to check if it was on. To check for the off/on string, equate mode
to args[1]
(Second element in array, because the first one would be pvp) and check before the conditions if args[0]
is equal to pvp
Also, you cannot add the parameter String arg2
if you are overriding the method from JavaPlugin - so just use the default parameters of the method onCommand,
see: OnCommand Bukkit method
Code:
Player player = (Player) sender;
if (label.equalsIgnoreCase("event")) {
if (args[0].equals("pvp")) { //check if args[0] is pvp, you can add an else statement to send an error message if it is not pvp, or manipulate it differently
if (args.length != 2) { //if it does not contain both [pvp] and [off/on]
player.sendMessage("Syntax: /event (Event) on/off");
return true;
} else { //if args length = 2
String mode = args[1]; //[off/on]
if (mode.equalsIgnoreCase("on")) { //if args[1] is "on"
for (Player onps : Bukkit.getOnlinePlayers()) {
onps.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event hat begonnen!");
onps.playSound(onps.getLocation(), Sound.ARROW_HIT, 2, 0);
}
return true;
} else if (mode.equalsIgnoreCase("off")) { //if args[1] is "off"
for (Player onps2 : Bukkit.getOnlinePlayers()) {
onps2.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event ist zu ende!");
onps2.playSound(onps2.getLocation(), Sound.ARROW_HIT, 2, 0);
}
HandlerList.unregisterAll();
return true;
} else {
player.sendMessage("Use: /event (event) on/off");
}
}
}
}
return false;
}
}
I really suggest you implement some sort of command listener in the near future instead of checking for each command on the onCommand method as it makes your code cleaner and easier to read