2018-02-15 18:49:01 -08:00
package WayofTime.bloodmagic.compress ;
2015-12-28 19:09:51 -05:00
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.util.Utils ;
2016-11-03 10:52:14 -04:00
import net.minecraft.item.ItemStack ;
import net.minecraft.tileentity.TileEntity ;
import net.minecraft.world.World ;
import net.minecraftforge.items.CapabilityItemHandler ;
import net.minecraftforge.items.IItemHandler ;
import org.apache.commons.lang3.tuple.Pair ;
2017-08-15 21:30:48 -07:00
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
2016-11-03 10:52:14 -04:00
2015-12-28 19:09:51 -05:00
/ * *
2015-12-30 15:34:40 -05:00
* A registry aimed to help compress items in an inventory into its compressible
* form .
2015-12-28 19:09:51 -05:00
* /
2017-08-15 21:30:48 -07:00
public class CompressionRegistry {
2018-03-01 19:27:38 -08:00
public static List < CompressionHandler > compressionRegistry = new ArrayList < > ( ) ;
public static Map < ItemStack , Integer > thresholdMap = new HashMap < > ( ) ;
2015-12-28 19:09:51 -05:00
2017-08-15 21:30:48 -07:00
public static void registerHandler ( CompressionHandler handler ) {
2015-12-28 19:09:51 -05:00
compressionRegistry . add ( handler ) ;
}
/ * *
* Registers an item so that it only compresses while above this threshold
2017-08-15 21:30:48 -07:00
*
* @param stack item / block to be compressed
* @param threshold amount that is to be compressed
2015-12-28 19:09:51 -05:00
* /
2017-08-15 21:30:48 -07:00
public static void registerItemThreshold ( ItemStack stack , int threshold ) {
2015-12-28 19:09:51 -05:00
thresholdMap . put ( stack , threshold ) ;
}
2017-08-15 21:30:48 -07:00
public static ItemStack compressInventory ( ItemStack [ ] inv , World world ) {
for ( CompressionHandler handler : compressionRegistry ) {
2015-12-28 19:09:51 -05:00
ItemStack stack = handler . compressInventory ( inv , world ) ;
2018-08-26 22:05:30 +02:00
if ( ! stack . isEmpty ( ) ) {
2015-12-28 19:09:51 -05:00
return stack ;
}
}
return null ;
}
2017-08-15 21:30:48 -07:00
public static Pair < ItemStack , Boolean > compressInventory ( TileEntity tile , World world ) {
if ( tile . hasCapability ( CapabilityItemHandler . ITEM_HANDLER_CAPABILITY , null ) ) {
2016-11-03 10:52:14 -04:00
IItemHandler itemHandler = tile . getCapability ( CapabilityItemHandler . ITEM_HANDLER_CAPABILITY , null ) ;
ItemStack [ ] inventory = new ItemStack [ itemHandler . getSlots ( ) ] ; //THIS MUST NOT BE EDITED!
ItemStack [ ] copyInventory = new ItemStack [ itemHandler . getSlots ( ) ] ;
2017-08-15 21:30:48 -07:00
for ( int slot = 0 ; slot < itemHandler . getSlots ( ) ; slot + + ) {
2016-11-03 10:52:14 -04:00
inventory [ slot ] = itemHandler . extractItem ( slot , 64 , true ) ;
2017-01-01 21:43:34 -08:00
copyInventory [ slot ] = inventory [ slot ] . copy ( ) ;
2016-11-03 10:52:14 -04:00
}
2017-08-15 21:30:48 -07:00
for ( CompressionHandler handler : compressionRegistry ) {
2016-11-03 10:52:14 -04:00
ItemStack stack = handler . compressInventory ( copyInventory , world ) ;
2017-08-15 21:30:48 -07:00
if ( ! stack . isEmpty ( ) ) {
for ( int slot = 0 ; slot < itemHandler . getSlots ( ) ; slot + + ) {
if ( inventory [ slot ] ! = null & & ! ItemStack . areItemStacksEqual ( inventory [ slot ] , copyInventory [ slot ] ) ) {
2017-01-01 21:43:34 -08:00
itemHandler . extractItem ( slot , inventory [ slot ] . getCount ( ) , false ) ;
2017-08-15 21:30:48 -07:00
if ( copyInventory [ slot ] ! = null ) {
2016-11-03 10:52:14 -04:00
itemHandler . insertItem ( slot , copyInventory [ slot ] , false ) ;
}
}
}
return Pair . of ( Utils . insertStackIntoTile ( stack , itemHandler ) , true ) ;
}
}
}
2017-01-01 21:43:34 -08:00
return Pair . of ( ItemStack . EMPTY , false ) ;
2016-11-03 10:52:14 -04:00
}
2017-08-15 21:30:48 -07:00
public static int getItemThreshold ( ItemStack stack ) {
2018-08-26 22:05:30 +02:00
return stack . getItem ( ) . getItemStackLimit ( stack ) ; //this should work according to the guide, leaving behind a full stack of the source item (unless otherwise specified with a BaseCompressionHandler recipe)
2015-12-28 19:09:51 -05:00
}
2017-08-15 21:30:48 -07:00
public static boolean areItemStacksEqual ( ItemStack stack , ItemStack compressedStack ) {
2017-01-01 21:43:34 -08:00
return stack . isItemEqual ( compressedStack ) & & ( stack . getTagCompound ( ) = = null ? ! compressedStack . hasTagCompound ( ) : stack . getTagCompound ( ) . equals ( compressedStack . getTagCompound ( ) ) ) ;
2015-12-28 19:09:51 -05:00
}
2018-08-26 22:05:30 +02:00
}