Provides an API to hide sub-flags #2437

This commit is contained in:
tastybento 2024-07-27 21:22:08 -07:00
parent 86945bdfbe
commit f4d3e791d6
3 changed files with 63 additions and 1 deletions

View File

@ -88,7 +88,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>2.4.2</build.version>
<build.version>2.4.3</build.version>
<sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<server.jars>${project.basedir}/lib</server.jars>

View File

@ -116,6 +116,25 @@ public class Flag implements Comparable<Flag> {
}
}
/**
* 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<Flag> {
private final int cooldown;
private final Mode mode;
private final Set<Flag> subflags;
private final HideWhen hideWhen;
private Flag(Builder builder) {
this.id = builder.id;
@ -148,6 +168,7 @@ public class Flag implements Comparable<Flag> {
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<Flag> {
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<Flag> {
// Subflags
private final Set<Flag> 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<Flag> {
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

View File

@ -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<Flag> 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;
}