diff options
Diffstat (limited to 'src/main/java/gregtech/common')
-rw-r--r-- | src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java | 96 |
1 files changed, 90 insertions, 6 deletions
diff --git a/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java b/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java index 7385bb31b1..e73631cd93 100644 --- a/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java +++ b/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java @@ -10,6 +10,12 @@ import gregtech.api.util.GT_Utility; import gregtech.common.gui.GT_Container_ChestBuffer; import gregtech.common.gui.GT_GUIContainer_ChestBuffer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; + +import java.util.Arrays; +import java.util.Comparator; public class GT_MetaTileEntity_ChestBuffer extends GT_MetaTileEntity_Buffer { @@ -49,16 +55,94 @@ public class GT_MetaTileEntity_ChestBuffer } protected void moveItems(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) { - fillStacksIntoFirstSlots(); - super.moveItems(aBaseMetaTileEntity, aTimer); - fillStacksIntoFirstSlots(); + if(aBaseMetaTileEntity.hasInventoryBeenModified()) { + fillStacksIntoFirstSlots(); + } + // mSuccess will be negative if the call is caused by the %200 aTimer, always try to push. Otherwise it will be positive. + // For the first 6 ticks after a successful move (49->44), push every tick. Then go to every 5 ticks. + if ( (mSuccess <= 0 ) || (mSuccess > 43) || ((mSuccess % 5) == 0 )){ + super.moveItems(aBaseMetaTileEntity, aTimer); + } + // mSuccesss is set to 50 on a successful move + if(mSuccess == 50) { + fillStacksIntoFirstSlots(); + } + if(mSuccess < 0) { + mSuccess = 0; + } + } + +// Implementation using Java built in sort algorithm +// Uses terribad string comparison to sort against. Would be better if we did something else? + protected void sortStacks() { + Arrays.sort(this.mInventory, new Comparator<ItemStack>() { + @Override + // Taken from https://gist.github.com/Choonster/876acc3217229e172e46 + public int compare(ItemStack o1, ItemStack o2) { + if( o2 == null ) + return -1; + if( o1 == null ) + return 1; + Item item1 = o1.getItem(); + Item item2 = o2.getItem(); + + // If item1 is a block and item2 isn't, sort item1 before item2 + if (((item1 instanceof ItemBlock)) && (!(item2 instanceof ItemBlock))) { + return -1; + } + + // If item2 is a block and item1 isn't, sort item1 after item2 + if (((item2 instanceof ItemBlock)) && (!(item1 instanceof ItemBlock))) { + return 1; + } + + // If the items are blocks, use the string comparison + if ((item1 instanceof ItemBlock)) { // only need to check one since we did the check above + String displayName1 = o1.getDisplayName(); + String displayName2 = o2.getDisplayName(); + int result = displayName1.compareToIgnoreCase(displayName2); + //System.out.println("sorter: " + displayName1 + " " + displayName2 + " " + result); + return result; + } else + { + // Not a block. Use the ID and damage to compare them. + int id1 = Item.getIdFromItem( item1 ); + int id2 = Item.getIdFromItem( item2 ); + if ( id1 < id2 ) { + return -1; + } + if ( id1 > id2 ) { + return 1; + } + // id1 must equal id2, get their damage and compare + id1 = o1.getItemDamage(); + id2 = o2.getItemDamage(); + + if ( id1 < id2 ) { + return -1; + } + if ( id1 > id2 ) { + return 1; + } + return 0; + } + } + }); } protected void fillStacksIntoFirstSlots() { - for (int i = 0; i < this.mInventory.length - 1; i++) { - for (int j = i + 1; j < this.mInventory.length - 1; j++) { - if ((this.mInventory[j] != null) && ((this.mInventory[i] == null) || (GT_Utility.areStacksEqual(this.mInventory[i], this.mInventory[j])))) { + sortStacks(); + // Merge small stacks together + for (int i = 0; i < this.mInventory.length-1;) { + //System.out.println( (this.mInventory[i] == null) ? "Slot empty " + i : "Slot " + i + " holds " + this.mInventory[i].getDisplayName()); + for (int j = i + 1; j < this.mInventory.length; j++) { + if ((this.mInventory[j] != null) && ((GT_Utility.areStacksEqual(this.mInventory[i], this.mInventory[j])))) { GT_Utility.moveStackFromSlotAToSlotB(getBaseMetaTileEntity(), getBaseMetaTileEntity(), j, i, (byte) 64, (byte) 1, (byte) 64, (byte) 1); + //System.out.println( "Moving slot " + j + " into slot " + i ); + } + else { + i=j; + break; // No more matching items for this i, do next i } } } |