Reduced cognitive complexity in IslandResetCommand

This commit is contained in:
Florian CUNY 2019-01-18 17:40:23 +01:00
parent 755433e108
commit c5f561c816
2 changed files with 38 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import java.util.Set;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason; import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
@ -15,6 +16,9 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.island.NewIsland; import world.bentobox.bentobox.managers.island.NewIsland;
/**
* @author tastybento
*/
public class IslandResetCommand extends ConfirmableCommand { public class IslandResetCommand extends ConfirmableCommand {
public IslandResetCommand(CompositeCommand islandCommand) { public IslandResetCommand(CompositeCommand islandCommand) {
@ -48,7 +52,7 @@ public class IslandResetCommand extends ConfirmableCommand {
user.sendMessage("commands.island.reset.must-remove-members"); user.sendMessage("commands.island.reset.must-remove-members");
return false; return false;
} }
if (getIWM().getResetLimit(getWorld()) >= 0 ) { if (getIWM().getResetLimit(getWorld()) >= 0) {
int resetsLeft = getIWM().getResetLimit(getWorld()) - getPlayers().getResets(getWorld(), user.getUniqueId()); int resetsLeft = getIWM().getResetLimit(getWorld()) - getPlayers().getResets(getWorld(), user.getUniqueId());
if (resetsLeft <= 0) { if (resetsLeft <= 0) {
user.sendMessage("commands.island.reset.none-left"); user.sendMessage("commands.island.reset.none-left");
@ -58,24 +62,22 @@ public class IslandResetCommand extends ConfirmableCommand {
user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft)); user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft));
} }
} }
// Default schem is 'island' // Default schem is 'island'
String name = getSchemName(args);
String name = args.isEmpty() ? "island" : args.get(0).toLowerCase(java.util.Locale.ENGLISH); if (name == null) {
if (!args.isEmpty()) { // The schem name is not valid.
// Permission check user.sendMessage("commands.island.create.unknown-schem");
String permission = this.getPermissionPrefix() + "island.create." + name; return false;
if (!user.isOp() && !user.hasPermission(permission)) {
user.sendMessage("general.errors.no-permission", TextVariables.PERMISSION, permission);
return false;
}
// Check the schem name exists
Set<String> validNames = getPlugin().getSchemsManager().get(getWorld()).keySet();
if (!validNames.contains(name)) {
user.sendMessage("commands.island.create.unknown-schem");
return false;
}
} }
// Permission check if the name is not the default one
String permission = getPermissionPrefix() + "island.create." + name;
if (!name.equals("island") && (!user.isOp() || !user.hasPermission(permission))) {
user.sendMessage("general.errors.no-permission", TextVariables.PERMISSION, permission);
return false;
}
// Request confirmation // Request confirmation
if (getSettings().isResetConfirmation()) { if (getSettings().isResetConfirmation()) {
this.askConfirmation(user, () -> resetIsland(user, name)); this.askConfirmation(user, () -> resetIsland(user, name));
@ -83,7 +85,24 @@ public class IslandResetCommand extends ConfirmableCommand {
} else { } else {
return resetIsland(user, name); return resetIsland(user, name);
} }
}
/**
* Returns the schem name from the args.
* "island" is the default.
* May be null if the schem does not exist.
* @param args args of the command
* @return schem name or null
* @since 1.1
*/
@Nullable
private String getSchemName(List<String> args) {
String name = args.isEmpty() ? "island" : args.get(0).toLowerCase(java.util.Locale.ENGLISH);
Set<String> validNames = getPlugin().getSchemsManager().get(getWorld()).keySet();
if (!validNames.contains(name)) {
return null;
}
return name;
} }
private boolean resetIsland(User user, String name) { private boolean resetIsland(User user, String name) {

View File

@ -170,8 +170,8 @@ public class User {
} }
/** /**
* @param permission - permission string * @param permission permission string
* @return true if permission is empty or if the player has that permission * @return true if permission is empty or if the player has that permission.
*/ */
public boolean hasPermission(String permission) { public boolean hasPermission(String permission) {
return permission.isEmpty() || sender.hasPermission(permission); return permission.isEmpty() || sender.hasPermission(permission);