Skip to content

Commit

Permalink
Fixed plunger not working (GregTechCEu#1061)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
minecraft7771 authored Apr 5, 2024
1 parent 7ef1bba commit 146d4b6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

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.component.IInteractionItem;
import com.gregtechceu.gtceu.api.item.component.IItemComponent;
import com.gregtechceu.gtceu.api.item.tool.GTToolItem;
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
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.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
Expand Down Expand Up @@ -251,6 +256,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<GTToolType> types = ToolHelper.getToolTypes(itemStack);
if (machine != null && !types.isEmpty() && ToolHelper.canUse(itemStack)) {
Expand All @@ -269,14 +275,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;
}


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,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<ItemStack> items) {
if (isElectric()) {
items.add(get(Integer.MAX_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ default InteractionResultHolder<ItemStack> 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<Component> tooltip, @Nonnull TooltipFlag flag) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();

Expand All @@ -32,22 +35,36 @@ protected static PlungerBehavior create() {
return new PlungerBehavior();
}

@Override
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;
}

Expand Down

0 comments on commit 146d4b6

Please sign in to comment.