Added projects and basic build scripts. Project for scripting engine, API, Main 1.7 version and one for each mod supported in 1.7.

This commit is contained in:
Stan Hebben committed 2014-06-09 20:33:30 +02:00
1 parent 219106f392
commit 38b78ce305
281 files changed
+25090 -49

No files matched your search

@@ -0,0 +1,19 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
/**
*
* @author Stan
*/
public class GlobalFunctions {
private GlobalFunctions() {}
public static void print(String message) {
MineTweakerAPI.logger.logInfo(message);
}
}
@@ -0,0 +1,211 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import minetweaker.minecraft.item.IIngredient;
import stanhebben.zenscript.IZenErrorLogger;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.annotations.ZenExpansion;
import stanhebben.zenscript.compiler.TypeRegistry;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaStaticField;
import stanhebben.zenscript.symbols.SymbolJavaStaticMethod;
import stanhebben.zenscript.symbols.SymbolPackage;
import stanhebben.zenscript.type.ZenTypeNative;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stan
*/
public class GlobalRegistry {
private static final Map<String, IZenSymbol> globals = new HashMap<String, IZenSymbol>();
private static final List<IRecipeRemover> removers = new ArrayList<IRecipeRemover>();
private static final List<IBracketHandler> bracketHandlers = new ArrayList<IBracketHandler>();
private static final TypeRegistry types = new TypeRegistry();
private static final SymbolPackage root = new SymbolPackage();
private static final IZenErrorLogger errors = new MyErrorLogger();
private static final IZenCompileEnvironment environment = new MyCompileEnvironment();
private static final Map<String, TypeExpansion> expansions = new HashMap<String, TypeExpansion>();
static {
register("print", getStaticFunction(GlobalFunctions.class, "print", String.class));
register("max", getStaticFunction(Math.class, "max", int.class, int.class));
register("min", getStaticFunction(Math.class, "min", int.class, int.class));
register("logger", getStaticField(MineTweakerAPI.class, "logger"));
register("minetweaker", getStaticField(MineTweakerAPI.class, "tweaker"));
register("recipes", getStaticField(MineTweakerAPI.class, "recipes"));
register("furnace", getStaticField(MineTweakerAPI.class, "furnace"));
register("oreDict", getStaticField(MineTweakerAPI.class, "oreDict"));
/*registerExpansion(ExpandAnyArray.class);
registerExpansion(ExpandAnyDict.class);
registerExpansion(ExpandBool.class);
registerExpansion(ExpandByte.class);
registerExpansion(ExpandDouble.class);
registerExpansion(ExpandFloat.class);
registerExpansion(ExpandInt.class);
registerExpansion(ExpandLong.class);
registerExpansion(ExpandShort.class);
registerExpansion(ExpandString.class);*/
/*registerNativeClass(IData.class);
registerNativeClass(IIngredient.class);
registerNativeClass(IItemCondition.class);
registerNativeClass(IItemDefinition.class);
registerNativeClass(IItemStack.class);
registerNativeClass(IItemTransformer.class);
registerNativeClass(Condition.class);
registerNativeClass(Transform.class);
registerNativeClass(ILiquidDefinition.class);
registerNativeClass(ILiquidStack.class);
registerNativeClass(IOreDict.class);
registerNativeClass(IOreDictEntry.class);
registerNativeClass(ICraftingInventory.class);
registerNativeClass(IFurnaceManager.class);
registerNativeClass(IRecipeFunction.class);*/
}
private GlobalRegistry() {}
public static void register(String name, IZenSymbol symbol) {
if (globals.containsKey(name)) {
throw new IllegalArgumentException("symbol already exists: " + name);
}
globals.put(name, symbol);
}
public static void registerExpansion(Class cls) {
for (Annotation annotation : cls.getAnnotationsByType(ZenExpansion.class)) {
ZenExpansion eAnnotation = (ZenExpansion) annotation;
if (!expansions.containsKey(eAnnotation.value())) {
expansions.put(eAnnotation.value(), new TypeExpansion());
}
expansions.get(eAnnotation.value()).expand(cls, types);
}
}
public static void registerRemover(IRecipeRemover remover) {
removers.add(remover);
}
public static void registerBracketHandler(IBracketHandler handler) {
bracketHandlers.add(handler);
}
public static void registerNativeClass(Class cls) {
ZenTypeNative type = new ZenTypeNative(cls);
type.complete(types);
root.put(type.getName(), root, errors);
}
public static TypeRegistry getTypeRegistry() {
return types;
}
public static void remove(IIngredient ingredient) {
for (IRecipeRemover remover : removers) {
remover.remove(ingredient);
}
}
public static IZenSymbol resolveBracket(List<Token> tokens) {
for (IBracketHandler handler : bracketHandlers) {
IZenSymbol symbol = handler.resolve(tokens);
if (symbol != null) {
return symbol;
}
}
return null;
}
public static IZenSymbol getStaticFunction(Class cls, String name, Class... arguments) {
JavaMethod method = JavaMethod.get(types, cls, name, arguments);
if (method == null) return null;
return new SymbolJavaStaticMethod(method);
}
public static IZenSymbol getStaticField(Class cls, String name) {
try {
Field field = cls.getDeclaredField(name);
return new SymbolJavaStaticField(cls, field, types);
} catch (NoSuchFieldException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (SecurityException ex) {
Logger.getLogger(GlobalRegistry.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private static class MyErrorLogger implements IZenErrorLogger {
@Override
public void error(ZenPosition position, String message) {
MineTweakerAPI.logger.logError(position.toString() + ": " + message);
}
@Override
public void warning(ZenPosition position, String message) {
MineTweakerAPI.logger.logWarning(position.toString() + ": " + message);
}
}
private static class MyCompileEnvironment implements IZenCompileEnvironment {
@Override
public IZenErrorLogger getErrorLogger() {
return errors;
}
@Override
public IZenSymbol getGlobal(String name) {
if (globals.containsKey(name)) {
return globals.get(name);
} else {
return root.get(name);
}
}
@Override
public IZenSymbol getDollar(String name) {
return null;
}
@Override
public IZenSymbol getBracketed(List<Token> tokens) {
return resolveBracket(tokens);
}
@Override
public TypeRegistry getTypeRegistry() {
return types;
}
@Override
public TypeExpansion getExpansion(String type) {
return expansions.get(type);
}
}
}
@@ -0,0 +1,19 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import java.util.List;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.symbols.IZenSymbol;
/**
*
* @author Stan
*/
public interface IBracketHandler {
public IZenSymbol resolve(List<Token> tokens);
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
public interface ILogger {
@ZenMethod
public void logInfo(String message);
@ZenMethod
public void logWarning(String message);
@ZenMethod
public void logError(String message);
}
@@ -0,0 +1,44 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import java.util.List;
import minetweaker.minecraft.item.IIngredient;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minetweaker.MineTweaker")
public interface IMineTweaker {
/**
* Executes a specified MineTweaker action. Will print a log message and
* adds the action to the undo list.
*
* @param action action to execute
*/
public void apply(IUndoableAction action);
/**
* Removes a specific item from all known recipes handlers.
*
* @param items items to remove
*/
@ZenMethod
public void remove(IIngredient items);
/**
* Rolls back all actions performed by MineTweaker. Returns the list of
* actions that could not be rolled back (the "stuck" ones that are not
* undoable).
*
* @return stuck action list
*/
public List<IUndoableAction> rollback();
}
@@ -0,0 +1,17 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import minetweaker.minecraft.item.IIngredient;
/**
*
* @author Stan
*/
public interface IRecipeRemover {
public void remove(IIngredient ingredient);
}
@@ -0,0 +1,63 @@
package minetweaker;
/**
* Defines a (possibly) undoable action.
*
* Every modification made by a command must be registered to the Tweaker with
* the Tweaker.apply method. This enables MineTweaker to keep track of actions
* that have been performed, and enables them to be rolled back later on.
*
* Actions exist in two flavors: undoable and permanent. If an action returns
* canUndo(), the action is undoable, otherwise it is permanent. Permanent
* actions cannot be used in server scripts.
*
* @author Stan Hebben
*/
public interface IUndoableAction {
/**
* Executes what the action is supposed to do. This method can be called
* again if undo() has been called in between.
*/
public void apply();
/**
* Checks if this action can be undone. If this method returns true, it
* must implement undo() properly. If this method returns false, the action
* is considered permanent, undo() will never be called, and the action
* cannot be executed as part of a server script. Additionally, the
* minetweaker.clear() function will not work and minetweaker.canClear
* will return false.
*
* @return undoable status
*/
public boolean canUndo();
/**
* Undoes the action. Will only be called after apply() has been executed.
* After an undo, apply may be called again. They could be done multiple
* times in certain scenarios.
*/
public void undo();
/**
* Describes, in a single human-readable sentence, what this specific action
* is doing. Used in logging messages, lists, ...
*
* Try to be as descriptive as possible without being too verbose.
*
* Examples:
* - Adding Peach planks to the woodPlanks ore dictionary entry
* - Removing a recipe for Iron Ore
*
* @return the description of this action
*/
public String describe();
/**
* Describes what this action does if it is undone. Similar to the describe()
* method. No implementation is needed if the action cannot be undone.
*
* @return the description of this action, when undone
*/
public String describeUndo();
}
@@ -0,0 +1,53 @@
package minetweaker;
import minetweaker.minecraft.recipes.IRecipeManager;
import minetweaker.minecraft.oredict.IOreDict;
import minetweaker.minecraft.recipes.IFurnaceManager;
/**
* Provides access to the MineTweaker API.
*
* An implementing platform needs to do the following:
* - Set a logger
* - Set the ore dictionary
* - Set the recipe manager
* - Set the furnace manager
*
* - Register additional global symbols to the GlobalRegistry (recipes,
* minetweaker, oreDict, logger, as well as the official set of functions)
* - Register native classes using the GlobalRegistry
* - Register bracket handlers to resolve block/item/... references using the
* bracket syntax
*
* @author Stan Hebben
*/
public class MineTweakerAPI {
private MineTweakerAPI() {}
/**
* The Tweaker is where you apply undoable actions. Any kind of action that
* reloads with the scripts should always be submitted to the tweaker.
*/
public static final IMineTweaker tweaker = new Tweaker();
/**
* The logger can be used to write logging messages to the client. Error and
* warning messages should be relayed to admins for further handling.
*/
public static ILogger logger = null;
/**
* Access point to the ore dictionary.
*/
public static IOreDict oreDict = null;
/**
* Access point to the recipe manager.
*/
public static IRecipeManager recipes = null;
/**
* Access point to the furnace manager.
*/
public static IFurnaceManager furnace = null;
}
@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
/**
*
* @author Stanneke
*/
public abstract class OneWayAction implements IUndoableAction {
@Override
public boolean canUndo() {
return false;
}
@Override
public void undo() {}
@Override
public String describeUndo() {
return "impossibru!";
}
}
@@ -0,0 +1,50 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import minetweaker.minecraft.item.IIngredient;
/**
*
*
* @author Stan Hebben
*/
public class Tweaker implements IMineTweaker {
List<IUndoableAction> actions = new ArrayList<IUndoableAction>();
Set<IUndoableAction> wereStuck = new HashSet<IUndoableAction>();
@Override
public void apply(IUndoableAction action) {
MineTweakerAPI.logger.logInfo(action.describe());
action.apply();
}
@Override
public void remove(IIngredient items) {
GlobalRegistry.remove(items);
}
@Override
public List<IUndoableAction> rollback() {
List<IUndoableAction> stuck = new ArrayList<IUndoableAction>();
for (int i = actions.size() - 1; i >= 0; i--) {
IUndoableAction action = actions.get(i);
if (action.canUndo()) {
MineTweakerAPI.logger.logInfo(action.describeUndo());
action.undo();
} else {
stuck.add(0, action);
wereStuck.add(action);
}
}
return stuck;
}
}
@@ -0,0 +1,22 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Stan
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ModOnly {
public String value();
}
@@ -0,0 +1,25 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import java.util.Arrays;
import minetweaker.minecraft.data.DataList;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("any[]")
public class ExpandAnyArray {
@ZenCaster
public IData asData(IData[] values) {
return new DataList(Arrays.asList(values), true);
}
}
@@ -0,0 +1,25 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import java.util.Map;
import minetweaker.minecraft.data.DataMap;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("any[any]")
public class ExpandAnyDict {
@ZenCaster
public IData asData(Map<String, IData> values) {
return new DataMap(values, true);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataBool;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("bool")
public class ExpandBool {
@ZenCaster
public IData asData(boolean value) {
return new DataBool(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataByte;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("byte")
public class ExpandByte {
@ZenCaster
public IData asData(byte value) {
return new DataByte(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataDouble;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("double")
public class ExpandDouble {
@ZenCaster
public IData asData(double value) {
return new DataDouble(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataFloat;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("float")
public class ExpandFloat {
@ZenCaster
public IData asData(float value) {
return new DataFloat(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataInt;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("int")
public class ExpandInt {
@ZenCaster
public IData toData(int value) {
return new DataInt(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataLong;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("long")
public class ExpandLong {
@ZenCaster
public IData asData(long value) {
return new DataLong(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataShort;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("short")
public class ExpandShort {
@ZenCaster
public IData asNBT(short value) {
return new DataShort(value);
}
}
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.expand;
import minetweaker.minecraft.data.DataString;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenExpansion;
/**
*
* @author Stanneke
*/
@ZenExpansion("string")
public class ExpandString {
@ZenCaster
public IData asData(String value) {
return new DataString(value);
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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() {
throw new UnsupportedOperationException("Cannot cast a bool to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a bool to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("cannot cast a bool to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("cannot cast a bool to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("a bool is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("a bool is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("a bool is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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);
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataByte implements IData {
private final byte value;
public DataByte(byte value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("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() {
throw new UnsupportedOperationException("Cannot convert a byte to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot convert a byte to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a byte to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a byte to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("A byte is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("A byte is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("A byte is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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));
}
}
@@ -0,0 +1,220 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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 UnsupportedOperationException("Cannot cast an array to bool");
}
@Override
public byte asByte() {
throw new UnsupportedOperationException("Cannot cast an array to byte");
}
@Override
public short asShort() {
throw new UnsupportedOperationException("Cannot cast an array to short");
}
@Override
public int asInt() {
throw new UnsupportedOperationException("Cannot cast an array to int");
}
@Override
public long asLong() {
throw new UnsupportedOperationException("Cannot cast an array to long");
}
@Override
public float asFloat() {
throw new UnsupportedOperationException("Cannot cast an array to float");
}
@Override
public double asDouble() {
throw new UnsupportedOperationException("Cannot cast an array to double");
}
@Override
public String asString() {
StringBuilder result = new StringBuilder();
result.append("X\"");
for (byte value : data) {
String str = Integer.toHexString(value);
if (str.length() == 1) {
result.append('0');
}
result.append(str);
}
result.append('"');
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() {
throw new UnsupportedOperationException("Cannot convert list to map");
}
@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);
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataDouble implements IData {
private final double value;
public DataDouble(double value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("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() {
throw new UnsupportedOperationException("Cannot cast a double to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a double to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a double to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a double to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("A double is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("A double is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("A double is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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");
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataFloat implements IData {
private final float value;
public DataFloat(float value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("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() {
throw new UnsupportedOperationException("Cannot cast a float to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a float to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a float to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a float to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("A float is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("A float is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("A float is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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");
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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() {
throw new UnsupportedOperationException("Cannot cast an int to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast an int to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast an int to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast an int to an int array");
}
@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);
}
}
@@ -0,0 +1,222 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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 UnsupportedOperationException("Cannot cast an array to bool");
}
@Override
public byte asByte() {
throw new UnsupportedOperationException("Cannot cast an array to byte");
}
@Override
public short asShort() {
throw new UnsupportedOperationException("Cannot cast an array to short");
}
@Override
public int asInt() {
throw new UnsupportedOperationException("Cannot cast an array to int");
}
@Override
public long asLong() {
throw new UnsupportedOperationException("Cannot cast an array to long");
}
@Override
public float asFloat() {
throw new UnsupportedOperationException("Cannot cast an array to float");
}
@Override
public double asDouble() {
throw new UnsupportedOperationException("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<IData>();
for (int value : data) {
result.add(new DataInt(value));
}
return result;
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot convert list to map");
}
@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);
}
}
@@ -0,0 +1,243 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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 RuntimeException("Cannot convert a list to a bool");
}
@Override
public byte asByte() {
throw new RuntimeException("Cannot convert a list to a byte");
}
@Override
public short asShort() {
throw new RuntimeException("Cannot convert a list to a short");
}
@Override
public int asInt() {
throw new RuntimeException("Cannot convert a list to an int");
}
@Override
public long asLong() {
throw new RuntimeException("Cannot convert a list to a long");
}
@Override
public float asFloat() {
throw new RuntimeException("Cannot convert a list to a float");
}
@Override
public double asDouble() {
throw new RuntimeException("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() {
throw new RuntimeException("Cannot convert a list to a map");
}
@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.asString());
}
output.append(']');
return output.toString();
}
@Override
public byte[] asByteArray() {
byte[] result = new byte[values.size()];
for (int i = 0; i < values.size(); i++) {
result[i] = values.get(i).asByte();
}
return result;
}
@Override
public int[] asIntArray() {
int[] result = new int[values.size()];
for (int i = 0; i < values.size(); i++) {
result[i] = values.get(i).asInt();
}
return result;
}
@Override
public IData getAt(int i) {
return values.get(i);
}
@Override
public void setAt(int i, IData value) {
if (immutable) {
throw new RuntimeException("this list is immutable");
} else {
values.set(i, value);
}
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("Lists don't have members");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("Lists don't have members");
}
@Override
public int length() {
return values.size();
}
@Override
public boolean contains(IData data) {
for (IData value : values) {
if (value.equals(data)) return true;
}
return false;
}
@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<IData>();
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<IData>();
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");
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataLong implements IData {
private final long value;
public DataLong(long value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("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() {
throw new UnsupportedOperationException("Cannot cast a long to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a long to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a long to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a long to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("A long is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("A long is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("A long is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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);
}
}
@@ -0,0 +1,272 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataMap implements IData {
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<String, IData>();
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<String, IData>();
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 = false;
for (Map.Entry<String, IData> entry : data.entrySet()) {
if (first) {
first = false;
} else {
result.append(", ");
}
result.append(entry.getKey());
result.append(": ");
result.append(entry.getValue().asString());
}
result.append('}');
return result.toString();
}
@Override
public List<IData> asList() {
throw new UnsupportedOperationException("Cannot cast map to list");
}
@Override
public Map<String, IData> asMap() {
if (immutable) {
return Collections.unmodifiableMap(data);
} else {
return data;
}
}
@Override
public byte[] asByteArray() {
throw new UnsupportedOperationException("Cannot cast map to byte[]");
}
@Override
public int[] asIntArray() {
throw new UnsupportedOperationException("Cannot cast map to int[]");
}
@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) {
Map<String, IData> dataMap = data.asMap();
for (Map.Entry<String, IData> dataEntry : dataMap.entrySet()) {
if (!this.data.containsKey(dataEntry.getKey())) {
return false;
} else if (!this.data.get(dataEntry.getKey()).contains(data)) {
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<String, IData>();
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<String, IData>();
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);
}
}
@@ -0,0 +1,187 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
public class DataShort implements IData {
private final short value;
public DataShort(short value) {
this.value = value;
}
@Override
public boolean asBool() {
throw new RuntimeException("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() {
throw new UnsupportedOperationException("Cannot cast a short to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a short to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a short to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a short to an int array");
}
@Override
public IData getAt(int i) {
throw new RuntimeException("A short is not indexable");
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("A short is not indexable");
}
@Override
public IData memberGet(String name) {
throw new RuntimeException("A short is not indexable");
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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));
}
}
@@ -0,0 +1,191 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
*/
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() {
throw new UnsupportedOperationException("Cannot cast a string to a list");
}
@Override
public Map<String, IData> asMap() {
throw new UnsupportedOperationException("Cannot cast a string to a map");
}
@Override
public byte[] asByteArray() {
throw new RuntimeException("Cannot cast a string to a byte array");
}
@Override
public int[] asIntArray() {
throw new RuntimeException("Cannot cast a string to an int array");
}
@Override
public IData getAt(int i) {
return new DataString(value.substring(i, i + 1));
}
@Override
public void setAt(int i, IData value) {
throw new RuntimeException("Strings are immutable");
}
@Override
public IData memberGet(String name) {
if (name.equals("length")) {
return new DataInt(value.length());
} else {
throw new RuntimeException("no such member: " + name);
}
}
@Override
public void memberSet(String name, IData data) {
throw new RuntimeException("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");
}
}
@@ -0,0 +1,123 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMemberGetter;
import stanhebben.zenscript.annotations.ZenMemberSetter;
import stanhebben.zenscript.annotations.ZenOperator;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minecraft.data.IData")
public interface IData {
@ZenOperator(OperatorType.ADD)
public IData add(IData other);
@ZenOperator(OperatorType.SUB)
public IData sub(IData other);
@ZenOperator(OperatorType.MUL)
public IData mul(IData other);
@ZenOperator(OperatorType.DIV)
public IData div(IData other);
@ZenOperator(OperatorType.MOD)
public IData mod(IData other);
@ZenOperator(OperatorType.AND)
public IData and(IData other);
@ZenOperator(OperatorType.OR)
public IData or(IData other);
@ZenOperator(OperatorType.XOR)
public IData xor(IData other);
@ZenOperator(OperatorType.NEG)
public IData neg();
@ZenOperator(OperatorType.NOT)
public IData not();
@ZenCaster
public boolean asBool();
@ZenCaster
public byte asByte();
@ZenCaster
public short asShort();
@ZenCaster
public int asInt();
@ZenCaster
public long asLong();
@ZenCaster
public float asFloat();
@ZenCaster
public double asDouble();
@ZenCaster
public String asString();
@ZenCaster
public List<IData> asList();
@ZenCaster
public Map<String, IData> asMap();
@ZenCaster
public byte[] asByteArray();
@ZenCaster
public int[] asIntArray();
@ZenOperator(OperatorType.INDEXGET)
public IData getAt(int i);
@ZenOperator(OperatorType.INDEXSET)
public void setAt(int i, IData value);
@ZenMemberGetter
public IData memberGet(String name);
@ZenMemberSetter
public void memberSet(String name, IData data);
@ZenGetter
public int length();
@ZenOperator(OperatorType.CONTAINS)
public boolean contains(IData data);
@ZenOperator(OperatorType.COMPARE)
public int compareTo(IData data);
@ZenOperator(OperatorType.EQUALS)
public boolean equals(IData data);
@ZenGetter
public IData immutable();
@ZenMethod
public IData update(IData data);
public <T> T convert(IDataConverter<T> converter);
}
@@ -0,0 +1,41 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.data;
import java.util.List;
import java.util.Map;
/**
*
* @author Stan
* @param <T> output type
*/
public interface IDataConverter<T> {
public T fromBool(boolean value);
public T fromByte(byte value);
public T fromShort(short value);
public T fromInt(int value);
public T fromLong(long value);
public T fromFloat(float value);
public T fromDouble(double value);
public T fromString(String value);
public T fromList(List<IData> values);
public T fromMap(Map<String, IData> values);
public T fromByteArray(byte[] value);
public T fromIntArray(int[] value);
}
@@ -0,0 +1,121 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMethod;
/**
* Conditions can be used to set requirements for matching items. They can be
* used to require specific NBT tags or damage values, requiring changed items
* or as complement to damage-consuming or stack-consuming recipes.
*
* @author Stan Hebben
*/
@ZenClass("minecraft.item.Condition")
public class Condition {
/**
* Requires the item to be damaged. A damage value of 1 is enough.
*
* @return damaged condition
*/
@ZenGetter
public static IItemCondition damaged() {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getDamage() > 0;
}
};
}
/**
* Requires the item to be damaged with at least a specified amount.
*
* @param minDamage minimum damage
* @return damage condition
*/
@ZenMethod
public static IItemCondition damageAtLeast(final int minDamage) {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getDamage() >= minDamage;
}
};
}
/**
* Requires the item to be damaged with at most a specified amount. An
* undamaged item is accepted.
*
* @param maxDamage maximum damage
* @return damage condition
*/
@ZenMethod
public static IItemCondition damageAtMost(final int maxDamage) {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getDamage() <= maxDamage;
}
};
}
/**
* Requires the item damage value to be between two values.
*
* @param minDamage minimum damage
* @param maxDamage maximum damage
* @return damage condition
*/
@ZenMethod
public static IItemCondition damageBetween(final int minDamage, final int maxDamage) {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getDamage() >= minDamage && stack.getDamage() <= maxDamage;
}
};
}
/**
* Requires the item to contain at least the given tags. It is OK for the
* item to contain more data, as remaining data is ignored.
*
* @param data required data
* @return data condition
*/
@ZenMethod
public static IItemCondition data(final IData data) {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getTag() != null && stack.getTag().contains(data);
}
};
}
/**
* Requires the input stack to contain at least the given number of items.
* Used in combination with stack consuming recipes.
*
* @param amount required stack size
* @return stack size condition
*/
@ZenMethod
public static IItemCondition stack(final int amount) {
return new IItemCondition() {
@Override
public boolean matches(IItemStack stack) {
return stack.getAmount() >= amount;
}
};
}
}
@@ -0,0 +1,121 @@
package minetweaker.minecraft.item;
import java.util.List;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMethod;
import stanhebben.zenscript.annotations.ZenOperator;
/**
* Represents a recipe ingredient. An ingredient can be an item, an ore
* dictionary entry, an item stack, an ore dictionary entry multiplied with
* an amount, or other kinds of ingredients as offered by the API.
*
* There is no guarantee that every mod will respect the conditions or
* transformations, or that every mod will support every kind of ingredient.
*
* @author Stan Hebben
*/
@ZenClass("minecraft.item.IIngredient")
public interface IIngredient {
/**
* Gets the mark of the ingredient. Ingredients can be marked so they can
* be used in crafting functions.
*
* @return ingredient mark
*/
@ZenGetter("mark")
public String getMark();
/**
* Gets the amount. Should return -1 if no amount is available and 1 for
* a single item. Stacks return the stack size.
*
* @return stack size
*/
@ZenGetter("amount")
public int getAmount();
/**
* Gets all possible items for this ingredient. If there is no item list
* (for example, it is the &lt;*&gt; wildcard item) null should be returned.
*
* @return the items for this ingredient, or null
*/
@ZenGetter("items")
public List<IItemStack> getItems();
/**
* Returns a new ingredient with the given stack size.
*
* @param amount new stack size
* @return modified ingredient
*/
@ZenOperator(OperatorType.MUL)
@ZenMethod
public IIngredient amount(int amount);
/**
* Returns a new ingredient with the given transform added to it.
*
* @param transformer transformer to add
* @return modified ingredient
*/
@ZenMethod
public IIngredient transform(IItemTransformer transformer);
/**
* Returns a new ingredient with the given condition added to it.
*
* @param condition condition to add
* @return modified ingredient
*/
@ZenMethod
public IIngredient only(IItemCondition condition);
/**
* Returns a new ingredient marked with the given name.
*
* @param mark mark to apply
* @return modified ingredient
*/
@ZenMethod
public IIngredient marked(String mark);
/**
* Checks if this ingredient matches the given item.
*
* @param item item to check
* @return true if the item matches
*/
@ZenMethod
public boolean matches(IItemStack item);
/**
* Check if this ingredient contains all possible values for the given
* ingredient.
*
* @param ingredient ingredient to check
* @return true if the ingredient contains the given one
*/
@ZenOperator(OperatorType.CONTAINS)
public boolean contains(IIngredient ingredient);
/**
* Applies transformations, if any, to the given item.
*
* @param item item to transform
* @return transformed item
*/
@ZenMethod
public IItemStack applyTransform(IItemStack item);
/**
* Gets the internal item backing this ingredient. The value is
* implementation-dependent.
*
* @return internal item
*/
public Object getInternal();
}
@@ -0,0 +1,18 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import stanhebben.zenscript.annotations.ZenClass;
/**
*
* @author Stan
*/
@ZenClass("minecraft.item.IItemCondition")
public interface IItemCondition {
public boolean matches(IItemStack stack);
}
@@ -0,0 +1,15 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
/**
*
* @author Stan
*/
public interface IItemDefinition {
}
@@ -0,0 +1,56 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import minetweaker.minecraft.data.IData;
import minetweaker.minecraft.liquid.ILiquidStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minecraft.item.IItemStack")
public interface IItemStack extends IIngredient {
@ZenGetter("definition")
public IItemDefinition getDefinition();
@ZenGetter("name")
public String getName();
@ZenGetter("displayName")
public String getDisplayName();
@ZenGetter("damage")
public int getDamage();
@ZenGetter("tag")
public IData getTag();
@ZenGetter("maxDamage")
public int getMaxDamage();
@ZenGetter("liquid")
public ILiquidStack getLiquid();
@ZenMethod
public IIngredient anyDamage();
@ZenMethod
public IItemStack withDamage(int damage);
@ZenMethod
public IItemStack withAmount(int amount);
@ZenMethod
public IItemStack withTag(IData tag);
@ZenMethod
public IItemStack updateTag(IData tagUpdate);
}
@@ -0,0 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
/**
* An item transformer modifies an item stack. Used to apply damage, grab multiple
* items, change NBT tags, replace the item with something else - or whatever
* else you'd want to happen to the input stack.
*
* @author Stan Hebben
*/
@ZenClass("minetweaker.IItemTransformer")
public interface IItemTransformer {
/**
* Transforms the specified item. May modify this item (and return it)
* or return an entirely new one.
*
* @param item input item
* @return output item
*/
@ZenMethod
public IItemStack transform(IItemStack item);
}
@@ -0,0 +1,75 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import java.util.List;
/**
*
* @author Stan
*/
public class IngredientAny implements IIngredient {
public static IngredientAny INSTANCE = new IngredientAny();
public static Object INTERNAL_ANY = null; // platforms supporting an "any" item should fill it here
private IngredientAny() {}
@Override
public String getMark() {
return null;
}
@Override
public int getAmount() {
return -1;
}
@Override
public List<IItemStack> getItems() {
return null;
}
@Override
public IIngredient amount(int amount) {
return new IngredientStack(this, amount);
}
@Override
public IIngredient transform(IItemTransformer transformer) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public IIngredient only(IItemCondition condition) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public IIngredient marked(String mark) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean matches(IItemStack item) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean contains(IIngredient ingredient) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public IItemStack applyTransform(IItemStack item) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Object getInternal() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
@@ -0,0 +1,102 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import java.util.Collections;
import java.util.List;
import minetweaker.minecraft.util.ArrayUtil;
/**
*
* @author Stan
*/
public class IngredientItem implements IIngredient {
private final IItemStack item;
private final String mark;
private final IItemCondition[] conditions;
private final IItemTransformer[] transformers;
private final List<IItemStack> items;
public IngredientItem(IItemStack item, String mark, IItemCondition[] conditions, IItemTransformer[] transformers) {
this.item = item;
this.mark = mark;
this.conditions = conditions;
this.transformers = transformers;
items = Collections.singletonList(item);
}
@Override
public String getMark() {
return mark;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public List<IItemStack> getItems() {
return items;
}
@Override
public IIngredient amount(int amount) {
return new IngredientStack(this, amount);
}
@Override
public IIngredient transform(IItemTransformer transformer) {
return new IngredientItem(item, mark, conditions, ArrayUtil.append(transformers, transformer));
}
@Override
public IIngredient only(IItemCondition condition) {
return new IngredientItem(item, mark, ArrayUtil.append(conditions, condition), transformers);
}
@Override
public IIngredient marked(String mark) {
return new IngredientItem(item, mark, conditions, transformers);
}
@Override
public boolean matches(IItemStack item) {
if (!item.matches(item)) return false;
for (IItemCondition condition : conditions) {
if (!condition.matches(item)) return false;
}
return true;
}
@Override
public boolean contains(IIngredient ingredient) {
List<IItemStack> iitems = ingredient.getItems();
for (IItemStack iitem : iitems) {
if (!matches(iitem)) return false;
}
return true;
}
@Override
public IItemStack applyTransform(IItemStack item) {
for (IItemTransformer transform : transformers) {
item = transform.transform(item);
}
return item;
}
@Override
public Object getInternal() {
return item.getInternal();
}
}
@@ -0,0 +1,78 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import java.util.List;
/**
*
* @author Stan
*/
public class IngredientStack implements IIngredient {
private final IIngredient ingredient;
private final int amount;
public IngredientStack(IIngredient ingredient, int amount) {
this.ingredient = ingredient;
this.amount = amount;
}
@Override
public String getMark() {
return null;
}
@Override
public int getAmount() {
return amount;
}
@Override
public List<IItemStack> getItems() {
return ingredient.getItems();
}
@Override
public IIngredient amount(int amount) {
return new IngredientStack(ingredient, amount);
}
@Override
public IIngredient transform(IItemTransformer transformer) {
return new IngredientStack(ingredient.transform(transformer), amount);
}
@Override
public IIngredient only(IItemCondition condition) {
return new IngredientStack(ingredient.only(condition), amount);
}
@Override
public IIngredient marked(String mark) {
return new IngredientStack(ingredient.marked(mark), amount);
}
@Override
public boolean matches(IItemStack item) {
return item.getAmount() == amount && ingredient.matches(item);
}
@Override
public boolean contains(IIngredient ingredient) {
return this.ingredient.contains(ingredient);
}
@Override
public IItemStack applyTransform(IItemStack item) {
return ingredient.applyTransform(item);
}
@Override
public Object getInternal() {
return null;
}
}
@@ -0,0 +1,114 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.item;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minecraft.item.Transform")
public class Transform {
/**
* Makes the item reusable. Prevents consumption of the item upon crafting.
*
* @return reuse transformer
*/
@ZenMethod
public IItemTransformer reuse() {
return new IItemTransformer() {
@Override
public IItemStack transform(IItemStack item) {
return item.withAmount(item.getAmount() + 1);
}
};
}
/**
* Damages the item. Also makes the item reusable. Will damage the item
* for 1 point upon crafting and consume it when broken.
*
* @return damage transformer
*/
@ZenMethod
public IItemTransformer damage() {
return new IItemTransformer() {
@Override
public IItemStack transform(IItemStack item) {
int newDamage = item.getDamage() + 1;
if (newDamage >= item.getMaxDamage()) {
return item.withAmount(item.getAmount() - 1).withDamage(0);
} else {
return item.withDamage(newDamage);
}
}
};
}
/**
* Damages the item for a specific amount. Also makes the item reusable.
* Upon reaching maximum damage, the item will be consumed. Take care to
* set the proper condition such that an almost-broken item becomes
* invalid for crafting.
*
* @param damage damage to be applied
* @return damage transformer
*/
@ZenMethod
public IItemTransformer damage(final int damage) {
return new IItemTransformer() {
@Override
public IItemStack transform(IItemStack item) {
int newDamage = item.getDamage() + damage;
if (newDamage >= item.getMaxDamage()) {
return item.withAmount(item.getAmount() - 1).withDamage(newDamage - item.getMaxDamage());
} else {
return item.withDamage(newDamage);
}
}
};
}
/**
* Causes the item to be replaced upon crafting. Can be used, for instance,
* to return empty bottles or buckets.
*
* @param withItem replacement item
* @return replacement transformer
*/
@ZenMethod
public IItemTransformer replaceWith(IItemStack withItem) {
final IItemStack result = withItem.withAmount(withItem.getAmount() + 1);
return new IItemTransformer() {
@Override
public IItemStack transform(IItemStack item) {
return result;
}
};
}
/**
* Causes multiple items to be consumed. Take care to set a condition for
* a minimum stack size too, as otherwise smaller stacks would still be
* accepted for input.
*
* @param amount consumption amount
* @return consuming transformer
*/
@ZenMethod
public IItemTransformer consume(final int amount) {
return new IItemTransformer() {
@Override
public IItemStack transform(IItemStack item) {
return item.withAmount(Math.max(item.getAmount() - amount, 0) + 1);
}
};
}
}
@@ -0,0 +1,53 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.liquid;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenOperator;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.annotations.ZenSetter;
/**
*
* @author Stanneke
*/
@ZenClass("minecraft.liquid.ILiquidDefinition")
public interface ILiquidDefinition {
@ZenOperator(OperatorType.MUL)
public ILiquidStack asStack(int millibuckets);
@ZenGetter("luminosity")
public int getLuminosity();
@ZenSetter("luminosity")
public void setLuminosity(int value);
@ZenGetter("density")
public int getDensity();
@ZenSetter("density")
public void setDensity(int density);
@ZenGetter("temperature")
public int getTemperature();
@ZenSetter("temperature")
public void setTemperature(int temperature);
@ZenGetter("viscosity")
public int getViscosity();
@ZenSetter("viscosity")
public void setViscosity(int viscosity);
@ZenGetter("gaseous")
public boolean isGaseous();
@ZenSetter("gaseous")
public void setGaseous(boolean gaseous);
}
@@ -0,0 +1,51 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.liquid;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stanneke
*/
@ZenClass("minecraft.liquid.ILiquidStack")
public interface ILiquidStack {
@ZenGetter("definition")
public ILiquidDefinition getDefinition();
@ZenGetter("amount")
public int getAmount();
@ZenGetter("luminosity")
public int getLuminosity();
@ZenGetter("density")
public int getDensity();
@ZenGetter("temperature")
public int getTemperature();
@ZenGetter("viscosity")
public int getViscosity();
@ZenGetter("gaseous")
public boolean isGaseous();
@ZenGetter("tag")
public IData getTag();
@ZenMethod
public ILiquidStack withTag(IData data);
@ZenMethod
public ILiquidStack withAmount(int amount);
public Object getInternal();
}
@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.oredict;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMemberGetter;
/**
*
* @author Stan
*/
@ZenClass("minecraft.oredict.IOreDict")
public interface IOreDict {
@ZenMemberGetter
public IOreDictEntry get(String name);
}
@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.oredict;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minecraft.oreDict.IOreDictEntry")
public interface IOreDictEntry extends IIngredient {
@ZenMethod
public void add(IItemStack item);
@ZenMethod
public void addAll(IOreDictEntry entry);
@ZenMethod
public void remove(IItemStack item);
@ZenMethod
public boolean contains(IItemStack item);
@ZenMethod
public void mirror(IOreDictEntry other);
}
@@ -0,0 +1,102 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.oredict;
import java.util.List;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemCondition;
import minetweaker.minecraft.item.IItemStack;
import minetweaker.minecraft.item.IItemTransformer;
import minetweaker.minecraft.item.IngredientStack;
import minetweaker.minecraft.util.ArrayUtil;
/**
*
* @author Stan Hebben
*/
public class IngredientOreDict implements IIngredient {
private final IOreDictEntry entry;
private final String mark;
private final IItemCondition[] conditions;
private final IItemTransformer[] transformers;
public IngredientOreDict(
IOreDictEntry entry,
String mark,
IItemCondition[] conditions,
IItemTransformer[] transformers) {
this.entry = entry;
this.mark = mark;
this.conditions = conditions;
this.transformers = transformers;
}
@Override
public String getMark() {
return mark;
}
@Override
public int getAmount() {
return 1;
}
@Override
public List<IItemStack> getItems() {
return entry.getItems();
}
@Override
public IIngredient amount(int amount) {
return new IngredientStack(this, amount);
}
@Override
public IIngredient transform(IItemTransformer transformer) {
return new IngredientOreDict(entry, mark, conditions, ArrayUtil.append(transformers, transformer));
}
@Override
public IIngredient only(IItemCondition condition) {
return new IngredientOreDict(entry, mark, ArrayUtil.append(conditions, condition), transformers);
}
@Override
public IIngredient marked(String mark) {
return new IngredientOreDict(entry, mark, conditions, transformers);
}
@Override
public boolean matches(IItemStack item) {
if (!entry.matches(item)) return false;
for (IItemCondition condition : conditions) {
if (!condition.matches(item)) return false;
}
return true;
}
@Override
public boolean contains(IIngredient ingredient) {
return entry.contains(ingredient);
}
@Override
public IItemStack applyTransform(IItemStack item) {
for (IItemTransformer transformer : transformers) {
item = transformer.transform(item);
}
return item;
}
@Override
public Object getInternal() {
return entry.getInternal();
}
}
@@ -0,0 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.recipes;
import minetweaker.minecraft.item.IItemStack;
/**
*
* @author Stan
*/
public interface ICraftingInventory {
public int getSize();
public int getWidth();
public int getHeight();
public IItemStack getStack(int i);
public IItemStack getStack(int x, int y);
public void setStack(int x, int y, IItemStack stack);
public void setStack(int i, IItemStack stack);
}
@@ -0,0 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.recipes;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemStack;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
public interface IFurnaceManager {
@ZenMethod
public void remove(IIngredient ingredient);
@ZenMethod
public void addRecipe(IIngredient output, IIngredient input, @Optional double xp);
@ZenMethod
public void setFuel(IIngredient item, int fuel);
@ZenMethod
public int getFuel(IItemStack item);
}
@@ -0,0 +1,18 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.recipes;
import java.util.Map;
import minetweaker.minecraft.item.IItemStack;
/**
*
* @author Stan
*/
public interface IRecipeFunction {
public IItemStack process(IItemStack output, Map<String, IItemStack> inputs);
}
@@ -0,0 +1,89 @@
package minetweaker.minecraft.recipes;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenMethod;
/**
* The RecipeManager adds and removes crafting recipes. The IRecipeManager
* instance is available throug the recipes global variable.
*
* @author Stan Hebben
*/
@ZenClass("minetweaker.recipes.IRecipeManager")
public interface IRecipeManager {
/**
* Removes all crafting recipes crafting the specified item.
*
* @param output recipe output pattern
* @return number of removed recipes
*/
@ZenMethod
public int remove(IIngredient output);
/**
* Adds a shaped recipe.
*
* @param output recipe output
* @param ingredients recipe ingredients
* @param function recipe function (optional)
* @param mirrored indicates if the mirrored recipe should be valid too (optional)
*/
@ZenMethod
public void addShaped(
IItemStack output,
IIngredient[][] ingredients,
@Optional IRecipeFunction function,
@Optional boolean mirrored);
/**
* Adds a shapeless recipe.
*
* @param output recipe output
* @param ingredients recipe ingredients
* @param function recipe function (optional)
*/
@ZenMethod
public void addShapeless(
IItemStack output,
IIngredient[] ingredients,
@Optional IRecipeFunction function);
/**
* Removes shaped recipes.
*
* @param output recipe output
* @param ingredients recipe ingredients
* @return number of removed recipes
*/
@ZenMethod
public int removeShaped(
IIngredient output,
@Optional IIngredient[][] ingredients);
/**
* Removes shapeless recipes.
*
* @param output recipe output
* @param ingredients recipe ingredients
* @param wildcard if the recipe may contain other ingredients too, besides the ones specified
* @return number of removed recipes
*/
@ZenMethod
public int removeShapeless(
IIngredient output,
@Optional IIngredient[] ingredients,
@Optional boolean wildcard);
/**
* Performs a crafting with the specified ingredients. Returns null if no
* crafting recipe exists.
*
* @param contents crafting inventory contents
* @return crafting result, or null
*/
@ZenMethod
public IItemStack craft(IItemStack[][] contents);
}
@@ -0,0 +1,217 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.recipes;
import java.util.HashMap;
import java.util.Map;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemStack;
/**
*
* @author Stan
*/
public class ShapedRecipe {
private final int width;
private final int height;
private final byte[] posx;
private final byte[] posy;
private final boolean mirrored;
private final IRecipeFunction function;
private final IItemStack output;
private final IIngredient[] ingredients;
public ShapedRecipe(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, boolean mirrored) {
int numIngredients = 0;
for (IIngredient[] row : ingredients) {
for (IIngredient ingredient : row) {
if (ingredient != null) {
numIngredients++;
}
}
}
this.posx = new byte[numIngredients];
this.posy = new byte[numIngredients];
this.output = output;
this.ingredients = new IIngredient[numIngredients];
this.function = function;
int width1 = 0;
int height1 = ingredients.length;
int ix = 0;
for (int j = 0; j < ingredients.length; j++) {
IIngredient[] row = ingredients[j];
width1 = Math.max(width1, row.length);
for (int i = 0; i < row.length; i++) {
if (row[i] != null) {
this.posx[ix] = (byte) i;
this.posy[ix] = (byte) j;
this.ingredients[ix] = row[i];
ix++;
}
}
}
this.width = width1;
this.height = height1;
this.mirrored = mirrored;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean isMirrored() {
return mirrored;
}
public IIngredient[] getIngredients() {
return ingredients;
}
public byte[] getIngredientsX() {
return posx;
}
public byte[] getIngredientsY() {
return posy;
}
public IItemStack getOutput() {
return output;
}
public boolean matches(ICraftingInventory inventory) {
int numItems = 0;
for (int i = 0; i < inventory.getSize(); i++) {
if (inventory.getStack(i) != null) {
numItems++;
}
}
if (numItems != ingredients.length) {
return false;
}
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
for (int k = 0; k < ingredients.length; k++) {
IItemStack item = inventory.getStack(posx[k] + i, posy[k] + j);
if (item == null) continue out;
if (!ingredients[k].matches(item)) continue out;
}
return true;
}
}
if (mirrored) {
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
for (int k = 0; k < ingredients.length; k++) {
IItemStack item = inventory.getStack(inventory.getWidth() - (posx[k] + i), posy[k] + j);
if (item == null) continue out;
if (!ingredients[k].matches(item)) continue out;
}
return true;
}
}
}
return false;
}
public IItemStack getCraftingResult(ICraftingInventory inventory) {
IItemStack[] stacks = new IItemStack[ingredients.length];
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
for (int k = 0; k < ingredients.length; k++) {
IItemStack item = inventory.getStack(posx[k] + i, posy[k] + j);
if (item == null) continue out;
if (!ingredients[k].matches(item)) continue out;
stacks[k] = item;
}
return doRecipe(inventory, stacks, i, j, output, false);
}
}
if (mirrored) {
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
for (int k = 0; k < ingredients.length; k++) {
IItemStack item = inventory.getStack(inventory.getWidth() - (posx[k] + i), posy[k] + j);
if (item == null) continue out;
if (!ingredients[k].matches(item)) continue out;
stacks[k] = item;
}
return doRecipe(inventory, stacks, i, j, output, true);
}
}
}
return null;
}
private IItemStack doRecipe(
ICraftingInventory inventory,
IItemStack[] stacks,
int offx, int offy,
IItemStack output,
boolean mirrored) {
// determine output and apply transformations
if (function != null) {
Map<String, IItemStack> tagged = new HashMap<String, IItemStack>();
for (int k = 0; k < ingredients.length; k++) {
if (ingredients[k].getMark() != null) {
tagged.put(ingredients[k].getMark(), stacks[k]);
}
}
output = function.process(output, tagged);
}
if (output == null) {
return null;
}
for (int i = 0; i < ingredients.length; i++) {
IItemStack transformed = ingredients[i].applyTransform(stacks[i]);
if (transformed != stacks[i]) {
if (mirrored) {
inventory.setStack(
inventory.getWidth() - (offx + posx[i]),
offy + posy[i],
transformed);
} else {
inventory.setStack(
offx + posx[i],
offy + posy[i],
transformed);
}
}
}
return output;
}
}
@@ -0,0 +1,128 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.recipes;
import java.util.HashMap;
import java.util.Map;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemStack;
/**
*
* @author Stan
*/
public class ShapelessRecipe {
private final IItemStack output;
private final IRecipeFunction function;
private final IIngredient[] ingredients;
public ShapelessRecipe(IItemStack output, IIngredient[] ingredients, IRecipeFunction function) {
this.output = output;
this.function = function;
this.ingredients = ingredients;
}
public int getSize() {
return ingredients.length;
}
public IIngredient[] getIngredients() {
return ingredients;
}
public IItemStack getOutput() {
return output;
}
public boolean matches(ICraftingInventory inventory) {
return matchShapeless(ingredients, inventory) != null;
}
public IItemStack getCraftingResult(ICraftingInventory inventory) {
RecipeMatching matching = matchShapeless(ingredients, inventory);
IItemStack actualOutput = output;
if (function != null) {
Map<String, IItemStack> map = new HashMap<String, IItemStack>();
for (int i = 0; i < ingredients.length; i++) {
if (ingredients[i].getMark() != null) {
map.put(ingredients[i].getMark(), matching.inputs[i]);
}
}
actualOutput = function.process(actualOutput, map);
}
if (actualOutput == null) {
return null;
}
for (int i = 0; i < ingredients.length; i++) {
IIngredient ingredient = ingredients[i];
IItemStack transformed = ingredient.applyTransform(matching.inputs[i]);
if (transformed != matching.inputs[i]) {
inventory.setStack(matching.indices[i], transformed);
}
}
return actualOutput;
}
/**
* Reorders ingredients to match the given shapeless recipe.
*
* @param recipe shapeless recipe
* @param ingredients crafting input
* @return reordered inputs, or null if no match was found
*/
private static RecipeMatching matchShapeless(
IIngredient[] recipe,
ICraftingInventory ingredients) {
int numItems = 0;
for (int i = 0; i < ingredients.getSize(); i++) {
if (ingredients.getStack(i) != null) {
numItems++;
}
}
if (numItems != recipe.length) {
return null;
}
IItemStack[] matched = new IItemStack[recipe.length];
int[] indices = new int[recipe.length];
outer: for (int i = 0; i < ingredients.getSize(); i++) {
IItemStack ingredient = ingredients.getStack(i);
if (ingredient == null) continue;
for (int j = 0; j < recipe.length; j++) {
if (matched[j] != null) continue;
if (recipe[j].matches(ingredient)) {
matched[j] = ingredient;
indices[j] = i;
continue outer;
}
}
return null;
}
return new RecipeMatching(matched, indices);
}
private static class RecipeMatching {
public final IItemStack[] inputs;
public final int[] indices;
public RecipeMatching(IItemStack[] inputs, int[] indices) {
this.inputs = inputs;
this.indices = indices;
}
}
}
@@ -0,0 +1,32 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package minetweaker.minecraft.util;
import java.util.Arrays;
import minetweaker.minecraft.item.IItemCondition;
import minetweaker.minecraft.item.IItemTransformer;
/**
*
* @author Stan
*/
public class ArrayUtil {
public static final IItemCondition[] EMPTY_CONDITIONS = new IItemCondition[0];
public static final IItemTransformer[] EMPTY_TRANSFORMERS = new IItemTransformer[0];
public static IItemCondition[] append(IItemCondition[] values, IItemCondition value) {
IItemCondition[] result = Arrays.copyOf(values, values.length + 1);
result[values.length] = value;
return result;
}
public static IItemTransformer[] append(IItemTransformer[] values, IItemTransformer value) {
IItemTransformer[] result = Arrays.copyOf(values, values.length + 1);
result[values.length] = value;
return result;
}
}