semi-auto cleanup

This commit is contained in:
gnembon committed 2023-02-04 13:26:21 +01:00
1 parent cad80f82c6
commit 8f61271606
99 files changed
+11463 -8007

No files matched your search

+96 -69
View File
@@ -4,23 +4,25 @@ import carpet.CarpetSettings;
import carpet.script.exception.InternalExpressionException;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import net.minecraft.nbt.Tag;
public abstract class Value implements Comparable<Value>, Cloneable
{
public static NumericValue FALSE = BooleanValue.FALSE;
public static NumericValue TRUE = BooleanValue.TRUE;
public static NumericValue ZERO = new NumericValue(0);
public static NumericValue ONE = new NumericValue(1);
public static final NumericValue FALSE = BooleanValue.FALSE;
public static final NumericValue TRUE = BooleanValue.TRUE;
public static final NumericValue ZERO = new NumericValue(0);
public static final NumericValue ONE = new NumericValue(1);
public static NullValue NULL = NullValue.NULL;
public static UndefValue UNDEF = UndefValue.UNDEF;
public static final NullValue NULL = NullValue.NULL;
public static final UndefValue UNDEF = UndefValue.UNDEF;
public String boundVariable;
@@ -28,29 +30,32 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
return boundVariable != null;
}
public String getVariable()
{
return boundVariable;
}
public Value reboundedTo(String var)
public Value reboundedTo(final String value)
{
Value copy;
final Value copy;
try
{
copy = (Value)clone();
copy = (Value) clone();
}
catch (CloneNotSupportedException e)
catch (final CloneNotSupportedException e)
{
// should not happen
CarpetSettings.LOG.error("Failed to clone variable", e);
throw new InternalExpressionException("Variable of type "+getTypeString()+" is not cloneable. Tell gnembon about it, this shoudn't happen");
throw new InternalExpressionException("Variable of type " + getTypeString() + " is not cloneable. Tell gnembon about it, this shoudn't happen");
}
copy.boundVariable = var;
copy.boundVariable = value;
return copy;
}
public Value bindTo(String var)
public Value bindTo(final String value)
{
this.boundVariable = var;
this.boundVariable = value;
return this;
}
@@ -64,35 +69,37 @@ public abstract class Value implements Comparable<Value>, Cloneable
public abstract boolean getBoolean();
public Value add(Value o) {
public Value add(final Value o)
{
if (o instanceof FormattedTextValue)
{
return FormattedTextValue.combine(this, o);
}
String leftStr = this.getString();
String rightStr = o.getString();
final String leftStr = this.getString();
final String rightStr = o.getString();
return new StringValue(leftStr + rightStr);
}
public Value subtract(Value v)
public Value subtract(final Value v)
{
return new StringValue(this.getString().replace(v.getString(),""));
return new StringValue(this.getString().replace(v.getString(), ""));
}
public Value multiply(Value v)
public Value multiply(final Value v)
{
if (v instanceof NumericValue || v instanceof ListValue)
{
return v.multiply(this);
}
return new StringValue(this.getString()+"."+v.getString());
return v instanceof NumericValue || v instanceof ListValue
? v.multiply(this)
: new StringValue(this.getString() + "." + v.getString());
}
public Value divide(Value v)
public Value divide(final Value v)
{
if (v instanceof NumericValue)
if (v instanceof final NumericValue number)
{
String lstr = getString();
return new StringValue(lstr.substring(0, (int)(lstr.length()/ ((NumericValue) v).getDouble())));
final String lstr = getString();
return new StringValue(lstr.substring(0, (int) (lstr.length() / number.getDouble())));
}
return new StringValue(getString()+"/"+v.getString());
return new StringValue(getString() + "/" + v.getString());
}
public Value()
@@ -103,18 +110,18 @@ public abstract class Value implements Comparable<Value>, Cloneable
@Override
public int compareTo(final Value o)
{
if (o instanceof NumericValue || o instanceof ListValue || o instanceof ThreadValue)
{
return -o.compareTo(this);
}
return getString().compareTo(o.getString());
return o instanceof NumericValue || o instanceof ListValue || o instanceof ThreadValue
? -o.compareTo(this)
: getString().compareTo(o.getString());
}
@Override // for hashmap key access, and == operator
public boolean equals(final Object o)
{
if (o instanceof Value)
return this.compareTo((Value) o)==0;
if (o instanceof final Value v)
{
return this.compareTo(v) == 0;
}
return false;
}
@@ -126,50 +133,65 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
throw new InternalExpressionException(boundVariable+ " cannot be assigned a new value");
}*/
throw new InternalExpressionException(getString()+ " is not a variable");
throw new InternalExpressionException(getString() + " is not a variable");
}
}
public Value in(Value value1)
public Value in(final Value value1)
{
final Pattern p;
try
{
p = Pattern.compile(value1.getString());
}
catch (PatternSyntaxException pse)
catch (final PatternSyntaxException pse)
{
throw new InternalExpressionException("Incorrect matching pattern: "+pse.getMessage());
throw new InternalExpressionException("Incorrect matching pattern: " + pse.getMessage());
}
final Matcher m = p.matcher(this.getString());
if (!m.find()) return Value.NULL;
int gc = m.groupCount();
if (gc == 0) return new StringValue(m.group());
if (gc == 1) return StringValue.of(m.group(1));
List<Value> groups = new ArrayList<>(gc);
if (!m.find())
{
return Value.NULL;
}
final int gc = m.groupCount();
if (gc == 0)
{
return new StringValue(m.group());
}
if (gc == 1)
{
return StringValue.of(m.group(1));
}
final List<Value> groups = new ArrayList<>(gc);
for (int i = 1; i <= gc; i++)
{
groups.add(StringValue.of(m.group(i)));
}
return ListValue.wrap(groups);
}
public int length()
{
return getString().length();
}
public Value slice(long fromDesc, Long toDesc)
public Value slice(final long fromDesc, final Long toDesc)
{
String value = this.getString();
int size = value.length();
int from = ListValue.normalizeIndex(fromDesc, size);
if (toDesc == null) return new StringValue(value.substring(from));
int to = ListValue.normalizeIndex(toDesc, size+1);
if (from > to) return StringValue.EMPTY;
final String value = this.getString();
final int size = value.length();
final int from = ListValue.normalizeIndex(fromDesc, size);
if (toDesc == null)
{
return new StringValue(value.substring(from));
}
final int to = ListValue.normalizeIndex(toDesc, size + 1);
if (from > to)
{
return StringValue.EMPTY;
}
return new StringValue(value.substring(from, to));
}
public Value split(Value delimiter)
{
if (delimiter == null)
@@ -180,20 +202,20 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
return ListValue.wrap(Arrays.stream(getString().split(delimiter.getString())).map(StringValue::new));
}
catch (PatternSyntaxException pse)
catch (final PatternSyntaxException pse)
{
throw new InternalExpressionException("Incorrect pattern for 'split': "+pse.getMessage());
throw new InternalExpressionException("Incorrect pattern for 'split': " + pse.getMessage());
}
}
public double readDoubleNumber()
{
String s = getString();
final String s = getString();
try
{
return Double.valueOf(s);
return Double.parseDouble(s);
}
catch (NumberFormatException e)
catch (final NumberFormatException e)
{
return Double.NaN;
}
@@ -212,21 +234,20 @@ public abstract class Value implements Comparable<Value>, Cloneable
@Override
public int hashCode()
{
String stringVal = getString();
if (stringVal.isEmpty()) return 0;
return ("s"+stringVal).hashCode();
final String stringVal = getString();
return stringVal.isEmpty() ? 0 : ("s" + stringVal).hashCode();
}
public Value deepcopy()
{
try
{
return (Value)this.clone();
return (Value) this.clone();
}
catch (CloneNotSupportedException e)
catch (final CloneNotSupportedException e)
{
// should never happen
throw new InternalExpressionException("Cannot make a copy of value: "+this);
throw new InternalExpressionException("Cannot make a copy of value: " + this);
}
}
@@ -237,12 +258,18 @@ public abstract class Value implements Comparable<Value>, Cloneable
return new JsonPrimitive(getString());
}
public boolean isNull() { return false; }
public boolean isNull()
{
return false;
}
/**
* @return retrieves useful in-run value of an optimized code-base value.
* For immutable values (most of them) it can return itself,
* but for mutables, it needs to be its copy or deep copy.
*/
public Value fromConstant() { return this; }
public Value fromConstant()
{
return this;
}
}