add a way to specify the comparison method for recipes

Closes #11
This commit is contained in:
Ellpeck 2018-11-19 21:55:00 +01:00
parent 8647425de6
commit 41300f0419
6 changed files with 28 additions and 27 deletions

View file

@ -84,15 +84,6 @@ public final class Helper {
return ((a & 255) << 24) | ((r & 255) << 16) | ((g & 255) << 8) | (b & 255); return ((a & 255) << 24) | ((r & 255) << 16) | ((g & 255) << 8) | (b & 255);
} }
public static int getItemIndex(List<ItemStack> list, ItemStack item, boolean nbt) {
for (int i = 0; i < list.size(); i++) {
if (areItemsEqual(list.get(i), item, nbt)) {
return i;
}
}
return -1;
}
public static boolean areItemsEqual(ItemStack first, ItemStack second, boolean nbt) { public static boolean areItemsEqual(ItemStack first, ItemStack second, boolean nbt) {
if (!ItemStack.areItemsEqual(first, second)) if (!ItemStack.areItemsEqual(first, second))
return false; return false;

View file

@ -23,6 +23,10 @@ public class AltarRecipe {
this.time = time; this.time = time;
} }
public boolean matches(ItemStack found) {
return ItemStack.areItemsEqual(this.input, found) && ItemStack.areItemStackShareTagsEqual(this.input, found);
}
public AltarRecipe register() { public AltarRecipe register() {
NaturesAuraAPI.ALTAR_RECIPES.put(this.name, this); NaturesAuraAPI.ALTAR_RECIPES.put(this.name, this);
return this; return this;

View file

@ -20,6 +20,10 @@ public class TreeRitualRecipe {
this.time = time; this.time = time;
} }
public boolean matches(ItemStack expected, ItemStack found) {
return ItemStack.areItemsEqual(expected, found) && ItemStack.areItemStackShareTagsEqual(expected, found);
}
public TreeRitualRecipe register() { public TreeRitualRecipe register() {
NaturesAuraAPI.TREE_RITUAL_RECIPES.put(this.name, this); NaturesAuraAPI.TREE_RITUAL_RECIPES.put(this.name, this);
return this; return this;

View file

@ -60,17 +60,19 @@ public class BlockWoodStand extends BlockContainerImpl {
TileEntityWoodStand stand = (TileEntityWoodStand) tile; TileEntityWoodStand stand = (TileEntityWoodStand) tile;
ItemStack stack = stand.items.getStackInSlot(0); ItemStack stack = stand.items.getStackInSlot(0);
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
int index = Helper.getItemIndex(required, stack, true); for (int i = required.size() - 1; i >= 0; i--) {
if (index >= 0) { ItemStack req = required.get(i);
required.remove(index); if (recipe.matches(req, stack)) {
required.remove(i);
if (toPick.getValue() == null) { if (toPick.getValue() == null) {
toPick.setValue(stand); toPick.setValue(stand);
} }
} else { return true;
return false;
} }
} }
return false;
}
} }
return true; return true;
}); });

View file

@ -1,6 +1,5 @@
package de.ellpeck.naturesaura.blocks.tiles; package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.container.BasicAuraContainer; import de.ellpeck.naturesaura.api.aura.container.BasicAuraContainer;
@ -116,7 +115,7 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
this.currentRecipe = getRecipeForInput(stack); this.currentRecipe = getRecipeForInput(stack);
} }
} else { } else {
if (stack.isEmpty() || !Helper.areItemsEqual(stack, this.currentRecipe.input, true)) { if (stack.isEmpty() || !this.currentRecipe.matches(stack)) {
this.currentRecipe = null; this.currentRecipe = null;
this.timer = 0; this.timer = 0;
} else if (this.hasCatalyst(this.currentRecipe.catalyst)) { } else if (this.hasCatalyst(this.currentRecipe.catalyst)) {
@ -181,7 +180,7 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
private static AltarRecipe getRecipeForInput(ItemStack input) { private static AltarRecipe getRecipeForInput(ItemStack input) {
for (AltarRecipe recipe : NaturesAuraAPI.ALTAR_RECIPES.values()) { for (AltarRecipe recipe : NaturesAuraAPI.ALTAR_RECIPES.values()) {
if (Helper.areItemsEqual(recipe.input, input, true)) { if (recipe.matches(input)) {
return recipe; return recipe;
} }
} }

View file

@ -1,12 +1,11 @@
package de.ellpeck.naturesaura.blocks.tiles; package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.recipes.TreeRitualRecipe;
import de.ellpeck.naturesaura.blocks.multi.Multiblocks; import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
import de.ellpeck.naturesaura.packet.PacketHandler; import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream; import de.ellpeck.naturesaura.packet.PacketParticleStream;
import de.ellpeck.naturesaura.packet.PacketParticles; import de.ellpeck.naturesaura.packet.PacketParticles;
import de.ellpeck.naturesaura.api.recipes.TreeRitualRecipe;
import net.minecraft.block.BlockLeaves; import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockLog; import net.minecraft.block.BlockLog;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
@ -152,13 +151,15 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
if (tile instanceof TileEntityWoodStand) { if (tile instanceof TileEntityWoodStand) {
ItemStack stack = ((TileEntityWoodStand) tile).items.getStackInSlot(0); ItemStack stack = ((TileEntityWoodStand) tile).items.getStackInSlot(0);
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
int index = Helper.getItemIndex(required, stack, true); for (int i = required.size() - 1; i >= 0; i--) {
if (index >= 0) { ItemStack req = required.get(i);
required.remove(index); if (this.recipe.matches(req, stack)) {
} else { required.remove(i);
return false; return true;
} }
} }
return false;
}
} }
return true; return true;
}); });