From ebdc66d0633624d85fc628bcfea2a09d582a6206 Mon Sep 17 00:00:00 2001 From: WayofTime Date: Fri, 13 Apr 2018 11:31:52 -0400 Subject: [PATCH] Changed the Alchemy Table to use Bindable for checking the Orb's stored LP. Also fixed a crafting exploit with the table. #1269 --- .../bloodmagic/tile/TileAlchemyTable.java | 334 +++++++++++------- 1 file changed, 207 insertions(+), 127 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/tile/TileAlchemyTable.java b/src/main/java/WayofTime/bloodmagic/tile/TileAlchemyTable.java index fd911ee6..ecbc8fa8 100644 --- a/src/main/java/WayofTime/bloodmagic/tile/TileAlchemyTable.java +++ b/src/main/java/WayofTime/bloodmagic/tile/TileAlchemyTable.java @@ -1,15 +1,8 @@ package WayofTime.bloodmagic.tile; -import WayofTime.bloodmagic.api.impl.BloodMagicAPI; -import WayofTime.bloodmagic.api.impl.recipe.RecipeAlchemyTable; -import WayofTime.bloodmagic.util.Constants; -import WayofTime.bloodmagic.orb.BloodOrb; -import WayofTime.bloodmagic.orb.IBloodOrb; -import WayofTime.bloodmagic.recipe.alchemyTable.AlchemyTableRecipe; -import WayofTime.bloodmagic.core.registry.AlchemyTableRecipeRegistry; -import WayofTime.bloodmagic.core.data.SoulNetwork; -import WayofTime.bloodmagic.util.helper.NetworkHelper; -import com.google.common.base.Strings; +import java.util.ArrayList; +import java.util.List; + import net.minecraft.block.state.IBlockState; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; @@ -22,11 +15,20 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.wrapper.SidedInvWrapper; +import WayofTime.bloodmagic.api.impl.BloodMagicAPI; +import WayofTime.bloodmagic.api.impl.recipe.RecipeAlchemyTable; +import WayofTime.bloodmagic.core.data.Binding; +import WayofTime.bloodmagic.core.data.SoulNetwork; +import WayofTime.bloodmagic.core.registry.AlchemyTableRecipeRegistry; +import WayofTime.bloodmagic.iface.IBindable; +import WayofTime.bloodmagic.orb.BloodOrb; +import WayofTime.bloodmagic.orb.IBloodOrb; +import WayofTime.bloodmagic.recipe.alchemyTable.AlchemyTableRecipe; +import WayofTime.bloodmagic.util.Constants; +import WayofTime.bloodmagic.util.helper.NetworkHelper; -import java.util.ArrayList; -import java.util.List; - -public class TileAlchemyTable extends TileInventory implements ISidedInventory, ITickable { +public class TileAlchemyTable extends TileInventory implements ISidedInventory, ITickable +{ public static final int orbSlot = 6; public static final int toolSlot = 7; public static final int outputSlot = 8; @@ -37,36 +39,43 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, public int ticksRequired = 1; public BlockPos connectedPos = BlockPos.ORIGIN; - public boolean[] blockedSlots = new boolean[]{false, false, false, false, false, false}; + public boolean[] blockedSlots = new boolean[] { false, false, false, false, false, false }; - public TileAlchemyTable() { + public TileAlchemyTable() + { super(9, "alchemyTable"); } - public void setInitialTableParameters(EnumFacing direction, boolean isSlave, BlockPos connectedPos) { + public void setInitialTableParameters(EnumFacing direction, boolean isSlave, BlockPos connectedPos) + { this.isSlave = isSlave; this.connectedPos = connectedPos; - if (!isSlave) { + if (!isSlave) + { this.direction = direction; } } - public boolean isInvisible() { + public boolean isInvisible() + { return isSlave(); } - public boolean isInputSlotAccessible(int slot) { + public boolean isInputSlotAccessible(int slot) + { return !(slot < 6 && slot >= 0) || !blockedSlots[slot]; } - public void toggleInputSlotAccessible(int slot) { + public void toggleInputSlotAccessible(int slot) + { if (slot < 6 && slot >= 0) blockedSlots[slot] = !blockedSlots[slot]; } @Override - public void deserialize(NBTTagCompound tag) { + public void deserialize(NBTTagCompound tag) + { super.deserialize(tag); isSlave = tag.getBoolean("isSlave"); @@ -82,7 +91,8 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, } @Override - public NBTTagCompound serialize(NBTTagCompound tag) { + public NBTTagCompound serialize(NBTTagCompound tag) + { super.serialize(tag); tag.setBoolean("isSlave", isSlave); @@ -104,14 +114,19 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, @SuppressWarnings("unchecked") @Override - public T getCapability(Capability capability, EnumFacing facing) { - if (facing != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { - if (this.isSlave()) { + public T getCapability(Capability capability, EnumFacing facing) + { + if (facing != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + { + if (this.isSlave()) + { TileEntity tile = getWorld().getTileEntity(connectedPos); - if (tile instanceof TileAlchemyTable) { + if (tile instanceof TileAlchemyTable) + { return (T) new SidedInvWrapper((TileAlchemyTable) tile, facing); } - } else { + } else + { return (T) new SidedInvWrapper(this, facing); } } @@ -120,58 +135,73 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, } @Override - public int[] getSlotsForFace(EnumFacing side) { - switch (side) { - case DOWN: - return new int[]{outputSlot}; - case UP: - return new int[]{orbSlot, toolSlot}; - default: - return new int[]{0, 1, 2, 3, 4, 5}; + public int[] getSlotsForFace(EnumFacing side) + { + switch (side) + { + case DOWN: + return new int[] { outputSlot }; + case UP: + return new int[] { orbSlot, toolSlot }; + default: + return new int[] { 0, 1, 2, 3, 4, 5 }; } } @Override - public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction) { - switch (direction) { - case DOWN: - return index != outputSlot && index != orbSlot && index != toolSlot; - case UP: - if (index == orbSlot && !stack.isEmpty() && stack.getItem() instanceof IBloodOrb) { - return true; - } else if (index == toolSlot) { - return false; //TODO: - } else { - return true; - } - default: - return getAccessibleInputSlots(direction).contains(index); + public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction) + { + switch (direction) + { + case DOWN: + return index != outputSlot && index != orbSlot && index != toolSlot; + case UP: + if (index == orbSlot && !stack.isEmpty() && stack.getItem() instanceof IBloodOrb) + { + return true; + } else if (index == toolSlot) + { + return false; //TODO: + } else + { + return true; + } + default: + return getAccessibleInputSlots(direction).contains(index); } } @Override - public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) { - switch (direction) { - case DOWN: - return index == outputSlot; - case UP: - if (index == orbSlot && !stack.isEmpty() && stack.getItem() instanceof IBloodOrb) { - return true; - } else if (index == toolSlot) { - return true; //TODO: - } else { - return true; - } - default: - return getAccessibleInputSlots(direction).contains(index); + public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) + { + switch (direction) + { + case DOWN: + return index == outputSlot; + case UP: + if (index == orbSlot && !stack.isEmpty() && stack.getItem() instanceof IBloodOrb) + { + return true; + } else if (index == toolSlot) + { + return true; //TODO: + } else + { + return true; + } + default: + return getAccessibleInputSlots(direction).contains(index); } } - public List getAccessibleInputSlots(EnumFacing direction) { + public List getAccessibleInputSlots(EnumFacing direction) + { List list = new ArrayList<>(); - for (int i = 0; i < 6; i++) { - if (isInputSlotAccessible(i)) { + for (int i = 0; i < 6; i++) + { + if (isInputSlotAccessible(i)) + { list.add(i); } } @@ -180,15 +210,19 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, } @Override - public void update() { - if (isSlave()) { + public void update() + { + if (isSlave()) + { return; } List inputList = new ArrayList<>(); - for (int i = 0; i < 6; i++) { - if (!getStackInSlot(i).isEmpty()) { + for (int i = 0; i < 6; i++) + { + if (!getStackInSlot(i).isEmpty()) + { inputList.add(getStackInSlot(i)); } } @@ -197,24 +231,31 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, // special recipes like dying AlchemyTableRecipe recipe = AlchemyTableRecipeRegistry.getMatchingRecipe(inputList, getWorld(), getPos()); - if (recipe != null && (burnTime > 0 || (!getWorld().isRemote && tier >= recipe.getTierRequired() && this.getContainedLp() >= recipe.getLpDrained()))) { + if (recipe != null && (burnTime > 0 || (!getWorld().isRemote && tier >= recipe.getTierRequired() && this.getContainedLp() >= recipe.getLpDrained()))) + { if (burnTime == 1) notifyUpdate(); - if (canCraft(recipe.getRecipeOutput(inputList))) { + if (canCraft(recipe.getRecipeOutput(inputList))) + { ticksRequired = recipe.getTicksRequired(); burnTime++; - if (burnTime == ticksRequired) { - if (!getWorld().isRemote) { + if (burnTime == ticksRequired) + { + if (!getWorld().isRemote) + { int requiredLp = recipe.getLpDrained(); - if (requiredLp > 0) { - if (!getWorld().isRemote) { + if (requiredLp > 0) + { + if (!getWorld().isRemote) + { consumeLp(requiredLp); } } - if (!getWorld().isRemote) { + if (!getWorld().isRemote) + { craftItem(inputList, recipe); } } @@ -223,25 +264,40 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, IBlockState state = getWorld().getBlockState(pos); getWorld().notifyBlockUpdate(getPos(), state, state, 3); - } else if (burnTime > ticksRequired + 10) { + } else if (burnTime > ticksRequired + 10) + { burnTime = 0; } - } else { + } else + { burnTime = 0; } - } else { // Simple recipes + } else + { // Simple recipes RecipeAlchemyTable recipeAlchemyTable = BloodMagicAPI.INSTANCE.getRecipeRegistrar().getAlchemyTable(inputList); - if (recipeAlchemyTable != null && (burnTime > 0 || (!getWorld().isRemote && tier >= recipeAlchemyTable.getMinimumTier() && getContainedLp() >= recipeAlchemyTable.getSyphon()))) { + if (recipeAlchemyTable != null && (burnTime > 0 || (!getWorld().isRemote && tier >= recipeAlchemyTable.getMinimumTier() && getContainedLp() >= recipeAlchemyTable.getSyphon()))) + { if (burnTime == 1) notifyUpdate(); - if (canCraft(recipeAlchemyTable.getOutput())) { + if (canCraft(recipeAlchemyTable.getOutput())) + { ticksRequired = recipeAlchemyTable.getTicks(); burnTime++; - if (burnTime >= ticksRequired) { - if (!getWorld().isRemote) { - if (recipeAlchemyTable.getSyphon() > 0 && !getWorld().isRemote) - consumeLp(recipeAlchemyTable.getSyphon()); + if (burnTime >= ticksRequired) + { + if (!getWorld().isRemote) + { + if (recipeAlchemyTable.getSyphon() > 0) + { + if (consumeLp(recipeAlchemyTable.getSyphon()) < recipeAlchemyTable.getSyphon()) + { + //There was not enough LP to craft or there was no orb + burnTime = 0; + notifyUpdate(); + return; + } + } ItemStack outputSlotStack = getStackInSlot(outputSlot); if (outputSlotStack.isEmpty()) @@ -249,7 +305,8 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, else outputSlotStack.grow(recipeAlchemyTable.getOutput().getCount()); - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 6; i++) + { ItemStack currentStack = getStackInSlot(i); if (currentStack.getItem().hasContainerItem(currentStack)) setInventorySlotContents(i, currentStack.getItem().getContainerItem(currentStack)); @@ -262,17 +319,20 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, } } } - } else { + } else + { burnTime = 0; } } } - public double getProgressForGui() { + public double getProgressForGui() + { return ((double) burnTime) / ticksRequired; } - private boolean canCraft(ItemStack output) { + private boolean canCraft(ItemStack output) + { ItemStack currentOutputStack = getStackInSlot(outputSlot); if (output.isEmpty()) return false; @@ -284,10 +344,13 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, return result <= getInventoryStackLimit() && result <= currentOutputStack.getMaxStackSize(); } - public int getTierOfOrb() { + public int getTierOfOrb() + { ItemStack orbStack = getStackInSlot(orbSlot); - if (!orbStack.isEmpty()) { - if (orbStack.getItem() instanceof IBloodOrb) { + if (!orbStack.isEmpty()) + { + if (orbStack.getItem() instanceof IBloodOrb) + { BloodOrb orb = ((IBloodOrb) orbStack.getItem()).getOrb(orbStack); return orb == null ? 0 : orb.getTier(); } @@ -296,23 +359,20 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, return 0; } - public int getContainedLp() { + public int getContainedLp() + { ItemStack orbStack = getStackInSlot(orbSlot); - if (!orbStack.isEmpty()) { - if (orbStack.getItem() instanceof IBloodOrb) { - NBTTagCompound itemTag = orbStack.getTagCompound(); - - if (itemTag == null) { + if (!orbStack.isEmpty()) + { + if (orbStack.getItem() instanceof IBloodOrb) + { + Binding binding = ((IBindable) orbStack.getItem()).getBinding(orbStack); + if (binding == null) + { return 0; } - String ownerUUID = itemTag.getString(Constants.NBT.OWNER_UUID); - - if (Strings.isNullOrEmpty(ownerUUID)) { - return 0; - } - - SoulNetwork network = NetworkHelper.getSoulNetwork(ownerUUID); + SoulNetwork network = NetworkHelper.getSoulNetwork(binding); return network.getCurrentEssence(); } @@ -321,14 +381,18 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, return 0; } - public void craftItem(List inputList, AlchemyTableRecipe recipe) { - if (this.canCraft(recipe.getRecipeOutput(inputList))) { + public void craftItem(List inputList, AlchemyTableRecipe recipe) + { + if (this.canCraft(recipe.getRecipeOutput(inputList))) + { ItemStack outputStack = recipe.getRecipeOutput(inputList); ItemStack currentOutputStack = getStackInSlot(outputSlot); - if (currentOutputStack.isEmpty()) { + if (currentOutputStack.isEmpty()) + { setInventorySlotContents(outputSlot, outputStack); - } else if (ItemHandlerHelper.canItemStacksStack(outputStack, currentOutputStack)) { + } else if (ItemHandlerHelper.canItemStacksStack(outputStack, currentOutputStack)) + { currentOutputStack.grow(outputStack.getCount()); } @@ -336,12 +400,16 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, } } - public int consumeLp(int requested) { + public int consumeLp(int requested) + { ItemStack orbStack = getStackInSlot(orbSlot); - if (!orbStack.isEmpty()) { - if (orbStack.getItem() instanceof IBloodOrb) { - if (NetworkHelper.syphonFromContainer(orbStack, requested)) { + if (!orbStack.isEmpty()) + { + if (orbStack.getItem() instanceof IBloodOrb) + { + if (NetworkHelper.syphonFromContainer(orbStack, requested)) + { return requested; } } @@ -350,52 +418,64 @@ public class TileAlchemyTable extends TileInventory implements ISidedInventory, return 0; } - public void consumeInventory(AlchemyTableRecipe recipe) { + public void consumeInventory(AlchemyTableRecipe recipe) + { ItemStack[] input = new ItemStack[6]; - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 6; i++) + { input[i] = getStackInSlot(i); } ItemStack[] result = recipe.getRemainingItems(input); - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 6; i++) + { setInventorySlotContents(i, result[i]); } } - public EnumFacing getDirection() { + public EnumFacing getDirection() + { return direction; } - public boolean isSlave() { + public boolean isSlave() + { return isSlave; } - public int getBurnTime() { + public int getBurnTime() + { return burnTime; } - public int getTicksRequired() { + public int getTicksRequired() + { return ticksRequired; } - public BlockPos getConnectedPos() { + public BlockPos getConnectedPos() + { return connectedPos; } - public boolean[] getBlockedSlots() { + public boolean[] getBlockedSlots() + { return blockedSlots; } - public static int getOrbSlot() { + public static int getOrbSlot() + { return orbSlot; } - public static int getToolSlot() { + public static int getToolSlot() + { return toolSlot; } - public static int getOutputSlot() { + public static int getOutputSlot() + { return outputSlot; } }