From 90d7db65cc85b84641d5f7327a86df3a27efdd1b Mon Sep 17 00:00:00 2001 From: bloodshot Date: Tue, 31 Dec 2019 20:12:55 -0500 Subject: [PATCH] Add server context support. Fixes #88 * Fix user permission check. * Add check for max inspection distance. * Add radius-inspect option to config. * Add active contexts when checking permissions. --- .../griefdefender/command/ClaimFlagBase.java | 13 +++++++++++-- .../griefdefender/command/ClaimOptionBase.java | 12 ++++++++++-- .../category/DefaultPermissionCategory.java | 17 +++++++++-------- .../listener/PlayerEventHandler.java | 3 +++ .../permission/GDPermissionManager.java | 8 +++++++- .../provider/LuckPermsProvider.java | 12 ++---------- .../com/griefdefender/storage/BaseStorage.java | 4 ++++ .../griefdefender/command/ClaimFlagBase.java | 13 +++++++++++-- .../griefdefender/command/ClaimOptionBase.java | 13 +++++++++++-- .../category/DefaultPermissionCategory.java | 17 +++++++++-------- .../listener/PlayerEventHandler.java | 3 +++ .../permission/GDPermissionManager.java | 8 +++++++- .../provider/LuckPermsProvider.java | 12 ++---------- .../com/griefdefender/storage/BaseStorage.java | 4 ++++ 14 files changed, 93 insertions(+), 46 deletions(-) diff --git a/bukkit/src/main/java/com/griefdefender/command/ClaimFlagBase.java b/bukkit/src/main/java/com/griefdefender/command/ClaimFlagBase.java index 1a52945..12a0531 100644 --- a/bukkit/src/main/java/com/griefdefender/command/ClaimFlagBase.java +++ b/bukkit/src/main/java/com/griefdefender/command/ClaimFlagBase.java @@ -950,12 +950,21 @@ private Consumer createFlagConsumer(GDPermissionUser src, GDClaim newContexts.addAll(contexts); } - // Remove server context + // Check server context final Iterator iterator = newContexts.iterator(); + boolean hasServerContext = false; while (iterator.hasNext()) { final Context context = iterator.next(); if (context.getKey().equals("server")) { - iterator.remove(); + hasServerContext = true; + break; + } + } + + if (!hasServerContext) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + newContexts.add(new Context("server", serverName)); } } diff --git a/bukkit/src/main/java/com/griefdefender/command/ClaimOptionBase.java b/bukkit/src/main/java/com/griefdefender/command/ClaimOptionBase.java index a622caa..f6a45c6 100644 --- a/bukkit/src/main/java/com/griefdefender/command/ClaimOptionBase.java +++ b/bukkit/src/main/java/com/griefdefender/command/ClaimOptionBase.java @@ -789,15 +789,23 @@ private Consumer newOptionValueConsumer(GDPermissionUser src, GDC newContexts.addAll(contexts); } - // Remove server context + // Check server context final Iterator iterator = newContexts.iterator(); + boolean hasServerContext = false; while (iterator.hasNext()) { final Context context = iterator.next(); if (context.getKey().equals("server")) { - iterator.remove(); + hasServerContext = true; + break; } } + if (!hasServerContext) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + newContexts.add(new Context("server", serverName)); + } + } final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts); if (result.successful()) { if (option == Options.PLAYER_WEATHER) { diff --git a/bukkit/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java b/bukkit/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java index 53c5e3c..7f7d3e8 100644 --- a/bukkit/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java +++ b/bukkit/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java @@ -75,24 +75,19 @@ public DefaultPermissionCategory() { this.defaultClaimFlags.put(ClaimTypes.WILDERNESS.getName().toLowerCase(), wildernessFlagMap); final int maxAccruedBlocks = GriefDefenderPlugin.CLAIM_BLOCK_SYSTEM == ClaimBlockSystem.VOLUME ? 20480000 : 80000; - this.defaultUserOptions.put(Options.EXPIRATION.getName(), "14"); - this.defaultUserOptions.put(Options.TAX_EXPIRATION.getName(), "7"); - this.defaultUserOptions.put(Options.TAX_EXPIRATION_DAYS_KEEP.getName(), "7"); - this.defaultUserOptions.put(Options.TAX_RATE.getName(), "1.0"); + this.defaultUserOptions.put(Options.ABANDON_DELAY.getName(), "0"); + this.defaultUserOptions.put(Options.ABANDON_RETURN_RATIO.getName(), "1.0"); this.defaultUserOptions.put(Options.BLOCKS_ACCRUED_PER_HOUR.getName(), "120"); this.defaultUserOptions.put(Options.CHEST_EXPIRATION.getName(), "7"); this.defaultUserOptions.put(Options.CREATE_LIMIT.getName(), "-1"); this.defaultUserOptions.put(Options.CREATE_MODE.getName(), "undefined"); this.defaultUserOptions.put(Options.ECONOMY_BLOCK_COST.getName(), "0.0"); this.defaultUserOptions.put(Options.ECONOMY_BLOCK_SELL_RETURN.getName(), "0.0"); + this.defaultUserOptions.put(Options.EXPIRATION.getName(), "14"); this.defaultUserOptions.put(Options.INITIAL_BLOCKS.getName(), "120"); this.defaultUserOptions.put(Options.MAX_ACCRUED_BLOCKS.getName(), Integer.toString(maxAccruedBlocks)); this.defaultUserOptions.put(Options.MIN_LEVEL.getName(), "0"); this.defaultUserOptions.put(Options.MAX_LEVEL.getName(), "255"); - this.defaultUserOptions.put(Options.ABANDON_DELAY.getName(), "0"); - this.defaultUserOptions.put(Options.ABANDON_RETURN_RATIO.getName(), "1.0"); - this.defaultUserOptions.put(Options.RAID.getName(), "true"); - this.defaultUserOptions.put(Options.SPAWN_LIMIT.getName(), "-1"); this.defaultUserOptions.put(Options.PLAYER_DENY_FLIGHT.getName(), "false"); this.defaultUserOptions.put(Options.PLAYER_DENY_GODMODE.getName(), "false"); this.defaultUserOptions.put(Options.PLAYER_DENY_HUNGER.getName(), "false"); @@ -107,6 +102,12 @@ public DefaultPermissionCategory() { this.defaultUserOptions.put(Options.PVP_COMBAT_COMMAND.getName(), "false"); this.defaultUserOptions.put(Options.PVP_COMBAT_TELEPORT.getName(), "false"); this.defaultUserOptions.put(Options.PVP_COMBAT_TIMEOUT.getName(), "15"); + this.defaultUserOptions.put(Options.RAID.getName(), "true"); + this.defaultUserOptions.put(Options.RADIUS_INSPECT.getName(), "100"); + this.defaultUserOptions.put(Options.SPAWN_LIMIT.getName(), "-1"); + this.defaultUserOptions.put(Options.TAX_EXPIRATION.getName(), "7"); + this.defaultUserOptions.put(Options.TAX_EXPIRATION_DAYS_KEEP.getName(), "7"); + this.defaultUserOptions.put(Options.TAX_RATE.getName(), "1.0"); this.defaultBasicOptions.put(Options.MIN_SIZE_X.getName(), "5"); this.defaultBasicOptions.put(Options.MIN_SIZE_Y.getName(), "5"); diff --git a/bukkit/src/main/java/com/griefdefender/listener/PlayerEventHandler.java b/bukkit/src/main/java/com/griefdefender/listener/PlayerEventHandler.java index 7172541..6b3febf 100644 --- a/bukkit/src/main/java/com/griefdefender/listener/PlayerEventHandler.java +++ b/bukkit/src/main/java/com/griefdefender/listener/PlayerEventHandler.java @@ -1539,6 +1539,9 @@ private boolean investigateClaim(PlayerInteractEvent event, Player player, Block } private GDClaim findNearbyClaim(Player player, int maxDistance) { + if (maxDistance <= 0) { + maxDistance = 100; + } BlockRay blockRay = BlockRay.from(player).distanceLimit(maxDistance).build(); GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId()); GDClaim claim = null; diff --git a/bukkit/src/main/java/com/griefdefender/permission/GDPermissionManager.java b/bukkit/src/main/java/com/griefdefender/permission/GDPermissionManager.java index 03cc9ee..3edfee0 100644 --- a/bukkit/src/main/java/com/griefdefender/permission/GDPermissionManager.java +++ b/bukkit/src/main/java/com/griefdefender/permission/GDPermissionManager.java @@ -313,12 +313,18 @@ private Tristate getUserPermission(GDPermissionHolder holder, Claim claim, Strin contexts.remove(parent.getContext()); } + // Check claim context contexts.add(claim.getContext()); - contexts.add(claim.getType().getContext()); Tristate value = PermissionUtil.getInstance().getPermissionValue((GDClaim) claim, holder, permission, contexts); if (value != Tristate.UNDEFINED) { return processResult(claim, permission, value, holder); } + // Check default type context + contexts.add(claim.getType().getContext()); + value = PermissionUtil.getInstance().getPermissionValue((GDClaim) claim, holder, permission, contexts); + if (value != Tristate.UNDEFINED) { + return processResult(claim, permission, value, holder); + } if (holder == GriefDefenderPlugin.DEFAULT_HOLDER) { return getFlagDefaultPermission(claim, permission, contexts); diff --git a/bukkit/src/main/java/com/griefdefender/provider/LuckPermsProvider.java b/bukkit/src/main/java/com/griefdefender/provider/LuckPermsProvider.java index 75da244..662dd4e 100644 --- a/bukkit/src/main/java/com/griefdefender/provider/LuckPermsProvider.java +++ b/bukkit/src/main/java/com/griefdefender/provider/LuckPermsProvider.java @@ -561,16 +561,7 @@ public Map, Map> getAllPermissions(GDPermissionHol public Set getGPContexts(ContextSet contextSet) { final Set gpContexts = new HashSet<>(); for (net.luckperms.api.context.Context context : contextSet.toSet()) { - if (context.getKey().startsWith("gd_") || context.getKey().equals("used_item") - || context.getKey().equals("source") || context.getKey().equals("target") - || context.getKey().equals("world") || context.getKey().equals("server") - || context.getKey().equals("state")) { - if (contextSet.containsKey(ContextKeys.CLAIM) && context.getKey().equals("server")) { - continue; - } - - gpContexts.add(new Context(context.getKey(), context.getValue())); - } + gpContexts.add(new Context(context.getKey(), context.getValue())); } return gpContexts; } @@ -586,6 +577,7 @@ public Tristate getPermissionValue(GDClaim claim, GDPermissionHolder holder, Str } public Tristate getPermissionValue(GDClaim claim, GDPermissionHolder holder, String permission, Set contexts) { + this.addActiveContexts(contexts, holder, null, claim); return this.getPermissionValue(holder, permission, contexts); } diff --git a/bukkit/src/main/java/com/griefdefender/storage/BaseStorage.java b/bukkit/src/main/java/com/griefdefender/storage/BaseStorage.java index 964cd89..c9f225f 100644 --- a/bukkit/src/main/java/com/griefdefender/storage/BaseStorage.java +++ b/bukkit/src/main/java/com/griefdefender/storage/BaseStorage.java @@ -378,6 +378,10 @@ public void setDefaultGlobalPermissions() { } private void setDefaultFlags(Set contexts, Map defaultFlags) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + contexts.add(new Context("server", serverName)); + } GriefDefenderPlugin.getInstance().executor.execute(() -> { for (Map.Entry mapEntry : defaultFlags.entrySet()) { final Flag flag = FlagRegistryModule.getInstance().getById(mapEntry.getKey()).orElse(null); diff --git a/sponge/src/main/java/com/griefdefender/command/ClaimFlagBase.java b/sponge/src/main/java/com/griefdefender/command/ClaimFlagBase.java index b462555..5ad058f 100644 --- a/sponge/src/main/java/com/griefdefender/command/ClaimFlagBase.java +++ b/sponge/src/main/java/com/griefdefender/command/ClaimFlagBase.java @@ -950,12 +950,21 @@ private Consumer createFlagConsumer(GDPermissionUser src, GDClaim newContexts.addAll(contexts); } - // Remove server context + // Check server context final Iterator iterator = newContexts.iterator(); + boolean hasServerContext = false; while (iterator.hasNext()) { final Context context = iterator.next(); if (context.getKey().equals("server")) { - iterator.remove(); + hasServerContext = true; + break; + } + } + + if (!hasServerContext) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + newContexts.add(new Context("server", serverName)); } } diff --git a/sponge/src/main/java/com/griefdefender/command/ClaimOptionBase.java b/sponge/src/main/java/com/griefdefender/command/ClaimOptionBase.java index bc207d1..318ca9a 100644 --- a/sponge/src/main/java/com/griefdefender/command/ClaimOptionBase.java +++ b/sponge/src/main/java/com/griefdefender/command/ClaimOptionBase.java @@ -789,15 +789,24 @@ private Consumer newOptionValueConsumer(GDPermissionUser src, GDC newContexts.addAll(contexts); } - // Remove server context + // Check server context final Iterator iterator = newContexts.iterator(); + boolean hasServerContext = false; while (iterator.hasNext()) { final Context context = iterator.next(); if (context.getKey().equals("server")) { - iterator.remove(); + //iterator.remove(); + hasServerContext = true; + break; } } + if (!hasServerContext) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + newContexts.add(new Context("server", serverName)); + } + } final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts); if (result.successful()) { if (option == Options.PLAYER_WEATHER) { diff --git a/sponge/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java b/sponge/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java index 53c5e3c..7f7d3e8 100644 --- a/sponge/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java +++ b/sponge/src/main/java/com/griefdefender/configuration/category/DefaultPermissionCategory.java @@ -75,24 +75,19 @@ public DefaultPermissionCategory() { this.defaultClaimFlags.put(ClaimTypes.WILDERNESS.getName().toLowerCase(), wildernessFlagMap); final int maxAccruedBlocks = GriefDefenderPlugin.CLAIM_BLOCK_SYSTEM == ClaimBlockSystem.VOLUME ? 20480000 : 80000; - this.defaultUserOptions.put(Options.EXPIRATION.getName(), "14"); - this.defaultUserOptions.put(Options.TAX_EXPIRATION.getName(), "7"); - this.defaultUserOptions.put(Options.TAX_EXPIRATION_DAYS_KEEP.getName(), "7"); - this.defaultUserOptions.put(Options.TAX_RATE.getName(), "1.0"); + this.defaultUserOptions.put(Options.ABANDON_DELAY.getName(), "0"); + this.defaultUserOptions.put(Options.ABANDON_RETURN_RATIO.getName(), "1.0"); this.defaultUserOptions.put(Options.BLOCKS_ACCRUED_PER_HOUR.getName(), "120"); this.defaultUserOptions.put(Options.CHEST_EXPIRATION.getName(), "7"); this.defaultUserOptions.put(Options.CREATE_LIMIT.getName(), "-1"); this.defaultUserOptions.put(Options.CREATE_MODE.getName(), "undefined"); this.defaultUserOptions.put(Options.ECONOMY_BLOCK_COST.getName(), "0.0"); this.defaultUserOptions.put(Options.ECONOMY_BLOCK_SELL_RETURN.getName(), "0.0"); + this.defaultUserOptions.put(Options.EXPIRATION.getName(), "14"); this.defaultUserOptions.put(Options.INITIAL_BLOCKS.getName(), "120"); this.defaultUserOptions.put(Options.MAX_ACCRUED_BLOCKS.getName(), Integer.toString(maxAccruedBlocks)); this.defaultUserOptions.put(Options.MIN_LEVEL.getName(), "0"); this.defaultUserOptions.put(Options.MAX_LEVEL.getName(), "255"); - this.defaultUserOptions.put(Options.ABANDON_DELAY.getName(), "0"); - this.defaultUserOptions.put(Options.ABANDON_RETURN_RATIO.getName(), "1.0"); - this.defaultUserOptions.put(Options.RAID.getName(), "true"); - this.defaultUserOptions.put(Options.SPAWN_LIMIT.getName(), "-1"); this.defaultUserOptions.put(Options.PLAYER_DENY_FLIGHT.getName(), "false"); this.defaultUserOptions.put(Options.PLAYER_DENY_GODMODE.getName(), "false"); this.defaultUserOptions.put(Options.PLAYER_DENY_HUNGER.getName(), "false"); @@ -107,6 +102,12 @@ public DefaultPermissionCategory() { this.defaultUserOptions.put(Options.PVP_COMBAT_COMMAND.getName(), "false"); this.defaultUserOptions.put(Options.PVP_COMBAT_TELEPORT.getName(), "false"); this.defaultUserOptions.put(Options.PVP_COMBAT_TIMEOUT.getName(), "15"); + this.defaultUserOptions.put(Options.RAID.getName(), "true"); + this.defaultUserOptions.put(Options.RADIUS_INSPECT.getName(), "100"); + this.defaultUserOptions.put(Options.SPAWN_LIMIT.getName(), "-1"); + this.defaultUserOptions.put(Options.TAX_EXPIRATION.getName(), "7"); + this.defaultUserOptions.put(Options.TAX_EXPIRATION_DAYS_KEEP.getName(), "7"); + this.defaultUserOptions.put(Options.TAX_RATE.getName(), "1.0"); this.defaultBasicOptions.put(Options.MIN_SIZE_X.getName(), "5"); this.defaultBasicOptions.put(Options.MIN_SIZE_Y.getName(), "5"); diff --git a/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java b/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java index e9ec56e..65937a0 100644 --- a/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java +++ b/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java @@ -1668,6 +1668,9 @@ private boolean investigateClaim(InteractEvent event, Player player, BlockSnapsh } private GDClaim findNearbyClaim(Player player, int maxDistance) { + if (maxDistance <= 0) { + maxDistance = 100; + } BlockRay blockRay = BlockRay.from(player).distanceLimit(maxDistance).build(); GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId()); GDClaim claim = null; diff --git a/sponge/src/main/java/com/griefdefender/permission/GDPermissionManager.java b/sponge/src/main/java/com/griefdefender/permission/GDPermissionManager.java index 7b145be..5b53532 100644 --- a/sponge/src/main/java/com/griefdefender/permission/GDPermissionManager.java +++ b/sponge/src/main/java/com/griefdefender/permission/GDPermissionManager.java @@ -357,12 +357,18 @@ private Tristate getUserPermission(GDPermissionHolder holder, Claim claim, Strin contexts.remove(parent.getContext()); } + // Check claim context contexts.add(claim.getContext()); - contexts.add(claim.getType().getContext()); Tristate value = PermissionUtil.getInstance().getPermissionValue((GDClaim) claim, holder, permission, contexts); if (value != Tristate.UNDEFINED) { return processResult(claim, permission, value, holder); } + // Check default type context + contexts.add(claim.getType().getContext()); + value = PermissionUtil.getInstance().getPermissionValue((GDClaim) claim, holder, permission, contexts); + if (value != Tristate.UNDEFINED) { + return processResult(claim, permission, value, holder); + } if (holder == GriefDefenderPlugin.DEFAULT_HOLDER) { return getFlagDefaultPermission(claim, permission, contexts); diff --git a/sponge/src/main/java/com/griefdefender/provider/LuckPermsProvider.java b/sponge/src/main/java/com/griefdefender/provider/LuckPermsProvider.java index bce0269..01738b4 100644 --- a/sponge/src/main/java/com/griefdefender/provider/LuckPermsProvider.java +++ b/sponge/src/main/java/com/griefdefender/provider/LuckPermsProvider.java @@ -557,16 +557,7 @@ public Map, Map> getAllPermissions(GDPermissionHol public Set getGPContexts(ContextSet contextSet) { final Set gpContexts = new HashSet<>(); for (net.luckperms.api.context.Context context : contextSet.toSet()) { - if (context.getKey().startsWith("gd_") || context.getKey().equals("used_item") - || context.getKey().equals("source") || context.getKey().equals("target") - || context.getKey().equals("world") || context.getKey().equals("server") - || context.getKey().equals("state")) { - if (contextSet.containsKey(ContextKeys.CLAIM) && context.getKey().equals("server")) { - continue; - } - - gpContexts.add(new Context(context.getKey(), context.getValue())); - } + gpContexts.add(new Context(context.getKey(), context.getValue())); } return gpContexts; } @@ -582,6 +573,7 @@ public Tristate getPermissionValue(GDClaim claim, GDPermissionHolder holder, Str } public Tristate getPermissionValue(GDClaim claim, GDPermissionHolder holder, String permission, Set contexts) { + this.addActiveContexts(contexts, holder, null, claim); return this.getPermissionValue(holder, permission, contexts); } diff --git a/sponge/src/main/java/com/griefdefender/storage/BaseStorage.java b/sponge/src/main/java/com/griefdefender/storage/BaseStorage.java index 361a20f..1a6656b 100644 --- a/sponge/src/main/java/com/griefdefender/storage/BaseStorage.java +++ b/sponge/src/main/java/com/griefdefender/storage/BaseStorage.java @@ -377,6 +377,10 @@ public void setDefaultGlobalPermissions() { } private void setDefaultFlags(Set contexts, Map defaultFlags) { + final String serverName = PermissionUtil.getInstance().getServerName(); + if (serverName != null) { + contexts.add(new Context("server", serverName)); + } GriefDefenderPlugin.getInstance().executor.execute(() -> { for (Map.Entry mapEntry : defaultFlags.entrySet()) { final Flag flag = FlagRegistryModule.getInstance().getById(mapEntry.getKey()).orElse(null);