Skip to content

Commit

Permalink
Disable redundant collision settings
Browse files Browse the repository at this point in the history
When "Ignore All Collision" is `ON`, "Ignore Transparent" and "Ignore Openable" will be disabled using the experimental Cloth Config requirements API.
  • Loading branch information
MattSturgeon committed Apr 2, 2024
1 parent 757c9b2 commit dde8a2b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and Freecam's versioning is based on [Semantic Versioning](https://semver.org/sp
### Changed

- Movement speed options now use sliders instead of text fields ([#190](https://github.com/MinecraftFreecam/Freecam/pull/190)).
- Redundant collision options are now dynamically hidden ([#121](https://github.com/MinecraftFreecam/Freecam/pull/121)).

### Removed

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/net/xolt/freecam/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public static class MovementConfig {
public CollisionConfig collision = new CollisionConfig();
public static class CollisionConfig {
@ConfigEntry.Gui.Tooltip
public boolean ignoreTransparent = true;
public boolean ignoreTransparent = false;

@ConfigEntry.Gui.Tooltip
public boolean ignoreOpenable = true;
public boolean ignoreOpenable = false;

@VariantTooltip(variant = "normal", count = 2)
@VariantTooltip(variant = "modrinth", count = 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private AutoConfigExtensions() {}

public static void apply(Class<? extends ConfigData> configClass) {
GuiRegistry registry = AutoConfig.getGuiRegistry(configClass);
Requirements.apply(registry);
ModBindingsConfigImpl.apply(registry);
VariantTooltipImpl.apply(registry);
BoundedContinuousImpl.apply(registry);
Expand Down
62 changes: 62 additions & 0 deletions common/src/main/java/net/xolt/freecam/config/gui/Requirements.java
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());
}
}

0 comments on commit dde8a2b

Please sign in to comment.