From 556d01d1b32843f58923383a960e33ca6106ebfa Mon Sep 17 00:00:00 2001 From: oddlama Date: Sun, 19 Mar 2023 17:33:08 +0100 Subject: [PATCH] feat: global role and environment setting overrides (closes #190) --- .../vane/core/config/TranslatedItemStack.java | 11 +++++ .../RegionGlobalEnvironmentOverrides.java | 48 +++++++++++++++++++ .../regions/RegionGlobalRoleOverrides.java | 40 ++++++++++++++++ .../org/oddlama/vane/regions/Regions.java | 5 ++ .../vane/regions/menu/RegionGroupMenu.java | 28 +++++++---- .../oddlama/vane/regions/menu/RoleMenu.java | 20 +++++++- .../regions/region/EnvironmentSetting.java | 10 ++++ .../vane/regions/region/RegionGroup.java | 3 ++ .../org/oddlama/vane/regions/region/Role.java | 4 ++ .../vane/regions/region/RoleSetting.java | 10 ++++ 10 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalEnvironmentOverrides.java create mode 100644 vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalRoleOverrides.java diff --git a/vane-core/src/main/java/org/oddlama/vane/core/config/TranslatedItemStack.java b/vane-core/src/main/java/org/oddlama/vane/core/config/TranslatedItemStack.java index fad237762..9e5125cc5 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/config/TranslatedItemStack.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/config/TranslatedItemStack.java @@ -2,6 +2,9 @@ import static org.oddlama.vane.util.ItemUtil.name_item; +import java.util.List; +import java.util.function.Consumer; + import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; @@ -16,6 +19,8 @@ import org.oddlama.vane.core.module.Module; import org.oddlama.vane.core.module.ModuleComponent; +import net.kyori.adventure.text.Component; + public class TranslatedItemStack> extends ModuleComponent { @ConfigInt(def = 1, min = 0, desc = "The item stack amount.") @@ -72,6 +77,12 @@ public ItemStack item(Object... args) { return name_item(config_material.item(config_amount), lang_name.format(args), lang_lore.format(args)); } + public ItemStack item_transform_lore(Consumer> f_lore, Object... args) { + final var lore = lang_lore.format(args); + f_lore.accept(lore); + return name_item(config_material.item(config_amount), lang_name.format(args), lore); + } + public ItemStack item_amount(int amount, Object... args) { return name_item(config_material.item(amount), lang_name.format(args), lang_lore.format(args)); } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalEnvironmentOverrides.java b/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalEnvironmentOverrides.java new file mode 100644 index 000000000..9bc95e06c --- /dev/null +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalEnvironmentOverrides.java @@ -0,0 +1,48 @@ +package org.oddlama.vane.regions; + +import org.oddlama.vane.annotation.config.ConfigInt; +import org.oddlama.vane.core.module.Context; +import org.oddlama.vane.core.module.ModuleComponent; +import org.oddlama.vane.regions.region.EnvironmentSetting; + +public class RegionGlobalEnvironmentOverrides extends ModuleComponent { + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether animals can spawn.") + public int config_animals; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether monsters can spawn.") + public int config_monsters; + + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether explosions can happen.") + public int config_explosions; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether fire spreads and consumes.") + public int config_fire; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether pvp is allowed.") + public int config_pvp; + + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether fields can be trampled.") + public int config_trample; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides whether vines can grow.") + public int config_vine_growth; + + public RegionGlobalEnvironmentOverrides(Context context) { + super(context.namespace("global_environment_overrides", "This controls global environment setting overrides for all regions on the server. `0` means no-override, the player-configured values are used normally, `1` force-enables this setting for all regions, `-1` force-disables respectively.")); + } + + public int get_override(final EnvironmentSetting setting) { + switch (setting) { + case ANIMALS: return config_animals; + case MONSTERS: return config_monsters; + case EXPLOSIONS: return config_explosions; + case FIRE: return config_fire; + case PVP: return config_pvp; + case TRAMPLE: return config_trample; + case VINE_GROWTH: return config_vine_growth; + } + return 0; + } + + @Override + public void on_enable() {} + + @Override + public void on_disable() {} +} diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalRoleOverrides.java b/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalRoleOverrides.java new file mode 100644 index 000000000..479fe2f00 --- /dev/null +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/RegionGlobalRoleOverrides.java @@ -0,0 +1,40 @@ +package org.oddlama.vane.regions; + +import org.oddlama.vane.annotation.config.ConfigInt; +import org.oddlama.vane.core.module.Context; +import org.oddlama.vane.core.module.ModuleComponent; +import org.oddlama.vane.regions.region.RoleSetting; + +public class RegionGlobalRoleOverrides extends ModuleComponent { + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the admin permission. Be careful, this is almost never what you want and may result in immutable regions.") + public int config_admin; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the build permission.") + public int config_build; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the use permission.") + public int config_use; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the container permission.") + public int config_container; + @ConfigInt(def = 0, min = -1, max = 1, desc = "Overrides the portal permission.") + public int config_portal; + + public RegionGlobalRoleOverrides(Context context) { + super(context.namespace("global_role_overrides", "This controls global role setting overrides for all roles in every region on the server. `0` means no-override, the player-configured values are used normally, `1` force-enables this setting for all roles in every region, `-1` force-disables respectively. Force-disable naturally also affects the owner, so be careful!")); + } + + public int get_override(final RoleSetting setting) { + switch (setting) { + case ADMIN: return config_admin; + case BUILD: return config_build; + case USE: return config_use; + case CONTAINER: return config_container; + case PORTAL: return config_portal; + } + return 0; + } + + @Override + public void on_enable() {} + + @Override + public void on_disable() {} +} diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/Regions.java b/vane-regions/src/main/java/org/oddlama/vane/regions/Regions.java index 3a8b3160e..8daf9a5aa 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/Regions.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/Regions.java @@ -189,8 +189,13 @@ public class Regions extends Module { public RegionEconomyDelegate economy; public boolean vane_portals_available = false; + public static RegionGlobalRoleOverrides role_overrides = null; + public static RegionGlobalEnvironmentOverrides environment_overrides = null; + public Regions() { menus = new RegionMenuGroup(this); + role_overrides = new RegionGlobalRoleOverrides(this); + environment_overrides = new RegionGlobalEnvironmentOverrides(this); new org.oddlama.vane.regions.commands.Region(this); diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RegionGroupMenu.java b/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RegionGroupMenu.java index 541531c92..f1de68216 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RegionGroupMenu.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RegionGroupMenu.java @@ -1,5 +1,7 @@ package org.oddlama.vane.regions.menu; +import java.util.List; +import java.util.function.Consumer; import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -22,6 +24,8 @@ import org.oddlama.vane.regions.region.Role; import org.oddlama.vane.util.StorageUtil; +import net.kyori.adventure.text.Component; + public class RegionGroupMenu extends ModuleComponent { @LangMessage @@ -201,13 +205,7 @@ public Menu create(final RegionGroup group, final Player player) { add_menu_item_setting(region_group_menu, group, 4, item_setting_info_fire, EnvironmentSetting.FIRE); add_menu_item_setting(region_group_menu, group, 5, item_setting_info_pvp, EnvironmentSetting.PVP); add_menu_item_setting(region_group_menu, group, 7, item_setting_info_trample, EnvironmentSetting.TRAMPLE); - add_menu_item_setting( - region_group_menu, - group, - 8, - item_setting_info_vine_growth, - EnvironmentSetting.VINE_GROWTH - ); + add_menu_item_setting(region_group_menu, group, 8, item_setting_info_vine_growth, EnvironmentSetting.VINE_GROWTH); region_group_menu.on_natural_close(player2 -> get_module().menus.main_menu.create(player2).open(player2)); @@ -364,6 +362,11 @@ private void add_menu_item_setting( 2 * 9 + col, null, (player, menu, self) -> { + // Prevent toggling when the setting is forced by the server + if (setting.has_override()) { + return ClickResult.ERROR; + } + group.settings().put(setting, !group.get_setting(setting)); mark_persistent_storage_dirty(); menu.update(); @@ -372,10 +375,17 @@ private void add_menu_item_setting( ) { @Override public void item(final ItemStack item) { + final Consumer> maybe_add_forced_hint = (lore) -> { + if (setting.has_override()) { + lore.add(Component.empty()); + lore.add(Component.text("FORCED BY SERVER")); + } + }; + if (group.get_setting(setting)) { - super.item(item_setting_toggle_on.item()); + super.item(item_setting_toggle_on.item_transform_lore(maybe_add_forced_hint)); } else { - super.item(item_setting_toggle_off.item()); + super.item(item_setting_toggle_off.item_transform_lore(maybe_add_forced_hint)); } } } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RoleMenu.java b/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RoleMenu.java index 91a2e00ec..120b44248 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RoleMenu.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/menu/RoleMenu.java @@ -1,5 +1,7 @@ package org.oddlama.vane.regions.menu; +import java.util.List; +import java.util.function.Consumer; import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -24,6 +26,8 @@ import org.oddlama.vane.util.ItemUtil; import org.oddlama.vane.util.StorageUtil; +import net.kyori.adventure.text.Component; + public class RoleMenu extends ModuleComponent { @LangMessage @@ -370,6 +374,11 @@ private void add_menu_item_setting( 2 * 9 + col, null, (player, menu, self) -> { + // Prevent toggling when the setting is forced by the server + if (setting.has_override()) { + return ClickResult.ERROR; + } + if (setting == RoleSetting.ADMIN) { // Admin setting is immutable return ClickResult.ERROR; @@ -383,10 +392,17 @@ private void add_menu_item_setting( ) { @Override public void item(final ItemStack item) { + final Consumer> maybe_add_forced_hint = (lore) -> { + if (setting.has_override()) { + lore.add(Component.empty()); + lore.add(Component.text("FORCED BY SERVER")); + } + }; + if (role.get_setting(setting)) { - super.item(item_setting_toggle_on.item()); + super.item(item_setting_toggle_on.item_transform_lore(maybe_add_forced_hint)); } else { - super.item(item_setting_toggle_off.item()); + super.item(item_setting_toggle_off.item_transform_lore(maybe_add_forced_hint)); } } } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/region/EnvironmentSetting.java b/vane-regions/src/main/java/org/oddlama/vane/regions/region/EnvironmentSetting.java index a6a520630..7db23334b 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/region/EnvironmentSetting.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/region/EnvironmentSetting.java @@ -1,5 +1,7 @@ package org.oddlama.vane.regions.region; +import org.oddlama.vane.regions.Regions; + public enum EnvironmentSetting { // Spawning ANIMALS(true), @@ -23,4 +25,12 @@ private EnvironmentSetting(final boolean def) { public boolean default_value() { return def; } + + public boolean has_override() { + return get_override() != 0; + } + + public int get_override() { + return Regions.environment_overrides.get_override(this); + } } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/region/RegionGroup.java b/vane-regions/src/main/java/org/oddlama/vane/regions/region/RegionGroup.java index b33bbbd6b..2b80be256 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/region/RegionGroup.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/region/RegionGroup.java @@ -142,6 +142,9 @@ public Map settings() { } public boolean get_setting(final EnvironmentSetting setting) { + if (setting.has_override()) { + return setting.get_override() == 1; + } return settings.getOrDefault(setting, setting.default_value()); } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/region/Role.java b/vane-regions/src/main/java/org/oddlama/vane/regions/region/Role.java index d6bb991a1..a11784741 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/region/Role.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/region/Role.java @@ -9,6 +9,7 @@ import java.util.UUID; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; +import org.oddlama.vane.regions.Regions; public class Role { @@ -86,6 +87,9 @@ public Map settings() { } public boolean get_setting(final RoleSetting setting) { + if (setting.has_override()) { + return setting.get_override() == 1; + } return settings.getOrDefault(setting, setting.default_value(false)); } diff --git a/vane-regions/src/main/java/org/oddlama/vane/regions/region/RoleSetting.java b/vane-regions/src/main/java/org/oddlama/vane/regions/region/RoleSetting.java index 52a788172..9ad9260da 100644 --- a/vane-regions/src/main/java/org/oddlama/vane/regions/region/RoleSetting.java +++ b/vane-regions/src/main/java/org/oddlama/vane/regions/region/RoleSetting.java @@ -1,5 +1,7 @@ package org.oddlama.vane.regions.region; +import org.oddlama.vane.regions.Regions; + public enum RoleSetting { ADMIN(false, true), BUILD(false, true), @@ -21,4 +23,12 @@ public boolean default_value(final boolean admin) { } return def; } + + public boolean has_override() { + return get_override() != 0; + } + + public int get_override() { + return Regions.role_overrides.get_override(this); + } }