Skip to content

Commit

Permalink
feat: switch to using crafting behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 20, 2024
1 parent 4963979 commit 3a3a971
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package com.klikli_dev.theurgy.content.apparatus.mercurycatalyst;

import com.klikli_dev.theurgy.content.behaviour.crafting.CraftingBehaviour;
import com.klikli_dev.theurgy.content.behaviour.crafting.HasCraftingBehaviour;
import com.klikli_dev.theurgy.content.capability.DefaultMercuryFluxStorage;
import com.klikli_dev.theurgy.content.recipe.CatalysationRecipe;
import com.klikli_dev.theurgy.content.recipe.input.ItemHandlerRecipeInput;
import com.klikli_dev.theurgy.content.storage.MonitoredItemStackHandler;
import com.klikli_dev.theurgy.registry.BlockEntityRegistry;
import com.klikli_dev.theurgy.registry.CapabilityRegistry;
Expand All @@ -21,6 +24,7 @@
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -29,7 +33,7 @@
import org.jetbrains.annotations.Nullable;


public class MercuryCatalystBlockEntity extends BlockEntity {
public class MercuryCatalystBlockEntity extends BlockEntity implements HasCraftingBehaviour<ItemHandlerRecipeInput, CatalysationRecipe, RecipeManager.CachedCheck<ItemHandlerRecipeInput, CatalysationRecipe>> {

public static final int CAPACITY = 50000;

Expand All @@ -39,7 +43,7 @@ public class MercuryCatalystBlockEntity extends BlockEntity {
public ItemStackHandler inventory;
public MercuryCatalystMercuryFluxStorage mercuryFluxStorage;

protected CraftingBehaviour<?, ?, ?> craftingBehaviour;
protected MercuryCatalystCraftingBehaviour craftingBehaviour;

public MercuryCatalystBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(BlockEntityRegistry.MERCURY_CATALYST.get(), pPos, pBlockState);
Expand Down Expand Up @@ -178,6 +182,11 @@ protected void collectImplicitComponents(DataComponentMap.Builder pComponents) {
this.craftingBehaviour.collectImplicitComponents(pComponents);
}

@Override
public CraftingBehaviour<ItemHandlerRecipeInput, CatalysationRecipe, RecipeManager.CachedCheck<ItemHandlerRecipeInput, CatalysationRecipe>> craftingBehaviour() {
return this.craftingBehaviour;
}

private class Inventory extends MonitoredItemStackHandler {
public Inventory() {
super(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

package com.klikli_dev.theurgy.content.apparatus.salammoniacaccumulator;

import com.klikli_dev.theurgy.content.behaviour.crafting.CraftingBehaviour;
import com.klikli_dev.theurgy.content.behaviour.crafting.HasCraftingBehaviour;
import com.klikli_dev.theurgy.content.particle.ParticleColor;
import com.klikli_dev.theurgy.content.particle.coloredbubble.ColoredBubbleParticleProvider;
import com.klikli_dev.theurgy.content.recipe.AccumulationRecipe;
import com.klikli_dev.theurgy.content.recipe.input.ItemHandlerWithFluidRecipeInput;
import com.klikli_dev.theurgy.content.storage.MonitoredItemStackHandler;
import com.klikli_dev.theurgy.registry.BlockEntityRegistry;
import com.klikli_dev.theurgy.registry.ItemTagRegistry;
Expand Down Expand Up @@ -36,7 +40,7 @@

import java.util.function.Predicate;

public class SalAmmoniacAccumulatorBlockEntity extends BlockEntity implements GeoBlockEntity {
public class SalAmmoniacAccumulatorBlockEntity extends BlockEntity implements GeoBlockEntity, HasCraftingBehaviour<ItemHandlerWithFluidRecipeInput, AccumulationRecipe, SalAmmoniacAccumulatorCachedCheck> {

protected final AnimatableInstanceCache animatableInstanceCache = GeckoLibUtil.createInstanceCache(this);

Expand Down Expand Up @@ -204,6 +208,11 @@ public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.animatableInstanceCache;
}

@Override
public CraftingBehaviour<ItemHandlerWithFluidRecipeInput, AccumulationRecipe, SalAmmoniacAccumulatorCachedCheck> craftingBehaviour() {
return this.craftingBehaviour;
}

public class WaterTank extends FluidTank {
public WaterTank(int capacity, Predicate<FluidStack> validator) {
super(capacity, validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* A custom cached check
*/
class SalAmmoniacAccumulatorCachedCheck implements RecipeManager.CachedCheck<ItemHandlerWithFluidRecipeInput, AccumulationRecipe> {
public class SalAmmoniacAccumulatorCachedCheck implements RecipeManager.CachedCheck<ItemHandlerWithFluidRecipeInput, AccumulationRecipe> {

private final RecipeType<AccumulationRecipe> type;
private final RecipeManager.CachedCheck<ItemHandlerWithFluidRecipeInput, AccumulationRecipe> internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ protected boolean alreadyHasInput(ItemStack stack) {
return IntStream.range(0, this.inputInventorySupplier.get().getSlots()).anyMatch(i -> ItemStack.isSameItemSameComponents(stack, this.inputInventorySupplier.get().getStackInSlot(i)));
}

public boolean canProcess(FluidStack stack) {
return false;
}

/**
* If progress has reached 100%, craft the item and reset progress.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.klikli_dev.theurgy.content.render.itemhud;

import com.klikli_dev.theurgy.content.behaviour.itemhandler.HasItemHandlerBehaviour;
import com.klikli_dev.theurgy.content.behaviour.crafting.CraftingBehaviour;
import com.klikli_dev.theurgy.content.behaviour.crafting.HasCraftingBehaviour;
import com.klikli_dev.theurgy.content.render.Color;
import com.klikli_dev.theurgy.content.render.outliner.Outliner;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.shapes.Shapes;
import net.neoforged.neoforge.capabilities.Capabilities;

public class ApparatusInsertItemHUD {

private static final ApparatusInsertItemHUD instance = new ApparatusInsertItemHUD();

private ItemInteractionResult lastInteractionResult = null;
private boolean lastCanProcess = false;
private BlockPos lastHitBlockPos = null;
private ItemStack lastStack = ItemStack.EMPTY;

Expand All @@ -37,24 +37,30 @@ public void tick(Player player) {

if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK && hitResult instanceof BlockHitResult blockHitResult) {
var blockPos = blockHitResult.getBlockPos();
var blockState = level.getBlockState(blockPos);
if (blockState.getBlock() instanceof HasItemHandlerBehaviour<?> hasItemHandlerBehaviour) {

if (level.getBlockEntity(blockPos) instanceof HasCraftingBehaviour<?, ?, ?> hasCraftingBehaviour) {

if (!blockPos.equals(this.lastHitBlockPos) || !ItemStack.isSameItemSameComponents(stack, this.lastStack)) {
this.lastHitBlockPos = blockPos;
this.lastStack = stack;
this.lastInteractionResult = hasItemHandlerBehaviour.itemHandlerBehaviour().useItemOn(stack, blockState, level, blockPos, player, InteractionHand.MAIN_HAND, blockHitResult, true);
this.lastCanProcess = this.canProcess(hasCraftingBehaviour.craftingBehaviour(), stack);
}

} else {
}
//TODO: here handle theurgy blocks without a crafting behaviour
// sal ammoniac tank
// brazier
// incubator vessels
// reformation pedestals
// this should likely be handled with the storage behaviour!
else {
this.reset();
}
} else {
this.reset();
}

if (this.lastInteractionResult != null && this.lastHitBlockPos != null) {
if (this.lastInteractionResult == ItemInteractionResult.SUCCESS) {
if (this.lastHitBlockPos != null) {
if (this.lastCanProcess) {
Outliner.get().showAABB(this.lastHitBlockPos, Shapes.block().bounds()
.move(this.lastHitBlockPos))
.colored(Color.GREEN)
Expand All @@ -68,11 +74,26 @@ public void tick(Player player) {
}
}

private boolean canProcess(CraftingBehaviour<?, ?, ?> craftingBehaviour, ItemStack stack) {
if (craftingBehaviour.canProcess(stack))
return true;

var fluidHandler = stack.getCapability(Capabilities.FluidHandler.ITEM);
if (fluidHandler == null)
return false;

var fluidStack = fluidHandler.getFluidInTank(0);
if (fluidStack.isEmpty())
return false;

return craftingBehaviour.canProcess(fluidStack);
}

private void reset() {
if (this.lastHitBlockPos != null)
Outliner.get().remove(this.lastHitBlockPos);

this.lastInteractionResult = null;
this.lastCanProcess = false;
this.lastHitBlockPos = null;
this.lastStack = ItemStack.EMPTY;
}
Expand Down

0 comments on commit 3a3a971

Please sign in to comment.