Skip to content

Commit

Permalink
Refactor DataStick item and associated interaction interface (GregTec…
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 committed Dec 25, 2024
1 parent 029f610 commit c0b5045
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -81,27 +80,24 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
}

@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.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);
}
}
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 @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,19 @@ 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());
dataStick.setTag(tag);
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,19 @@ 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());
dataStick.setTag(tag);
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<CompoundTag>, IContentChangeAware {

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

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c0b5045

Please sign in to comment.