From 308dc205c0e9d58dfd0a4c4fbe8a57d3834ed382 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 1 Jul 2015 15:14:39 +0200 Subject: [PATCH] -Generalized FluidContainer Emptying and Filling --- .../crafting/GrinderCrafting.java | 2 +- .../recipe/GrinderRecipeManualRegistry.java | 4 ++ .../tile/TileEntityCanolaPress.java | 9 +---- .../tile/TileEntityCoffeeMachine.java | 11 +----- .../tile/TileEntityFermentingBarrel.java | 20 +--------- .../tile/TileEntityFluidCollector.java | 24 ++---------- .../tile/TileEntityOilGenerator.java | 10 +---- .../actuallyadditions/util/WorldUtil.java | 38 +++++++++++++++++++ 8 files changed, 52 insertions(+), 66 deletions(-) diff --git a/src/main/java/ellpeck/actuallyadditions/crafting/GrinderCrafting.java b/src/main/java/ellpeck/actuallyadditions/crafting/GrinderCrafting.java index e5c2cb6ee..ca6c1721d 100644 --- a/src/main/java/ellpeck/actuallyadditions/crafting/GrinderCrafting.java +++ b/src/main/java/ellpeck/actuallyadditions/crafting/GrinderCrafting.java @@ -15,7 +15,7 @@ public class GrinderCrafting{ public static void init(){ Util.logInfo("Initializing Crusher Recipes..."); - GrinderRecipeManualRegistry.recipes.clear(); + GrinderRecipeManualRegistry.clearRecipeList(); GrinderRecipeManualRegistry.registerRecipe(new ItemStack(Blocks.redstone_ore), new ItemStack(Items.redstone, 10)); GrinderRecipeManualRegistry.registerRecipe(new ItemStack(Blocks.lapis_ore), new ItemStack(InitItems.itemDust, 12, TheDusts.LAPIS.ordinal())); diff --git a/src/main/java/ellpeck/actuallyadditions/recipe/GrinderRecipeManualRegistry.java b/src/main/java/ellpeck/actuallyadditions/recipe/GrinderRecipeManualRegistry.java index f4b73cc4f..96077ecfa 100644 --- a/src/main/java/ellpeck/actuallyadditions/recipe/GrinderRecipeManualRegistry.java +++ b/src/main/java/ellpeck/actuallyadditions/recipe/GrinderRecipeManualRegistry.java @@ -9,6 +9,10 @@ public class GrinderRecipeManualRegistry{ public static ArrayList recipes = new ArrayList(); + public static void clearRecipeList(){ + recipes.clear(); + } + public static void registerRecipe(ItemStack input, ItemStack outputOne, ItemStack outputTwo, int secondChance){ recipes.add(new GrinderRecipe(input, outputOne, outputTwo, secondChance)); } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCanolaPress.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCanolaPress.java index 31fbf5d2c..0efba8ae2 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCanolaPress.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCanolaPress.java @@ -52,14 +52,7 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IE } else this.currentProcessTime = 0; - if(this.slots[1] != null && this.slots[1].getItem() == Items.bucket && this.slots[2] == null){ - if(this.tank.getFluidAmount() > 0 && this.tank.getFluid().getFluid() == InitBlocks.fluidCanolaOil && this.tank.getFluidAmount() >= FluidContainerRegistry.BUCKET_VOLUME){ - this.slots[2] = new ItemStack(InitItems.itemBucketCanolaOil); - this.slots[1].stackSize--; - if(this.slots[1].stackSize == 0) this.slots[1] = null; - this.tank.drain(FluidContainerRegistry.BUCKET_VOLUME, true); - } - } + WorldUtil.fillBucket(tank, slots, 1, 2); if(this.tank.getFluidAmount() > 0){ WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, this.tank); diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCoffeeMachine.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCoffeeMachine.java index be3b7c1ab..5fe50c06e 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCoffeeMachine.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityCoffeeMachine.java @@ -9,8 +9,8 @@ import ellpeck.actuallyadditions.items.InitItems; import ellpeck.actuallyadditions.items.ItemCoffee; import ellpeck.actuallyadditions.items.metalists.TheMiscItems; import ellpeck.actuallyadditions.network.gui.IButtonReactor; +import ellpeck.actuallyadditions.util.WorldUtil; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; @@ -62,14 +62,7 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements } } - if(this.slots[SLOT_WATER_INPUT] != null && FluidContainerRegistry.containsFluid(this.slots[SLOT_WATER_INPUT], new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME)) && (this.slots[SLOT_WATER_OUTPUT] == null || (this.slots[SLOT_WATER_OUTPUT].stackSize < this.slots[SLOT_WATER_OUTPUT].getMaxStackSize()))){ - if(FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount()){ - if(this.slots[SLOT_WATER_OUTPUT] == null) this.slots[SLOT_WATER_OUTPUT] = new ItemStack(Items.bucket); - else this.slots[SLOT_WATER_OUTPUT].stackSize++; - this.slots[SLOT_WATER_INPUT] = null; - this.tank.fill(new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME), true); - } - } + WorldUtil.emptyBucket(tank, slots, SLOT_WATER_INPUT, SLOT_WATER_OUTPUT, FluidRegistry.WATER); } public void brew(){ diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFermentingBarrel.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFermentingBarrel.java index 3198d74c1..276bb5b47 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFermentingBarrel.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFermentingBarrel.java @@ -4,7 +4,6 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import ellpeck.actuallyadditions.blocks.InitBlocks; import ellpeck.actuallyadditions.config.values.ConfigIntValues; -import ellpeck.actuallyadditions.items.InitItems; import ellpeck.actuallyadditions.util.WorldUtil; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; @@ -42,23 +41,8 @@ public class TileEntityFermentingBarrel extends TileEntityInventoryBase implemen } else this.currentProcessTime = 0; - if(this.slots[0] != null && FluidContainerRegistry.containsFluid(this.slots[0], new FluidStack(InitBlocks.fluidCanolaOil, FluidContainerRegistry.BUCKET_VOLUME)) && (this.slots[1] == null || (this.slots[1].stackSize < this.slots[1].getMaxStackSize()))){ - if(FluidContainerRegistry.BUCKET_VOLUME <= this.canolaTank.getCapacity()-this.canolaTank.getFluidAmount()){ - if(this.slots[1] == null) this.slots[1] = new ItemStack(Items.bucket); - else this.slots[1].stackSize++; - this.slots[0] = null; - this.canolaTank.fill(new FluidStack(InitBlocks.fluidCanolaOil, FluidContainerRegistry.BUCKET_VOLUME), true); - } - } - - if(this.slots[2] != null && this.slots[2].getItem() == Items.bucket && this.slots[3] == null){ - if(this.oilTank.getFluidAmount() > 0 && this.oilTank.getFluid().getFluid() == InitBlocks.fluidOil && this.oilTank.getFluidAmount() >= FluidContainerRegistry.BUCKET_VOLUME){ - this.slots[3] = new ItemStack(InitItems.itemBucketOil); - this.slots[2].stackSize--; - if(this.slots[2].stackSize == 0) this.slots[2] = null; - this.oilTank.drain(FluidContainerRegistry.BUCKET_VOLUME, true); - } - } + WorldUtil.emptyBucket(canolaTank, slots, 0, 1, InitBlocks.fluidCanolaOil); + WorldUtil.fillBucket(oilTank, slots, 2, 3); if(this.oilTank.getFluidAmount() > 0){ WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, this.oilTank); diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFluidCollector.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFluidCollector.java index 9e51d1ce4..0b2c0bc16 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFluidCollector.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFluidCollector.java @@ -9,7 +9,6 @@ import ellpeck.actuallyadditions.network.PacketHandler; import ellpeck.actuallyadditions.util.WorldUtil; import net.minecraft.block.Block; import net.minecraft.init.Blocks; -import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChunkCoordinates; @@ -137,26 +136,8 @@ public class TileEntityFluidCollector extends TileEntityInventoryBase implements else this.currentTime = this.timeNeeded; } - if(!this.isPlacer){ - if(this.slots[0] != null && this.slots[0].getItem() == Items.bucket && this.slots[1] == null){ - if(this.tank.getFluidAmount() >= FluidContainerRegistry.BUCKET_VOLUME){ - this.slots[1] = FluidContainerRegistry.fillFluidContainer(this.tank.getFluid(), this.slots[0].copy()); - this.slots[0].stackSize--; - if(this.slots[0].stackSize == 0) this.slots[0] = null; - this.tank.drain(FluidContainerRegistry.BUCKET_VOLUME, true); - } - } - } - else{ - if(this.slots[0] != null && FluidContainerRegistry.isBucket(this.slots[0]) && !this.slots[0].isItemEqual(FluidContainerRegistry.EMPTY_BUCKET) && (this.slots[1] == null || this.slots[1].stackSize < this.slots[1].getMaxStackSize())){ - if(FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount()){ - if(this.slots[1] == null) this.slots[1] = new ItemStack(Items.bucket); - else this.slots[1].stackSize++; - this.tank.fill(FluidContainerRegistry.getFluidForFilledItem(this.slots[0]), true); - this.slots[0] = null; - } - } - } + if(!this.isPlacer) WorldUtil.fillBucket(tank, slots, 0, 1); + else WorldUtil.emptyBucket(tank, slots, 0, 1); if(this.tank.getFluidAmount() > 0 && !this.isPlacer){ WorldUtil.pushFluid(worldObj, xCoord, yCoord, zCoord, ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord)).getOpposite(), this.tank); @@ -164,6 +145,7 @@ public class TileEntityFluidCollector extends TileEntityInventoryBase implements if(amountBefore != this.tank.getFluidAmount()){ this.sendPacket(); + this.markDirty(); } } } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityOilGenerator.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityOilGenerator.java index 78e01fa16..626bca499 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityOilGenerator.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityOilGenerator.java @@ -7,7 +7,6 @@ import cpw.mods.fml.relauncher.SideOnly; import ellpeck.actuallyadditions.blocks.InitBlocks; import ellpeck.actuallyadditions.config.values.ConfigIntValues; import ellpeck.actuallyadditions.util.WorldUtil; -import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; @@ -48,14 +47,7 @@ public class TileEntityOilGenerator extends TileEntityInventoryBase implements I } } - if(this.slots[0] != null && FluidContainerRegistry.containsFluid(this.slots[0], new FluidStack(InitBlocks.fluidOil, FluidContainerRegistry.BUCKET_VOLUME)) && (this.slots[1] == null || (this.slots[1].stackSize < this.slots[1].getMaxStackSize()))){ - if(FluidContainerRegistry.BUCKET_VOLUME <= this.tank.getCapacity()-this.tank.getFluidAmount()){ - if(this.slots[1] == null) this.slots[1] = new ItemStack(Items.bucket); - else this.slots[1].stackSize++; - this.slots[0] = null; - this.tank.fill(new FluidStack(InitBlocks.fluidOil, FluidContainerRegistry.BUCKET_VOLUME), true); - } - } + WorldUtil.emptyBucket(tank, slots, 0, 1, InitBlocks.fluidOil); if(this.getEnergyStored(ForgeDirection.UNKNOWN) > 0){ WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UP, storage); diff --git a/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java b/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java index f374926bc..2ccd6ac6a 100644 --- a/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java +++ b/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java @@ -113,6 +113,44 @@ public class WorldUtil{ } return null; } + + public static void fillBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot){ + if(slots[inputSlot] != null && tank.getFluid() != null){ + ItemStack filled = FluidContainerRegistry.fillFluidContainer(tank.getFluid(), slots[inputSlot].copy()); + if(filled != null && FluidContainerRegistry.isEmptyContainer(slots[inputSlot]) && (slots[outputSlot] == null || (slots[outputSlot].isItemEqual(filled) && slots[outputSlot].stackSize < slots[outputSlot].getMaxStackSize()))){ + int cap = FluidContainerRegistry.getContainerCapacity(tank.getFluid(), slots[inputSlot]); + if(cap > 0 && cap <= tank.getFluidAmount()){ + if(slots[outputSlot] == null) slots[outputSlot] = FluidContainerRegistry.fillFluidContainer(tank.getFluid(), slots[inputSlot].copy()); + else slots[outputSlot].stackSize++; + + if(slots[outputSlot] != null){ + tank.drain(cap, true); + slots[inputSlot].stackSize--; + if(slots[inputSlot].stackSize <= 0) slots[inputSlot] = null; + } + } + } + } + } + + public static void emptyBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot){ + emptyBucket(tank, slots, inputSlot, outputSlot, null); + } + + public static void emptyBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot, Fluid containedFluid){ + if(slots[inputSlot] != null && FluidContainerRegistry.isFilledContainer(slots[inputSlot]) && (slots[outputSlot] == null || (slots[outputSlot].isItemEqual(FluidContainerRegistry.drainFluidContainer(slots[inputSlot].copy())) && slots[outputSlot].stackSize < slots[outputSlot].getMaxStackSize()))){ + if(containedFluid == null || FluidContainerRegistry.containsFluid(slots[inputSlot], new FluidStack(containedFluid, 0))){ + if((tank.getFluid() == null || FluidContainerRegistry.getFluidForFilledItem(slots[inputSlot]).isFluidEqual(tank.getFluid())) && tank.getCapacity()-tank.getFluidAmount() >= FluidContainerRegistry.getContainerCapacity(slots[inputSlot])){ + if(slots[outputSlot] == null) slots[outputSlot] = FluidContainerRegistry.drainFluidContainer(slots[inputSlot].copy()); + else slots[outputSlot].stackSize++; + + tank.fill(FluidContainerRegistry.getFluidForFilledItem(slots[inputSlot]), true); + slots[inputSlot].stackSize--; + if(slots[inputSlot].stackSize <= 0) slots[inputSlot] = null; + } + } + } + } public static ForgeDirection getDirectionByRotatingSide(int side){ switch(side){