From 7239e7bdace87378a285dc26ed375a1b96888b27 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 23 Nov 2021 00:37:41 +0000 Subject: [PATCH] Add option to stack limit permissions, Close #108 --- .../huskhomes2/config/Settings.java | 11 ++- .../huskhomes2/teleport/points/Home.java | 96 +++++++++++-------- src/main/resources/config.yml | 3 +- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/config/Settings.java b/src/main/java/me/william278/huskhomes2/config/Settings.java index 21ec909e..3d8d5831 100644 --- a/src/main/java/me/william278/huskhomes2/config/Settings.java +++ b/src/main/java/me/william278/huskhomes2/config/Settings.java @@ -96,6 +96,9 @@ public class Settings { // Whether to autocomplete player names for tpa (e.g /tpa William will assume you mean /tpa William278) private boolean autoCompletePlayerNames; + // Whether to stack limit permissions (add them) if a user has multiple. By default, the highest one will be returned + private boolean homeLimitPermissionStacking; + // RTP command settings private boolean doRtpCommand; private int rtpRange; @@ -242,7 +245,9 @@ public class Settings { this.warpRestrictionPermissionFormat = config.getString("warp_command.permission_restrictions.format", "huskhomes.warp."); this.hideRestrictedWarps = config.getBoolean("warp_command.permission_restrictions.hide_restricted_warps", true); - this.autoCompletePlayerNames = config.getBoolean("general.auto_complete_player_names", false); + this.autoCompletePlayerNames = config.getBoolean("general.auto_complete_player_names", true); + + this.homeLimitPermissionStacking = config.getBoolean("general.home_limit_permission_stacking", false); this.doRtpCommand = config.getBoolean("random_teleport_command.enabled", true); this.rtpRange = config.getInt("random_teleport_command.range", 5000); @@ -520,4 +525,8 @@ public class Settings { public boolean doAutoCompletePlayerNames() { return autoCompletePlayerNames; } + + public boolean doHomeLimitPermissionStacking() { + return homeLimitPermissionStacking; + } } diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/Home.java b/src/main/java/me/william278/huskhomes2/teleport/points/Home.java index 201ef3e4..1ba6021e 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/Home.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/Home.java @@ -8,9 +8,11 @@ import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionAttachmentInfo; import java.util.UUID; +import java.util.regex.PatternSyntaxException; /** * An object representing a Player's home in-game + * * @author William */ public class Home extends SetPoint { @@ -21,11 +23,12 @@ public class Home extends SetPoint { /** * An object representing a Player's home in-game - * @param location The Bukkit {@link Location} of the home - * @param server The Bungee server ID of the home + * + * @param location The Bukkit {@link Location} of the home + * @param server The Bungee server ID of the home * @param homeOwner The Player who owns the home - * @param name The name of the home - * @param isPublic Whether the home is public + * @param name The name of the home + * @param isPublic Whether the home is public */ public Home(Location location, String server, Player homeOwner, String name, boolean isPublic) { super(location, server, name, MessageManager.getRawMessage("home_default_description", homeOwner.getName())); @@ -36,13 +39,14 @@ public class Home extends SetPoint { /** * An object representing a Player's home in-game - * @deprecated Use other method that accepts UUID instead of UUID-string + * * @param teleportationPoint The {@link TeleportationPoint} position of the home - * @param ownerUsername The username of the owner - * @param ownerUUID The UUID string of the owner - * @param name The name of the home - * @param description The description of the home - * @param isPublic Whether the home is public + * @param ownerUsername The username of the owner + * @param ownerUUID The UUID string of the owner + * @param name The name of the home + * @param description The description of the home + * @param isPublic Whether the home is public + * @deprecated Use other method that accepts UUID instead of UUID-string */ public Home(TeleportationPoint teleportationPoint, String ownerUsername, String ownerUUID, String name, String description, boolean isPublic, long creationTime) throws IllegalArgumentException { super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server, name, description, creationTime); @@ -53,12 +57,13 @@ public class Home extends SetPoint { /** * An object representing a Player's home in-game + * * @param teleportationPoint The {@link TeleportationPoint} position of the home - * @param ownerUsername The username of the owner - * @param ownerUUID The {@link UUID} of the owner - * @param name The name of the home - * @param description The description of the home - * @param isPublic Whether the home is public + * @param ownerUsername The username of the owner + * @param ownerUUID The {@link UUID} of the owner + * @param name The name of the home + * @param description The description of the home + * @param isPublic Whether the home is public */ public Home(TeleportationPoint teleportationPoint, String ownerUsername, UUID ownerUUID, String name, String description, boolean isPublic, long creationTime) { super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server, name, description, creationTime); @@ -69,6 +74,7 @@ public class Home extends SetPoint { /** * Returns whether the Home is public + * * @return if the Home is public */ public boolean isPublic() { @@ -77,6 +83,7 @@ public class Home extends SetPoint { /** * Set the privacy of the home + * * @param isPublic should the home be public? */ public void setPublic(boolean isPublic) { @@ -85,6 +92,7 @@ public class Home extends SetPoint { /** * Returns the UUID of the Home's owner + * * @return the owner's UUID */ public UUID getOwnerUUID() { @@ -93,6 +101,7 @@ public class Home extends SetPoint { /** * Returns the username of the Home's owner + * * @return the owner's username */ public String getOwnerUsername() { @@ -101,6 +110,7 @@ public class Home extends SetPoint { /** * Returns the Player who owns the home + * * @return the Player owner of the home */ public Player getOwner() { @@ -114,6 +124,7 @@ public class Home extends SetPoint { /** * Set the owner of the home + * * @param player The new owner of the home */ public void setOwner(Player player) { @@ -121,20 +132,13 @@ public class Home extends SetPoint { this.ownerUsername = player.getName(); } + /* + * Static methods for fetching player set home limits + */ + // Returns the maximum number of set homes a player can make - public static int getSetHomeLimit(Player p) { - p.recalculatePermissions(); - int maxSetHomes = -1; - for (PermissionAttachmentInfo permissionAI : p.getEffectivePermissions()) { - String permission = permissionAI.getPermission(); - if (permission.contains("huskhomes.max_sethomes.")) { - try { - if (Integer.parseInt(permission.split("\\.")[2]) > maxSetHomes) { - maxSetHomes = Integer.parseInt(permission.split("\\.")[2]); - } - } catch (Exception ignored) { } - } - } + public static int getSetHomeLimit(Player player) { + int maxSetHomes = getCountFromPermissions(player, "huskhomes.max_sethomes."); if (maxSetHomes != -1) { return maxSetHomes; } @@ -142,21 +146,33 @@ public class Home extends SetPoint { } // Returns the number of set homes a player can set for free - public static int getFreeHomes(Player p) { - int freeSetHomes = -1; - for (PermissionAttachmentInfo permissionAI : p.getEffectivePermissions()) { - String permission = permissionAI.getPermission(); - if (permission.contains("huskhomes.free_sethomes.")) { - try { - if (Integer.parseInt(permission.split("\\.")[2]) > freeSetHomes) { - freeSetHomes = Integer.parseInt(permission.split("\\.")[2]); - } - } catch(Exception ignored) { } - } - } + public static int getFreeHomes(Player player) { + int freeSetHomes = getCountFromPermissions(player, "huskhomes.free_sethomes."); if (freeSetHomes != -1) { return freeSetHomes; } return HuskHomes.getSettings().getFreeHomeSlots(); } + + // Return the value from the player's permissions + private static int getCountFromPermissions(Player player, String permissionPrefix) { + player.recalculatePermissions(); + int count = -1 + (HuskHomes.getSettings().doHomeLimitPermissionStacking() ? 1 : 0); + for (PermissionAttachmentInfo permissionAI : player.getEffectivePermissions()) { + String permission = permissionAI.getPermission(); + if (permission.contains(permissionPrefix)) { + try { + int value = Integer.parseInt(permission.split("\\.")[2]); + if (HuskHomes.getSettings().doHomeLimitPermissionStacking()) { + count += value; + } else { + if (value > count) { + count = value; + } + } + } catch (NumberFormatException | PatternSyntaxException ignored) {} + } + } + return count; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 629999fb..1a61be15 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -28,7 +28,8 @@ data_storage_options: warps_data: 'huskhomes_warp_data' general: max_sethomes: 10 - auto_complete_player_names: false + auto_complete_player_names: true + home_limit_permission_stacking: false teleport_warmup_time: 5 teleport_request_expiry_time: 60 allow_unicode_descriptions: true