1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-21 12:15:50 +00:00
Slimefun4/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunMachine.java

133 lines
4.6 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem;
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 {
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
public SlimefunMachine(Category category, ItemStack item, String id, ItemStack[] recipe, ItemStack[] machineRecipes, Material trigger) {
super(category, item, id, RecipeType.MULTIBLOCK, recipe);
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 SlimefunMachine(Category category, ItemStack item, String id, ItemStack[] recipe, ItemStack[] machineRecipes, Material trigger, boolean ghost) {
super(category, item, id, RecipeType.MULTIBLOCK, recipe, ghost);
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 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);
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() {
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:52:10 +00:00
String id = BlockStorage.checkID(potentialOutput);
if (id != null && id.equals("OUTPUT_CHEST")) {
2019-08-03 15:39:41 +00:00
// 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
}