From c0b5045939e9ed71e30b9f533d8af812b7c01135 Mon Sep 17 00:00:00 2001 From: omergunr100 <24607757+omergunr100@users.noreply.github.com> Date: Fri, 20 Dec 2024 22:48:09 +0200 Subject: [PATCH] Refactor DataStick item and associated interaction interface (#2561) --- .../gtceu/api/block/MetaMachineBlock.java | 18 ++-------- .../feature/IDataStickInteractable.java | 33 +++-------------- .../gtceu/common/item/DataItemBehavior.java | 36 +++++++++---------- .../MetaMachineConfigCopyBehaviour.java | 3 +- .../common/machine/owner/IMachineOwner.java | 16 +++++++++ .../gtceu/forge/ForgeCommonEventListener.java | 3 +- .../ae2/machine/MEInputBusPartMachine.java | 6 ++-- .../ae2/machine/MEInputHatchPartMachine.java | 6 ++-- .../machine/MEPatternBufferPartMachine.java | 12 ++++++- .../MEPatternBufferProxyPartMachine.java | 20 ++++++++++- 10 files changed, 79 insertions(+), 74 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 c13b36bc4b..3c209bc0ed 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java +++ b/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java @@ -11,7 +11,7 @@ import com.gregtechceu.gtceu.api.machine.MultiblockMachineDefinition; import com.gregtechceu.gtceu.api.machine.feature.*; import com.gregtechceu.gtceu.common.data.GTItems; -import com.gregtechceu.gtceu.config.ConfigHolder; +import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner; import com.gregtechceu.gtceu.utils.GTUtil; import com.lowdragmc.lowdraglib.client.renderer.IRenderer; @@ -321,26 +321,12 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player if (result != InteractionResult.PASS) return result; } if (shouldOpenUi && machine instanceof IUIMachine uiMachine && - canOpenOwnerMachine(player, machine.getHolder())) { + IMachineOwner.canOpenOwnerMachine(player, machine.getHolder())) { return uiMachine.tryToOpenUI(player, hand, hit); } return shouldOpenUi ? InteractionResult.PASS : InteractionResult.CONSUME; } - public boolean canOpenOwnerMachine(Player player, IMachineBlockEntity machine) { - if (!ConfigHolder.INSTANCE.machines.onlyOwnerGUI) return true; - if (player.hasPermissions(ConfigHolder.INSTANCE.machines.ownerOPBypass)) return true; - if (machine.getOwner() == null) return true; - return machine.getOwner().isPlayerInTeam(player) || machine.getOwner().isPlayerFriendly(player); - } - - public static boolean canBreakOwnerMachine(Player player, IMachineBlockEntity machine) { - if (!ConfigHolder.INSTANCE.machines.onlyOwnerBreak) return true; - if (player.hasPermissions(ConfigHolder.INSTANCE.machines.ownerOPBypass)) return true; - if (machine.getOwner() == null) return true; - return machine.getOwner().isPlayerInTeam(player); - } - public boolean canConnectRedstone(BlockGetter level, BlockPos pos, Direction side) { return getMachine(level, pos).canConnectRedstone(side); } diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/IDataStickInteractable.java b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/IDataStickInteractable.java index 983e462e50..797445fe0f 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/feature/IDataStickInteractable.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/feature/IDataStickInteractable.java @@ -1,39 +1,16 @@ package com.gregtechceu.gtceu.api.machine.feature; -import com.gregtechceu.gtceu.common.data.GTItems; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.BlockHitResult; -public interface IDataStickInteractable extends IInteractedMachine { +public interface IDataStickInteractable { - @Override - default InteractionResult onUse(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, - BlockHitResult hit) { - var item = player.getItemInHand(hand); - if (item.is(GTItems.TOOL_DATA_STICK.asItem())) { - return onDataStickRightClick(player, item); - } - return IInteractedMachine.super.onUse(state, world, pos, player, hand, hit); + default InteractionResult onDataStickUse(Player player, ItemStack dataStick) { + return InteractionResult.PASS; } - @Override - default boolean onLeftClick(Player player, Level world, InteractionHand hand, BlockPos pos, Direction direction) { - var item = player.getItemInHand(hand); - if (item.is(GTItems.TOOL_DATA_STICK.asItem())) { - return onDataStickLeftClick(player, item); - } - return IInteractedMachine.super.onLeftClick(player, world, hand, pos, direction); + default InteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { + return InteractionResult.PASS; } - - InteractionResult onDataStickRightClick(Player player, ItemStack dataStick); - - boolean onDataStickLeftClick(Player player, ItemStack dataStick); } diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/DataItemBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/DataItemBehavior.java index beabbd19f6..c07e63f0cf 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/DataItemBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/DataItemBehavior.java @@ -1,18 +1,17 @@ package com.gregtechceu.gtceu.common.item; +import com.gregtechceu.gtceu.api.blockentity.MetaMachineBlockEntity; import com.gregtechceu.gtceu.api.capability.recipe.ItemRecipeCapability; import com.gregtechceu.gtceu.api.item.component.IAddInformation; import com.gregtechceu.gtceu.api.item.component.IDataItem; import com.gregtechceu.gtceu.api.item.component.IInteractionItem; -import com.gregtechceu.gtceu.api.machine.MetaMachine; +import com.gregtechceu.gtceu.api.machine.feature.IDataStickInteractable; import com.gregtechceu.gtceu.api.recipe.GTRecipe; import com.gregtechceu.gtceu.api.recipe.GTRecipeType; -import com.gregtechceu.gtceu.integration.ae2.machine.MEPatternBufferPartMachine; -import com.gregtechceu.gtceu.integration.ae2.machine.MEPatternBufferProxyPartMachine; +import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner; import com.gregtechceu.gtceu.utils.ResearchManager; import net.minecraft.ChatFormatting; -import net.minecraft.core.BlockPos; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionResult; @@ -81,27 +80,24 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List researchData = ResearchManager.readResearchId(stack); - if (machine instanceof MEPatternBufferPartMachine && researchData == null) { - stack.getOrCreateTag().putIntArray("pos", new int[] { pos.getX(), pos.getY(), pos.getZ() }); - } else if (machine instanceof MEPatternBufferProxyPartMachine proxy) { - if (stack.hasTag()) { - if (stack.getOrCreateTag().contains("pos", Tag.TAG_INT_ARRAY)) { - int[] posArray = stack.getOrCreateTag().getIntArray("pos"); - BlockPos bufferPos = new BlockPos(posArray[0], posArray[1], posArray[2]); - proxy.setBuffer(bufferPos); + public InteractionResult onItemUseFirst(ItemStack itemStack, UseOnContext context) { + if (context.getLevel().getBlockEntity(context.getClickedPos()) instanceof MetaMachineBlockEntity blockEntity) { + if (!IMachineOwner.canOpenOwnerMachine(context.getPlayer(), blockEntity)) { + return InteractionResult.FAIL; + } + var machine = blockEntity.getMetaMachine(); + if (machine instanceof IDataStickInteractable interactable) { + if (context.isSecondaryUseActive()) { + if (ResearchManager.readResearchId(itemStack) == null) { + return interactable.onDataStickShiftUse(context.getPlayer(), itemStack); } + } else { + return interactable.onDataStickUse(context.getPlayer(), itemStack); } } else { return InteractionResult.PASS; } } - return InteractionResult.sidedSuccess(level.isClientSide); + return InteractionResult.sidedSuccess(context.getLevel().isClientSide); } } diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/MetaMachineConfigCopyBehaviour.java b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/MetaMachineConfigCopyBehaviour.java index cd169863a8..9bd7f12d4b 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/MetaMachineConfigCopyBehaviour.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/MetaMachineConfigCopyBehaviour.java @@ -7,6 +7,7 @@ import com.gregtechceu.gtceu.api.machine.feature.IAutoOutputFluid; import com.gregtechceu.gtceu.api.machine.feature.IAutoOutputItem; import com.gregtechceu.gtceu.api.machine.feature.IMufflableMachine; +import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner; import net.minecraft.ChatFormatting; import net.minecraft.client.gui.screens.Screen; @@ -97,7 +98,7 @@ public InteractionResultHolder use(Item item, Level level, Player pla @Override public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { if (context.getLevel().getBlockEntity(context.getClickedPos()) instanceof MetaMachineBlockEntity blockEntity) { - if (blockEntity.getOwner() != null && !blockEntity.getOwner().isPlayerInTeam(context.getPlayer())) + if (!IMachineOwner.canOpenOwnerMachine(context.getPlayer(), blockEntity)) return InteractionResult.FAIL; MetaMachine machine = blockEntity.getMetaMachine(); if (context.isSecondaryUseActive()) diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java b/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java index 9f90afd18f..01107662b5 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/owner/IMachineOwner.java @@ -1,6 +1,8 @@ package com.gregtechceu.gtceu.common.machine.owner; import com.gregtechceu.gtceu.GTCEu; +import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; +import com.gregtechceu.gtceu.config.ConfigHolder; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; @@ -47,6 +49,20 @@ default CompoundTag write() { boolean isPlayerFriendly(Player player); + static boolean canOpenOwnerMachine(Player player, IMachineBlockEntity machine) { + if (!ConfigHolder.INSTANCE.machines.onlyOwnerGUI) return true; + if (player.hasPermissions(ConfigHolder.INSTANCE.machines.ownerOPBypass)) return true; + if (machine.getOwner() == null) return true; + return machine.getOwner().isPlayerInTeam(player) || machine.getOwner().isPlayerFriendly(player); + } + + static boolean canBreakOwnerMachine(Player player, IMachineBlockEntity machine) { + if (!ConfigHolder.INSTANCE.machines.onlyOwnerBreak) return true; + if (player.hasPermissions(ConfigHolder.INSTANCE.machines.ownerOPBypass)) return true; + if (machine.getOwner() == null) return true; + return machine.getOwner().isPlayerInTeam(player); + } + enum MachineOwnerType { PLAYER, diff --git a/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java b/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java index 0660903260..c0a1397cb8 100644 --- a/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java +++ b/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java @@ -37,6 +37,7 @@ import com.gregtechceu.gtceu.common.fluid.potion.PotionFluidHelper; import com.gregtechceu.gtceu.common.item.ToggleEnergyConsumerBehavior; import com.gregtechceu.gtceu.common.item.armor.IJetpack; +import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner; import com.gregtechceu.gtceu.common.network.GTNetwork; import com.gregtechceu.gtceu.common.network.packets.*; import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketAddHazardZone; @@ -267,7 +268,7 @@ public static void onLeftClickBlock(PlayerInteractEvent.LeftClickBlock event) { public static void onBreakEvent(BlockEvent.BreakEvent event) { var machine = MetaMachine.getMachine(event.getLevel(), event.getPos()); if (machine != null) { - if (!MetaMachineBlock.canBreakOwnerMachine(event.getPlayer(), machine.holder)) { + if (!IMachineOwner.canBreakOwnerMachine(event.getPlayer(), machine.holder)) { event.setCanceled(true); } } diff --git a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputBusPartMachine.java b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputBusPartMachine.java index ca0e2ba41b..d1c45b4f5c 100644 --- a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputBusPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputBusPartMachine.java @@ -145,7 +145,7 @@ public Widget createUIWidget() { //////////////////////////////// @Override - public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { + public final InteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { if (!isRemote()) { CompoundTag tag = new CompoundTag(); tag.put("MEInputBus", writeConfigToTag()); @@ -153,11 +153,11 @@ public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { dataStick.setHoverName(Component.translatable("gtceu.machine.me.item_import.data_stick.name")); player.sendSystemMessage(Component.translatable("gtceu.machine.me.import_copy_settings")); } - return true; + return InteractionResult.SUCCESS; } @Override - public final InteractionResult onDataStickRightClick(Player player, ItemStack dataStick) { + public final InteractionResult onDataStickUse(Player player, ItemStack dataStick) { CompoundTag tag = dataStick.getTag(); if (tag == null || !tag.contains("MEInputBus")) { return InteractionResult.PASS; diff --git a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputHatchPartMachine.java b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputHatchPartMachine.java index 50b156f348..14b821bf6d 100644 --- a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputHatchPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEInputHatchPartMachine.java @@ -146,7 +146,7 @@ public Widget createUIWidget() { //////////////////////////////// @Override - public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { + public final InteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { if (!isRemote()) { CompoundTag tag = new CompoundTag(); tag.put("MEInputHatch", writeConfigToTag()); @@ -154,11 +154,11 @@ public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { dataStick.setHoverName(Component.translatable("gtceu.machine.me.fluid_import.data_stick.name")); player.sendSystemMessage(Component.translatable("gtceu.machine.me.import_copy_settings")); } - return true; + return InteractionResult.SUCCESS; } @Override - public final InteractionResult onDataStickRightClick(Player player, ItemStack dataStick) { + public final InteractionResult onDataStickUse(Player player, ItemStack dataStick) { CompoundTag tag = dataStick.getTag(); if (tag == null || !tag.contains("MEInputHatch")) { return InteractionResult.PASS; diff --git a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferPartMachine.java b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferPartMachine.java index 22556ec766..2d17c7d54d 100644 --- a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferPartMachine.java @@ -13,6 +13,7 @@ import com.gregtechceu.gtceu.api.machine.fancyconfigurator.CircuitFancyConfigurator; import com.gregtechceu.gtceu.api.machine.fancyconfigurator.FancyInvConfigurator; import com.gregtechceu.gtceu.api.machine.fancyconfigurator.FancyTankConfigurator; +import com.gregtechceu.gtceu.api.machine.feature.IDataStickInteractable; import com.gregtechceu.gtceu.api.machine.feature.IHasCircuitSlot; import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiController; import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank; @@ -47,6 +48,8 @@ import net.minecraft.network.chat.Component; import net.minecraft.server.TickTask; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.minecraftforge.fluids.FluidStack; @@ -79,7 +82,8 @@ @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault public class MEPatternBufferPartMachine extends MEBusPartMachine - implements ICraftingProvider, PatternContainer, IHasCircuitSlot { + implements ICraftingProvider, PatternContainer, IHasCircuitSlot, + IDataStickInteractable { protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder( MEPatternBufferPartMachine.class, MEBusPartMachine.MANAGED_FIELD_HOLDER); @@ -415,6 +419,12 @@ public void onMachineRemoved() { clearInventory(shareInventory); } + @Override + public InteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { + dataStick.getOrCreateTag().putIntArray("pos", new int[] { getPos().getX(), getPos().getY(), getPos().getZ() }); + return InteractionResult.SUCCESS; + } + public class InternalSlot implements ITagSerializable, IContentChangeAware { @Getter diff --git a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferProxyPartMachine.java b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferProxyPartMachine.java index 953f8f5475..4b25e2beea 100644 --- a/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferProxyPartMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/integration/ae2/machine/MEPatternBufferProxyPartMachine.java @@ -7,6 +7,7 @@ import com.gregtechceu.gtceu.api.capability.recipe.ItemRecipeCapability; import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; import com.gregtechceu.gtceu.api.machine.MetaMachine; +import com.gregtechceu.gtceu.api.machine.feature.IDataStickInteractable; import com.gregtechceu.gtceu.api.machine.feature.IMachineLife; import com.gregtechceu.gtceu.api.machine.multiblock.part.TieredIOPartMachine; import com.gregtechceu.gtceu.api.machine.trait.*; @@ -20,10 +21,13 @@ import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.Tag; import net.minecraft.server.TickTask; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.phys.BlockHitResult; @@ -37,7 +41,8 @@ @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault -public class MEPatternBufferProxyPartMachine extends TieredIOPartMachine implements IMachineLife { +public class MEPatternBufferProxyPartMachine extends TieredIOPartMachine + implements IMachineLife, IDataStickInteractable { protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder( MEPatternBufferProxyPartMachine.class, TieredIOPartMachine.MANAGED_FIELD_HOLDER); @@ -138,4 +143,17 @@ public void onMachineRemoved() { machine.removeProxy(this); } } + + @Override + public InteractionResult onDataStickUse(Player player, ItemStack dataStick) { + if (dataStick.hasTag()) { + if (dataStick.getOrCreateTag().contains("pos", Tag.TAG_INT_ARRAY)) { + var posArray = dataStick.getOrCreateTag().getIntArray("pos"); + var bufferPos = new BlockPos(posArray[0], posArray[1], posArray[2]); + setBuffer(bufferPos); + return InteractionResult.SUCCESS; + } + } + return InteractionResult.PASS; + } }