Skip to content

Commit

Permalink
Refactor DataStick item and associated interaction interface (#2561)
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 authored and screret committed Dec 28, 2024
1 parent 927ec3c commit 6b19c94
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -82,24 +85,24 @@ public void appendHoverText(ItemStack stack, Item.TooltipContext context, List<C
}

@Override
public InteractionResult useOn(UseOnContext context) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
ItemStack stack = context.getItemInHand();
if (!level.isClientSide) {
MetaMachine machine = MetaMachine.getMachine(level, pos);
Pair<GTRecipeType, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +98,7 @@ public InteractionResultHolder<ItemStack> 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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
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;
import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler;
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<CompoundTag>, IContentChangeAware {

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 6b19c94

Please sign in to comment.