Skip to content

Commit

Permalink
Arguments iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Techlone committed Mar 7, 2016
1 parent e106b2a commit 4bf022e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
74 changes: 55 additions & 19 deletions src/main/java/gttweaker/mods/gregtech/AddMultipleRecipeAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import minetweaker.api.liquid.ILiquidStack;
import minetweaker.api.minecraft.MineTweakerMC;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import java.util.*;

Expand Down Expand Up @@ -43,21 +44,7 @@ private static List<List<Object>> fullCopy(List<List<Object>> recipesData) {

private String description;
private List<List<Object>> recipesData;

protected AddMultipleRecipeAction(String description, Object... recipeArgs) {
this.description = description;

recipesData = new ArrayList<List<Object>>(recipeArgs.length);
recipesData.add(new ArrayList<Object>());

try {
for (Object recipeArg : recipeArgs) {
addArgument(recipeArg);
}
} catch (Exception e) {
MineTweakerAPI.logError(e.toString());
}
}
private int aid; // argument index, equate to zero before applySingleRecipe call

private void addArgument(Object recipeArg) {
if (recipeArg instanceof ILiquidStack) {
Expand Down Expand Up @@ -97,12 +84,63 @@ private List<ItemStack> getItemStacks(IIngredient ingredientArg) {
return itemStackList;
}

protected abstract void applySingleRecipe(Object[] args);
protected AddMultipleRecipeAction(String description, Object... recipeArgs) {
this.description = description;

recipesData = new ArrayList<List<Object>>(recipeArgs.length);
recipesData.add(new ArrayList<Object>());

try {
for (Object recipeArg : recipeArgs) {
addArgument(recipeArg);
}
} catch (Exception e) {
MineTweakerAPI.logError(e.toString());
}
}

protected abstract void applySingleRecipe(ArgIterator i);

protected static class ArgIterator {
private Iterator<Object> iterator;

public ArgIterator(List<Object> args) {
this.iterator = args.iterator();
}

public ItemStack nextItem() {
return (ItemStack) iterator.next();
}

public ItemStack[] nextItemArr() {
return (ItemStack[]) iterator.next();
}

public FluidStack nextFluid() {
return (FluidStack) iterator.next();
}

public FluidStack[] nextFluidArr() {
return (FluidStack[]) iterator.next();
}

public int nextInt() {
return (Integer) iterator.next();
}

public int[] nextIntArr() {
return (int[]) iterator.next();
}

public boolean nextBool() {
return (Boolean) iterator.next();
}
}

@Override
public void apply() {
for (List<Object> recipeData : recipesData) {
applySingleRecipe(recipeData.toArray());
applySingleRecipe(new ArgIterator(recipeData));
}
}

Expand All @@ -122,9 +160,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

AddMultipleRecipeAction that = (AddMultipleRecipeAction) o;

return recipesData != null ? recipesData.equals(that.recipesData) : that.recipesData == null;

}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gttweaker/util/ArrayHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gttweaker.util;

/**
* @author Techlone
*/
public class ArrayHelper {
public static <T> T itemOrNull(T[] array, int i) {
return array.length > i ? array[i] : null;
}
}

0 comments on commit 4bf022e

Please sign in to comment.