Files
CraftTweaker/MineTweaker3-API/src/main/java/minetweaker/minecraft/recipes/ShapedRecipe.java
T
Stan Hebben 489347558f - 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
2014-06-20 14:13:55 +02:00

217 lines
5.3 KiB
Java

/*
* 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;
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() {
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) {
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 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;
}
}