Jump to content

My mixing furnace doesnt work


vntlyy007

Recommended Posts

Hi guys, i have a not default furnace (with one extra slot), but it has default furnace code in basic

I cant set lit the furnace(smth with serverTick or components in it)

 

Spoiler
public MixingBlockEntity(BlockPos p_155229_, BlockState p_155230_) {
        super(ModBlockEntities.MIXING_BLOCK_ENTITY.get(), p_155229_, p_155230_);
        this.recipeType = MixingRecipe.Type.INSTANCE;
        this.data = new ContainerData() {
            public int get(int p_58431_) {
                switch (p_58431_) {
                    case 0:
                        return MixingBlockEntity.this.litTime;
                    case 1:
                        return MixingBlockEntity.this.litDuration;
                    case 2:
                        return MixingBlockEntity.this.cookingProgress;
                    case 3:
                        return MixingBlockEntity.this.cookingTotalTime;
                    default:
                        return 0;
                }
            }

            public void set(int p_58433_, int p_58434_) {
                switch (p_58433_) {
                    case 0:
                        MixingBlockEntity.this.litTime = p_58434_;
                    case 1:
                        MixingBlockEntity.this.litDuration = p_58434_;
                    case 2:
                        MixingBlockEntity.this.cookingProgress = p_58434_;
                    case 3:
                        MixingBlockEntity.this.cookingTotalTime = p_58434_;
                }

            }

            public int getCount() {
                return 3;
            }
        };
    }

    @Override
    public Component getDisplayName() {
        return new TextComponent("Mixing Block");
    }

    @Nullable
    @Override
    public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, Player pPlayer) {
        return new MixingMenu(pContainerId, pInventory, this, this.data);
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @javax.annotation.Nullable Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return lazyItemHandler.cast();
        }

        return super.getCapability(cap, side);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        lazyItemHandler = LazyOptional.of(() -> itemHandler);
    }

    @Override
    public void invalidateCaps()  {
        super.invalidateCaps();
        lazyItemHandler.invalidate();
    }

    @Override
    protected void saveAdditional(@NotNull CompoundTag tag) {
        tag.put("inventory", itemHandler.serializeNBT());
        ContainerHelper.saveAllItems(tag, items);
        tag.putInt("BurnTime", litTime);
        tag.putInt("CookTime", cookingProgress);
        tag.putInt("CookTimeTotal", cookingTotalTime);
        super.saveAdditional(tag);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        items = NonNullList.withSize(getContainerSize(), ItemStack.EMPTY);
        itemHandler.deserializeNBT(nbt.getCompound("inventory"));
        cookingProgress = nbt.getInt("CookTime");
        cookingTotalTime = nbt.getInt("CookTimeTotal");
        litDuration = getBurnDuration(items.get(2));
    }


    public int getContainerSize() {
        return this.items.size();
    }

    public void drops() {
        SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            inventory.setItem(i, itemHandler.getStackInSlot(i));
        }

        Containers.dropContents(this.level, this.worldPosition, inventory);
    }

    protected int getBurnDuration(ItemStack p_58343_) {
        if (p_58343_.isEmpty()) {
            return 0;
        } else {
            Item item = p_58343_.getItem();
            return net.minecraftforge.common.ForgeHooks.getBurnTime(p_58343_, null);
        }
    }
    private static int getTotalCookTime(@NotNull MixingBlockEntity entity) {
        Level level = entity.level;
        SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
        for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
            inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
        }

        Optional<MixingRecipe> match = level.getRecipeManager()
                .getRecipeFor(MixingRecipe.Type.INSTANCE, inventory, level);
        if(!match.isPresent()) {return match.map(MixingRecipe::getCookingTime).orElse(200);}
        else return 200;
    }
    private boolean isLit() {
        return this.litTime > 0;
    }

//    public static void serverTicks(Level pLevel, BlockPos pPos, BlockState pState, MixingBlockEntity pBlockEntity) {
//        if (isLit(pBlockEntity)) {
//            --pBlockEntity.litTime;
//            System.out.println(" stas gay == false ");
//        }
//
//        boolean flag = isLit(pBlockEntity);
//        boolean flag1 = false;
//        ItemStack itemstack = pBlockEntity.items.get(1);
//        if(isLit(pBlockEntity) || hasRecipe(pBlockEntity)) {
//            if (!isLit(pBlockEntity) && canBurn(pBlockEntity)) {
//                pBlockEntity.litTime = pBlockEntity.getBurnDuration(itemstack);
//                pBlockEntity.litDuration = pBlockEntity.litTime;
//                timeSet(pBlockEntity);
//                //TRUE
//                if (isLit(pBlockEntity)) {
//                    System.out.println('A');
//                    flag1 = true;
//                    if (itemstack.hasContainerItem())
//                        pBlockEntity.items.set(1, itemstack.getContainerItem());
//                    else if (!itemstack.isEmpty()) {
//                        //Item item = itemstack.getItem();
//                        itemstack.shrink(1);
//                        if (itemstack.isEmpty()) {
//                            pBlockEntity.items.set(1, itemstack.getContainerItem());
//                        }
//                    }
//                }
//            }
//            if (isLit(pBlockEntity) && canBurn(pBlockEntity)) {
//                System.out.println('B');
//                ++pBlockEntity.cookingProgress;
//                if (pBlockEntity.cookingProgress == pBlockEntity.cookingTotalTime) {
//                    pBlockEntity.cookingProgress = 0;
//                    craftItem(pBlockEntity);
//                    timeSet(pBlockEntity);
//                    flag1 = true;
//                }
//            }
//        } else if (!isLit(pBlockEntity) && pBlockEntity.cookingProgress > 0) {
//            System.out.println('J');
//            pBlockEntity.cookingProgress = Mth.clamp(pBlockEntity.cookingProgress - 2, 0, pBlockEntity.cookingTotalTime);
//        }
//        if (flag1) {
//            setChanged(pLevel, pPos, pState);
//        }
//    }

    public static void serverTick(Level p_155014_, BlockPos p_155015_, BlockState p_155016_, MixingBlockEntity p_155017_) {
        boolean flag = p_155017_.isLit();
        boolean flag1 = false;
        if (p_155017_.isLit()) {
            System.out.println('1');
            --p_155017_.litTime;
        }
        if (p_155017_.canBurn(p_155017_)) {
            System.out.println('B');
        }


        ItemStack itemstack = p_155017_.items.get(1);
        if (p_155017_.isLit() || !itemstack.isEmpty() && !p_155017_.items.get(0).isEmpty()) {
            if (!p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                p_155017_.litTime = p_155017_.getBurnDuration(itemstack);
                p_155017_.litDuration = p_155017_.litTime;
                System.out.println('P');
                if (p_155017_.isLit()) {
                    System.out.println('2');
                    flag1 = true;
                    if (itemstack.hasContainerItem())
                        p_155017_.items.set(1, itemstack.getContainerItem());
                    else
                    if (!itemstack.isEmpty()) {
                        Item item = itemstack.getItem();
                        itemstack.shrink(1);
                        if (itemstack.isEmpty()) {
                            p_155017_.items.set(1, itemstack.getContainerItem());
                        }
                    }
                }
            }

            if (p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                ++p_155017_.cookingProgress;
                if (p_155017_.cookingProgress == p_155017_.cookingTotalTime) {
                    p_155017_.cookingProgress = 0;
                    craftItem(p_155017_);
                    p_155017_.cookingTotalTime = getTotalCookTime(p_155017_);
                    flag1 = true;
                }
            } else {
                p_155017_.cookingProgress = 0;
            }
        } else if (!p_155017_.isLit() && p_155017_.cookingProgress > 0) {
            p_155017_.cookingProgress = Mth.clamp(p_155017_.cookingProgress - 2, 0, p_155017_.cookingTotalTime);
        }

        if (flag != p_155017_.isLit()) {
            flag1 = true;
            p_155016_ = p_155016_.setValue(AbstractFurnaceBlock.LIT, Boolean.valueOf(p_155017_.isLit()));
            p_155014_.setBlock(p_155015_, p_155016_, 3);
        }

        if (flag1) {
            setChanged(p_155014_, p_155015_, p_155016_);
        }

    }

    public static void timeSet(@NotNull MixingBlockEntity pBlockEntity){
        pBlockEntity.cookingTotalTime = getTotalCookTime(pBlockEntity);
    }
    private static boolean hasRecipe(@NotNull MixingBlockEntity entity) {
        Level level = entity.level;
        SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
        for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
            inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
        }

        Optional<MixingRecipe> match = level.getRecipeManager()
                .getRecipeFor(MixingRecipe.Type.INSTANCE, inventory, level);

        return match.isPresent() && canInsertAmountIntoOutputSlot(inventory)
                && canInsertItemIntoOutputSlot(inventory, match.get().getResultItem());
    }


    private boolean canBurn(@NotNull MixingBlockEntity entity) {
        NonNullList<ItemStack> items = this.items;
        if (!items.get(1).isEmpty() && !items.get(0).isEmpty() &&
                !items.get(3).isEmpty() && hasRecipe(entity)) {
            return true;
        } else {
            return false;
        }
    }

    private static void craftItem(MixingBlockEntity entity) {
        Level level = entity.level;
        SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
        for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
            inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
        }

        Optional<CleaningTableRecipe> match = level.getRecipeManager()
                .getRecipeFor(CleaningTableRecipe.Type.INSTANCE, inventory, level);

        if(match.isPresent()) {
            entity.itemHandler.extractItem(0,1, false);

            entity.itemHandler.extractItem(3,1, false);


            entity.itemHandler.setStackInSlot(2, new ItemStack(match.get().getResultItem().getItem(),
                    entity.itemHandler.getStackInSlot(2).getCount() + 1));

            entity.resetProgress();
        }
    }


    private void resetProgress() {
        this.cookingProgress = 0;
    }
    private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
        return inventory.getItem(2).getItem() == output.getItem() || inventory.getItem(2).isEmpty();
    }

    private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
        return inventory.getItem(2).getMaxStackSize() > inventory.getItem(2).getCount();
    }

 

The result - System.out.println('P')

 

Annotated code is my version of serverTick

Not annotated - AbstractFurnaceBlockEnity 

Edited by vntlyy007
Link to comment
Share on other sites

Did you update the Slot indexes of your MixingBlockEntity? If you have 2 input slots, I assume that slot index 0 & 1 are input but in vanilla 0 is the input Slot and 1 is the Fuel slot.

Did you tried to use debugger to check which method does not return the correct value?

Link to comment
Share on other sites

Hi man, i have:

0 - input slot

1 - fuel slot

2 - output slot

3 - addition slot

 

I've just checked debug mod, so it doesnt work:

    protected int getBurnDuration(ItemStack p_58343_) {
        if (p_58343_.isEmpty()) {
            return 0;
        } else {
            Item item = p_58343_.getItem();
            return net.minecraftforge.common.ForgeHooks.getBurnTime(p_58343_, null);
        }
    }

it return 0*

My server tick using this int:

ItemStack itemstack = p_155017_.items.get(1);
p_155017_.litTime = p_155017_.getBurnDuration(itemstack);
p_155017_.litDuration = p_155017_.litTime;

 

Link to comment
Share on other sites

4 hours ago, Luis_ST said:

Причина цього проста: вам потрібно передати RecipeType у ForgeHooks#getBurnTime.

Які предмети ви пробували як пальне?

Wood and coal

How can i pass it? I don't really understand))

Link to comment
Share on other sites

4 minutes ago, vntlyy007 said:

How can i pass it? I don't really understand

Put the item in your fuel slot!?

3 minutes ago, vntlyy007 said:

Okay, even when getBurmDuration return the 200, my code still doesn't work. It's sad

Did you check with debugger where to code now stops working?

Link to comment
Share on other sites

12 minutes ago, Luis_ST said:

Did you check with debugger where to code now stops working?

    public static void serverTick(Level p_155014_, BlockPos p_155015_, BlockState p_155016_, MixingBlockEntity p_155017_) {
        boolean flag = p_155017_.isLit();
        boolean flag1 = false;
        if (p_155017_.isLit()) {
            System.out.println('1');
            --p_155017_.litTime;
        }
        if (p_155017_.canBurn(p_155017_)) {
            System.out.println('B');
        }																// HERE
        ItemStack itemstack = p_155017_.items.get(1);
        if (p_155017_.isLit() || !itemstack.isEmpty() && !p_155017_.items.get(0).isEmpty()) {
            if (!p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                p_155017_.litTime = p_155017_.getBurnDuration(itemstack);
                p_155017_.litDuration = p_155017_.litTime;
                if (p_155017_.isLit()) {
                    flag1 = true;
                    if (itemstack.hasContainerItem())
                        p_155017_.items.set(1, itemstack.getContainerItem());
                    else
                    if (!itemstack.isEmpty()) {
                        Item item = itemstack.getItem();
                        itemstack.shrink(1);
                        if (itemstack.isEmpty()) {
                            p_155017_.items.set(1, itemstack.getContainerItem());
                        }
                    }
                }
            }

            if (p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                ++p_155017_.cookingProgress;
                if (p_155017_.cookingProgress == p_155017_.cookingTotalTime) {
                    p_155017_.cookingProgress = 0;
                    craftItem(p_155017_);
                    p_155017_.cookingTotalTime = getTotalCookTime(p_155017_);
                    flag1 = true;
                }
            } else {
                p_155017_.cookingProgress = 0;
            }
        } else if (!p_155017_.isLit() && p_155017_.cookingProgress > 0) {
            p_155017_.cookingProgress = Mth.clamp(p_155017_.cookingProgress - 2, 0, p_155017_.cookingTotalTime);
        }

        if (flag != p_155017_.isLit()) {
            flag1 = true;
            p_155016_ = p_155016_.setValue(AbstractFurnaceBlock.LIT, Boolean.valueOf(p_155017_.isLit()));
            p_155014_.setBlock(p_155015_, p_155016_, 3);
        }

        if (flag1) {
            System.out.println('1');
            setChanged(p_155014_, p_155015_, p_155016_);
        }

    }

It's strange, idk, i didn't change anything))

Link to comment
Share on other sites

47 minutes ago, Luis_ST said:

Будь ласка, опублікуйте git repo вашого мода, я хочу налагодити це локально.

Після публікації повного репозиторію Git, будь ласка, скажіть мені, як відтворити вашу проблему (якомога детальніше).

GIT, I'll be grateful for any help)

Link to comment
Share on other sites

There are two serverTick voids. 

These two options are the basis of what I did (they don't work) in which you can do everything from scratch if you need to. I have one version where smelting is possible, but with a bunch of unfinished stuff, such as the need for coal for smelting.

public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, MixingBlockEntity pBlockEntity) {
        if (pBlockEntity.isLit()) {
            --pBlockEntity.litTime;
            System.out.println("GOOD");
        }

        boolean flag = pBlockEntity.isLit();
        boolean flag1 = false;
        ItemStack itemstack = pBlockEntity.items.get(1);
        if(pBlockEntity.isLit() || hasRecipe(pBlockEntity)) {
            if (!pBlockEntity.isLit() && pBlockEntity.canBurn(pBlockEntity)) {
                pBlockEntity.litTime = pBlockEntity.getBurnDuration(itemstack);
                pBlockEntity.litDuration = pBlockEntity.litTime;
                timeSet(pBlockEntity);
                //TRUE
                if (pBlockEntity.isLit()) {
                    System.out.println('A');
                    flag1 = true;
                    itemstack.shrink(1);
                }
            }
            if (pBlockEntity.isLit() && pBlockEntity.canBurn(pBlockEntity)) {
                System.out.println('B');
                ++pBlockEntity.cookingProgress;
                if (pBlockEntity.cookingProgress == pBlockEntity.cookingTotalTime) {
                    pBlockEntity.cookingProgress = 0;
                    craftItem(pBlockEntity);
                    timeSet(pBlockEntity);
                    flag1 = true;
                }
            }
        } else if (!pBlockEntity.isLit() && pBlockEntity.cookingProgress > 0) {
            System.out.println('C');
            pBlockEntity.cookingProgress = Mth.clamp(pBlockEntity.cookingProgress - 2, 0, pBlockEntity.cookingTotalTime);
        }
        if (flag1) {
            setChanged(pLevel, pPos, pState);
        }
    }

 

I'm trying to add a dependence on coal by using, but it does not work:

if(pBlockEntity.isLit() || hasRecipe(pBlockEntity) && !itemstack.isEmpty()) {

As far as I understand, the problem is that this option of getting a slot does not work in this method(it was copied from AbstractFurnaceBlockEntity)

So you can use this void to try find a problem

The code of mod in GIT maximally simplified and in methods where it is necessary to obtain some information (for example: getTotalCookingTime always gives 400, etc.) I have a done code for obtaining these data from the recipe, so this is not a problem to get it if you need

Edited by vntlyy007
Link to comment
Share on other sites

You main issue is that you copied the vanilla Furnace code, then you add some stuff without changing the code you copied.
Your Menu use a ItemStackHandler, your BlockEntity a NonNullList.
If you add Items to the Menu they are stored in the ItemStackHandler of your BlockEntity but the code still use the vanilla NonNullList.
If you then check if there is a Item in Slot 0, you're using the NonNullList which is empty, since you add the Items to the ItemStackHandler.

How to fix your code:

  1. Remove all vanilla code which is unnecessary (the Constans, unused methods, ...), this include the NonNullList
  2. Add names to the local variables and parameter
  3. Initialize your LazyOptional for the ItemStackHandler directly at declaration
  4. Fix all errors caused by step 1
  5. Then choose on serverTick method which you want to keep, remove the other
    Put a few breakpoints into the serverTick method to see if it work now:
    1. If it work try to add the missing logic for your BlockEntity
    2. If not fix the issues in the serverTick method, your IDE should support hot swapping,
      this means you can edit the code while the game is still running (only works in debug mode).
      After saving the changes will be load in the JVM and you can directly test them.

If you have issues while debugging feel free to come back and ask.

Note to hot swapping: Hot Swapping only allows changes in loaded class structure, this means you can not add or remove methods/fields.

Link to comment
Share on other sites

On 8/25/2022 at 9:26 PM, Luis_ST said:

You main issue is that you copied the vanilla Furnace code, then you add some stuff without changing the code you copied.
Your Menu use a ItemStackHandler, your BlockEntity a NonNullList.
If you add Items to the Menu they are stored in the ItemStackHandler of your BlockEntity but the code still use the vanilla NonNullList.
If you then check if there is a Item in Slot 0, you're using the NonNullList which is empty, since you add the Items to the ItemStackHandler.

Thanks a lot man you helped me a lot! I seem to have figured everything out, cleaned the code and everything works)

My final code is:

Spoiler
public class MixingBlockEntity extends BlockEntity implements  MenuProvider {

    private final ItemStackHandler itemHandler = new ItemStackHandler(4) {
        @Override
        protected void onContentsChanged(int Slot) {
            setChanged();
        }
    };

    protected final ContainerData data;

    int litTime;
    int litDuration;
    int cookingProgress;
    int cookingTotalTime;

    private final RecipeType<MixingRecipe> recipeType;

    public MixingBlockEntity(BlockPos p_155229_, BlockState p_155230_) {
        super(ModBlockEntities.MIXING_BLOCK_ENTITY.get(), p_155229_, p_155230_);
        this.recipeType = MixingRecipe.Type.INSTANCE;
        this.data = new ContainerData() {
            public int get(int p_58431_) {
                switch (p_58431_) {
                    case 0:
                        return MixingBlockEntity.this.litTime;
                    case 1:
                        return MixingBlockEntity.this.litDuration;
                    case 2:
                        return MixingBlockEntity.this.cookingProgress;
                    case 3:
                        return MixingBlockEntity.this.cookingTotalTime;
                    default:
                        return 0;
                }
            }

            public void set(int p_58433_, int p_58434_) {
                switch (p_58433_) {
                    case 0:
                        MixingBlockEntity.this.litTime = p_58434_;
                    case 1:
                        MixingBlockEntity.this.litDuration = p_58434_;
                    case 2:
                        MixingBlockEntity.this.cookingProgress = p_58434_;
                    case 3:
                        MixingBlockEntity.this.cookingTotalTime = p_58434_;
                }
            }
            public int getCount() {
                return 4;
            }
        };
    }

    @Override
    public Component getDisplayName() {
        return new TextComponent("Mixing Block");
    }

    @Nullable
    @Override
    public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, Player pPlayer) {
        return new MixingMenu(pContainerId, pInventory, this, this.data);
    }

    private LazyOptional<IItemHandler> lazyItemHandler;
    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @javax.annotation.Nullable Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return lazyItemHandler.cast();
        }

        return super.getCapability(cap, side);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        lazyItemHandler = LazyOptional.of(() -> itemHandler);
    }

    @Override
    public void invalidateCaps()  {
        super.invalidateCaps();
        lazyItemHandler.invalidate();
    }
    @Override
    protected void saveAdditional(@NotNull CompoundTag tag) {
        tag.put("inventory", itemHandler.serializeNBT());
        tag.putInt("BurnTime", litTime);
        tag.putInt("CookTime", cookingProgress);
        tag.putInt("CookTimeTotal", cookingTotalTime);
        super.saveAdditional(tag);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        itemHandler.deserializeNBT(nbt.getCompound("inventory"));
        cookingProgress = nbt.getInt("CookTime");
        cookingTotalTime = nbt.getInt("CookTimeTotal");
        litDuration = getBurnDuration(itemHandler.getStackInSlot(1));
    }


    public void drops() {
        SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            inventory.setItem(i, itemHandler.getStackInSlot(i));
        }

        Containers.dropContents(this.level, this.worldPosition, inventory);
    }

    protected int getBurnDuration(ItemStack p_58343_) {
        if (p_58343_.isEmpty()) {
            return 0;
        } else {
            return ForgeHooks.getBurnTime(p_58343_, this.recipeType);
        }
    }

    private Optional<MixingRecipe> match(@NotNull MixingBlockEntity entity){
        Level level = entity.level;
        SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
        for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
            inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
        }

        return level.getRecipeManager()
                .getRecipeFor(MixingRecipe.Type.INSTANCE, inventory, level);
    }

    private static int getTotalCookTime(@NotNull MixingBlockEntity entity) {
        Optional<MixingRecipe> match = entity.match(entity);
    //    match.map(MixingRecipe::getCookingTime).orElse(200);
        return match.map(MixingRecipe::getCookingTime).orElse(200);
    }
    private boolean isLit() {
        return this.litTime > 0;
    }


    public static void serverTick(Level p_155014_, BlockPos p_155015_, BlockState p_155016_, MixingBlockEntity p_155017_) {
        boolean flag = p_155017_.isLit();
        boolean flag1 = false;
        if (p_155017_.isLit()) {
            --p_155017_.litTime;
        }


        ItemStack itemstack = p_155017_.itemHandler.getStackInSlot(1);

        if (p_155017_.isLit() || !itemstack.isEmpty() && p_155017_.hasRecipe(p_155017_) && !p_155017_.itemHandler.getStackInSlot(0).isEmpty() && !p_155017_.itemHandler.getStackInSlot(3).isEmpty()) {
            if (!p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                p_155017_.litTime = p_155017_.getBurnDuration(itemstack);
                p_155017_.litDuration = p_155017_.litTime;
                if (p_155017_.isLit()) {
                    flag1 = true;
                    if (itemstack.hasContainerItem())
                        p_155017_.itemHandler.setStackInSlot(1, itemstack.getContainerItem());
                    else if (!itemstack.isEmpty()) {
                        Item item = itemstack.getItem();
                        itemstack.shrink(1);
                        if (itemstack.isEmpty()) {
                            p_155017_.itemHandler.setStackInSlot(1, itemstack.getContainerItem());
                        }
                    }
                }
            }


            if (p_155017_.isLit() && p_155017_.canBurn(p_155017_)) {
                ++p_155017_.cookingProgress;
                if (p_155017_.cookingProgress == p_155017_.cookingTotalTime) {
                    p_155017_.cookingProgress = 0;
                    p_155017_.cookingTotalTime = getTotalCookTime(p_155017_);
                    flag1 = true;
                    craftItem(p_155017_);
                }
            } else {
                p_155017_.cookingProgress = 0;
            }
        } else if (!p_155017_.isLit() && p_155017_.cookingProgress > 0) {
            p_155017_.cookingProgress = Mth.clamp(p_155017_.cookingProgress - 2, 0, p_155017_.cookingTotalTime);
        }

        if (flag != p_155017_.isLit()) {
            flag1 = true;
            p_155016_ = p_155016_.setValue(MixingBlock.LIT, Boolean.valueOf(p_155017_.isLit()));
            p_155014_.setBlock(p_155015_, p_155016_, 3);
        }

        if (flag1) {
            setChanged(p_155014_, p_155015_, p_155016_);
        }
    }

    private boolean hasRecipe(@NotNull MixingBlockEntity entity) {
        Optional<MixingRecipe> match = entity.match(entity);
        SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots());
        for (int i = 0; i < entity.itemHandler.getSlots(); i++) {
            inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
        }
        return match.isPresent() && canInsertAmountIntoOutputSlot(inventory)
                && canInsertItemIntoOutputSlot(inventory, match.get().getResultItem()) ;
    }

//                match.get().getIngredients().get(0).test(inventory.getItem(0)) &&
//            match.get().getIngredients().get(1).test(inventory.getItem(3))

    private boolean canBurn(@NotNull MixingBlockEntity entity) {
        ItemStackHandler items = entity.itemHandler;
        return !items.getStackInSlot(0).isEmpty() && !items.getStackInSlot(3).isEmpty() &&
                hasRecipe(entity);
    }

    private static void craftItem(MixingBlockEntity entity) {
        Optional<MixingRecipe> match = entity.match(entity);

        if(match.isPresent()) {
            entity.itemHandler.extractItem(0,1, false);

            entity.itemHandler.extractItem(3,1, false);


            entity.itemHandler.setStackInSlot(2, new ItemStack(match.get().getResultItem().getItem(),
                    entity.itemHandler.getStackInSlot(2).getCount() + 1));

            entity.cookingProgress = 0;
        }
    }


    private static boolean canInsertItemIntoOutputSlot(SimpleContainer inventory, ItemStack output) {
        return inventory.getItem(2).getItem() == output.getItem() || inventory.getItem(2).isEmpty();
    }

    private static boolean canInsertAmountIntoOutputSlot(SimpleContainer inventory) {
        return inventory.getItem(2).getMaxStackSize() > inventory.getItem(2).getCount();
    }
}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.