Replaced Util#getPermValue(Player, String, int) with User#getPermissionValue(String, int)

This commit is contained in:
Florian CUNY 2018-10-20 11:32:42 +02:00
parent 3102195156
commit 95bfab916e
9 changed files with 43 additions and 51 deletions

View File

@ -4,7 +4,6 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
* This is a custom help for the /island go and /island sethome commands. It overrides the default help sub command.
@ -12,7 +11,6 @@ import world.bentobox.bentobox.util.Util;
* This is an example of a custom help as much as anything.
*
* @author tastybento
*
*/
public class CustomIslandMultiHomeHelp extends CompositeCommand {
@ -47,7 +45,7 @@ public class CustomIslandMultiHomeHelp extends CompositeCommand {
private void showPrettyHelp(User user, String usage, String params, String desc) {
// Player. Check perms
if (user.hasPermission(getPermission())) {
int maxHomes = Util.getPermValue(user.getPlayer(), getPermissionPrefix() + "island.maxhomes", getIWM().getMaxHomes(getWorld()));
int maxHomes = user.getPermissionValue(getPermissionPrefix() + "island.maxhomes", getIWM().getMaxHomes(getWorld()));
if (maxHomes > 1) {
params = getParameters().isEmpty() ? "" : user.getTranslation(getParameters());
}

View File

@ -7,7 +7,6 @@ import org.apache.commons.lang.math.NumberUtils;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
@ -34,7 +33,7 @@ public class IslandGoCommand extends CompositeCommand {
}
if (!args.isEmpty() && NumberUtils.isDigits(args.get(0))) {
int homeValue = Integer.parseInt(args.get(0));
int maxHomes = Util.getPermValue(user.getPlayer(), "island.maxhomes", getIWM().getMaxHomes(getWorld()));
int maxHomes = user.getPermissionValue("island.maxhomes", getIWM().getMaxHomes(getWorld()));
if (homeValue > 1 && homeValue <= maxHomes) {
getIslands().homeTeleport(getWorld(), user.getPlayer(), homeValue);
user.sendMessage("commands.island.go.tip", TextVariables.LABEL, getTopLabel());

View File

@ -6,7 +6,6 @@ import java.util.UUID;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
public class IslandSethomeCommand extends CompositeCommand {
@ -40,7 +39,7 @@ public class IslandSethomeCommand extends CompositeCommand {
user.sendMessage("commands.island.sethome.home-set");
} else {
// Dynamic home sizes with permissions
int maxHomes = Util.getPermValue(user.getPlayer(), "island.maxhomes", getIWM().getMaxHomes(getWorld()));
int maxHomes = user.getPermissionValue("island.maxhomes", getIWM().getMaxHomes(getWorld()));
if (maxHomes > 1) {
// Check the number given is a number
int number;

View File

@ -151,7 +151,7 @@ public class IslandTeamInviteCommand extends CompositeCommand {
* @return max team size of user
*/
public int getMaxTeamSize(User user) {
return Util.getPermValue(user.getPlayer(), getPermissionPrefix() + "team.maxsize.", getIWM().getMaxTeamSize(getWorld()));
return user.getPermissionValue(getPermissionPrefix() + "team.maxsize.", getIWM().getMaxTeamSize(getWorld()));
}
}

View File

@ -6,6 +6,7 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.*;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -182,6 +183,38 @@ public class User {
return false;
}
/**
* Get the maximum value of a numerical permission setting
* @param permissionPrefix the start of the perm, e.g., bskyblock.maxhomes
* @param defaultValue the default value; the result may be higher or lower than this
* @return max value
*/
public int getPermissionValue(String permissionPrefix, int defaultValue) {
int value = defaultValue;
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (perms.getPermission().startsWith(permissionPrefix + ".")) {
// Get the max value should there be more than one
if (perms.getPermission().contains(permissionPrefix + ".*")) {
return value;
} else {
String[] spl = perms.getPermission().split(permissionPrefix + ".");
if (spl.length > 1) {
if (!NumberUtils.isDigits(spl[1])) {
plugin.logError("Player " + player.getName() + " has permission: '" + perms.getPermission() + "' <-- the last part MUST be a number! Ignoring...");
} else {
value = Math.max(value, Integer.valueOf(spl[1]));
}
}
}
}
// Do some sanity checking
if (value < 1) {
value = 1;
}
}
return value;
}
/**
* Gets a translation of this reference for this user.
* @param reference - reference found in a locale file

View File

@ -792,7 +792,7 @@ public class IslandsManager {
target.sendMessage("commands.island.team.setowner.you-are-the-owner");
if (target.isOnline()) {
// Check if new leader has a different range permission than the island size
int range = Util.getPermValue(target.getPlayer(), permPrefix + "island.range.", plugin.getIWM().getIslandProtectionRange(Util.getWorld(island.getWorld())));
int range = target.getPermissionValue(permPrefix + "island.range", plugin.getIWM().getIslandProtectionRange(Util.getWorld(island.getWorld())));
// Range can go up or down
if (range != island.getProtectionRange()) {
user.sendMessage("commands.admin.setrange.range-updated", TextVariables.NUMBER, String.valueOf(range));
@ -801,7 +801,6 @@ public class IslandsManager {
+ range + " for " + user.getName() + " due to permission.");
}
island.setProtectionRange(range);
}
}

View File

@ -172,44 +172,10 @@ public class Util {
return returned;
}
/**
* Get the maximum value of a numerical perm setting
* @param player - the player - the player to check
* @param perm - the start of the perm, e.g., bskyblock.maxhomes
* @param permValue - the default value - the result may be higher or lower than this
* @return max value
*/
public static int getPermValue(Player player, String perm, int permValue) {
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (perms.getPermission().startsWith(perm + ".")) {
// Get the max value should there be more than one
if (perms.getPermission().contains(perm + ".*")) {
return permValue;
} else {
String[] spl = perms.getPermission().split(perm + ".");
if (spl.length > 1) {
if (!NumberUtils.isDigits(spl[1])) {
plugin.logError("Player " + player.getName() + " has permission: " + perms.getPermission() + " <-- the last part MUST be a number! Ignoring...");
} else {
permValue = Math.max(permValue, Integer.valueOf(spl[1]));
}
}
}
}
// Do some sanity checking
if (permValue < 1) {
permValue = 1;
}
}
return permValue;
}
public static String xyz(Vector location) {
return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
}
/**
* Checks is world = world2 irrespective of the world type
* @param world - world

View File

@ -113,7 +113,7 @@ public class IslandGoCommandTest {
// Number of homes
PowerMockito.mockStatic(Util.class);
// 1 home for now
when(Util.getPermValue(Mockito.any(Player.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(1);
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(1);
}
/**
@ -125,7 +125,6 @@ public class IslandGoCommandTest {
IslandGoCommand igc = new IslandGoCommand(ic);
assertFalse(igc.execute(user, igc.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage("general.errors.no-island");
}
/**
@ -144,7 +143,7 @@ public class IslandGoCommandTest {
@Test
public void testExecuteNoArgsMultipleHomes() {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(Util.getPermValue(Mockito.any(Player.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
IslandGoCommand igc = new IslandGoCommand(ic);
assertTrue(igc.execute(user, igc.getLabel(), new ArrayList<>()));
}
@ -155,7 +154,7 @@ public class IslandGoCommandTest {
@Test
public void testExecuteArgs1MultipleHomes() {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(Util.getPermValue(Mockito.any(Player.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
IslandGoCommand igc = new IslandGoCommand(ic);
List<String> args = new ArrayList<>();
args.add("1");
@ -168,7 +167,7 @@ public class IslandGoCommandTest {
@Test
public void testExecuteArgs2MultipleHomes() {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(Util.getPermValue(Mockito.any(Player.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
IslandGoCommand igc = new IslandGoCommand(ic);
List<String> args = new ArrayList<>();
args.add("2");
@ -183,7 +182,7 @@ public class IslandGoCommandTest {
@Test
public void testExecuteArgsJunkMultipleHomes() {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(Util.getPermValue(Mockito.any(Player.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(3);
IslandGoCommand igc = new IslandGoCommand(ic);
List<String> args = new ArrayList<>();
args.add("sdfsdf");

View File

@ -152,7 +152,6 @@ public class PlayersManagerTest {
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
System.out.println(file.exists());
}
}