From f4d3e791d6f591dc1c61dcfac2dd4be9fbf14d53 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 27 Jul 2024 21:22:08 -0700 Subject: [PATCH] Provides an API to hide sub-flags #2437 --- pom.xml | 2 +- .../bentobox/bentobox/api/flags/Flag.java | 44 +++++++++++++++++++ .../bentobox/panels/settings/SettingsTab.java | 18 ++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 89e22cc51..1d1183dbd 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ -LOCAL - 2.4.2 + 2.4.3 bentobox-world https://sonarcloud.io ${project.basedir}/lib diff --git a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java index 89b13cb09..ca9c0a3b2 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java @@ -116,6 +116,25 @@ public class Flag implements Comparable { } } + /** + * Options for hiding of sub flags + * @since 2.4.3 + */ + public enum HideWhen { + /** + * Never hide sub-flags + */ + NEVER, + /** + * Hide subflags if the setting of the parent flag is true + */ + SETTING_TRUE, + /** + * Hide subflags if the setting of the parent flag is false + */ + SETTING_FALSE + } + private static final String PROTECTION_FLAGS = "protection.flags."; private final String id; @@ -131,6 +150,7 @@ public class Flag implements Comparable { private final int cooldown; private final Mode mode; private final Set subflags; + private final HideWhen hideWhen; private Flag(Builder builder) { this.id = builder.id; @@ -148,6 +168,7 @@ public class Flag implements Comparable { this.addon = builder.addon; this.mode = builder.mode; this.subflags = builder.subflags; + this.hideWhen = builder.hideWhen; } public String getID() { @@ -276,6 +297,14 @@ public class Flag implements Comparable { return addon; } + /** + * Get when sub-flags should be hidden + * @return + */ + public HideWhen getHideWhen() { + return hideWhen; + } + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @@ -553,6 +582,9 @@ public class Flag implements Comparable { // Subflags private final Set subflags; + // Hide when indicator + private HideWhen hideWhen = HideWhen.NEVER; + /** * Builder for making flags * @param id - a unique id that MUST be the same as the enum of the flag @@ -682,6 +714,18 @@ public class Flag implements Comparable { return this; } + /** + * When should sub-flags be hidden, if ever + * {@see HideWhen} + * @param hideWhen hide when indicator + * @return Builder - flag builder + * @since 2.4.3 + */ + public Builder hideWhen(HideWhen hideWhen) { + this.hideWhen = hideWhen; + return this; + } + /** * Build the flag * @return Flag diff --git a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java index b558cc9d4..934b22848 100644 --- a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java +++ b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java @@ -2,8 +2,10 @@ package world.bentobox.bentobox.panels.settings; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -17,6 +19,7 @@ import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; +import world.bentobox.bentobox.api.flags.Flag.HideWhen; import world.bentobox.bentobox.api.flags.Flag.Mode; import world.bentobox.bentobox.api.flags.Flag.Type; import world.bentobox.bentobox.api.localization.TextVariables; @@ -131,10 +134,25 @@ public class SettingsTab implements Tab, ClickHandler { currentMode.put(user.getUniqueId(), currentMode.getOrDefault(user.getUniqueId(), Mode.BASIC).getNext()); flags = getFlags(); } + // Remove any sub-flags that shouldn't be shown + Set toBeRemoved = new HashSet<>(); + flags.stream().forEach(flag -> { + if ((flag.getType() == Type.SETTING || flag.getType() == Type.WORLD_SETTING) && flag.hasSubflags()) { + if (flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_TRUE) { + toBeRemoved.addAll(flag.getSubflags()); + } else if (!flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_FALSE) { + toBeRemoved.addAll(flag.getSubflags()); + } + } + + }); + flags.removeAll(toBeRemoved); + List<@Nullable PanelItem> result = flags.stream().map( (f -> f.toPanelItem(plugin, user, world, island, plugin.getIWM().getHiddenFlags(world).contains(f.getID())))) .toList(); + return result; }