Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlatingPressBlockEntity extends InventoryBlockEntity {
- public static final Component TITLE = new TranslatableComponent(
- "container." + TankMod.MOD_ID + ".plating_press");
- public PlatingPressBlockEntity(BlockPos pos, BlockState state) {
- super(TankModBlockEntities.PLATING_PRESS.get(), pos, state, 5);
- }
- public static void tick(Level world, PlatingPressBlockEntity entity) {
- if (hasRecipe(entity)) {
- craftItem(entity);
- }
- }
- private static boolean hasRecipe(PlatingPressBlockEntity entity) {
- Level world = entity.level;
- SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
- for (int i = 0; i < entity.inventory.getSlots(); i++) {
- inventory.setItem(i, entity.getItemInSlot(i));
- }
- Optional<PlatingPressRecipe> match = world.getRecipeManager()
- .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
- return match.isPresent()
- && canInsertAmountIntoOutputSlot(inventory)
- && canInsertItemIntoOutputSlot(inventory, match.get().getResultItem());
- }
- private static void craftItem(PlatingPressBlockEntity entity) {
- Level world = entity.level;
- SimpleContainer inventory = new SimpleContainer(entity.inventory.getSlots());
- for (int i = 0; i < entity.inventory.getSlots(); i++) {
- inventory.setItem(i, entity.inventory.getStackInSlot(i));
- }
- Optional<PlatingPressRecipe> match = world.getRecipeManager()
- .getRecipeFor(PlatingPressRecipe.Type.INSTANCE, inventory, world);
- if (match.isPresent()) {
- entity.extractItem(0);
- entity.extractItem(1);
- entity.extractItem(2);
- entity.extractItem(3);
- entity.insertItem(4, new ItemStack(match.get().getResultItem().getItem(),
- 1));
- }
- }
- private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
- return inventory.getItem(4).getItem() == output.getItem() || inventory.getItem(4).isEmpty();
- }
- private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
- return inventory.getItem(4).getMaxStackSize() > inventory.getItem(4).getCount();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment