Potion command fix and refreshment

The potion command didn't work for OPs due to some misstake that made it think that all commands were executed on other players (a staff only feature).
It now instead uses switches to execute for better performance and readability.
This commit is contained in:
William Bergh 2020-09-06 21:25:15 +02:00
parent 2072c89f77
commit 7af53448be

View File

@ -17,7 +17,7 @@ import org.bukkit.potion.PotionEffectType;
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH) @CommandPermissions(level = Rank.OP, source = SourceType.BOTH)
@CommandParameters( @CommandParameters(
description = "Manipulate your potion effects. Duration is measured in server ticks (~20 ticks per second).", description = "Manipulate your potion effects. Duration is measured in server ticks (~20 ticks per second).",
usage = "/<command> <list | clear [target name] | add <type> <duration> <amplifier> [target name]>", usage = "/<command> <list | clearall | clear [target name] | add <type> <duration> <amplifier> [target name]>",
aliases="effect") aliases="effect")
public class Command_potion extends FreedomCommand public class Command_potion extends FreedomCommand
{ {
@ -25,8 +25,9 @@ public class Command_potion extends FreedomCommand
@Override @Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
if (args.length == 1 || args.length == 2) switch (args.length)
{ {
case 1:
if (args[0].equalsIgnoreCase("list")) if (args[0].equalsIgnoreCase("list"))
{ {
List<String> potionEffectTypeNames = new ArrayList<>(); List<String> potionEffectTypeNames = new ArrayList<>();
@ -46,6 +47,7 @@ public class Command_potion extends FreedomCommand
noPerms(); noPerms();
return true; return true;
} }
FUtil.staffAction(sender.getName(), "Cleared all potion effects from all players", true); FUtil.staffAction(sender.getName(), "Cleared all potion effects from all players", true);
for (Player target : server.getOnlinePlayers()) for (Player target : server.getOnlinePlayers())
{ {
@ -55,30 +57,32 @@ public class Command_potion extends FreedomCommand
} }
} }
} }
else if (args[0].equalsIgnoreCase("clear"))
case 2:
if (args[0].equalsIgnoreCase("clear"))
{ {
Player target = playerSender; Player target = playerSender;
if(args.length == 2)
if (args.length == 2)
{ {
target = getPlayer(args[1], true); if (!plugin.sl.isStaff(sender) && !target.equals(getPlayer(sender.getName())))
if (target == null)
{ {
msg(FreedomCommand.PLAYER_NOT_FOUND, ChatColor.RED); msg(ChatColor.RED + "Only staff can clear potion effects from other players.");
return true; return true;
} }
target = getPlayer(args[1], true);
} }
else
{
if (senderIsConsole) if (senderIsConsole)
{ {
msg("You must specify a target player when using this command from the console."); msg("You must specify a target player when using this command from the console.");
return true; return true;
} }
}
if (!plugin.sl.isStaff(sender)) if (target == null)
{ {
msg(ChatColor.RED + "Only staff can clear potion effects from other players."); msg(FreedomCommand.PLAYER_NOT_FOUND, ChatColor.RED);
return true; return true;
} }
@ -89,19 +93,22 @@ public class Command_potion extends FreedomCommand
msg("Cleared all active potion effects " + (!target.equals(playerSender) ? "from player " + target.getName() + "." : "from yourself."), ChatColor.AQUA); msg("Cleared all active potion effects " + (!target.equals(playerSender) ? "from player " + target.getName() + "." : "from yourself."), ChatColor.AQUA);
} }
else break;
{
return false; case 4:
} case 5:
}
else if (args.length == 4 || args.length == 5)
{
if (args[0].equalsIgnoreCase("add")) if (args[0].equalsIgnoreCase("add"))
{ {
Player target = playerSender; Player target = playerSender;
if (args.length == 5) if (args.length == 5)
{ {
if (!plugin.sl.isStaff(sender) && !getPlayer(args[4]).equals(getPlayer(sender.getName())))
{
sender.sendMessage(ChatColor.RED + "Only staff can apply potion effects to other players.");
return true;
}
target = getPlayer(args[4]); target = getPlayer(args[4]);
if (target == null || plugin.sl.isVanished(target.getName()) && !plugin.sl.isStaff(sender)) if (target == null || plugin.sl.isVanished(target.getName()) && !plugin.sl.isStaff(sender))
@ -110,17 +117,13 @@ public class Command_potion extends FreedomCommand
return true; return true;
} }
} }
else
{
if (senderIsConsole) if (senderIsConsole)
{ {
sender.sendMessage("You must specify a target player when using this command from the console."); sender.sendMessage("You must specify a target player when using this command from the console.");
return true; return true;
} }
if (!plugin.sl.isStaff(sender))
{
sender.sendMessage(ChatColor.RED + "Only staff can apply potion effects to other players.");
return true;
} }
PotionEffectType potion_effect_type = PotionEffectType.getByName(args[1]); PotionEffectType potion_effect_type = PotionEffectType.getByName(args[1]);
@ -161,16 +164,9 @@ public class Command_potion extends FreedomCommand
+ ", Duration: " + new_effect.getDuration() + ", Duration: " + new_effect.getDuration()
+ ", Amplifier: " + new_effect.getAmplifier() + ", Amplifier: " + new_effect.getAmplifier()
+ (!target.equals(playerSender) ? " to player " + target.getName() + "." : " to yourself."), ChatColor.AQUA); + (!target.equals(playerSender) ? " to player " + target.getName() + "." : " to yourself."), ChatColor.AQUA);
return true;
} }
else break;
{ default:
return false;
}
}
else
{
return false; return false;
} }
return true; return true;
@ -179,8 +175,9 @@ public class Command_potion extends FreedomCommand
@Override @Override
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args) public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{ {
if (args.length == 1) switch (args.length)
{ {
case 1:
List<String> arguments = new ArrayList<>(); List<String> arguments = new ArrayList<>();
arguments.addAll(Arrays.asList("list", "clear", "add")); arguments.addAll(Arrays.asList("list", "clear", "add"));
if (plugin.sl.isStaff(sender)) if (plugin.sl.isStaff(sender))
@ -188,9 +185,8 @@ public class Command_potion extends FreedomCommand
arguments.add("clearall"); arguments.add("clearall");
} }
return arguments; return arguments;
}
else if (args.length == 2) case 2:
{
if (args[0].equals("clear")) if (args[0].equals("clear"))
{ {
if (plugin.sl.isStaff(sender)) if (plugin.sl.isStaff(sender))
@ -202,28 +198,32 @@ public class Command_potion extends FreedomCommand
{ {
return getAllPotionTypes(); return getAllPotionTypes();
} }
} break;
else if (args.length == 3)
{ case 3:
if (args[0].equals("add")) if (args[0].equals("add"))
{ {
return Arrays.asList("<duration>"); return Arrays.asList("<duration>");
} }
} break;
else if (args.length == 4)
{ case 4:
if (args[0].equals("add")) if (args[0].equals("add"))
{ {
return Arrays.asList("<amplifier>"); return Arrays.asList("<amplifier>");
} }
} break;
else if (args.length == 5 && plugin.sl.isStaff(sender))
case 5:
if (plugin.sl.isStaff(sender))
{ {
if (args[0].equals("add")) if (args[0].equals("add"))
{ {
return FUtil.getPlayerList(); return FUtil.getPlayerList();
} }
} }
break;
}
return Collections.emptyList(); return Collections.emptyList();
} }