Skip to content

Commit

Permalink
Fixed negative statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 11, 2024
1 parent 2162c15 commit c01f11a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class DurabilityStatistic extends IntegerStatistic {

public DurabilityStatistic(String id) {
super(id, "§5");
super(id, "§5", 10);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public class FloatStatistic extends PartStatistic<Float> {

public static final DecimalFormat FORMAT = new DecimalFormat("#.##", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

public FloatStatistic(String id, String color, float minValue) {
super(id, color, Float.class, minValue);
}

public FloatStatistic(String id, String color) {
super(id, color, Float.class, 0f);
this(id, color, 0f);
}

@Override
Expand All @@ -25,6 +29,11 @@ public FloatStatistic(String id, String color) {
return value * mult;
}

@Override
public @NotNull Float max(Float value1, Float value2) {
return Math.max(value1, value2);
}

@Override
public String formatToolValue(ItemStack itemStack, Float value) {
return getColor() + FORMAT.format(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

public class IntegerStatistic extends PartStatistic<Integer> {

public IntegerStatistic(String id, String color, int minValue) {
super(id, color, Integer.class, minValue);
}

public IntegerStatistic(String id, String color) {
super(id, color, Integer.class, 0);
this(id, color, 0);
}

@Override
Expand All @@ -19,6 +23,11 @@ public IntegerStatistic(String id, String color) {
return (int) (input * mult);
}

@Override
public @NotNull Integer max(Integer value1, Integer value2) {
return Math.max(value1, value2);
}

@Override
public String formatToolValue(ItemStack itemStack, Integer value) {
return getColor() + value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ public abstract class PartStatistic<V extends Number> {
private final String id;
private final String color;
private final Class<V> valueClass;
private final V noneValue;
private final V minValue;

public PartStatistic(String id, String color, Class<V> valueClass, V noneValue) {
public PartStatistic(String id, String color, Class<V> valueClass, V minValue) {
if(VALUES.contains(this)) throw new IllegalStateException("Duplicate id: " + id);
this.id = id;
this.color = color;
this.valueClass = valueClass;
this.noneValue = noneValue;
this.minValue = minValue;
VALUES.add(this);
}

public abstract @NotNull V sumValue(@NotNull V value1, @NotNull V value2);

public abstract @NotNull V multiplyValue(@NotNull V value, float mult);

public abstract @NotNull V max(V value1, V value2);

public abstract String formatToolValue(ItemStack itemStack, V value);

public String formatPartValue(ItemStack itemStack, V value) {
Expand Down Expand Up @@ -69,8 +71,8 @@ public Class<V> getValueClass() {
return valueClass;
}

public V getNoneValue() {
return noneValue;
public V getMinValue() {
return minValue;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public <V extends Number> void setStatistic(@NotNull PartStatistic<V> statistic,
@SuppressWarnings("unchecked")
public <V extends Number> @NotNull V getStatistic(@NotNull PartStatistic<V> statistic) {
Object o = statisticMap.get(statistic);
return o != null ? (V) o : statistic.getNoneValue();
return o != null ? (V) o : statistic.getMinValue();
}

public boolean hasStatistic(PartStatistic<?> statistic) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/github/kill05/items/tool/ArchitectTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ protected void addValidStatistic(PartStatistic<?> type, float multiplier) {

public <V extends Number> V getStatistic(ItemStack item, PartStatistic<V> statistic) {
Float mult = validStatistics.get(statistic);
if(mult == null) return statistic.getNoneValue();
if(mult == null) return statistic.getMinValue();

V value = statistic.getNoneValue();
V value = statistic.getMinValue();
for (ToolPartInfo info : getPartList()) {
ArchitectMaterial part = ArchitectTools.getToolPart(item, info);
if(part == null) continue;
Expand All @@ -165,7 +165,7 @@ public <V extends Number> V getStatistic(ItemStack item, PartStatistic<V> statis
value = statistic.sumValue(value, statistics.getStatistic(statistic));
}

return statistic.multiplyValue(value, mult);
return statistic.max(statistic.multiplyValue(value, mult), statistic.getMinValue());
}


Expand Down

0 comments on commit c01f11a

Please sign in to comment.