Skip to content

Commit

Permalink
fix clamped constraint applying too liberally
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Sep 11, 2024
1 parent 745b7ed commit 0f16071
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.1.1
- Fix clamped constraint applying to any configurable element

# 1.1.0
- Support split-sources
- Fix source provider locator being OS dependent
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G
fabric_versions=1.21.1
neoforge_versions=1.21.1

mod_version=1.1.0
mod_version=1.1.1
mod_group=com.bawnorton
mod_id=configurable
mod_name=Configurable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ private void addRegexConstraint(ConfigurableHolder holder, StringBuilder constra
}

private void addClampedConstraint(ConfigurableHolder holder, Element element, StringBuilder constraintSet) {
double min = holder.min();
double max = holder.max();
if (max != Double.MAX_VALUE || min != Double.MIN_NORMAL) {
if (holder.isMaxSet() || holder.isMinSet()) {
double min = holder.min();
double max = holder.max();
if (min > max) {
messager.printError("min must be smaller than or equal to the max", element);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.bawnorton.configurable.ap.helper;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import java.util.Objects;
import java.util.Optional;

public class AnnotationHelper {
Expand All @@ -16,4 +19,22 @@ public static Optional<AnnotationMirror> getSubAnnotation(AnnotationMirror paren
.findFirst()
.map(entry -> (AnnotationMirror) entry.getValue());
}

public static boolean isDefaultValue(AnnotationMirror annotation, String methodName) {
if(annotation == null) return true;

ExecutableElement methodElement = null;
for (ExecutableElement executableElement : annotation.getElementValues().keySet()) {
if (executableElement.getSimpleName().contentEquals(methodName)) {
methodElement = executableElement;
break;
}
}
if (methodElement == null) return true;

AnnotationValue defaultValue = methodElement.getDefaultValue();
AnnotationValue actualValue = annotation.getElementValues().get(methodElement);

return Objects.equals(actualValue, defaultValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.bawnorton.configurable.ap.helper;

import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

public class MappingsHelper {
//? if yarn {
public static String getMinecraftClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,12 @@ public String[] listener() {
public boolean collapsed() {
return annotation.yacl().collapsed();
}

public boolean isMinSet() {
return !AnnotationHelper.isDefaultValue(configurableMirror, "min");
}

public boolean isMaxSet() {
return !AnnotationHelper.isDefaultValue(configurableMirror, "max");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import com.bawnorton.configurable.Image;
import com.bawnorton.configurable.OptionType;
import com.bawnorton.configurable.ap.helper.AnnotationHelper;
import com.bawnorton.configurable.util.Pair;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

public final class ConfigurableOverrides {
Expand Down Expand Up @@ -56,29 +54,11 @@ private static class Builder {
private final Map<String, Pair<Boolean, Supplier<?>>> overrides = new HashMap<>();

public <T> Builder addOverride(AnnotationMirror mirror, String name, Supplier<T> override, Supplier<T> natural) {
boolean isDefaultValue = isDefaultValue(mirror, name);
boolean isDefaultValue = AnnotationHelper.isDefaultValue(mirror, name);
overrides.put(name, Pair.of(isDefaultValue, isDefaultValue ? override : natural));
return this;
}

private static boolean isDefaultValue(AnnotationMirror annotation, String methodName) {
if(annotation == null) return true;

ExecutableElement methodElement = null;
for (ExecutableElement executableElement : annotation.getElementValues().keySet()) {
if (executableElement.getSimpleName().contentEquals(methodName)) {
methodElement = executableElement;
break;
}
}
if (methodElement == null) return true;

AnnotationValue defaultValue = methodElement.getDefaultValue();
AnnotationValue actualValue = annotation.getElementValues().get(methodElement);

return Objects.equals(actualValue, defaultValue);
}

public ConfigurableOverrides build() {
return new ConfigurableOverrides(overrides);
}
Expand Down

0 comments on commit 0f16071

Please sign in to comment.