migrate to new buildscript

Class Registries are merged together into one. All subprojects are also merged into one. It's probably possible to split stuff into multiple source sets and not merging the registries, but I'm just too lazy for that.
This commit is contained in:
Glease committed 2022-03-05 03:02:05 +08:00
1 parent 7c4b38e853
commit 38c46d61d9
341 files changed
+1027 -11951

No files matched your search

@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a boolean value (true or false).
*
* @author Stan Hebben
*/
public class DataBool implements IData {
private final boolean value;
public DataBool(boolean value) {
this.value = value;
}
@Override
public boolean asBool() {
return value;
}
@Override
public byte asByte() {
return (byte) (value ? 1 : 0);
}
@Override
public short asShort() {
return (short) (value ? 1 : 0);
}
@Override
public int asInt() {
return value ? 1 : 0;
}
@Override
public long asLong() {
return value ? 1 : 0;
}
@Override
public float asFloat() {
return value ? 1 : 0;
}
@Override
public double asDouble() {
return value ? 1 : 0;
}
@Override
public String asString() {
return Boolean.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("a bool is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("a bool is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("a bool is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("a bool is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asBool() == value;
}
@Override
public boolean equals(IData other) {
return value == other.asBool();
}
@Override
public int compareTo(IData other) {
throw new UnsupportedOperationException("Cannot compare bool values");
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromBool(value);
}
@Override
public IData add(IData other) {
throw new UnsupportedOperationException("Cannot add bool values");
}
@Override
public IData sub(IData other) {
throw new UnsupportedOperationException("Cannot subtract bool values");
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply bool values");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide bool values");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo on bool values");
}
@Override
public IData and(IData other) {
return new DataBool(value & other.asBool());
}
@Override
public IData or(IData other) {
return new DataBool(value | other.asBool());
}
@Override
public IData xor(IData other) {
return new DataBool(value ^ other.asBool());
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate a bool value");
}
@Override
public IData not() {
return new DataBool(!value);
}
@Override
public String toString() {
return asString();
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a byte value (-128 .. 127)
*
* @author Stan Hebben
*/
public class DataByte implements IData {
private final byte value;
public DataByte(byte value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast a byte to a bool");
}
@Override
public byte asByte() {
return value;
}
@Override
public short asShort() {
return value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Byte.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("A byte is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("A byte is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("A byte is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("A byte is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asByte() == value;
}
@Override
public boolean equals(IData data) {
return value == data.asByte();
}
@Override
public int compareTo(IData data) {
return Byte.compare(value, data.asByte());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromByte(value);
}
@Override
public IData add(IData other) {
return new DataByte((byte) (value + other.asByte()));
}
@Override
public IData sub(IData other) {
return new DataByte((byte) (value - other.asByte()));
}
@Override
public IData mul(IData other) {
return new DataByte((byte) (value * other.asByte()));
}
@Override
public IData div(IData other) {
return new DataByte((byte) (value / other.asByte()));
}
@Override
public IData mod(IData other) {
return new DataByte((byte) (value % other.asByte()));
}
@Override
public IData and(IData other) {
return new DataByte((byte) (value & other.asByte()));
}
@Override
public IData or(IData other) {
return new DataByte((byte) (value | other.asByte()));
}
@Override
public IData xor(IData other) {
return new DataByte((byte) (value ^ other.asByte()));
}
@Override
public IData neg() {
return new DataByte((byte) (-value));
}
@Override
public IData not() {
return new DataByte((byte) (~value));
}
@Override
public String toString() {
return asString() + " as byte";
}
}
@@ -0,0 +1,220 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a byte array.
*
* @author Stan Hebben
*/
public class DataByteArray implements IData {
private final byte[] data;
private final boolean immutable;
public DataByteArray(byte[] data, boolean immutable) {
this.data = data;
this.immutable = immutable;
}
@Override
public IData add(IData other) {
byte[] otherData = other.asByteArray();
byte[] result = Arrays.copyOf(data, data.length + otherData.length);
System.arraycopy(otherData, 0, result, data.length, otherData.length);
return new DataByteArray(result, immutable);
}
@Override
public IData sub(IData other) {
throw new UnsupportedOperationException("Cannot subtract from a byte array");
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply with an arary");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide an array");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo on an array");
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate arrays");
}
@Override
public IData not() {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast an array to bool");
}
@Override
public byte asByte() {
throw new IllegalDataException("Cannot cast an array to byte");
}
@Override
public short asShort() {
throw new IllegalDataException("Cannot cast an array to short");
}
@Override
public int asInt() {
throw new IllegalDataException("Cannot cast an array to int");
}
@Override
public long asLong() {
throw new IllegalDataException("Cannot cast an array to long");
}
@Override
public float asFloat() {
throw new IllegalDataException("Cannot cast an array to float");
}
@Override
public double asDouble() {
throw new IllegalDataException("Cannot cast an array to double");
}
@Override
public String asString() {
StringBuilder result = new StringBuilder();
result.append("[");
boolean first = true;
for(byte value : data) {
if(first)
first = false;
else
result.append(", ");
result.append(value);
}
result.append("] as byte[]");
return result.toString();
}
@Override
public List<IData> asList() {
List<IData> result = new ArrayList<IData>();
for(byte value : data) {
result.add(new DataByte(value));
}
return result;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return data;
}
@Override
public int[] asIntArray() {
int[] result = new int[data.length];
for(int i = 0; i < result.length; i++) {
result[i] = data[i];
}
return result;
}
@Override
public IData getAt(int i) {
return new DataByte(data[i]);
}
@Override
public void setAt(int i, IData value) {
if(immutable) {
throw new UnsupportedOperationException("Cannot modify this byte array");
} else {
data[i] = value.asByte();
}
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("byte[] doesn't have members");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("cannot set byte[] members");
}
@Override
public int length() {
return data.length;
}
@Override
public boolean contains(IData data) {
return equals(data);
}
@Override
public int compareTo(IData data) {
throw new UnsupportedOperationException("Cannot compare arrays");
}
@Override
public boolean equals(IData data) {
return Arrays.equals(this.data, data.asByteArray());
}
@Override
public IData immutable() {
if(immutable) {
return this;
} else {
return new DataByteArray(Arrays.copyOf(this.data, this.data.length), true);
}
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromByteArray(this.data);
}
@Override
public String toString() {
return asString() + " as byte[]";
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a double-precision value.
*
* @author Stan Hebben
*/
public class DataDouble implements IData {
private final double value;
public DataDouble(double value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast a double to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return (short) value;
}
@Override
public int asInt() {
return (int) value;
}
@Override
public long asLong() {
return (long) value;
}
@Override
public float asFloat() {
return (float) value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Double.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("A double is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("A double is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("A double is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("A double is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asDouble() == value;
}
@Override
public boolean equals(IData other) {
return value == other.asDouble();
}
@Override
public int compareTo(IData other) {
return Double.compare(value, other.asDouble());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromDouble(value);
}
@Override
public IData add(IData other) {
return new DataDouble(value + other.asDouble());
}
@Override
public IData sub(IData other) {
return new DataDouble(value - other.asDouble());
}
@Override
public IData mul(IData other) {
return new DataDouble(value * other.asDouble());
}
@Override
public IData div(IData other) {
return new DataDouble(value / other.asDouble());
}
@Override
public IData mod(IData other) {
return new DataDouble(value % other.asDouble());
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on double");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on double");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on double");
}
@Override
public IData neg() {
return new DataDouble(-value);
}
@Override
public IData not() {
throw new UnsupportedOperationException("Cannot perform bitwise operations on double");
}
@Override
public String toString() {
return asString();
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a single-precision floating point value.
*
* @author Stan Hebben
*/
public class DataFloat implements IData {
private final float value;
public DataFloat(float value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast a float to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return (short) value;
}
@Override
public int asInt() {
return (int) value;
}
@Override
public long asLong() {
return (long) value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Float.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("A float is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("A float is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("A float is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("A float is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asFloat() == value;
}
@Override
public boolean equals(IData data) {
return value == data.asFloat();
}
@Override
public int compareTo(IData data) {
return Float.compare(value, data.asFloat());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromFloat(value);
}
@Override
public IData add(IData other) {
return new DataFloat(value + other.asFloat());
}
@Override
public IData sub(IData other) {
return new DataFloat(value - other.asFloat());
}
@Override
public IData mul(IData other) {
return new DataFloat(value * other.asFloat());
}
@Override
public IData div(IData other) {
return new DataFloat(value / other.asFloat());
}
@Override
public IData mod(IData other) {
return new DataFloat(value % other.asFloat());
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on float");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on float");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise operations on float");
}
@Override
public IData neg() {
return new DataFloat(-value);
}
@Override
public IData not() {
throw new UnsupportedOperationException("Cannot perform bitwise operations on float");
}
@Override
public String toString() {
return asString() + " as float";
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a 32-bit integer value.
*
* @author Stan Hebben
*/
public class DataInt implements IData {
private final int value;
public DataInt(int value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("Cannot cast an int to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return (short) value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Integer.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new RuntimeException("An int is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("An int is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("An int is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("An int is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asInt() == value;
}
@Override
public boolean equals(IData data) {
return value == data.asInt();
}
@Override
public int compareTo(IData data) {
return Integer.compare(value, data.asInt());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromInt(value);
}
@Override
public IData add(IData other) {
return new DataInt(value + other.asInt());
}
@Override
public IData sub(IData other) {
return new DataInt(value - other.asInt());
}
@Override
public IData mul(IData other) {
return new DataInt(value * other.asInt());
}
@Override
public IData div(IData other) {
return new DataInt(value / other.asInt());
}
@Override
public IData mod(IData other) {
return new DataInt(value % other.asInt());
}
@Override
public IData and(IData other) {
return new DataInt(value & other.asInt());
}
@Override
public IData or(IData other) {
return new DataInt(value | other.asInt());
}
@Override
public IData xor(IData other) {
return new DataInt(value ^ other.asInt());
}
@Override
public IData neg() {
return new DataInt(-value);
}
@Override
public IData not() {
return new DataInt(~value);
}
@Override
public String toString() {
return asString();
}
}
@@ -0,0 +1,220 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains an int array.
*
* @author Stan Hebben
*/
public class DataIntArray implements IData {
private final int[] data;
private final boolean immutable;
public DataIntArray(int[] data, boolean immutable) {
this.data = data;
this.immutable = immutable;
}
@Override
public IData add(IData other) {
int[] otherData = other.asIntArray();
int[] result = Arrays.copyOf(data, data.length + otherData.length);
System.arraycopy(otherData, 0, result, data.length, otherData.length);
return new DataIntArray(result, immutable);
}
@Override
public IData sub(IData other) {
throw new UnsupportedOperationException("Cannot subtract from an int array");
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply with an arary");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide an array");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo on an array");
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate arrays");
}
@Override
public IData not() {
throw new UnsupportedOperationException("Arrays don't support bitwise operations");
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast an array to bool");
}
@Override
public byte asByte() {
throw new IllegalDataException("Cannot cast an array to byte");
}
@Override
public short asShort() {
throw new IllegalDataException("Cannot cast an array to short");
}
@Override
public int asInt() {
throw new IllegalDataException("Cannot cast an array to int");
}
@Override
public long asLong() {
throw new IllegalDataException("Cannot cast an array to long");
}
@Override
public float asFloat() {
throw new IllegalDataException("Cannot cast an array to float");
}
@Override
public double asDouble() {
throw new IllegalDataException("Cannot cast an array to double");
}
@Override
public String asString() {
StringBuilder result = new StringBuilder();
result.append('[');
boolean first = true;
for(int value : data) {
if(first) {
first = false;
} else {
result.append(", ");
}
result.append(value);
}
result.append(']');
return result.toString();
}
@Override
public List<IData> asList() {
List<IData> result = new ArrayList<>();
for(int value : data) {
result.add(new DataInt(value));
}
return result;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
byte[] result = new byte[data.length];
for(int i = 0; i < result.length; i++) {
result[i] = (byte) data[i];
}
return result;
}
@Override
public int[] asIntArray() {
return data;
}
@Override
public IData getAt(int i) {
return new DataInt(data[i]);
}
@Override
public void setAt(int i, IData value) {
if(immutable) {
throw new UnsupportedOperationException("Cannot modify this byte array");
} else {
data[i] = value.asInt();
}
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("int[] doesn't have members");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("cannot set int[] members");
}
@Override
public int length() {
return data.length;
}
@Override
public boolean contains(IData data) {
return equals(data);
}
@Override
public int compareTo(IData data) {
throw new UnsupportedOperationException("Cannot compare arrays");
}
@Override
public boolean equals(IData data) {
return Arrays.equals(this.data, data.asIntArray());
}
@Override
public IData immutable() {
if(immutable) {
return this;
} else {
return new DataIntArray(Arrays.copyOf(this.data, this.data.length), true);
}
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromIntArray(this.data);
}
@Override
public String toString() {
return asString() + " as int[]";
}
}
@@ -0,0 +1,271 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a list.
*
* @author Stan Hebben
*/
public class DataList implements IData {
private final List<IData> values;
private final boolean immutable;
public DataList(List<IData> values, boolean immutable) {
this.values = values;
this.immutable = immutable;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot convert a list to a bool");
}
@Override
public byte asByte() {
throw new IllegalDataException("Cannot convert a list to a byte");
}
@Override
public short asShort() {
throw new IllegalDataException("Cannot convert a list to a short");
}
@Override
public int asInt() {
throw new IllegalDataException("Cannot convert a list to an int");
}
@Override
public long asLong() {
throw new IllegalDataException("Cannot convert a list to a long");
}
@Override
public float asFloat() {
throw new IllegalDataException("Cannot convert a list to a float");
}
@Override
public double asDouble() {
throw new IllegalDataException("Cannot convert a list to a double");
}
@Override
public List<IData> asList() {
if(immutable) {
return Collections.unmodifiableList(values);
} else {
return values;
}
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public String asString() {
StringBuilder output = new StringBuilder();
output.append('[');
boolean first = true;
for(IData value : values) {
if(first) {
first = false;
} else {
output.append(", ");
}
output.append(value.toString());
}
output.append(']');
return output.toString();
}
@Override
public byte[] asByteArray() {
try {
byte[] result = new byte[values.size()];
for(int i = 0; i < values.size(); i++) {
result[i] = values.get(i).asByte();
}
return result;
} catch(IllegalDataException ex) {
return null;
}
}
@Override
public int[] asIntArray() {
try {
int[] result = new int[values.size()];
for(int i = 0; i < values.size(); i++) {
result[i] = values.get(i).asInt();
}
return result;
} catch(IllegalDataException ex) {
return null;
}
}
@Override
public IData getAt(int i) {
return values.get(i);
}
@Override
public void setAt(int i, IData value) {
if(immutable) {
throw new UnsupportedOperationException("this list is immutable");
} else {
values.set(i, value);
}
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("Lists don't have members");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("Lists don't have members");
}
@Override
public int length() {
return values.size();
}
@Override
public boolean contains(IData data) {
List<IData> dataValues = data.asList();
if(dataValues != null && containsList(dataValues))
return true;
for(IData value : values) {
if(value.contains(data))
return true;
}
return false;
}
private boolean containsList(List<IData> dataValues) {
outer:
for(IData dataValue : dataValues) {
for(IData value : values) {
if(value.contains(dataValue))
continue outer;
}
return false;
}
return true;
}
@Override
public boolean equals(IData data) {
List<IData> otherValues = data.asList();
if(otherValues.size() != values.size())
return false;
for(int i = 0; i < values.size(); i++) {
if(!values.get(i).equals(otherValues.get(i)))
return false;
}
return true;
}
@Override
public int compareTo(IData data) {
throw new UnsupportedOperationException("Cannot compare with a list");
}
@Override
public IData immutable() {
if(immutable) {
return this;
} else {
List<IData> copy = new ArrayList<>();
for(IData value : values) {
copy.add(value.immutable());
}
return new DataList(copy, true);
}
}
@Override
public IData update(IData data) {
if(immutable)
data = data.immutable();
List<IData> result = new ArrayList<>();
result.addAll(values);
result.addAll(data.asList());
return new DataList(result, immutable);
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromList(values);
}
@Override
public IData add(IData other) {
return update(other);
}
@Override
public IData sub(IData other) {
throw new UnsupportedOperationException("Cannot subtract from a list");
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply a list");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide a list");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo on a list");
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Lists don't support bitwise operators");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Lists don't support bitwise operators");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Lists don't support bitwise operators");
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate a list");
}
@Override
public IData not() {
throw new UnsupportedOperationException("Lists don't support bitwise operators");
}
@Override
public String toString() {
return asString();
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a 64-bit long value.
*
* @author Stan Hebben
*/
public class DataLong implements IData {
private final long value;
public DataLong(long value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast a long to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return (short) value;
}
@Override
public int asInt() {
return (int) value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Long.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("A long is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("A long is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("A long is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("A long is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asLong() == value;
}
@Override
public boolean equals(IData data) {
return value == data.asLong();
}
@Override
public int compareTo(IData data) {
return Long.compare(value, data.asLong());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromLong(value);
}
@Override
public IData add(IData other) {
return new DataLong(value + other.asLong());
}
@Override
public IData sub(IData other) {
return new DataLong(value - other.asLong());
}
@Override
public IData mul(IData other) {
return new DataLong(value * other.asLong());
}
@Override
public IData div(IData other) {
return new DataLong(value / other.asLong());
}
@Override
public IData mod(IData other) {
return new DataLong(value % other.asLong());
}
@Override
public IData and(IData other) {
return new DataLong(value & other.asLong());
}
@Override
public IData or(IData other) {
return new DataLong(value | other.asLong());
}
@Override
public IData xor(IData other) {
return new DataLong(value ^ other.asLong());
}
@Override
public IData neg() {
return new DataLong(-value);
}
@Override
public IData not() {
return new DataLong(~value);
}
@Override
public String toString() {
return asString() + " as long";
}
}
@@ -0,0 +1,300 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a map, mapping strings to data elements.
*
* @author Stan Hebben
*/
public class DataMap implements IData {
public static final DataMap EMPTY = new DataMap(new HashMap<>(), true);
private final Map<String, IData> data;
private final boolean immutable;
public DataMap(Map<String, IData> data, boolean immutable) {
this.data = data;
this.immutable = immutable;
}
@Override
public IData add(IData other) {
Map<String, IData> result = new HashMap<>();
Map<String, IData> otherMap = other.asMap();
for(Map.Entry<String, IData> entry : data.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
for(Map.Entry<String, IData> entry : otherMap.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
return new DataMap(result, immutable);
}
@Override
public IData sub(IData other) {
Map<String, IData> result = new HashMap<>();
Map<String, IData> otherMap = other.asMap();
for(Map.Entry<String, IData> entry : data.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
for(String key : otherMap.keySet()) {
result.remove(key);
}
return new DataMap(result, immutable);
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply maps");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide maps");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo with maps");
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Maps do not support bitwise operations");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Maps do not support bitwise operations");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Maps do not support bitwise operations");
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate maps.");
}
@Override
public IData not() {
throw new UnsupportedOperationException("Maps do not support bitwise operations");
}
@Override
public boolean asBool() {
throw new UnsupportedOperationException("Cannot cast map to bool");
}
@Override
public byte asByte() {
throw new UnsupportedOperationException("Cannot cast map to byte");
}
@Override
public short asShort() {
throw new UnsupportedOperationException("Cannot cast map to short");
}
@Override
public int asInt() {
throw new UnsupportedOperationException("Cannot cast map to int");
}
@Override
public long asLong() {
throw new UnsupportedOperationException("Cannot cast map to long");
}
@Override
public float asFloat() {
throw new UnsupportedOperationException("Cannot cast map to float");
}
@Override
public double asDouble() {
throw new UnsupportedOperationException("Cannot cast map to double");
}
@Override
public String asString() {
StringBuilder result = new StringBuilder();
result.append('{');
boolean first = true;
for(Map.Entry<String, IData> entry : data.entrySet()) {
if(first) {
first = false;
} else {
result.append(", ");
}
if(isValidIdentifier(entry.getKey())) {
result.append(entry.getKey());
} else {
result.append("\"").append(entry.getKey()).append("\"");
}
result.append(": ");
result.append(entry.getValue().toString());
}
result.append('}');
return result.toString();
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
if(immutable) {
return Collections.unmodifiableMap(data);
} else {
return data;
}
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("Cannot index maps with integers");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("Cannot index maps with integers");
}
@Override
public IData memberGet(String name) {
return data.get(name);
}
@Override
public void memberSet(String name, IData data) {
if(immutable) {
throw new UnsupportedOperationException("this map is not modifiable");
} else {
this.data.put(name, data);
}
}
@Override
public int length() {
return data.size();
}
@Override
public boolean contains(IData data) {
if(data instanceof DataString) {
return this.data.containsKey(data.asString());
}
Map<String, IData> dataMap = data.asMap();
if(dataMap == null)
return false;
for(Map.Entry<String, IData> dataEntry : dataMap.entrySet()) {
if(!this.data.containsKey(dataEntry.getKey())) {
return false;
} else if(!this.data.get(dataEntry.getKey()).contains(dataEntry.getValue())) {
return false;
}
}
return true;
}
@Override
public int compareTo(IData data) {
throw new UnsupportedOperationException("Cannot compare maps");
}
@Override
public boolean equals(IData data) {
if(this == data)
return true;
Map<String, IData> dataMap = data.asMap();
if(dataMap.size() != this.data.size())
return false;
for(Map.Entry<String, IData> dataEntry : this.data.entrySet()) {
if(!dataMap.containsKey(dataEntry.getKey())) {
return false;
} else if(!dataMap.get(dataEntry.getKey()).equals(dataEntry.getValue())) {
return false;
}
}
return true;
}
@Override
public IData immutable() {
if(immutable) {
return this;
} else {
Map<String, IData> result = new HashMap<>();
for(Map.Entry<String, IData> entry : this.data.entrySet()) {
result.put(entry.getKey(), entry.getValue().immutable());
}
return new DataMap(result, true);
}
}
@Override
public IData update(IData data) {
if(immutable)
data = data.immutable();
Map<String, IData> result = new HashMap<>();
for(Map.Entry<String, IData> entry : this.data.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
for(Map.Entry<String, IData> entry : data.asMap().entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
return new DataMap(result, immutable);
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromMap(data);
}
@Override
public String toString() {
return asString();
}
private boolean isValidIdentifier(String str) {
if(!Character.isJavaIdentifierStart(str.charAt(0)))
return false;
for(int i = 1; i < str.length(); i++) {
if(!Character.isJavaIdentifierPart(str.charAt(i)))
return false;
}
return true;
}
}
@@ -0,0 +1,187 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a 16-bit short value.
*
* @author Stan Hebben
*/
public class DataShort implements IData {
private final short value;
public DataShort(short value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new IllegalDataException("Cannot cast a short to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Short.toString(value);
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
throw new UnsupportedOperationException("A short is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("A short is not indexable");
}
@Override
public IData memberGet(String name) {
throw new UnsupportedOperationException("A short is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("A short is not indexable");
}
@Override
public int length() {
return 0;
}
@Override
public boolean contains(IData data) {
return data.asShort() == value;
}
@Override
public boolean equals(IData data) {
return value == data.asShort();
}
@Override
public int compareTo(IData data) {
return Short.compare(value, data.asShort());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromShort(value);
}
@Override
public IData add(IData other) {
return new DataShort((short) (value + other.asShort()));
}
@Override
public IData sub(IData other) {
return new DataShort((short) (value - other.asShort()));
}
@Override
public IData mul(IData other) {
return new DataShort((short) (value * other.asShort()));
}
@Override
public IData div(IData other) {
return new DataShort((short) (value / other.asShort()));
}
@Override
public IData mod(IData other) {
return new DataShort((short) (value % other.asShort()));
}
@Override
public IData and(IData other) {
return new DataShort((short) (value & other.asShort()));
}
@Override
public IData or(IData other) {
return new DataShort((short) (value | other.asShort()));
}
@Override
public IData xor(IData other) {
return new DataShort((short) (value ^ other.asShort()));
}
@Override
public IData neg() {
return new DataShort((short) (-value));
}
@Override
public IData not() {
return new DataShort((short) (~value));
}
@Override
public String toString() {
return asString() + " as short";
}
}
@@ -0,0 +1,191 @@
package minetweaker.api.data;
import java.util.*;
/**
* Contains a string value.
*
* @author Stan Hebben
*/
public class DataString implements IData {
private final String value;
public DataString(String value) {
this.value = value;
}
@Override
public boolean asBool() {
return Boolean.parseBoolean(value);
}
@Override
public byte asByte() {
return Byte.parseByte(value);
}
@Override
public short asShort() {
return Short.parseShort(value);
}
@Override
public int asInt() {
return Integer.parseInt(value);
}
@Override
public long asLong() {
return Long.parseLong(value);
}
@Override
public float asFloat() {
return Float.parseFloat(value);
}
@Override
public double asDouble() {
return Double.parseDouble(value);
}
@Override
public String asString() {
return value;
}
@Override
public List<IData> asList() {
return null;
}
@Override
public Map<String, IData> asMap() {
return null;
}
@Override
public byte[] asByteArray() {
return null;
}
@Override
public int[] asIntArray() {
return null;
}
@Override
public IData getAt(int i) {
return new DataString(value.substring(i, i + 1));
}
@Override
public void setAt(int i, IData value) {
throw new UnsupportedOperationException("Strings are immutable");
}
@Override
public IData memberGet(String name) {
if(name.equals("length")) {
return new DataInt(value.length());
} else {
throw new UnsupportedOperationException("no such member: " + name);
}
}
@Override
public void memberSet(String name, IData data) {
throw new UnsupportedOperationException("Strings are immutable");
}
@Override
public int length() {
return value.length();
}
@Override
public boolean contains(IData data) {
return data.asString().equals(value);
}
@Override
public boolean equals(IData data) {
return value.equals(data.asString());
}
@Override
public int compareTo(IData data) {
return value.compareTo(data.asString());
}
@Override
public IData immutable() {
return this;
}
@Override
public IData update(IData data) {
return data;
}
@Override
public <T> T convert(IDataConverter<T> converter) {
return converter.fromString(value);
}
@Override
public IData add(IData other) {
return new DataString(value + other.asString());
}
@Override
public IData sub(IData other) {
throw new UnsupportedOperationException("Cannot subtract from a string");
}
@Override
public IData mul(IData other) {
throw new UnsupportedOperationException("Cannot multiply a string");
}
@Override
public IData div(IData other) {
throw new UnsupportedOperationException("Cannot divide a string");
}
@Override
public IData mod(IData other) {
throw new UnsupportedOperationException("Cannot perform modulo on a string");
}
@Override
public IData and(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise arithmetic on a string");
}
@Override
public IData or(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise arithmetic on a string");
}
@Override
public IData xor(IData other) {
throw new UnsupportedOperationException("Cannot perform bitwise arithmetic on a string");
}
@Override
public IData neg() {
throw new UnsupportedOperationException("Cannot negate a string");
}
@Override
public IData not() {
throw new UnsupportedOperationException("Cannot perform bitwise arithmetic on a string");
}
@Override
public String toString() {
return '\"' + value.replace("\"", "\\\"") + "\"";
}
}
@@ -0,0 +1,141 @@
package minetweaker.api.data;
import stanhebben.zenscript.annotations.*;
import java.util.*;
/**
* Generic data interface. A data element may contain any kind of basic data
* element (bool, byte, short, int, long, float, double, string, list, map, int
* array or byte array). Used to store data with stacks, blocks, world, ...
*
* @author Stan Hebben
*/
@ZenClass("minetweaker.data.IData")
public interface IData {
@ZenOperator(OperatorType.ADD)
IData add(IData other);
@ZenOperator(OperatorType.SUB)
IData sub(IData other);
@ZenOperator(OperatorType.MUL)
IData mul(IData other);
@ZenOperator(OperatorType.DIV)
IData div(IData other);
@ZenOperator(OperatorType.MOD)
IData mod(IData other);
@ZenOperator(OperatorType.AND)
IData and(IData other);
@ZenOperator(OperatorType.OR)
IData or(IData other);
@ZenOperator(OperatorType.XOR)
IData xor(IData other);
@ZenOperator(OperatorType.NEG)
IData neg();
@ZenOperator(OperatorType.NOT)
IData not();
@ZenCaster
boolean asBool();
@ZenCaster
byte asByte();
@ZenCaster
short asShort();
@ZenCaster
int asInt();
@ZenCaster
long asLong();
@ZenCaster
float asFloat();
@ZenCaster
double asDouble();
@ZenCaster
String asString();
/**
* Attempts to convert this value to a List. Returns null if this value
* cannot be converted to a list.
*
* @return list data of this value, if any
*/
@ZenCaster
List<IData> asList();
/**
* Attempts to convert this value to a Map. Returns null if this value
* cannot be converted to a map.
*
* @return map data of this value, if any
*/
@ZenCaster
Map<String, IData> asMap();
/**
* Attempts to convert this value to a byte array. Returns null if this
* value cannot be converted to a byte array.
*
* @return byte array data of this value, if any
*/
@ZenCaster
byte[] asByteArray();
/**
* Attempts to convert this value to an int array. Returns null if this
* value cannot be converted to an int array.
*
* @return int array data of this value, if any
*/
@ZenCaster
int[] asIntArray();
@ZenOperator(OperatorType.INDEXGET)
IData getAt(int i);
@ZenOperator(OperatorType.INDEXSET)
void setAt(int i, IData value);
@ZenMemberGetter
IData memberGet(String name);
@ZenMemberSetter
void memberSet(String name, IData data);
@ZenGetter
int length();
@ZenOperator(OperatorType.CONTAINS)
boolean contains(IData data);
@ZenOperator(OperatorType.COMPARE)
int compareTo(IData data);
@ZenOperator(OperatorType.EQUALS)
boolean equals(IData data);
@ZenGetter
IData immutable();
@ZenMethod
IData update(IData data);
<T> T convert(IDataConverter<T> converter);
@Override
String toString();
}
@@ -0,0 +1,37 @@
package minetweaker.api.data;
import java.util.*;
/**
* Data converter interface. Used to convert data elements to other types.
*
* @param <T> output type
*
* @author Stan Hebben
*/
public interface IDataConverter<T> {
T fromBool(boolean value);
T fromByte(byte value);
T fromShort(short value);
T fromInt(int value);
T fromLong(long value);
T fromFloat(float value);
T fromDouble(double value);
T fromString(String value);
T fromList(List<IData> values);
T fromMap(Map<String, IData> values);
T fromByteArray(byte[] value);
T fromIntArray(int[] value);
}
@@ -0,0 +1,16 @@
/*
* This file is part of MineTweaker API, licensed under the MIT License (MIT).
*
* Copyright (c) 2014 MineTweaker <http://minetweaker3.powerofbytes.com>
*/
package minetweaker.api.data;
/**
* @author Stan
*/
public class IllegalDataException extends RuntimeException {
public IllegalDataException(String message) {
super(message);
}
}