From 822afc659903692f7512d4c04872088949e918dc Mon Sep 17 00:00:00 2001 From: Maxim Date: Fri, 5 Apr 2024 20:33:37 +0300 Subject: [PATCH] Fixed plunger not working (#1061) * Fixed plunger not doing anything but consuming durability * Added API to prevent the plunger from opening the UI when using it * Addressed reviews * Replaced isCrouching with isShiftKeyDown for plunger behavior --- .../gtceu/api/block/MetaMachineBlock.java | 11 +++- .../gregtechceu/gtceu/api/item/IGTTool.java | 10 +++ .../api/item/tool/behavior/IToolBehavior.java | 10 +++ .../item/tool/behavior/PlungerBehavior.java | 63 ++++++++++++------- 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java b/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java index c3b57e7ab5..32d23b9fb1 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java +++ b/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java @@ -1,7 +1,7 @@ package com.gregtechceu.gtceu.api.block; import com.gregtechceu.gtceu.api.data.RotationState; -import com.gregtechceu.gtceu.api.item.ComponentItem; +import com.gregtechceu.gtceu.api.item.IGTTool; import com.gregtechceu.gtceu.api.item.MetaMachineItem; import com.gregtechceu.gtceu.api.item.tool.GTToolType; import com.gregtechceu.gtceu.api.item.tool.ToolHelper; @@ -251,6 +251,7 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { var machine = getMachine(world, pos); ItemStack itemStack = player.getItemInHand(hand); + boolean shouldOpenUi = true; Set types = ToolHelper.getToolTypes(itemStack); if (machine != null && !types.isEmpty() && ToolHelper.canUse(itemStack)) { @@ -269,14 +270,18 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player return itemStack.getItem().use(world, player, hand).getResult(); } + if (itemStack.getItem() instanceof IGTTool gtToolItem) { + shouldOpenUi = gtToolItem.definition$shouldOpenUIAfterUse(new UseOnContext(player, hand, hit)); + } + if (machine instanceof IInteractedMachine interactedMachine) { var result = interactedMachine.onUse(state, world, pos, player, hand, hit); if (result != InteractionResult.PASS) return result; } - if (machine instanceof IUIMachine uiMachine) { + if (shouldOpenUi && machine instanceof IUIMachine uiMachine) { return uiMachine.tryToOpenUI(player, hand, hit); } - return InteractionResult.PASS; + return shouldOpenUi ? InteractionResult.PASS : InteractionResult.CONSUME; } diff --git a/src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java b/src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java index a98ef0040b..fc0261dd76 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java +++ b/src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java @@ -587,6 +587,16 @@ default int getTotalHarvestLevel(ItemStack stack) { return InteractionResultHolder.pass(stack); } + default boolean definition$shouldOpenUIAfterUse(UseOnContext context) { + for (IToolBehavior behavior : getToolStats().getBehaviors()) { + if (!behavior.shouldOpenUIAfterUse(context)) { + return false; + } + } + + return true; + } + default void definition$fillItemCategory(CreativeModeTab category, @Nonnull NonNullList items) { if (category != GTCreativeModeTabs.TOOL && category != CreativeModeTab.TAB_SEARCH) return; diff --git a/src/main/java/com/gregtechceu/gtceu/api/item/tool/behavior/IToolBehavior.java b/src/main/java/com/gregtechceu/gtceu/api/item/tool/behavior/IToolBehavior.java index 0be1c69825..cf492b8d5f 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/item/tool/behavior/IToolBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/api/item/tool/behavior/IToolBehavior.java @@ -116,6 +116,16 @@ default InteractionResultHolder onItemRightClick(@Nonnull Level world return InteractionResultHolder.pass(player.getItemInHand(hand)); } + /** + * Called when a block is right-clicked with this Item + * + * @param context The UseOnContext used to determine the result. + * @return True if the UI of the MetaMachine should open after using this tool. + */ + default boolean shouldOpenUIAfterUse(UseOnContext context) { + return true; + } + @OnlyIn(Dist.CLIENT) default void addInformation(@Nonnull ItemStack stack, @Nullable Level world, @Nonnull List tooltip, @Nonnull TooltipFlag flag) { } diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/PlungerBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/PlungerBehavior.java index d1b3bc28af..979659d88f 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/PlungerBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/PlungerBehavior.java @@ -1,8 +1,11 @@ package com.gregtechceu.gtceu.common.item.tool.behavior; +import com.gregtechceu.gtceu.api.blockentity.MetaMachineBlockEntity; +import com.gregtechceu.gtceu.api.item.component.IInteractionItem; import com.gregtechceu.gtceu.api.item.component.forge.IComponentCapability; import com.gregtechceu.gtceu.api.item.tool.ToolHelper; import com.gregtechceu.gtceu.api.item.tool.behavior.IToolBehavior; +import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; import com.gregtechceu.gtceu.api.misc.forge.VoidFluidHandlerItemStack; import com.lowdragmc.lowdraglib.side.fluid.FluidHelper; import com.lowdragmc.lowdraglib.side.fluid.FluidTransferHelper; @@ -22,7 +25,7 @@ import java.util.List; -public class PlungerBehavior implements IToolBehavior, IComponentCapability { +public class PlungerBehavior implements IToolBehavior, IComponentCapability, IInteractionItem { public static final PlungerBehavior INSTANCE = PlungerBehavior.create(); @@ -33,38 +36,35 @@ protected static PlungerBehavior create() { } @Override - public @NotNull LazyOptional getCapability(ItemStack itemStack, @NotNull Capability cap) { - if (cap == ForgeCapabilities.FLUID_HANDLER_ITEM) { - return ForgeCapabilities.FLUID_HANDLER_ITEM.orEmpty(cap, LazyOptional.of(() -> new VoidFluidHandlerItemStack(itemStack) { - @Override - public int fill(FluidStack resource, FluidAction doFill) { - int result = super.fill(resource, doFill); - if (result > 0) { - ToolHelper.damageItem(getContainer(), null); - } - return result; - } - })); - } - return LazyOptional.empty(); + public boolean shouldOpenUIAfterUse(UseOnContext context) { + return !(context.getPlayer() != null && context.getPlayer().isShiftKeyDown()); } @Override public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { - IFluidTransfer fluidHandler = FluidTransferHelper.getFluidTransfer(context.getLevel(), context.getClickedPos(), context.getClickedFace()); - if (fluidHandler == null) { + + if (context.getPlayer() == null || !context.getPlayer().isShiftKeyDown()) { return InteractionResult.PASS; } - IFluidTransfer handlerToRemoveFrom = fluidHandler; -// player.isCrouching() ? -// (fluidHandler instanceof IOFluidTransferList ? ((IOFluidTransferList) fluidHandler).input : null) : -// (fluidHandler instanceof IOFluidTransferList ? ((IOFluidTransferList) fluidHandler).output : fluidHandler); + IFluidTransfer fluidHandler; - if (handlerToRemoveFrom != null && handlerToRemoveFrom.drain(FluidHelper.getBucket(), true) != null) { - ToolHelper.onActionDone(context.getPlayer(), context.getLevel(), context.getHand()); + if (context.getLevel().getBlockEntity(context.getClickedPos()) instanceof IMachineBlockEntity metaMachineBlockEntity) { + fluidHandler = metaMachineBlockEntity.getMetaMachine().getFluidTransferCap(context.getClickedFace(), false); + } else { + fluidHandler = FluidTransferHelper.getFluidTransfer(context.getLevel(), context.getClickedPos(), context.getClickedFace()); + } + + if (fluidHandler == null) { return InteractionResult.PASS; } + + com.lowdragmc.lowdraglib.side.fluid.FluidStack drained = fluidHandler.drain(FluidHelper.getBucket(), true); + if (drained != null && !drained.isEmpty()) { + fluidHandler.drain(FluidHelper.getBucket(), false); + ToolHelper.onActionDone(context.getPlayer(), context.getLevel(), context.getHand()); + return InteractionResult.CONSUME; + } return InteractionResult.PASS; } @@ -73,4 +73,21 @@ public void addInformation(@NotNull ItemStack stack, @Nullable Level world, @Not @NotNull TooltipFlag flag) { tooltip.add(Component.translatable("item.gtceu.tool.behavior.plunger")); } + + @Override + public @NotNull LazyOptional getCapability(ItemStack itemStack, @NotNull Capability cap) { + if (cap == ForgeCapabilities.FLUID_HANDLER_ITEM) { + return ForgeCapabilities.FLUID_HANDLER_ITEM.orEmpty(cap, LazyOptional.of(() -> new VoidFluidHandlerItemStack(itemStack) { + @Override + public int fill(FluidStack resource, FluidAction doFill) { + int result = super.fill(resource, doFill); + if (result > 0) { + ToolHelper.damageItem(getContainer(), null); + } + return result; + } + })); + } + return LazyOptional.empty(); + } } \ No newline at end of file