-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable redundant collision settings
When "Ignore All Collision" is `ON`, "Ignore Transparent" and "Ignore Openable" will be disabled using the experimental Cloth Config requirements API.
- Loading branch information
1 parent
757c9b2
commit dde8a2b
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
common/src/main/java/net/xolt/freecam/config/gui/Requirements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.xolt.freecam.config.gui; | ||
|
||
import me.shedaniel.autoconfig.gui.registry.GuiRegistry; | ||
import me.shedaniel.clothconfig2.api.ValueHolder; | ||
import me.shedaniel.clothconfig2.gui.entries.BooleanListEntry; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.List; | ||
|
||
import static java.lang.Boolean.FALSE; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
class Requirements { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(); | ||
private static ValueHolder<Boolean> ignoreAllWidget; | ||
|
||
private Requirements() {} | ||
|
||
static void apply(GuiRegistry guiRegistry) { | ||
// FIXME These transformers assume that no subsequent GUI transformers will replace | ||
// the widgets. That's fine, so long as nothing changes, however a dedicated | ||
// AutoConfig requirements API would be better. | ||
// | ||
// NOTE The Cloth Config Requirements API is currently marked "unstable", although | ||
// significant changes seem unlikely. | ||
|
||
// Register a transformer to capture the ignoreAll GUI | ||
guiRegistry.registerPredicateTransformer((guis, i18n, field, config, defaults, registry) -> { | ||
// Filter out unrelated widgets, such as PrefixText. | ||
// Also allows us to safely cast. | ||
List<BooleanListEntry> widgets = guis.stream() | ||
.filter(BooleanListEntry.class::isInstance) | ||
.map(BooleanListEntry.class::cast) | ||
.toList(); | ||
if (widgets.isEmpty()) { | ||
LOGGER.error("Unable to find ignoreAll widget."); | ||
return guis; | ||
} | ||
if (widgets.size() > 1) { | ||
LOGGER.warn("Multiple ignoreAll widgets, choosing first."); | ||
} | ||
ignoreAllWidget = widgets.get(0); | ||
return guis; | ||
}, field -> field.getName().equals("ignoreAll")); | ||
|
||
// Register a transformer to set requirements for ignoreTransparent & ignoreOpenable | ||
guiRegistry.registerPredicateTransformer((guis, i18n, field, config, defaults, registry) -> { | ||
guis.stream() | ||
.filter(BooleanListEntry.class::isInstance) | ||
.map(BooleanListEntry.class::cast) | ||
.forEach(gui -> gui.setRequirement(Requirements::notIgnoreAll)); | ||
return guis; | ||
}, field -> List.of("ignoreTransparent", "ignoreOpenable").contains(field.getName())); | ||
} | ||
|
||
// Requirement handler: require ignoreAll is set to "No" | ||
private static boolean notIgnoreAll() { | ||
return ignoreAllWidget == null || FALSE.equals(ignoreAllWidget.getValue()); | ||
} | ||
} |