From 258d8b417eae813944fc1bafc7316a1bbd2ac1e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= <tseeker@nocternity.net>
Date: Sat, 25 Jun 2016 08:54:22 +0200
Subject: [PATCH] Alloy furnace - Output hopper

Takes invalid input and invalid fuel in addition to actual output
---
 TODO.txt                                      |   8 +-
 .../tech/base/TBAlloyFurnaceOutputHopper.java | 114 ++++++++++++++++++
 .../tech/base/TBAlloyFurnaceTileEntity.java   |   2 +-
 3 files changed, 120 insertions(+), 4 deletions(-)
 create mode 100644 src/java/mmm/tech/base/TBAlloyFurnaceOutputHopper.java

diff --git a/TODO.txt b/TODO.txt
index 6918a91..c8502bd 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -44,10 +44,12 @@ materials.ores	No		Tin
 materials.ores	No		Zinc
 -------------------------------------------------------------------------------------------------------
 tech.base		No		Alloy furnace
-							-> let output hoppers take empty buckets / invalid input
-								(make that configurable)
+							-> redstone activation
+							-> make redstone activation configurable
+							-> make output hopper configurable
 							-> comparator signal configuration
-								(inventory mode w/ combinations, valid ingredients, burn time, alloying time)
+								(inventory mode w/ combinations, valid ingredients, burn time,
+								alloying time)
 							-> code clean-up
 tech.base		No		Coke oven
 -------------------------------------------------------------------------------------------------------
diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceOutputHopper.java b/src/java/mmm/tech/base/TBAlloyFurnaceOutputHopper.java
new file mode 100644
index 0000000..2e59702
--- /dev/null
+++ b/src/java/mmm/tech/base/TBAlloyFurnaceOutputHopper.java
@@ -0,0 +1,114 @@
+package mmm.tech.base;
+
+
+import mmm.utils.UInventoryGrid;
+import net.minecraft.item.ItemStack;
+import net.minecraft.tileentity.TileEntityFurnace;
+import net.minecraftforge.items.IItemHandler;
+
+
+
+public class TBAlloyFurnaceOutputHopper
+		implements IItemHandler
+{
+	private final TBAlloyFurnaceTileEntity tileEntity;
+	private final int nSlots;
+
+
+	public TBAlloyFurnaceOutputHopper( final TBAlloyFurnaceTileEntity tileEntity )
+	{
+		this.tileEntity = tileEntity;
+		this.nSlots = this.tileEntity.input.getSizeInventory( ) + this.tileEntity.fuel.getSizeInventory( )
+				+ this.tileEntity.output.getSizeInventory( );
+	}
+
+
+	@Override
+	public int getSlots( )
+	{
+		return this.nSlots;
+	}
+
+
+	@Override
+	public ItemStack getStackInSlot( final int slot )
+	{
+		final int nInputSlots = this.tileEntity.input.getSizeInventory( );
+		if ( slot < nInputSlots ) {
+			return this.tileEntity.input.getStackInSlot( slot );
+		} else {
+			final int nFuelSlots = this.tileEntity.fuel.getSizeInventory( );
+			if ( slot < nInputSlots + nFuelSlots ) {
+				return this.tileEntity.fuel.getStackInSlot( slot - nInputSlots );
+			} else {
+				return this.tileEntity.output.getStackInSlot( slot - nInputSlots - nFuelSlots );
+			}
+		}
+	}
+
+
+	@Override
+	public ItemStack insertItem( final int slot , final ItemStack stack , final boolean simulate )
+	{
+		return null;
+	}
+
+
+	@Override
+	public ItemStack extractItem( final int slot , final int amount , final boolean simulate )
+	{
+		if ( amount == 0 ) {
+			return null;
+		}
+
+		final ItemStack stackInSlot = this.getStackInSlot( slot );
+
+		if ( stackInSlot == null ) {
+			return null;
+		}
+
+		// Extracting invalid input items
+		final int nInputSlots = this.tileEntity.input.getSizeInventory( );
+		if ( slot < nInputSlots ) {
+			if ( this.tileEntity.recipe.hasInput( stackInSlot ) ) {
+				return null;
+			}
+			// XXX check configuration
+			return this.doExtractItems( this.tileEntity.input , stackInSlot , slot , amount , simulate );
+		}
+
+		// Extracting invalid fuel items
+		final int nFuelSlots = this.tileEntity.fuel.getSizeInventory( );
+		if ( slot < nInputSlots + nFuelSlots ) {
+			if ( TileEntityFurnace.isItemFuel( stackInSlot ) ) {
+				return null;
+			}
+			return this.doExtractItems( this.tileEntity.fuel , stackInSlot , slot - nInputSlots , amount , simulate );
+		}
+
+		return this.doExtractItems( this.tileEntity.output , stackInSlot , slot - ( nInputSlots + nFuelSlots ) ,
+				amount , simulate );
+	}
+
+
+	private ItemStack doExtractItems( final UInventoryGrid inventory , final ItemStack stackInSlot , final int slot ,
+			final int amount , final boolean simulate )
+	{
+		if ( simulate ) {
+			if ( stackInSlot.stackSize < amount ) {
+				return stackInSlot.copy( );
+			} else {
+				final ItemStack copy = stackInSlot.copy( );
+				copy.stackSize = amount;
+				return copy;
+			}
+		} else {
+			final int m = Math.min( stackInSlot.stackSize , amount );
+
+			final ItemStack decrStackSize = inventory.decrStackSize( slot , m );
+			inventory.markDirty( );
+			return decrStackSize;
+		}
+	}
+
+}
diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java b/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java
index ecd0efe..d33868c 100644
--- a/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java
+++ b/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java
@@ -52,7 +52,7 @@ public class TBAlloyFurnaceTileEntity
 
 		this.inputHopper = new InvWrapper( this.input );
 		this.fuelHopper = new InvWrapper( this.fuel );
-		this.outputHopper = new InvWrapper( this.output );
+		this.outputHopper = new TBAlloyFurnaceOutputHopper( this );
 	}