Add null on zero for DoubleSettingGui

This commit is contained in:
alexcrea 2024-04-11 01:53:37 +02:00
parent cb934e8a89
commit cfab871838
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
2 changed files with 25 additions and 12 deletions

View file

@ -65,7 +65,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
material.name()+"."+materialName, material.name()+"."+materialName,
ConfigHolder.UNIT_REPAIR_HOLDER, ConfigHolder.UNIT_REPAIR_HOLDER,
2, 2,
true, true, true,
0, 0,
1, 1,
0.25, 0.25,

View file

@ -29,6 +29,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
protected final DoubleSettingFactory holder; protected final DoubleSettingFactory holder;
protected final boolean asPercentage; protected final boolean asPercentage;
protected final boolean nullOnZero;
@NotNull @NotNull
protected final BigDecimal before; protected final BigDecimal before;
@NotNull @NotNull
@ -40,12 +41,16 @@ public class DoubleSettingGui extends AbstractSettingGui {
* *
* @param holder Configuration factory of this setting. * @param holder Configuration factory of this setting.
* @param now The defined value of this setting. * @param now The defined value of this setting.
* @param asPercentage If the value represent a %
* @param nullOnZero If the value should be set as null on zero
*/ */
protected DoubleSettingGui(DoubleSettingFactory holder, @NotNull BigDecimal now, boolean asPercentage) { protected DoubleSettingGui(DoubleSettingFactory holder, @NotNull BigDecimal now,
boolean asPercentage, boolean nullOnZero) {
super(3, holder.getTitle(), holder.parent); super(3, holder.getTitle(), holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9; assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder; this.holder = holder;
this.asPercentage = asPercentage; this.asPercentage = asPercentage;
this.nullOnZero = nullOnZero;
this.before = now; this.before = now;
this.now = now; this.now = now;
@ -127,7 +132,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
// reset to default // reset to default
GuiItem returnToDefault; GuiItem returnToDefault;
if (!now.equals(holder.defaultVal)) { if (now.compareTo(holder.defaultVal) != 0) {
returnToDefault = this.returnToDefault; returnToDefault = this.returnToDefault;
} else { } else {
returnToDefault = GuiGlobalItems.backgroundItem(); returnToDefault = GuiGlobalItems.backgroundItem();
@ -217,7 +222,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
StringBuilder stepName = new StringBuilder("\u00A7"); StringBuilder stepName = new StringBuilder("\u00A7");
List<String> stepLore; List<String> stepLore;
Consumer<InventoryClickEvent> clickEvent; Consumer<InventoryClickEvent> clickEvent;
if (Objects.equals(stepValue, step)) { if (stepValue.compareTo(step) == 0) {
stepMat = Material.GREEN_STAINED_GLASS_PANE; stepMat = Material.GREEN_STAINED_GLASS_PANE;
stepName.append('a'); stepName.append('a');
stepLore = Collections.singletonList("\u00A77Value is changing by " + displayValue(stepValue)); stepLore = Collections.singletonList("\u00A77Value is changing by " + displayValue(stepValue));
@ -257,7 +262,11 @@ public class DoubleSettingGui extends AbstractSettingGui {
@Override @Override
public boolean onSave() { public boolean onSave() {
holder.config.getConfig().set(holder.configPath, now.doubleValue()); if(this.nullOnZero && (this.now.compareTo(BigDecimal.ZERO) == 0)){
this.holder.config.getConfig().set(this.holder.configPath, null);
}else{
this.holder.config.getConfig().set(this.holder.configPath, now.doubleValue());
}
MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath);
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
@ -268,7 +277,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
@Override @Override
public boolean hadChange() { public boolean hadChange() {
return !now.equals(before); return now.compareTo(before) != 0;
} }
private static final BigDecimal PERCENTAGE_OFFSET = BigDecimal.valueOf(100); private static final BigDecimal PERCENTAGE_OFFSET = BigDecimal.valueOf(100);
@ -292,6 +301,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
* @param config Configuration holder of this setting. * @param config Configuration holder of this setting.
* @param scale The scale of the decimal. * @param scale The scale of the decimal.
* @param asPercentage If we should display the value as a %. * @param asPercentage If we should display the value as a %.
* @param nullOnZero Set the value as null (deleting it) when equal to 0
* @param min Minimum value of this setting. * @param min Minimum value of this setting.
* @param max Maximum value of this setting. * @param max Maximum value of this setting.
* @param defaultVal Default value if not found on the config. * @param defaultVal Default value if not found on the config.
@ -303,12 +313,12 @@ public class DoubleSettingGui extends AbstractSettingGui {
*/ */
public static DoubleSettingFactory doubleFactory(@NotNull String title, ValueUpdatableGui parent, public static DoubleSettingFactory doubleFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
int scale, boolean asPercentage, int scale, boolean asPercentage, boolean nullOnZero,
double min, double max, double defaultVal, double... steps) { double min, double max, double defaultVal, double... steps) {
return new DoubleSettingFactory( return new DoubleSettingFactory(
title, parent, title, parent,
configPath, config, configPath, config,
scale, asPercentage, scale, asPercentage, nullOnZero,
min, max, defaultVal, steps); min, max, defaultVal, steps);
} }
@ -322,6 +332,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
int scale; int scale;
boolean asPercentage; boolean asPercentage;
boolean nullOnZero;
BigDecimal min; BigDecimal min;
BigDecimal max; BigDecimal max;
BigDecimal defaultVal; BigDecimal defaultVal;
@ -336,6 +347,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
* @param config Configuration holder of this setting. * @param config Configuration holder of this setting.
* @param scale The scale of the decimal. * @param scale The scale of the decimal.
* @param asPercentage If we should display the value as a %. * @param asPercentage If we should display the value as a %.
* @param nullOnZero Set the value as null (deleting it) when equal to 0
* @param min Minimum value of this setting. * @param min Minimum value of this setting.
* @param max Maximum value of this setting. * @param max Maximum value of this setting.
* @param defaultVal Default value if not found on the config. * @param defaultVal Default value if not found on the config.
@ -347,13 +359,14 @@ public class DoubleSettingGui extends AbstractSettingGui {
protected DoubleSettingFactory( protected DoubleSettingFactory(
@NotNull String title, ValueUpdatableGui parent, @NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
int scale, boolean asPercentage, int scale, boolean asPercentage, boolean nullOnZero,
double min, double max, double defaultVal, double... steps) { double min, double max, double defaultVal, double... steps) {
super(configPath, config); super(configPath, config);
this.title = title; this.title = title;
this.parent = parent; this.parent = parent;
this.scale = scale; this.scale = scale;
this.asPercentage = asPercentage; this.asPercentage = asPercentage;
this.nullOnZero = nullOnZero;
this.min = BigDecimal.valueOf(min).setScale(scale, RoundingMode.HALF_UP); this.min = BigDecimal.valueOf(min).setScale(scale, RoundingMode.HALF_UP);
this.max = BigDecimal.valueOf(max).setScale(scale, RoundingMode.HALF_UP); this.max = BigDecimal.valueOf(max).setScale(scale, RoundingMode.HALF_UP);
this.defaultVal = BigDecimal.valueOf(defaultVal).setScale(scale, RoundingMode.HALF_UP); this.defaultVal = BigDecimal.valueOf(defaultVal).setScale(scale, RoundingMode.HALF_UP);
@ -388,7 +401,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
// Get value or default // Get value or default
BigDecimal now = getConfiguredValue(); BigDecimal now = getConfiguredValue();
// create new gui // create new gui
return new DoubleSettingGui(this, now, this.asPercentage); return new DoubleSettingGui(this, now, this.asPercentage, this.nullOnZero);
} }