Skip to content

Commit

Permalink
random properties now also allow ranges with the format <high>-<low> …
Browse files Browse the repository at this point in the history
…instead of only <low>-<high> *(OptiFine parity)*
  • Loading branch information
Traben-0 committed Dec 29, 2023
1 parent 86d2806 commit 98b3a9b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
2 changes: 2 additions & 0 deletions common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

- mob spawner entities can now variate their textures again.
- moved some temp rework code into more compatible mixins for mod compatibility
- more stable implementation of the 5.0 rework changes for emissives
- random properties now also allow ranges with the format <high>-<low> instead of only <low>-<high> *(OptiFine parity)*


[5.0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ protected FloatRangeFromStringArrayProperty(String string) throws RandomProperty
try {
if (possibleRange.matches("([\\d.]+|-[\\d.]+)-([\\d.]+|-[\\d.]+)")) {
String[] str = possibleRange.split("(?<!^|-)-");
float small = Float.parseFloat(str[0].replaceAll("[^0-9.-]", ""));
float big = Float.parseFloat(str[1].replaceAll("[^0-9.-]", ""));
return (value) -> value >= small && value <= big;
float left = Float.parseFloat(str[0].replaceAll("[^0-9.-]", ""));
float right = Float.parseFloat(str[1].replaceAll("[^0-9.-]", ""));
if(left == right){
return (value) -> value == left;
} else if (right > left) {
return (value) -> value >= left && value <= right;
} else {
return (value) -> value >= right && value <= left;
}
} else {
float single = Float.parseFloat(possibleRange.replaceAll("[^0-9.-]", ""));
return (value) -> value == single;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package traben.entity_texture_features.features.property_reading.properties.generic_properties;

import org.jetbrains.annotations.Nullable;

public abstract class LongRangeFromStringArrayProperty extends NumberRangeFromStringArrayProperty<Long> {


protected LongRangeFromStringArrayProperty(String string) throws RandomPropertyException {
super(string);
}


@Override
protected @Nullable RangeTester<Long> getRangeTesterFromString(String possibleRange) {
try {
if (possibleRange.matches("(\\d+|-\\d+)-(\\d+|-\\d+)")) {
String[] str = possibleRange.split("(?<!^|-)-");
long left = Long.parseLong(str[0].replaceAll("[^0-9-]", ""));
long right = Long.parseLong(str[1].replaceAll("[^0-9-]", ""));
if(left == right){
return (value) -> value == left;
} else if (right > left) {
return (value) -> value >= left && value <= right;
} else {
return (value) -> value >= right && value <= left;
}
} else {
long single = Long.parseLong(possibleRange.replaceAll("[^0-9-]", ""));
return (value) -> value == single;
}
} catch (Exception ignored) {
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ protected String getPrintableRuleInfo() {
return String.valueOf(ARRAY);
}

public record IntRange(int lower, int higher) {
public static class IntRange {
final int lower;
final int higher;

public IntRange(int left, int right){
if(left > right){
higher = left;
lower = right;
}else{
higher = right;
lower = left;
}
}
public boolean isWithinRange(int value) {
return value >= lower && value <= higher;
}
Expand All @@ -109,7 +121,7 @@ public Integer[] getAllWithinRangeAsList() {
}

ArrayList<Integer> builder = new ArrayList<>();
for (int i = Math.min(lower, higher); i <= Math.max(lower, higher); i++) {
for (int i = lower; i <= higher; i++) {
builder.add(i);
}
return builder.toArray(new Integer[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import traben.entity_texture_features.features.property_reading.properties.generic_properties.NumberRangeFromStringArrayProperty;
import traben.entity_texture_features.features.property_reading.properties.generic_properties.LongRangeFromStringArrayProperty;
import traben.entity_texture_features.utils.ETFEntity;

import java.util.Properties;

import static traben.entity_texture_features.ETFClientCommon.ETFConfigData;

public class TimeOfDayProperty extends NumberRangeFromStringArrayProperty<Long> {
public class TimeOfDayProperty extends LongRangeFromStringArrayProperty {


protected TimeOfDayProperty(Properties properties, int propertyNum) throws RandomPropertyException {
Expand All @@ -33,22 +33,6 @@ protected Long getRangeValueFromEntity(ETFEntity entity) {
return null;
}

@Override
protected @Nullable RangeTester<Long> getRangeTesterFromString(String possibleRange) {
try {
if (possibleRange.matches("(\\d+|-\\d+)-(\\d+|-\\d+)")) {
String[] str = possibleRange.split("(?<!^|-)-");
long small = Long.parseLong(str[0].replaceAll("[^0-9-]", ""));
long big = Long.parseLong(str[1].replaceAll("[^0-9-]", ""));
return (value) -> value >= small && value <= big;
} else {
long single = Long.parseLong(possibleRange.replaceAll("[^0-9-]", ""));
return (value) -> value == single;
}
} catch (Exception ignored) {
}
return null;
}


@Override
Expand Down

0 comments on commit 98b3a9b

Please sign in to comment.