Skip to content

Commit 55bde8f

Browse files
authored
Refactor BE interaction methods (#4702)
1 parent e126099 commit 55bde8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+363
-493
lines changed

docs/content/Modpacks/Changes/v8.0.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,5 @@ A large number of machine feature interfaces have been removed, and have had the
145145
- `BlastingRecipeBuilder`, `CampfireRecipeBuilder`, `SmeltingRecipeBuilder` and `SmokingRecipeBuilder` have been merged into `SimpleCookingRecipeBuilder`
146146
- Example usage: `SimpleCookingRecipeBuilder.campfireCooking("cooking_chicken").input(new ItemStack(Items.CHICKEN)).output(new ItemStacks(Items.COOKED_CHICKEN)).cookingTime(100).experience(100).save(provider);`
147147
- `GTFluidImpl` has been merged into `GTFluid`, use `GTFluid.Flowing` and `GTFluid.Source` instead of `GTFluidImpl.Flowing` and `GTFluidImpl.Source`.
148-
- Item behaviors have been moved to `common/item/behavior`, and some items have been moved from `api/item` to `common/item`
148+
- Item behaviors have been moved to `common/item/behavior`, and some items have been moved from `api/item` to `common/item`.
149+
- Methods for processing machine interactions have changed, and all now take a single `ExtendedUseOnContext` argument.

src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.gregtechceu.gtceu.api.sync_system.ManagedSyncBlockEntity;
1212
import com.gregtechceu.gtceu.common.data.GTItems;
1313
import com.gregtechceu.gtceu.common.machine.owner.MachineOwner;
14+
import com.gregtechceu.gtceu.utils.ExtendedUseOnContext;
1415
import com.gregtechceu.gtceu.utils.GTUtil;
1516

1617
import net.minecraft.MethodsReturnNonnullByDefault;
@@ -273,7 +274,13 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
273274
machine.setOwnerUUID(sPlayer.getUUID());
274275
}
275276

276-
InteractionResult machineInteractResult = machine.onUse(state, world, pos, player, hand, hit);
277+
InteractionResult machineInteractResult;
278+
if (itemStack.isEmpty()) {
279+
machineInteractResult = machine.onUse(new ExtendedUseOnContext(player, hand, hit));
280+
} else {
281+
machineInteractResult = machine.onUseWithItem(new ExtendedUseOnContext(player, hand, hit));
282+
}
283+
277284
if (machineInteractResult != InteractionResult.PASS) return machineInteractResult;
278285

279286
if (itemStack.is(GTItems.PORTABLE_SCANNER.get())) {

src/main/java/com/gregtechceu/gtceu/api/block/PipeBlock.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.gregtechceu.gtceu.common.item.behavior.CoverPlaceBehavior;
2222
import com.gregtechceu.gtceu.config.ConfigHolder;
2323
import com.gregtechceu.gtceu.data.recipe.VanillaRecipeHelper;
24+
import com.gregtechceu.gtceu.utils.ExtendedUseOnContext;
2425
import com.gregtechceu.gtceu.utils.GTMath;
2526
import com.gregtechceu.gtceu.utils.GTUtil;
2627

@@ -352,8 +353,8 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
352353
}
353354

354355
Set<GTToolType> types = ToolHelper.getToolTypes(itemStack);
355-
if ((!types.isEmpty() && ToolHelper.canUse(itemStack)) || (types.isEmpty() && player.isShiftKeyDown())) {
356-
var result = pipeBlockEntity.onToolClick(types, itemStack, new UseOnContext(player, hand, hit));
356+
if ((!types.isEmpty() && ToolHelper.canUse(itemStack))) {
357+
var result = pipeBlockEntity.onToolClick(new ExtendedUseOnContext(player, hand, hit));
357358
if (result.getSecond() == InteractionResult.CONSUME && player instanceof ServerPlayer serverPlayer) {
358359
ToolHelper.playToolSound(result.getFirst(), serverPlayer);
359360

src/main/java/com/gregtechceu/gtceu/api/blockentity/PipeBlockEntity.java

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.gregtechceu.gtceu.api.GTValues;
44
import com.gregtechceu.gtceu.api.block.MaterialPipeBlock;
5-
import com.gregtechceu.gtceu.api.capability.ICoverable;
6-
import com.gregtechceu.gtceu.api.capability.IToolable;
75
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
86
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
97
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
@@ -18,6 +16,7 @@
1816
import com.gregtechceu.gtceu.api.sync_system.annotations.SyncToClient;
1917
import com.gregtechceu.gtceu.common.data.GTMaterialBlocks;
2018
import com.gregtechceu.gtceu.common.data.GTMaterials;
19+
import com.gregtechceu.gtceu.utils.ExtendedUseOnContext;
2120
import com.gregtechceu.gtceu.utils.GTUtil;
2221

2322
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
@@ -32,13 +31,11 @@
3231
import net.minecraft.world.InteractionResult;
3332
import net.minecraft.world.entity.player.Player;
3433
import net.minecraft.world.item.ItemStack;
35-
import net.minecraft.world.item.context.UseOnContext;
3634
import net.minecraft.world.level.Level;
3735
import net.minecraft.world.level.block.Block;
3836
import net.minecraft.world.level.block.entity.BlockEntity;
3937
import net.minecraft.world.level.block.entity.BlockEntityType;
4038
import net.minecraft.world.level.block.state.BlockState;
41-
import net.minecraft.world.phys.BlockHitResult;
4239

4340
import com.mojang.datafixers.util.Pair;
4441
import lombok.Getter;
@@ -55,7 +52,7 @@
5552
@MethodsReturnNonnullByDefault
5653
public abstract class PipeBlockEntity<PipeType extends Enum<PipeType> & IPipeType<NodeDataType>, NodeDataType>
5754
extends ManagedSyncBlockEntity
58-
implements IPipeNode<PipeType, NodeDataType>, IToolGridHighlight, IToolable,
55+
implements IPipeNode<PipeType, NodeDataType>, IToolGridHighlight,
5956
ICopyable {
6057

6158
private final long offset = GTValues.RNG.nextInt(20);
@@ -350,56 +347,41 @@ public ResourceTexture getPipeTexture(boolean isBlock) {
350347
return null;
351348
}
352349

353-
@Override
354-
public Pair<@Nullable GTToolType, InteractionResult> onToolClick(Set<GTToolType> toolTypes, ItemStack itemStack,
355-
UseOnContext context) {
350+
public Pair<@Nullable GTToolType, InteractionResult> onToolClick(ExtendedUseOnContext context) {
356351
// the side hit from the machine grid
357-
var playerIn = context.getPlayer();
358-
if (playerIn == null) return Pair.of(null, InteractionResult.PASS);
359-
360-
var hand = context.getHand();
361-
var hitResult = new BlockHitResult(context.getClickLocation(), context.getClickedFace(),
362-
context.getClickedPos(), false);
363-
Direction gridSide = ICoverable.determineGridSideHit(hitResult);
364-
CoverBehavior coverBehavior = gridSide == null ? null : coverContainer.getCoverAtSide(gridSide);
365-
if (gridSide == null) gridSide = hitResult.getDirection();
366-
367-
// Prioritize covers where they apply (Screwdriver, Soft Mallet)
368-
if (toolTypes.isEmpty() && playerIn.isShiftKeyDown()) {
369-
if (coverBehavior != null) {
370-
return Pair.of(null, coverBehavior.onScrewdriverClick(playerIn, hand, hitResult));
352+
var player = context.getPlayer();
353+
var toolType = context.getToolType();
354+
var gridSide = context.getGridSide();
355+
356+
if (player == null) return Pair.of(null, InteractionResult.PASS);
357+
358+
// Prioritize covers
359+
var cover = getCoverContainer().getCoverAtSide(context.getClickedFace());
360+
if (cover != null) {
361+
var result = cover.onToolClick(context);
362+
if (result.getSecond() != InteractionResult.PASS) return result;
363+
364+
if (toolType.contains(GTToolType.CROWBAR) && !isRemote()) {
365+
getCoverContainer().removeCover(context.getGridSide(), player);
366+
return Pair.of(GTToolType.CROWBAR, InteractionResult.SUCCESS);
371367
}
372368
}
373-
if (toolTypes.contains(GTToolType.SCREWDRIVER)) {
374-
if (coverBehavior != null) {
375-
return Pair.of(GTToolType.SCREWDRIVER, coverBehavior.onScrewdriverClick(playerIn, hand, hitResult));
376-
}
377-
} else if (toolTypes.contains(GTToolType.SOFT_MALLET)) {
378-
if (coverBehavior != null) {
379-
return Pair.of(GTToolType.SOFT_MALLET, coverBehavior.onSoftMalletClick(playerIn, hand, hitResult));
380-
}
381-
} else if (toolTypes.contains(getPipeTuneTool())) {
382-
if (playerIn.isShiftKeyDown() && this.canHaveBlockedFaces()) {
369+
370+
if (toolType.contains(getPipeTuneTool())) {
371+
if (player.isShiftKeyDown() && this.canHaveBlockedFaces()) {
383372
boolean isBlocked = this.isBlocked(gridSide);
384373
this.setBlocked(gridSide, !isBlocked);
385374
} else {
386375
boolean isOpen = this.isConnected(gridSide);
387376
this.setConnection(gridSide, !isOpen, false);
388377
}
389-
return Pair.of(getPipeTuneTool(), InteractionResult.sidedSuccess(playerIn.level().isClientSide));
390-
} else if (toolTypes.contains(GTToolType.CROWBAR)) {
391-
if (coverBehavior != null) {
392-
if (!isRemote()) {
393-
getCoverContainer().removeCover(gridSide, playerIn);
394-
return Pair.of(GTToolType.CROWBAR, InteractionResult.sidedSuccess(playerIn.level().isClientSide));
395-
}
396-
} else {
397-
if (!frameMaterial.isNull()) {
398-
Block.popResource(getLevel(), this.getBlockPos(),
399-
GTMaterialBlocks.MATERIAL_BLOCKS.get(TagPrefix.frameGt, frameMaterial).asStack());
400-
frameMaterial = GTMaterials.NULL;
401-
return Pair.of(GTToolType.CROWBAR, InteractionResult.sidedSuccess(playerIn.level().isClientSide));
402-
}
378+
return Pair.of(getPipeTuneTool(), InteractionResult.sidedSuccess(isRemote()));
379+
} else if (toolType.contains(GTToolType.CROWBAR)) {
380+
if (!frameMaterial.isNull()) {
381+
Block.popResource(context.getLevel(), this.getBlockPos(),
382+
GTMaterialBlocks.MATERIAL_BLOCKS.get(TagPrefix.frameGt, frameMaterial).asStack());
383+
frameMaterial = GTMaterials.NULL;
384+
return Pair.of(GTToolType.CROWBAR, InteractionResult.sidedSuccess(isRemote()));
403385
}
404386
}
405387

src/main/java/com/gregtechceu/gtceu/api/capability/GTCapability.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class GTCapability {
1414
public static final Capability<IEnergyInfoProvider> CAPABILITY_ENERGY_INFO_PROVIDER = CapabilityManager
1515
.get(new CapabilityToken<>() {});
1616
public static final Capability<ICoverable> CAPABILITY_COVERABLE = CapabilityManager.get(new CapabilityToken<>() {});
17-
public static final Capability<IToolable> CAPABILITY_TOOLABLE = CapabilityManager.get(new CapabilityToken<>() {});
1817
public static final Capability<IWorkable> CAPABILITY_WORKABLE = CapabilityManager.get(new CapabilityToken<>() {});
1918
public static final Capability<IControllable> CAPABILITY_CONTROLLABLE = CapabilityManager
2019
.get(new CapabilityToken<>() {});
@@ -40,7 +39,6 @@ public static void register(RegisterCapabilitiesEvent event) {
4039
event.register(IEnergyContainer.class);
4140
event.register(IEnergyInfoProvider.class);
4241
event.register(ICoverable.class);
43-
event.register(IToolable.class);
4442
event.register(IWorkable.class);
4543
event.register(IControllable.class);
4644
event.register(IElectricItem.class);

src/main/java/com/gregtechceu/gtceu/api/capability/GTCapabilityHelper.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public static ICoverable getCoverable(Level level, BlockPos pos, @Nullable Direc
5353
return getBlockEntityCapability(GTCapability.CAPABILITY_COVERABLE, level, pos, side);
5454
}
5555

56-
@Nullable
57-
public static IToolable getToolable(Level level, BlockPos pos, @Nullable Direction side) {
58-
return getBlockEntityCapability(GTCapability.CAPABILITY_TOOLABLE, level, pos, side);
59-
}
60-
6156
@Nullable
6257
public static IWorkable getWorkable(Level level, BlockPos pos, @Nullable Direction side) {
6358
return getBlockEntityCapability(GTCapability.CAPABILITY_WORKABLE, level, pos, side);

src/main/java/com/gregtechceu/gtceu/api/capability/IToolable.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/com/gregtechceu/gtceu/api/cover/CoverBehavior.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
1717
import com.gregtechceu.gtceu.client.renderer.cover.ICoverRenderer;
1818
import com.gregtechceu.gtceu.client.renderer.cover.IDynamicCoverRenderer;
19+
import com.gregtechceu.gtceu.utils.ExtendedUseOnContext;
1920

2021
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
2122

2223
import net.minecraft.MethodsReturnNonnullByDefault;
2324
import net.minecraft.core.BlockPos;
2425
import net.minecraft.core.Direction;
2526
import net.minecraft.server.level.ServerPlayer;
26-
import net.minecraft.world.InteractionHand;
2727
import net.minecraft.world.InteractionResult;
2828
import net.minecraft.world.entity.player.Player;
2929
import net.minecraft.world.item.ItemStack;
3030
import net.minecraft.world.level.block.Block;
3131
import net.minecraft.world.level.block.state.BlockState;
32-
import net.minecraft.world.phys.BlockHitResult;
3332
import net.minecraftforge.items.IItemHandlerModifiable;
3433

34+
import com.mojang.datafixers.util.Pair;
3535
import lombok.Getter;
3636
import org.jetbrains.annotations.MustBeInvokedByOverriders;
3737
import org.jetbrains.annotations.Nullable;
@@ -150,17 +150,28 @@ public boolean canConnectRedstone() {
150150
//////////////////////////////////////
151151
// ******* Interaction *******//
152152
//////////////////////////////////////
153-
public InteractionResult onScrewdriverClick(Player playerIn, InteractionHand hand, BlockHitResult hitResult) {
153+
154+
public final Pair<@Nullable GTToolType, InteractionResult> onToolClick(ExtendedUseOnContext context) {
155+
var toolType = context.getToolType();
156+
if (toolType.contains(GTToolType.SCREWDRIVER)) {
157+
return Pair.of(GTToolType.SCREWDRIVER, onScrewdriverClick(context));
158+
} else if (toolType.contains(GTToolType.SOFT_MALLET)) {
159+
return Pair.of(GTToolType.SOFT_MALLET, onSoftMalletClick(context));
160+
}
161+
return Pair.of(null, InteractionResult.PASS);
162+
}
163+
164+
public InteractionResult onScrewdriverClick(ExtendedUseOnContext context) {
154165
if (this instanceof IUICover) {
155-
if (playerIn instanceof ServerPlayer serverPlayer) {
166+
if (context.getPlayer() instanceof ServerPlayer serverPlayer) {
156167
CoverUIFactory.INSTANCE.openUI(this, serverPlayer);
157168
}
158-
return InteractionResult.sidedSuccess(playerIn.level().isClientSide);
169+
return InteractionResult.sidedSuccess(coverHolder.isRemote());
159170
}
160171
return InteractionResult.PASS;
161172
}
162173

163-
public InteractionResult onSoftMalletClick(Player playerIn, InteractionHand hand, BlockHitResult hitResult) {
174+
public InteractionResult onSoftMalletClick(ExtendedUseOnContext context) {
164175
return InteractionResult.PASS;
165176
}
166177

0 commit comments

Comments
 (0)