Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: further api improvements to hideWhen #2451

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/main/java/world/bentobox/bentobox/api/flags/Flag.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public enum HideWhen {
private final Mode mode;
private final Set<Flag> subflags;
private final HideWhen hideWhen;
private boolean isSubFlag;
private Flag parentFlag;

private Flag(Builder builder) {
this.id = builder.id;
Expand All @@ -169,6 +171,8 @@ private Flag(Builder builder) {
this.mode = builder.mode;
this.subflags = builder.subflags;
this.hideWhen = builder.hideWhen;
this.isSubFlag = false;
this.parentFlag = null;
}

public String getID() {
Expand Down Expand Up @@ -305,6 +309,20 @@ public HideWhen getHideWhen() {
return hideWhen;
}

/**
* @return the isSubFlag
*/
public boolean isSubFlag() {
return isSubFlag;
}

/**
* @return the parentFlag
*/
public Flag getParentFlag() {
return parentFlag;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
Expand Down Expand Up @@ -711,6 +729,9 @@ public Builder mode(Mode mode) {
*/
public Builder subflags(Flag... flags) {
this.subflags.addAll(Arrays.asList(flags));
for (Flag flag : flags) {
flag.isSubFlag = true;
}
return this;
}

Expand Down Expand Up @@ -739,9 +760,9 @@ public Flag build() {
default -> new CycleClick(id);
};
}

return new Flag(this);
Flag flag = new Flag(this);
subflags.forEach(subflag -> subflag.parentFlag = flag);
return flag;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,14 @@ public String getName() {
}
// 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.forEach(flag -> {
if (flag.isSubFlag() && flag.getHideWhen() != HideWhen.NEVER) {
if (!flag.getParentFlag().isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_FALSE) {
toBeRemoved.add(flag);
} else if (flag.getParentFlag().isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_TRUE) {
toBeRemoved.add(flag);
}
}

});
flags.removeAll(toBeRemoved);

Expand Down
Loading