2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem ;
2019-05-04 07:25:54 +00:00
import java.util.* ;
2016-04-14 16:24:03 +00:00
2019-08-01 09:01:43 +00:00
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.InvUtils ;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.Lists.RecipeType ;
import me.mrCookieSlime.Slimefun.Objects.Category ;
import me.mrCookieSlime.Slimefun.Objects.MultiBlock ;
2019-08-01 09:01:43 +00:00
import me.mrCookieSlime.Slimefun.api.BlockStorage ;
2016-04-14 16:24:03 +00:00
import org.bukkit.Material ;
2019-08-01 09:01:43 +00:00
import org.bukkit.block.Block ;
import org.bukkit.block.BlockFace ;
2019-08-03 15:39:41 +00:00
import org.bukkit.block.Container ;
2019-08-01 09:01:43 +00:00
import org.bukkit.inventory.Inventory ;
2016-04-14 16:24:03 +00:00
import org.bukkit.inventory.ItemStack ;
public class SlimefunMachine extends SlimefunItem {
2019-05-04 07:25:54 +00:00
private List < ItemStack [ ] > recipes ;
private List < ItemStack > shownRecipes ;
private Material trigger ;
2019-08-01 09:01:43 +00:00
//Adjacent blockfaces for iterative output chest checks
2019-08-03 15:39:41 +00:00
private static final BlockFace [ ] outputFaces = {
2019-08-01 09:01:43 +00:00
BlockFace . UP ,
BlockFace . NORTH ,
BlockFace . EAST ,
BlockFace . SOUTH ,
BlockFace . WEST
} ;
2016-04-14 16:24:03 +00:00
2017-10-13 18:59:52 +00:00
public SlimefunMachine ( Category category , ItemStack item , String id , ItemStack [ ] recipe , ItemStack [ ] machineRecipes , Material trigger ) {
super ( category , item , id , RecipeType . MULTIBLOCK , recipe ) ;
2019-05-04 07:25:54 +00:00
this . recipes = new ArrayList < > ( ) ;
this . shownRecipes = new ArrayList < > ( ) ;
this . shownRecipes . addAll ( Arrays . asList ( machineRecipes ) ) ;
2016-04-14 16:24:03 +00:00
this . trigger = trigger ;
}
2017-10-13 18:59:52 +00:00
public SlimefunMachine ( Category category , ItemStack item , String id , ItemStack [ ] recipe , ItemStack [ ] machineRecipes , Material trigger , boolean ghost ) {
super ( category , item , id , RecipeType . MULTIBLOCK , recipe , ghost ) ;
2019-05-04 07:25:54 +00:00
this . recipes = new ArrayList < > ( ) ;
this . shownRecipes = new ArrayList < > ( ) ;
this . shownRecipes . addAll ( Arrays . asList ( machineRecipes ) ) ;
2016-04-14 16:24:03 +00:00
this . trigger = trigger ;
}
2017-10-13 18:59:52 +00:00
public SlimefunMachine ( Category category , ItemStack item , String id , ItemStack [ ] recipe , ItemStack [ ] machineRecipes , Material trigger , String [ ] keys , Object [ ] values ) {
super ( category , item , id , RecipeType . MULTIBLOCK , recipe , keys , values ) ;
2019-05-04 07:25:54 +00:00
this . recipes = new ArrayList < > ( ) ;
this . shownRecipes = new ArrayList < > ( ) ;
this . shownRecipes . addAll ( Arrays . asList ( machineRecipes ) ) ;
2016-04-14 16:24:03 +00:00
this . trigger = trigger ;
}
public List < ItemStack [ ] > getRecipes ( ) {
return this . recipes ;
}
public List < ItemStack > getDisplayRecipes ( ) {
return this . shownRecipes ;
}
2019-08-03 15:39:41 +00:00
public static BlockFace [ ] getOutputFaces ( ) {
return outputFaces ;
2019-08-01 09:01:43 +00:00
}
2016-04-14 16:24:03 +00:00
public void addRecipe ( ItemStack [ ] input , ItemStack output ) {
this . recipes . add ( input ) ;
this . recipes . add ( new ItemStack [ ] { output } ) ;
}
@Override
public void create ( ) {
this . toMultiBlock ( ) . register ( ) ;
}
@Override
public void install ( ) {
for ( ItemStack i : this . getDisplayRecipes ( ) ) {
SlimefunItem item = SlimefunItem . getByItem ( i ) ;
if ( item = = null ) this . recipes . add ( new ItemStack [ ] { i } ) ;
else if ( ! SlimefunItem . isDisabled ( i ) ) this . recipes . add ( new ItemStack [ ] { i } ) ;
}
}
public MultiBlock toMultiBlock ( ) {
2019-05-04 07:25:54 +00:00
List < Material > mats = new ArrayList < > ( ) ;
2017-10-13 13:56:48 +00:00
for ( ItemStack i : this . getRecipe ( ) ) {
2016-04-14 16:24:03 +00:00
if ( i = = null ) mats . add ( null ) ;
else if ( i . getType ( ) = = Material . FLINT_AND_STEEL ) mats . add ( Material . FIRE ) ;
else mats . add ( i . getType ( ) ) ;
}
Material [ ] build = mats . toArray ( new Material [ mats . size ( ) ] ) ;
return new MultiBlock ( build , this . trigger ) ;
}
public Iterator < ItemStack [ ] > recipeIterator ( ) {
return this . recipes . iterator ( ) ;
}
2019-08-01 09:01:43 +00:00
// Overloaded method for finding a potential output chest. Fallbacks to the old system of putting the adding back into the dispenser.
// Optional last argument Inventory placeCheckerInv is for multiblock machines that create a dummy inventory to check if there's a space for the adding,
// i.e. Enhanced crafting table
public static Inventory findValidOutputInv ( ItemStack adding , Block dispBlock , Inventory dispInv ) {
return findValidOutputInv ( adding , dispBlock , dispInv , dispInv ) ;
}
public static Inventory findValidOutputInv ( ItemStack product , Block dispBlock , Inventory dispInv , Inventory placeCheckerInv ) {
Inventory outputInv = null ;
2019-08-03 15:39:41 +00:00
for ( BlockFace face : outputFaces ) {
2019-08-01 09:01:43 +00:00
Block potentialOutput = dispBlock . getRelative ( face ) ;
2019-08-03 15:39:41 +00:00
String id = BlockStorage . checkID ( potentialOutput ) ;
if ( id ! = null & & id . equals ( " OUTPUT_CHEST " ) ) {
// Found the output chest! Now, let's check if we can fit the product in it.
Inventory inv = ( ( Container ) potentialOutput . getState ( ) ) . getInventory ( ) ;
if ( InvUtils . fits ( inv , product ) ) {
2019-08-01 09:01:43 +00:00
// It fits! Let's set the inventory to that now.
2019-08-03 15:39:41 +00:00
outputInv = inv ;
2019-08-01 10:03:08 +00:00
break ;
2019-08-01 09:01:43 +00:00
}
}
}
// This if-clause will trigger if no suitable output chest was found. It's functionally the same as the old fit check for the dispenser, only refactored.
if ( outputInv = = null & & InvUtils . fits ( placeCheckerInv , product ) ) outputInv = dispInv ;
return outputInv ;
}
2016-04-14 16:24:03 +00:00
}