- Continued writing on the IC2 integration code

- Implemented advanced recipes on <*> values
- Removed unused ZenScript classes
- Continued work on any values
- Moved documentation generation to buildSrc
- Started work on a jar merging task
- Added toString implementation to ingredients
- Added stack count to ICraftingInventory
- Fixed imports
- Added logCommand to the logger to log command output
- Implemented memory script provider to load scripts received from the server
- Added exception handling for script execution
- First attempt at creating a transformer to generate a list of ZenClass and ZenExpansion classes
- Added console commands (reload, names, liquids, inventory, hand, oredict)
- Added general logic
- Fixed item ingredients for wildcard sizes
- Implemented selective recipe removal
- Fixed null recipe results
- Started implementation of player-specific recipes
- Removed redundant IC2 mod copy
- Other minor fixes
This commit is contained in:
Stan Hebben committed 2014-06-20 14:13:55 +02:00
1 parent f3ea7f7831
commit 489347558f
124 files changed
+3942 -419

No files matched your search

@@ -1,6 +1,6 @@
package minetweaker;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import minetweaker.runtime.IMineTweaker;
import minetweaker.runtime.ILogger;
import minetweaker.runtime.Tweaker;
@@ -11,6 +11,10 @@ import minetweaker.runtime.GlobalRegistry;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenExpansion;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaStaticField;
import stanhebben.zenscript.symbols.SymbolJavaStaticGetter;
import stanhebben.zenscript.symbols.SymbolJavaStaticMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
/**
* Provides access to the MineTweaker API.
@@ -67,12 +71,12 @@ public class MineTweakerAPI {
* @param annotatedClass
*/
public static void registerClass(Class annotatedClass) {
for (Annotation annotation : annotatedClass.getAnnotations()) {
if (annotation instanceof ZenExpansion) {
GlobalRegistry.registerExpansion(annotatedClass);
} else if (annotation instanceof ZenClass) {
GlobalRegistry.registerNativeClass(annotatedClass);
}
if (annotatedClass.isAnnotationPresent(ZenExpansion.class)) {
GlobalRegistry.registerExpansion(annotatedClass);
}
if (annotatedClass.isAnnotationPresent(ZenClass.class)) {
GlobalRegistry.registerNativeClass(annotatedClass);
}
}
@@ -107,4 +111,63 @@ public class MineTweakerAPI {
public static void registerBracketHandler(IBracketHandler handler) {
GlobalRegistry.registerBracketHandler(handler);
}
/**
* Creates a symbol that refers to a java method.
*
* @param cls class that contains the method
* @param name method name
* @param arguments method argument types
* @return corresponding symbol
*/
public static IZenSymbol getJavaStaticMethodSymbol(Class cls, String name, Class... arguments) {
JavaMethod method = JavaMethod.get(GlobalRegistry.getTypeRegistry(), cls, name, arguments);
return new SymbolJavaStaticMethod(method);
}
/**
* Creates a symbol that refers to a java getter. The getter must be a method
* with no arguments. The given symbol will act as a variable of which the
* value can be retrieved but not set.
*
* @param cls class that contains the getter method
* @param name name of the method
* @return corresponding symbol
*/
public static IZenSymbol getJavaStaticGetterSymbol(Class cls, String name) {
JavaMethod method = JavaMethod.get(GlobalRegistry.getTypeRegistry(), cls, name);
return new SymbolJavaStaticGetter(method);
}
/**
* Creates a symbol that refers to a static field. The field must be an
* existing public field in the given class. The field will act as a
* variable that can be retrieved but not set.
*
* @param cls class that contains the field
* @param name field name (must be public)
* @return corresponding symbol
*/
public static IZenSymbol getJavaStaticFieldSymbol(Class cls, String name) {
try {
Field field = cls.getField(name);
return new SymbolJavaStaticField(cls, field, GlobalRegistry.getTypeRegistry());
} catch (NoSuchFieldException ex) {
return null;
} catch (SecurityException ex) {
return null;
}
}
/**
* Loads a Java method from an existing class.
*
* @param cls method class
* @param name method name
* @param arguments argument types
* @return java method
*/
public static JavaMethod getJavaMethod(Class cls, String name, Class... arguments) {
return JavaMethod.get(GlobalRegistry.getTypeRegistry(), cls, name, arguments);
}
}
@@ -0,0 +1,18 @@
package minetweaker.annotations;
/**
* Marks a bracket handler. Bracket handlers are automatically registered.
* The marked class should have an empty constructor.
*
* @author Stan Hebben
*/
public @interface BracketHandler {
/**
* Indicates priority. A lower value means a higher priority. Only change
* if you have issues, default value is 10. Internal minetweaker handlers
* have priorities up to 100.
*
* @return
*/
public int priority() default 10;
}
@@ -8,9 +8,11 @@ package minetweaker.minecraft.item;
import minetweaker.minecraft.data.IData;
import minetweaker.minecraft.liquid.ILiquidStack;
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;
/**
*
@@ -39,6 +41,10 @@ public interface IItemStack extends IIngredient {
@ZenGetter("liquid")
public ILiquidStack getLiquid();
@ZenOperator(OperatorType.MUL)
@ZenMethod
public IItemStack amount(int amount);
@ZenMethod
public IIngredient anyDamage();
@@ -7,13 +7,15 @@
package minetweaker.minecraft.item;
import java.util.List;
import minetweaker.minecraft.util.ArrayUtil;
/**
*
* @author Stan
*/
public class IngredientAny implements IIngredient {
public static IngredientAny INSTANCE = new IngredientAny();
public static final IngredientAny INSTANCE = new IngredientAny();
public static Object INTERNAL_ANY = null; // platforms supporting an "any" item should fill it here
private IngredientAny() {}
@@ -40,36 +42,36 @@ public class IngredientAny implements IIngredient {
@Override
public IIngredient transform(IItemTransformer transformer) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return new IngredientAnyAdvanced(null, ArrayUtil.EMPTY_CONDITIONS, new IItemTransformer[] { transformer });
}
@Override
public IIngredient only(IItemCondition condition) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return new IngredientAnyAdvanced(null, new IItemCondition[] { condition }, ArrayUtil.EMPTY_TRANSFORMERS);
}
@Override
public IIngredient marked(String mark) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return new IngredientAnyAdvanced(mark, ArrayUtil.EMPTY_CONDITIONS, ArrayUtil.EMPTY_TRANSFORMERS);
}
@Override
public boolean matches(IItemStack item) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return true;
}
@Override
public boolean contains(IIngredient ingredient) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return true;
}
@Override
public IItemStack applyTransform(IItemStack item) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return item;
}
@Override
public Object getInternal() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return INTERNAL_ANY;
}
}
@@ -0,0 +1,96 @@
/*
* 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;
import minetweaker.minecraft.util.ArrayUtil;
/**
*
* @author Stan
*/
public class IngredientAnyAdvanced implements IIngredient {
private final String mark;
private final IItemCondition[] conditions;
private final IItemTransformer[] transformers;
public IngredientAnyAdvanced(String mark, IItemCondition[] conditions, IItemTransformer[] transformers) {
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 null;
}
@Override
public IIngredient amount(int amount) {
return new IngredientStack(this, amount);
}
@Override
public IIngredient transform(IItemTransformer transformer) {
return new IngredientAnyAdvanced(mark, conditions, ArrayUtil.append(transformers, transformer));
}
@Override
public IIngredient only(IItemCondition condition) {
return new IngredientAnyAdvanced(mark, ArrayUtil.append(conditions, condition), transformers);
}
@Override
public IIngredient marked(String mark) {
return new IngredientAnyAdvanced(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 IngredientAny.INTERNAL_ANY;
}
}
@@ -99,4 +99,13 @@ public class IngredientItem implements IIngredient {
public Object getInternal() {
return item.getInternal();
}
// #############################
// ### Object implementation ###
// #############################
@Override
public String toString() {
return "(Ingredient) " + item.toString();
}
}
@@ -21,6 +21,12 @@ public interface ILiquidDefinition {
@ZenOperator(OperatorType.MUL)
public ILiquidStack asStack(int millibuckets);
@ZenGetter("name")
public String getName();
@ZenGetter("displayName")
public String getDisplayName();
@ZenGetter("luminosity")
public int getLuminosity();
@@ -20,6 +20,12 @@ public interface ILiquidStack {
@ZenGetter("definition")
public ILiquidDefinition getDefinition();
@ZenGetter("name")
public String getName();
@ZenGetter("displayName")
public String getDisplayName();
@ZenGetter("amount")
public int getAmount();
@@ -0,0 +1,28 @@
/*
* 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.player;
import minetweaker.minecraft.data.IData;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenMethod;
/**
*
* @author Stan
*/
@ZenClass("minecraft.player.IPlayer")
public interface IPlayer {
@ZenGetter("name")
public String getName();
@ZenGetter("data")
public IData getData();
@ZenMethod
public void update(IData data);
}
@@ -26,6 +26,9 @@ public interface ICraftingInventory {
@ZenGetter("height")
public int getHeight();
@ZenGetter("stackCount")
public int getStackCount();
@ZenMethod
public IItemStack getStack(int i);
@@ -63,6 +63,12 @@ public class ShapedRecipe {
this.width = width1;
this.height = height1;
this.mirrored = mirrored;
System.out.println("ShapedRecipe width=" + width + ", height=" + height);
System.out.println("Ingredients: ");
for (int i = 0; i < ingredients.length; i++) {
System.out.println("(" + posx[i] + ", " + posy[i] + "): " + this.ingredients[i]);
}
}
public int getWidth() {
@@ -94,19 +100,12 @@ public class ShapedRecipe {
}
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) {
if (inventory.getStackCount() != 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 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;
@@ -119,8 +118,8 @@ public class ShapedRecipe {
}
if (mirrored) {
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
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;
@@ -139,8 +138,8 @@ public class ShapedRecipe {
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 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;
@@ -154,8 +153,8 @@ public class ShapedRecipe {
}
if (mirrored) {
for (int i = 0; i < inventory.getWidth() - width; i++) {
out: for (int j = 0; j < inventory.getHeight() - height; j++) {
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;
@@ -18,7 +18,21 @@ import java.util.logging.Logger;
import minetweaker.IBracketHandler;
import minetweaker.IRecipeRemover;
import minetweaker.MineTweakerAPI;
import minetweaker.minecraft.data.IData;
import minetweaker.minecraft.item.Condition;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.item.IItemCondition;
import minetweaker.minecraft.item.IItemDefinition;
import minetweaker.minecraft.item.IItemStack;
import minetweaker.minecraft.item.IItemTransformer;
import minetweaker.minecraft.item.Transform;
import minetweaker.minecraft.liquid.ILiquidDefinition;
import minetweaker.minecraft.liquid.ILiquidStack;
import minetweaker.minecraft.oredict.IOreDict;
import minetweaker.minecraft.oredict.IOreDictEntry;
import minetweaker.minecraft.recipes.ICraftingInventory;
import minetweaker.minecraft.recipes.IFurnaceManager;
import minetweaker.minecraft.recipes.IRecipeFunction;
import stanhebben.zenscript.IZenErrorLogger;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.annotations.ZenExpansion;
@@ -27,11 +41,12 @@ import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.TypeRegistry;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.symbols.IZenCompileEnvironment;
import stanhebben.zenscript.IZenCompileEnvironment;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaStaticField;
import stanhebben.zenscript.symbols.SymbolJavaStaticMethod;
import stanhebben.zenscript.symbols.SymbolPackage;
import stanhebben.zenscript.symbols.SymbolType;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeNative;
import stanhebben.zenscript.type.natives.JavaMethod;
@@ -46,7 +61,7 @@ public class GlobalRegistry {
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 SymbolPackage root = new SymbolPackage("<root>");
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>();
@@ -73,7 +88,7 @@ public class GlobalRegistry {
registerExpansion(ExpandShort.class);
registerExpansion(ExpandString.class);*/
/*registerNativeClass(IData.class);
registerNativeClass(IData.class);
registerNativeClass(IIngredient.class);
registerNativeClass(IItemCondition.class);
@@ -91,7 +106,7 @@ public class GlobalRegistry {
registerNativeClass(ICraftingInventory.class);
registerNativeClass(IFurnaceManager.class);
registerNativeClass(IRecipeFunction.class);*/
registerNativeClass(IRecipeFunction.class);
}
private GlobalRegistry() {}
@@ -126,7 +141,7 @@ public class GlobalRegistry {
ZenTypeNative type = new ZenTypeNative(cls);
type.complete(types);
root.put(type.getName(), root, errors);
root.put(type.getName(), new SymbolType(type), errors);
}
public static TypeRegistry getTypeRegistry() {
@@ -285,12 +300,12 @@ public class GlobalRegistry {
@Override
public void error(ZenPosition position, String message) {
MineTweakerAPI.logger.logError("[" + position.getFile() + ":" + position.getLine() + "] Error: " + message);
MineTweakerAPI.logger.logError(position.toString() + " > " + message);
}
@Override
public void warning(ZenPosition position, String message) {
MineTweakerAPI.logger.logWarning("[" + position.getFile() + ":" + position.getLine() + "] Warning: " + message);
MineTweakerAPI.logger.logWarning(position.toString() + " > " + message);
}
}
}
@@ -13,6 +13,9 @@ import stanhebben.zenscript.annotations.ZenMethod;
* @author Stan
*/
public interface ILogger {
@ZenMethod
public void logCommand(String message);
@ZenMethod
public void logInfo(String message);
@@ -45,12 +45,22 @@ public interface IMineTweaker {
public List<IUndoableAction> rollback();
/**
* Registers a script provider.
* Sets the script provider.
*
* @param provider
* @return
*/
public Closeable registerScriptProvider(IScriptProvider provider);
public void setScriptProvider(IScriptProvider provider);
/**
* Executes all scripts provided by the script provider.
*/
public void load();
/**
* Retrieves the data from the scripts that were loaded last.
*
* @return scripts data
*/
public byte[] getScriptData();
}
@@ -7,7 +7,6 @@
package minetweaker.runtime;
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
@@ -21,13 +20,14 @@ import java.util.Set;
import minetweaker.IUndoableAction;
import minetweaker.MineTweakerAPI;
import minetweaker.minecraft.item.IIngredient;
import minetweaker.minecraft.util.CloseablePriorityList;
import minetweaker.runtime.providers.ScriptProviderMemory;
import stanhebben.zenscript.ZenModule;
import static stanhebben.zenscript.ZenModule.compileScripts;
import static stanhebben.zenscript.ZenModule.extractClassName;
import stanhebben.zenscript.ZenParsedFile;
import stanhebben.zenscript.ZenTokener;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.parser.ParseException;
/**
*
@@ -35,14 +35,17 @@ import stanhebben.zenscript.compiler.IEnvironmentGlobal;
* @author Stan Hebben
*/
public class Tweaker implements IMineTweaker {
private final CloseablePriorityList<IScriptProvider> scriptProviders = new CloseablePriorityList<IScriptProvider>();
private final List<IUndoableAction> actions = new ArrayList<IUndoableAction>();
private final Set<IUndoableAction> wereStuck = new HashSet<IUndoableAction>();
private IScriptProvider scriptProvider;
private byte[] scriptData;
@Override
public void apply(IUndoableAction action) {
MineTweakerAPI.logger.logInfo(action.describe());
action.apply();
actions.add(action);
}
@Override
@@ -68,49 +71,65 @@ public class Tweaker implements IMineTweaker {
}
@Override
public Closeable registerScriptProvider(IScriptProvider provider) {
return scriptProviders.add(provider);
public void setScriptProvider(IScriptProvider provider) {
scriptProvider = provider;
}
@Override
public void load() {
scriptData = ScriptProviderMemory.collect(scriptProvider);
Set<String> executed = new HashSet<String>();
for (IScriptProvider provider : scriptProviders) {
Iterator<IScriptIterator> scripts = provider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
Map<String, byte[]> classes = new HashMap<String, byte[]>();
IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
List<ZenParsedFile> files = new ArrayList<ZenParsedFile>();
while (script.next()) {
try {
String filename = script.getName();
String className = extractClassName(filename);
Reader reader = new InputStreamReader(new BufferedInputStream(script.open()));
ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment());
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
files.add(pfile);
reader.close();
} catch (IOException ex) {
MineTweakerAPI.logger.logError("Could not load script " + script.getName() + ": " + ex.getMessage());
}
Iterator<IScriptIterator> scripts = scriptProvider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
Map<String, byte[]> classes = new HashMap<String, byte[]>();
IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
List<ZenParsedFile> files = new ArrayList<ZenParsedFile>();
while (script.next()) {
Reader reader = null;
try {
reader = new InputStreamReader(new BufferedInputStream(script.open()));
String filename = script.getName();
String className = extractClassName(filename);
ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment());
ZenParsedFile pfile = new ZenParsedFile(filename, className, parser, environmentGlobal);
files.add(pfile);
} catch (IOException ex) {
MineTweakerAPI.logger.logError("Could not load script " + script.getName() + ": " + ex.getMessage());
} catch (ParseException ex) {
MineTweakerAPI.logger.logError("Error parsing " + ex.getFile() + ":" + ex.getLine() + " -- " + ex.getExplanation());
} catch (Exception ex) {
MineTweakerAPI.logger.logError("Error loading " + script.getName() + ": " + ex.getMessage());
ex.printStackTrace();
}
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {}
}
}
try {
String filename = script.getGroupName();
System.out.println("MineTweaker: Loading " + filename);
compileScripts(filename, files, environmentGlobal);
// execute scripts
ZenModule module = new ZenModule(classes);
ZenModule module = new ZenModule(classes, MineTweakerAPI.class.getClassLoader());
module.getMain().run();
} catch (Exception ex) {
MineTweakerAPI.logger.logError("Error executing " + script.getGroupName() + ": " + ex.getMessage());
ex.printStackTrace();
}
}
}
@@ -122,4 +141,9 @@ public class Tweaker implements IMineTweaker {
}
}
}
@Override
public byte[] getScriptData() {
return scriptData;
}
}
@@ -0,0 +1,74 @@
/*
* 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.runtime.providers;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import minetweaker.runtime.IScriptIterator;
import minetweaker.runtime.IScriptProvider;
/**
*
* @author Stan Hebben
*/
public class ScriptProviderCascade implements IScriptProvider {
private final IScriptProvider[] providers;
public ScriptProviderCascade(IScriptProvider... providers) {
this.providers = providers;
}
@Override
public Iterator<IScriptIterator> getScripts() {
return new MyScripterator();
}
private class MyScripterator implements Iterator<IScriptIterator> {
private final Set<String> executed = new HashSet<String>();
private int currentIndex = providers.length - 1;
private Iterator<IScriptIterator> current;
private IScriptIterator currentValue;
public MyScripterator() {
currentIndex = providers.length - 1;
current = providers[currentIndex].getScripts();
advance();
}
@Override
public boolean hasNext() {
return currentIndex >= 0;
}
@Override
public IScriptIterator next() {
IScriptIterator result = currentValue;
executed.add(result.getGroupName());
advance();
return result;
}
private void advance() {
do {
while (!current.hasNext()) {
currentIndex--;
if (currentIndex < 0) return;
current = providers[currentIndex].getScripts();
}
if (currentIndex < 0) break;
currentValue = current.next();
} while (executed.contains(currentValue.getGroupName()) && hasNext());
}
}
}
@@ -31,33 +31,31 @@ import minetweaker.runtime.IScriptProvider;
* @author Stan
*/
public class ScriptProviderMemory implements IScriptProvider {
public static byte[] collect(Iterable<IScriptProvider> providers) {
public static byte[] collect(IScriptProvider provider) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
DeflaterOutputStream deflater = new DeflaterOutputStream(output);
DataOutputStream deflaterData = new DataOutputStream(deflater);
Set<String> executed = new HashSet<String>();
for (IScriptProvider provider : providers) {
Iterator<IScriptIterator> scripts = provider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
deflaterData.writeUTF(script.getGroupName());
while (script.next()) {
String name = script.getName();
byte[] data = FileUtil.read(script.open());
deflaterData.writeUTF(name);
deflaterData.writeInt(data.length);
deflaterData.write(data);
}
deflaterData.writeUTF("");
Iterator<IScriptIterator> scripts = provider.getScripts();
while (scripts.hasNext()) {
IScriptIterator script = scripts.next();
if (!executed.contains(script.getGroupName())) {
executed.add(script.getGroupName());
deflaterData.writeUTF(script.getGroupName());
while (script.next()) {
String name = script.getName();
byte[] data = FileUtil.read(script.open());
deflaterData.writeUTF(name);
deflaterData.writeInt(data.length);
deflaterData.write(data);
}
deflaterData.writeUTF("");
}
}