From 50557e6d0af91e2759b7104ed5c22de97558d6d3 Mon Sep 17 00:00:00 2001 From: Boblet Date: Thu, 17 Feb 2022 16:43:05 +0100 Subject: [PATCH] finished new chemplant functionality, cleared up some fluid network code --- .../com/hbm/interfaces/IFluidAcceptor.java | 11 ++ .../java/com/hbm/interfaces/IFluidSource.java | 11 ++ .../hbm/inventory/recipes/SILEXRecipes.java | 11 +- .../com/hbm/items/machine/ItemFELCrystal.java | 2 +- src/main/java/com/hbm/lib/Library.java | 16 +- .../TileEntityMachineChemplantNew.java | 171 ++++++++++++++++-- .../tileentity/machine/TileEntitySILEX.java | 4 +- src/main/java/com/hbm/util/InventoryUtil.java | 93 +++++++--- 8 files changed, 265 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/hbm/interfaces/IFluidAcceptor.java b/src/main/java/com/hbm/interfaces/IFluidAcceptor.java index a4230ce76..85b3f78a5 100644 --- a/src/main/java/com/hbm/interfaces/IFluidAcceptor.java +++ b/src/main/java/com/hbm/interfaces/IFluidAcceptor.java @@ -5,4 +5,15 @@ import com.hbm.inventory.fluid.FluidType; public interface IFluidAcceptor extends IFluidContainer { int getMaxFillForReceive(FluidType type); + + /* + * Behavior for overriding when a fluid container has matching types as in and outputs + * Only a temporary fix until the fluid system is rewritten + */ + default void setFillForTransferIncoming(int fill, FluidType type) { + this.setFillForTransfer(fill, type); + } + default int getFluidFillIncoming(FluidType type) { + return this.getFluidFill(type); + } } diff --git a/src/main/java/com/hbm/interfaces/IFluidSource.java b/src/main/java/com/hbm/interfaces/IFluidSource.java index b3b8def87..8151691cd 100644 --- a/src/main/java/com/hbm/interfaces/IFluidSource.java +++ b/src/main/java/com/hbm/interfaces/IFluidSource.java @@ -14,4 +14,15 @@ public interface IFluidSource extends IFluidContainer { List getFluidList(FluidType type); void clearFluidList(FluidType type); + /* + * Behavior for overriding when a fluid container has matching types as in and outputs + * Only a temporary fix until the fluid system is rewritten + */ + default void setFillForTransferOutgoing(int fill, FluidType type) { + this.setFillForTransfer(fill, type); + } + default int getFluidFillOutgoing(FluidType type) { + return this.getFluidFill(type); + } + } diff --git a/src/main/java/com/hbm/inventory/recipes/SILEXRecipes.java b/src/main/java/com/hbm/inventory/recipes/SILEXRecipes.java index c614d2674..2b21c3bb8 100644 --- a/src/main/java/com/hbm/inventory/recipes/SILEXRecipes.java +++ b/src/main/java/com/hbm/inventory/recipes/SILEXRecipes.java @@ -9,6 +9,7 @@ import java.util.Map.Entry; import com.hbm.inventory.RecipesCommon.ComparableStack; import com.hbm.inventory.fluid.Fluids; import com.hbm.items.ModItems; +import com.hbm.items.machine.ItemFELCrystal.EnumWavelengths; import com.hbm.items.special.ItemWasteLong; import com.hbm.items.special.ItemWasteShort; import com.hbm.util.WeightedRandomObject; @@ -532,7 +533,7 @@ public class SILEXRecipes { .addOut(new WeightedRandomObject(new ItemStack(ModItems.nugget_au198), 1)) ); - recipes.put(new ComparableStack(Blocks.gravel, 1), new SILEXRecipe(1000, 250, 0) + recipes.put(new ComparableStack(Blocks.gravel, 1), new SILEXRecipe(1000, 250, EnumWavelengths.VISIBLE) .addOut(new WeightedRandomObject(new ItemStack(Items.flint), 80)) .addOut(new WeightedRandomObject(new ItemStack(ModItems.powder_boron), 5)) .addOut(new WeightedRandomObject(new ItemStack(ModItems.powder_lithium), 10)) @@ -627,15 +628,19 @@ public static class SILEXRecipe { public int fluidProduced; public int fluidConsumed; - public int laserStrength; + public EnumWavelengths laserStrength; public List outputs = new ArrayList(); - public SILEXRecipe(int fluidProduced, int fluidConsumed, int laserStrength) { + public SILEXRecipe(int fluidProduced, int fluidConsumed, EnumWavelengths laserStrength) { this.fluidProduced = fluidProduced; this.fluidConsumed = fluidConsumed; this.laserStrength = laserStrength; } + public SILEXRecipe(int fluidProduced, int fluidConsumed, int laserStrength) { + this(fluidProduced, fluidConsumed, EnumWavelengths.values()[laserStrength]); + } + public SILEXRecipe addOut(WeightedRandomObject entry) { outputs.add(entry); return this; diff --git a/src/main/java/com/hbm/items/machine/ItemFELCrystal.java b/src/main/java/com/hbm/items/machine/ItemFELCrystal.java index 4b0e03748..0a105cb2b 100644 --- a/src/main/java/com/hbm/items/machine/ItemFELCrystal.java +++ b/src/main/java/com/hbm/items/machine/ItemFELCrystal.java @@ -31,7 +31,7 @@ public class ItemFELCrystal extends Item { } public static enum EnumWavelengths{ - NULL("la creatura", "6 dollar", 0x010101, 0x010101, EnumChatFormatting.WHITE), + NULL("la creatura", "6 dollar", 0x010101, 0x010101, EnumChatFormatting.WHITE), //why do you exist? IR("wavelengths.name.ir", "wavelengths.waveRange.ir", 0xBB1010, 0xCC4040, EnumChatFormatting.RED), VISIBLE("wavelengths.name.visible", "wavelengths.waveRange.visible", 0, 0, EnumChatFormatting.GREEN), diff --git a/src/main/java/com/hbm/lib/Library.java b/src/main/java/com/hbm/lib/Library.java index de7f93d79..b606b087d 100644 --- a/src/main/java/com/hbm/lib/Library.java +++ b/src/main/java/com/hbm/lib/Library.java @@ -619,7 +619,7 @@ public class Library { } if(tileentity instanceof IFluidAcceptor && newTact && ((IFluidAcceptor)tileentity).getMaxFillForReceive(type) > 0 && - ((IFluidAcceptor)tileentity).getMaxFillForReceive(type) - ((IFluidAcceptor)tileentity).getFluidFill(type) > 0) { + ((IFluidAcceptor)tileentity).getMaxFillForReceive(type) - ((IFluidAcceptor)tileentity).getMaxFillForReceive(type) > 0) { that.getFluidList(type).add((IFluidAcceptor)tileentity); } @@ -628,18 +628,18 @@ public class Library { int size = that.getFluidList(type).size(); if(size > 0) { - int part = that.getFluidFill(type) / size; + int part = that.getFluidFillOutgoing(type) / size; for(IFluidAcceptor consume : that.getFluidList(type)) { - if(consume.getFluidFill(type) < consume.getMaxFillForReceive(type)) + if(consume.getFluidFillIncoming(type) < consume.getMaxFillForReceive(type)) { - if(consume.getMaxFillForReceive(type) - consume.getFluidFill(type) >= part) + if(consume.getMaxFillForReceive(type) - consume.getFluidFillIncoming(type) >= part) { - that.setFillForTransfer(that.getFluidFill(type) - part, type); - consume.setFillForTransfer(consume.getFluidFill(type) + part, type); + that.setFillForTransferOutgoing(that.getFluidFillOutgoing(type) - part, type); + consume.setFillForTransferIncoming(consume.getFluidFillIncoming(type) + part, type); } else { - that.setFillForTransfer(that.getFluidFill(type) - (consume.getMaxFillForReceive(type) - consume.getFluidFill(type)), type); - consume.setFillForTransfer(consume.getMaxFillForReceive(type), type); + that.setFillForTransferOutgoing(that.getFluidFillOutgoing(type) - (consume.getMaxFillForReceive(type) - consume.getFluidFillIncoming(type)), type); + consume.setFillForTransferIncoming(consume.getMaxFillForReceive(type), type); } } } diff --git a/src/main/java/com/hbm/tileentity/machine/TileEntityMachineChemplantNew.java b/src/main/java/com/hbm/tileentity/machine/TileEntityMachineChemplantNew.java index dd2ef4d7d..51bf6bed4 100644 --- a/src/main/java/com/hbm/tileentity/machine/TileEntityMachineChemplantNew.java +++ b/src/main/java/com/hbm/tileentity/machine/TileEntityMachineChemplantNew.java @@ -1,17 +1,26 @@ package com.hbm.tileentity.machine; +import java.util.List; + +import com.hbm.interfaces.IFluidAcceptor; +import com.hbm.interfaces.IFluidSource; import com.hbm.inventory.FluidTank; +import com.hbm.inventory.RecipesCommon.AStack; import com.hbm.inventory.UpgradeManager; +import com.hbm.inventory.fluid.FluidType; import com.hbm.inventory.fluid.Fluids; import com.hbm.inventory.recipes.ChemplantRecipes; import com.hbm.inventory.recipes.ChemplantRecipes.ChemRecipe; import com.hbm.items.ModItems; import com.hbm.items.machine.ItemMachineUpgrade.UpgradeType; import com.hbm.tileentity.TileEntityMachineBase; +import com.hbm.util.InventoryUtil; +import api.hbm.energy.IEnergyUser; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -public class TileEntityMachineChemplantNew extends TileEntityMachineBase { +public class TileEntityMachineChemplantNew extends TileEntityMachineBase implements IEnergyUser, IFluidSource, IFluidAcceptor { public long power; public static final long maxPower = 100000; @@ -60,24 +69,39 @@ public class TileEntityMachineChemplantNew extends TileEntityMachineBase { int powerLevel = Math.min(UpgradeManager.getLevel(UpgradeType.POWER), 3); int overLevel = UpgradeManager.getLevel(UpgradeType.OVERDRIVE); - speed -= speedLevel * 25; - consumption += speedLevel * 300; - speed += powerLevel * 5; - consumption -= powerLevel * 30; - speed /= (overLevel + 1); - consumption *= (overLevel + 1); + this.speed -= speedLevel * 25; + this.consumption += speedLevel * 300; + this.speed += powerLevel * 5; + this.consumption -= powerLevel * 30; + this.speed /= (overLevel + 1); + this.consumption *= (overLevel + 1); if(!canProcess()) { this.progress = 0; } else { process(); } + + NBTTagCompound data = new NBTTagCompound(); + data.setLong("power", this.power); + data.setInteger("progress", this.progress); + data.setInteger("maxProgress", this.maxProgress); + + for(int i = 0; i < tanks.length; i++) { + tanks[i].writeToNBT(data, "t" + i); + } } } @Override public void networkUnpack(NBTTagCompound nbt) { - + this.power = nbt.getLong("power"); + this.progress = nbt.getInteger("progress"); + this.maxProgress = nbt.getInteger("maxProgress"); + + for(int i = 0; i < tanks.length; i++) { + tanks[i].readFromNBT(nbt, "t" + i); + } } private boolean canProcess() { @@ -92,11 +116,11 @@ public class TileEntityMachineChemplantNew extends TileEntityMachineBase { setupTanks(recipe); - if(!hasRequiredFluids(recipe)) - return false; - - if(!hasSpaceForFluids(recipe)) - return false; + if(this.power < this.consumption) return false; + if(!hasRequiredFluids(recipe)) return false; + if(!hasSpaceForFluids(recipe)) return false; + if(!hasRequiredItems(recipe)) return false; + if(!hasSpaceForItems(recipe)) return false; return true; } @@ -121,10 +145,129 @@ public class TileEntityMachineChemplantNew extends TileEntityMachineBase { } private boolean hasRequiredItems(ChemRecipe recipe) { - return false; + return InventoryUtil.doesArrayHaveIngredients(slots, 13, 16, recipe.inputs); + } + + private boolean hasSpaceForItems(ChemRecipe recipe) { + return InventoryUtil.doesArrayHaveSpace(slots, 5, 8, recipe.outputs); } private void process() { + this.power -= this.consumption; + this.progress++; + + ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[4].getItemDamage()); + + this.maxProgress = recipe.getDuration() * this.speed / 100; + + if(this.progress >= this.maxProgress) { + consumeFluids(recipe); + produceFluids(recipe); + consumeItems(recipe); + produceItems(recipe); + } + } + + private void consumeFluids(ChemRecipe recipe) { + if(recipe.inputFluids.length > 0) tanks[0].setFill(tanks[0].getFill() - recipe.inputFluids[0].fill); + if(recipe.inputFluids.length > 1) tanks[1].setFill(tanks[1].getFill() - recipe.inputFluids[1].fill); + } + + private void produceFluids(ChemRecipe recipe) { + if(recipe.outputFluids.length > 0) tanks[2].setFill(tanks[2].getFill() + recipe.outputFluids[0].fill); + if(recipe.outputFluids.length > 1) tanks[3].setFill(tanks[3].getFill() + recipe.outputFluids[1].fill); + } + + private void consumeItems(ChemRecipe recipe) { + + for(AStack in : recipe.inputs) { + InventoryUtil.tryConsumeAStack(slots, 13, 16, in); + } + } + + private void produceItems(ChemRecipe recipe) { + + for(ItemStack out : recipe.outputs) { + InventoryUtil.tryAddItemToInventory(slots, 5, 8, out); + } + } + + @Override + public long getPower() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long getMaxPower() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setFillForSync(int fill, int index) { + // TODO Auto-generated method stub + + } + + @Override + public void setFillForTransfer(int fill, FluidType type) { + // TODO Auto-generated method stub + + } + + @Override + public void setTypeForSync(FluidType type, int index) { + // TODO Auto-generated method stub + + } + + @Override + public int getFluidFill(FluidType type) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getMaxFillForReceive(FluidType type) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void fillFluidInit(FluidType type) { + // TODO Auto-generated method stub + + } + + @Override + public void fillFluid(int x, int y, int z, boolean newTact, FluidType type) { + // TODO Auto-generated method stub + + } + + @Override + public boolean getTact() { + // TODO Auto-generated method stub + return false; + } + + @Override + public List getFluidList(FluidType type) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void clearFluidList(FluidType type) { + // TODO Auto-generated method stub + + } + + @Override + public void setPower(long power) { + // TODO Auto-generated method stub + } } diff --git a/src/main/java/com/hbm/tileentity/machine/TileEntitySILEX.java b/src/main/java/com/hbm/tileentity/machine/TileEntitySILEX.java index 0c5ed3f67..7cb62da73 100644 --- a/src/main/java/com/hbm/tileentity/machine/TileEntitySILEX.java +++ b/src/main/java/com/hbm/tileentity/machine/TileEntitySILEX.java @@ -185,7 +185,7 @@ public class TileEntitySILEX extends TileEntityMachineBase implements IFluidAcce if(recipe == null) return false; - if(recipe.laserStrength > this.mode.ordinal()) + if(recipe.laserStrength.ordinal() > this.mode.ordinal()) return false; if(currentFill < recipe.fluidConsumed) @@ -194,7 +194,7 @@ public class TileEntitySILEX extends TileEntityMachineBase implements IFluidAcce if(slots[4] != null) return false; - int progressSpeed = (int) Math.pow(2, this.mode.ordinal() - recipe.laserStrength + 1) / 2; + int progressSpeed = (int) Math.pow(2, this.mode.ordinal() - recipe.laserStrength.ordinal() + 1) / 2; progress += progressSpeed; diff --git a/src/main/java/com/hbm/util/InventoryUtil.java b/src/main/java/com/hbm/util/InventoryUtil.java index dbd64d813..62186a921 100644 --- a/src/main/java/com/hbm/util/InventoryUtil.java +++ b/src/main/java/com/hbm/util/InventoryUtil.java @@ -37,6 +37,10 @@ public class InventoryUtil { return rem; } + public static ItemStack tryAddItemToInventory(ItemStack[] inv, ItemStack stack) { + return tryAddItemToInventory(inv, 0, inv.length - 1, stack); + } + /** * Functionally equal to tryAddItemToInventory, but will not try to create new stacks in empty slots * @param inv @@ -156,6 +160,32 @@ public class InventoryUtil { return false; } + public static boolean tryConsumeAStack(ItemStack[] inv, int start, int end, AStack stack) { + + if(stack == null) + return true; + + AStack copy = stack.copy(); + + for(int i = start; i <= end; i++) { + ItemStack in = inv[i]; + + if(stack.matchesRecipe(in, true)) { + int size = Math.min(copy.stacksize, in.stackSize); + + in.stackSize -= size; + copy.stacksize -= size; + + if(in.stackSize == 0) + inv[i] = null; + if(copy.stacksize == 0) + return true; + } + } + + return false; + } + /** * Compares item, metadata and NBT data of two stacks. Also handles null values! * @param stack1 @@ -164,29 +194,14 @@ public class InventoryUtil { */ public static boolean doesStackDataMatch(ItemStack stack1, ItemStack stack2) { - if(stack1 == null && stack2 == null) - return true; - - if(stack1 == null && stack2 != null) - return false; - - if(stack1 != null && stack2 == null) - return false; - - if(stack1.getItem() != stack2.getItem()) - return false; - - if(stack1.getItemDamage() != stack2.getItemDamage()) - return false; - - if(!stack1.hasTagCompound() && !stack2.hasTagCompound()) - return true; - - if(stack1.hasTagCompound() && !stack2.hasTagCompound()) - return false; - - if(!stack1.hasTagCompound() && stack2.hasTagCompound()) - return false; + if(stack1 == null && stack2 == null) return true; + if(stack1 == null && stack2 != null) return false; + if(stack1 != null && stack2 == null) return false; + if(stack1.getItem() != stack2.getItem()) return false; + if(stack1.getItemDamage() != stack2.getItemDamage()) return false; + if(!stack1.hasTagCompound() && !stack2.hasTagCompound()) return true; + if(stack1.hasTagCompound() && !stack2.hasTagCompound()) return false; + if(!stack1.hasTagCompound() && stack2.hasTagCompound()) return false; return stack1.getTagCompound().equals(stack2.getTagCompound()); } @@ -381,21 +396,47 @@ public class InventoryUtil { ItemStack[] copy = ItemStackUtil.carefulCopyArrayTruncate(array, start, end); AStack[] req = new AStack[ingredients.length]; - for(int i = 0; i < req.length; i++) { req[i] = ingredients[i] == null ? null : ingredients[i].copy(); } for(AStack ingredient : req) { + + if(ingredient == null) + continue; + for(ItemStack input : copy) { + + if(input == null) + continue; + if(ingredient.matchesRecipe(input, true)) { int size = Math.min(input.stackSize, ingredient.stacksize); - //TODO: yada yada yada + ingredient.stacksize -= size; + input.stackSize -= size; + + if(ingredient.stacksize == 0) + break; } } - return false; + //we have iterated over the entire input array and removed all matching entries, yet the ingredient is still not exhausted, meaning the input wasn't enough + if(ingredient.stacksize > 0) + return false; + } + + return true; + } + + public static boolean doesArrayHaveSpace(ItemStack[] array, int start, int end, ItemStack[] items) { + ItemStack[] copy = ItemStackUtil.carefulCopyArrayTruncate(array, start, end); + + for(ItemStack item : items) { + ItemStack remainder = tryAddItemToInventory(copy, item); + if(remainder != null) { + return false; + } } return true;