Skip to content

Commit

Permalink
feat: BaritoneSettings - maxValues
Browse files Browse the repository at this point in the history
  • Loading branch information
RuriYS committed Oct 25, 2024
1 parent 42fb908 commit d2d7e8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class BaritoneSettings implements IPathManager.ISettings {
private Setting<Boolean> walkOnWater, walkOnLava;
private Setting<Boolean> step, noFall;

private static final Map<String, Double> SETTING_MAX_VALUES = new HashMap<>();

public BaritoneSettings() {
createWrappers();
}
Expand Down Expand Up @@ -58,6 +60,10 @@ public void save() {
SettingsUtil.save(BaritoneAPI.getSettings());
}

static {
SETTING_MAX_VALUES.put("pathCutoffFactor", 1.0);
}

// Wrappers

@SuppressWarnings({"rawtypes", "unchecked"})
Expand Down Expand Up @@ -99,24 +105,32 @@ private void createWrappers() {
}
}
else if (value instanceof Double) {
sgDouble.add(new DoubleSetting.Builder()
DoubleSetting.Builder builder = new DoubleSetting.Builder()
.name(setting.getName())
.description(getDescription(setting.getName()))
.defaultValue((double) setting.defaultValue)
.onChanged(aDouble -> setting.value = aDouble)
.onModuleActivated(doubleSetting -> doubleSetting.set((Double) setting.value))
.build()
);
.onModuleActivated(doubleSetting -> doubleSetting.set((Double) setting.value));

if (SETTING_MAX_VALUES.containsKey(setting.getName())) {
builder.maxValue(SETTING_MAX_VALUES.get(setting.getName()));
}

sgDouble.add(builder.build());
}
else if (value instanceof Float) {
sgDouble.add(new DoubleSetting.Builder()
DoubleSetting.Builder builder = new DoubleSetting.Builder()
.name(setting.getName())
.description(getDescription(setting.getName()))
.defaultValue(((Float) setting.defaultValue).doubleValue())
.onChanged(aDouble -> setting.value = aDouble.floatValue())
.onModuleActivated(doubleSetting -> doubleSetting.set(((Float) setting.value).doubleValue()))
.build()
);
.onModuleActivated(doubleSetting -> doubleSetting.set(((Float) setting.value).doubleValue()));

if (SETTING_MAX_VALUES.containsKey(setting.getName())) {
builder.maxValue(SETTING_MAX_VALUES.get(setting.getName()));
}

sgDouble.add(builder.build());
}
else if (value instanceof Integer) {
Setting<Integer> wrapper = sgInt.add(new IntSetting.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public class DoubleSetting extends Setting<Double> {
public final boolean onSliderRelease;
public final int decimalPlaces;
public final boolean noSlider;
public final double maxValue;

private DoubleSetting(String name, String description, double defaultValue, Consumer<Double> onChanged, Consumer<Setting<Double>> onModuleActivated, IVisible visible, double min, double max, double sliderMin, double sliderMax, boolean onSliderRelease, int decimalPlaces, boolean noSlider) {
private DoubleSetting(String name, String description, double defaultValue, Consumer<Double> onChanged, Consumer<Setting<Double>> onModuleActivated, IVisible visible, double min, double max, double sliderMin, double sliderMax, boolean onSliderRelease, int decimalPlaces, boolean noSlider, double maxValue) {
super(name, description, defaultValue, onChanged, onModuleActivated, visible);

this.min = min;
this.max = max;
this.sliderMin = sliderMin;
this.sliderMax = sliderMax;
this.maxValue = maxValue;
this.sliderMax = Math.min(sliderMax, maxValue);
this.decimalPlaces = decimalPlaces;
this.onSliderRelease = onSliderRelease;
this.noSlider = noSlider;
Expand All @@ -39,7 +41,7 @@ protected Double parseImpl(String str) {

@Override
protected boolean isValueValid(Double value) {
return value >= min && value <= max;
return value >= min && value <= Math.min(max, maxValue);
}

@Override
Expand All @@ -62,6 +64,7 @@ public static class Builder extends SettingBuilder<Builder, Double, DoubleSettin
public boolean onSliderRelease = false;
public int decimalPlaces = 3;
public boolean noSlider = false;
public double maxValue = Double.POSITIVE_INFINITY;

public Builder() {
super(0D);
Expand Down Expand Up @@ -119,8 +122,17 @@ public Builder noSlider() {
return this;
}

public void maxValue(Double maxValue) {
this.maxValue = maxValue;
}

public DoubleSetting build() {
return new DoubleSetting(name, description, defaultValue, onChanged, onModuleActivated, visible, min, max, Math.max(sliderMin, min), Math.min(sliderMax, max), onSliderRelease, decimalPlaces, noSlider);
return new DoubleSetting(name, description, defaultValue, onChanged, onModuleActivated, visible, min, max,
Math.max(sliderMin, min),
Math.min(sliderMax, max),
onSliderRelease, decimalPlaces, noSlider,
maxValue
);
}
}
}

0 comments on commit d2d7e8d

Please sign in to comment.