Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed plunger not working #1061

Merged
merged 7 commits into from
Apr 5, 2024
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
Loading