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 cafdd05c39..0f9933a404 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java +++ b/src/main/java/com/gregtechceu/gtceu/api/block/MetaMachineBlock.java @@ -334,26 +334,12 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev if (result.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 ? ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION : ItemInteractionResult.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 7884d33bb8..54956269de 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,40 +1,16 @@ package com.gregtechceu.gtceu.api.machine.feature; -import com.gregtechceu.gtceu.data.item.GTItems; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.world.InteractionHand; import net.minecraft.world.ItemInteractionResult; 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 ItemInteractionResult 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 ItemInteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } - @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 ItemInteractionResult onDataStickUse(Player player, ItemStack dataStick) { + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } - - ItemInteractionResult onDataStickRightClick(Player player, ItemStack dataStick); - - boolean onDataStickLeftClick(Player player, ItemStack dataStick); } diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/behavior/DataItemBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/behavior/DataItemBehavior.java index f60ce84634..82840c85a1 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/behavior/DataItemBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/behavior/DataItemBehavior.java @@ -1,19 +1,22 @@ package com.gregtechceu.gtceu.common.item.behavior; +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.data.tag.GTDataComponents; 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; import net.minecraft.world.item.Item; @@ -82,24 +85,24 @@ public void appendHoverText(ItemStack stack, Item.TooltipContext context, List researchData = ResearchManager.readResearchId(stack); - if (machine instanceof MEPatternBufferPartMachine && researchData == null) { - stack.set(GTDataComponents.DATA_COPY_POS, pos); - } else if (machine instanceof MEPatternBufferProxyPartMachine proxy) { - if (stack.has(GTDataComponents.DATA_COPY_POS)) { - BlockPos bufferPos = stack.get(GTDataComponents.DATA_COPY_POS); - 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 638a63cf95..eb7f391146 100644 --- a/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java +++ b/src/main/java/com/gregtechceu/gtceu/forge/ForgeCommonEventListener.java @@ -29,6 +29,9 @@ import com.gregtechceu.gtceu.common.network.packets.SPacketSyncBedrockOreVeins; import com.gregtechceu.gtceu.common.network.packets.SPacketSyncFluidVeins; import com.gregtechceu.gtceu.common.network.packets.SPacketSyncOreVeins; +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; import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketRemoveHazardZone; import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketSyncLevelHazards; @@ -168,7 +171,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 55acb45b0c..19e4c1da0e 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 @@ -149,7 +149,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(player.registryAccess())); @@ -158,11 +158,11 @@ public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { 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 ItemInteractionResult onDataStickRightClick(Player player, ItemStack dataStick) { + public final ItemInteractionResult onDataStickUse(Player player, ItemStack dataStick) { CustomData tag = dataStick.get(GTDataComponents.DATA_COPY_TAG); if (tag == null || !tag.contains("MEInputBus")) { return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; 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 fb4092eb5d..daac2b6b2a 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 @@ -150,7 +150,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(player.registryAccess())); @@ -159,11 +159,11 @@ public final boolean onDataStickLeftClick(Player player, ItemStack dataStick) { 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 ItemInteractionResult onDataStickRightClick(Player player, ItemStack dataStick) { + public final ItemInteractionResult onDataStickUse(Player player, ItemStack dataStick) { CustomData tag = dataStick.get(GTDataComponents.DATA_COPY_TAG); if (tag == null || !tag.contains("MEInputHatch")) { return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; 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 5a47c118a0..3a878b5bb9 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; @@ -20,6 +21,7 @@ import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler; import com.gregtechceu.gtceu.common.item.behavior.IntCircuitBehaviour; import com.gregtechceu.gtceu.data.machine.GTAEMachines; +import com.gregtechceu.gtceu.data.tag.GTDataComponents; import com.gregtechceu.gtceu.integration.ae2.gui.widget.AETextInputButtonWidget; import com.gregtechceu.gtceu.integration.ae2.gui.widget.slot.AEPatternViewSlotWidget; import com.gregtechceu.gtceu.integration.ae2.machine.trait.MEPatternBufferRecipeHandler; @@ -48,6 +50,9 @@ 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.ItemInteractionResult; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.neoforged.neoforge.common.crafting.SizedIngredient; import net.neoforged.neoforge.common.util.INBTSerializable; @@ -80,7 +85,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); @@ -416,6 +422,12 @@ public void onMachineRemoved() { clearInventory(shareInventory); } + @Override + public ItemInteractionResult onDataStickShiftUse(Player player, ItemStack dataStick) { + dataStick.set(GTDataComponents.DATA_COPY_POS, getPos()); + return ItemInteractionResult.SUCCESS; + } + public class InternalSlot implements INBTSerializable, 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 9e9b8eaf77..b1b86747c1 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,9 +7,11 @@ 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.*; +import com.gregtechceu.gtceu.data.tag.GTDataComponents; import com.gregtechceu.gtceu.integration.ae2.machine.trait.MEPatternBufferProxyRecipeHandler; import com.lowdragmc.lowdraglib.gui.modular.ModularUI; @@ -19,10 +21,14 @@ 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.ItemInteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.BlockHitResult; import net.neoforged.neoforge.common.crafting.SizedIngredient; import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient; @@ -37,7 +43,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 +145,13 @@ public void onMachineRemoved() { machine.removeProxy(this); } } + + @Override + public ItemInteractionResult onDataStickUse(Player player, ItemStack dataStick) { + if (dataStick.has(GTDataComponents.DATA_COPY_POS)) { + setBuffer(dataStick.get(GTDataComponents.DATA_COPY_POS)); + return ItemInteractionResult.SUCCESS; + } + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + } }