Skip to content

Commit

Permalink
Allow Data Stick to copy-paste ME bus settings (GregTechCEu#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss authored Mar 28, 2024
1 parent bf9ce4d commit 111326a
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/main/java/gregtech/api/capability/IDataStickIntractable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gregtech.api.capability;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

public interface IDataStickIntractable {

void onDataStickLeftClick(EntityPlayer player, ItemStack dataStick);

boolean onDataStickRightClick(EntityPlayer player, ItemStack dataStick);
}
17 changes: 16 additions & 1 deletion src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.IDataStickIntractable;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.impl.AbstractRecipeLogic;
import gregtech.api.capability.impl.FluidHandlerProxy;
Expand Down Expand Up @@ -35,6 +36,7 @@
import gregtech.client.utils.BloomEffectUtil;
import gregtech.common.ConfigHolder;
import gregtech.common.creativetab.GTCreativeTabs;
import gregtech.common.items.MetaItems;

import net.minecraft.block.Block;
import net.minecraft.block.state.BlockFaceShape;
Expand Down Expand Up @@ -487,6 +489,12 @@ public final void onCoverLeftClick(EntityPlayer playerIn, CuboidRayTraceResult r
public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing,
CuboidRayTraceResult hitResult) {
ItemStack heldStack = playerIn.getHeldItem(hand);
if (this instanceof IDataStickIntractable dsi) {
if (MetaItems.TOOL_DATA_STICK.isItemEqual(heldStack) && dsi.onDataStickRightClick(playerIn, heldStack)) {
return true;
}
}

if (!playerIn.isSneaking() && openGUIOnRightClick()) {
if (getWorld() != null && !getWorld().isRemote) {
if (usesMui2()) {
Expand Down Expand Up @@ -642,7 +650,14 @@ public boolean onHardHammerClick(EntityPlayer playerIn, EnumHand hand, EnumFacin
return true;
}

public void onLeftClick(EntityPlayer player, EnumFacing facing, CuboidRayTraceResult hitResult) {}
public void onLeftClick(EntityPlayer player, EnumFacing facing, CuboidRayTraceResult hitResult) {
if (this instanceof IDataStickIntractable dsi) {
ItemStack stack = player.getHeldItemMainhand();
if (MetaItems.TOOL_DATA_STICK.isItemEqual(stack)) {
dsi.onDataStickLeftClick(player, stack);
}
}
}

/**
* @return true if the player must sneak to rotate this metatileentity, otherwise false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IDataStickIntractable;
import gregtech.api.capability.IGhostSlotConfigurable;
import gregtech.api.capability.INotifiableHandler;
import gregtech.api.capability.impl.GhostCircuitItemStackHandler;
Expand Down Expand Up @@ -54,15 +55,15 @@

public class MetaTileEntityMEInputBus extends MetaTileEntityAEHostablePart<IAEItemStack>
implements IMultiblockAbilityPart<IItemHandlerModifiable>,
IGhostSlotConfigurable {
IGhostSlotConfigurable, IDataStickIntractable {

public final static String ITEM_BUFFER_TAG = "ItemSlots";
public final static String WORKING_TAG = "WorkingEnabled";
private final static int CONFIG_SIZE = 16;
private boolean workingEnabled = true;
protected ExportOnlyAEItemList aeItemHandler;
private GhostCircuitItemStackHandler circuitInventory;
private NotifiableItemStackHandler extraSlotInventory;
protected GhostCircuitItemStackHandler circuitInventory;
protected NotifiableItemStackHandler extraSlotInventory;
private ItemHandlerList actualImportItems;

public MetaTileEntityMEInputBus(ResourceLocation metaTileEntityId) {
Expand Down Expand Up @@ -318,6 +319,7 @@ public void addInformation(ItemStack stack, @Nullable World player, @NotNull Lis
tooltip.add(I18n.format("gregtech.machine.item_bus.import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.item_import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me_import_item_hatch.configs.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.copy_paste.tooltip"));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}

Expand Down Expand Up @@ -346,4 +348,61 @@ public void setGhostCircuitConfig(int config) {
markDirty();
}
}

@Override
public final void onDataStickLeftClick(EntityPlayer player, ItemStack dataStick) {
NBTTagCompound tag = new NBTTagCompound();
tag.setTag("MEInputBus", writeConfigToTag());
dataStick.setTagCompound(tag);
dataStick.setTranslatableName("gregtech.machine.me.item_import.data_stick.name");
player.sendStatusMessage(new TextComponentTranslation("gregtech.machine.me.import_copy_settings"), true);
}

protected NBTTagCompound writeConfigToTag() {
NBTTagCompound tag = new NBTTagCompound();
NBTTagCompound configStacks = new NBTTagCompound();
tag.setTag("ConfigStacks", configStacks);
for (int i = 0; i < CONFIG_SIZE; i++) {
var slot = this.aeItemHandler.getInventory()[i];
IAEItemStack config = slot.getConfig();
if (config == null) {
continue;
}
NBTTagCompound stackNbt = new NBTTagCompound();
config.getDefinition().writeToNBT(stackNbt);
configStacks.setTag(Integer.toString(i), stackNbt);
}
tag.setByte("GhostCircuit", (byte) this.circuitInventory.getCircuitValue());
return tag;
}

@Override
public final boolean onDataStickRightClick(EntityPlayer player, ItemStack dataStick) {
NBTTagCompound tag = dataStick.getTagCompound();
if (tag == null || !tag.hasKey("MEInputBus")) {
return false;
}
readConfigFromTag(tag.getCompoundTag("MEInputBus"));
syncME();
player.sendStatusMessage(new TextComponentTranslation("gregtech.machine.me.import_paste_settings"), true);
return true;
}

protected void readConfigFromTag(NBTTagCompound tag) {
if (tag.hasKey("ConfigStacks")) {
NBTTagCompound configStacks = tag.getCompoundTag("ConfigStacks");
for (int i = 0; i < CONFIG_SIZE; i++) {
String key = Integer.toString(i);
if (configStacks.hasKey(key)) {
NBTTagCompound configTag = configStacks.getCompoundTag(key);
this.aeItemHandler.getInventory()[i].setConfig(WrappedItemStack.fromNBT(configTag));
} else {
this.aeItemHandler.getInventory()[i].setConfig(null);
}
}
}
if (tag.hasKey("GhostCircuit")) {
this.setGhostCircuitConfig(tag.getByte("GhostCircuit"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IDataStickIntractable;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
Expand All @@ -26,6 +27,7 @@
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.IFluidTank;
Expand All @@ -44,7 +46,7 @@
import java.util.List;

public class MetaTileEntityMEInputHatch extends MetaTileEntityAEHostablePart<IAEFluidStack>
implements IMultiblockAbilityPart<IFluidTank> {
implements IMultiblockAbilityPart<IFluidTank>, IDataStickIntractable {

public final static String FLUID_BUFFER_TAG = "FluidTanks";
public final static String WORKING_TAG = "WorkingEnabled";
Expand Down Expand Up @@ -257,6 +259,7 @@ public void addInformation(ItemStack stack, @Nullable World player, @NotNull Lis
tooltip.add(I18n.format("gregtech.machine.fluid_hatch.import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.fluid_import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me_import_fluid_hatch.configs.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.copy_paste.tooltip"));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}

Expand All @@ -269,4 +272,57 @@ public MultiblockAbility<IFluidTank> getAbility() {
public void registerAbilities(List<IFluidTank> list) {
list.addAll(Arrays.asList(this.getAEFluidHandler().getInventory()));
}

@Override
public final void onDataStickLeftClick(EntityPlayer player, ItemStack dataStick) {
NBTTagCompound tag = new NBTTagCompound();
tag.setTag("MEInputHatch", writeConfigToTag());
dataStick.setTagCompound(tag);
dataStick.setTranslatableName("gregtech.machine.me.fluid_import.data_stick.name");
player.sendStatusMessage(new TextComponentTranslation("gregtech.machine.me.import_copy_settings"), true);
}

protected NBTTagCompound writeConfigToTag() {
NBTTagCompound tag = new NBTTagCompound();
NBTTagCompound configStacks = new NBTTagCompound();
tag.setTag("ConfigStacks", configStacks);
for (int i = 0; i < CONFIG_SIZE; i++) {
var slot = this.aeFluidHandler.getInventory()[i];
IAEFluidStack config = slot.getConfig();
if (config == null) {
continue;
}
NBTTagCompound stackNbt = new NBTTagCompound();
config.writeToNBT(stackNbt);
configStacks.setTag(Integer.toString(i), stackNbt);
}
return tag;
}

@Override
public final boolean onDataStickRightClick(EntityPlayer player, ItemStack dataStick) {
NBTTagCompound tag = dataStick.getTagCompound();
if (tag == null || !tag.hasKey("MEInputHatch")) {
return false;
}
readConfigFromTag(tag.getCompoundTag("MEInputHatch"));
syncME();
player.sendStatusMessage(new TextComponentTranslation("gregtech.machine.me.import_paste_settings"), true);
return true;
}

protected void readConfigFromTag(NBTTagCompound tag) {
if (tag.hasKey("ConfigStacks")) {
NBTTagCompound configStacks = tag.getCompoundTag("ConfigStacks");
for (int i = 0; i < CONFIG_SIZE; i++) {
String key = Integer.toString(i);
if (configStacks.hasKey(key)) {
NBTTagCompound configTag = configStacks.getCompoundTag(key);
this.aeFluidHandler.getInventory()[i].setConfig(WrappedFluidStack.fromNBT(configTag));
} else {
this.aeFluidHandler.getInventory()[i].setConfig(null);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,38 @@ public void addInformation(ItemStack stack, @Nullable World player, @NotNull Lis
tooltip.add(I18n.format("gregtech.machine.item_bus.import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.stocking_item.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me_import_item_hatch.configs.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.copy_paste.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.stocking_item.tooltip.2"));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}

@Override
protected NBTTagCompound writeConfigToTag() {
if (!autoPull) {
NBTTagCompound tag = super.writeConfigToTag();
tag.setBoolean("AutoPull", false);
return tag;
}
// if in auto-pull, no need to write actual configured slots, but still need to write the ghost circuit
NBTTagCompound tag = new NBTTagCompound();
tag.setBoolean("AutoPull", true);
tag.setByte("GhostCircuit", (byte) this.circuitInventory.getCircuitValue());
return tag;
}

@Override
protected void readConfigFromTag(NBTTagCompound tag) {
if (tag.getBoolean("AutoPull")) {
// if being set to auto-pull, no need to read the configured slots
this.setAutoPull(true);
this.setGhostCircuitConfig(tag.getByte("GhostCircuit"));
return;
}
// set auto pull first to avoid issues with clearing the config after reading from the data stick
this.setAutoPull(false);
super.readConfigFromTag(tag);
}

private static class ExportOnlyAEStockingItemList extends ExportOnlyAEItemList {

private final MetaTileEntityMEStockingBus holder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,36 @@ public void addInformation(ItemStack stack, @Nullable World player, @NotNull Lis
tooltip.add(I18n.format("gregtech.machine.fluid_hatch.import.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.stocking_fluid.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me_import_fluid_hatch.configs.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.copy_paste.tooltip"));
tooltip.add(I18n.format("gregtech.machine.me.stocking_fluid.tooltip.2"));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}

@Override
protected NBTTagCompound writeConfigToTag() {
if (!autoPull) {
NBTTagCompound tag = super.writeConfigToTag();
tag.setBoolean("AutoPull", false);
return tag;
}
// if in auto-pull, no need to write actual configured slots, but still need to write the ghost circuit
NBTTagCompound tag = new NBTTagCompound();
tag.setBoolean("AutoPull", true);
return tag;
}

@Override
protected void readConfigFromTag(NBTTagCompound tag) {
if (tag.getBoolean("AutoPull")) {
// if being set to auto-pull, no need to read the configured slots
this.setAutoPull(true);
return;
}
// set auto pull first to avoid issues with clearing the config after reading from the data stick
this.setAutoPull(false);
super.readConfigFromTag(tag);
}

private static class ExportOnlyAEStockingFluidSlot extends ExportOnlyAEFluidSlot {

public ExportOnlyAEStockingFluidSlot(MetaTileEntityMEStockingHatch holder, IAEFluidStack config,
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -5278,6 +5278,11 @@ gregtech.machine.me.fluid_export.tooltip=Stores fluids directly into the ME netw
gregtech.machine.me.fluid_export.tooltip.2=Can cache an infinite amount of fluid
gregtech.machine.me.stocking_auto_pull_enabled=Auto-Pull Enabled
gregtech.machine.me.stocking_auto_pull_disabled=Auto-Pull Disabled
gregtech.machine.me.copy_paste.tooltip=Left-click with Data Stick to copy settings, right-click to apply
gregtech.machine.me.import_copy_settings=Saved settings to Data Stick
gregtech.machine.me.import_paste_settings=Applied settings from Data Stick
gregtech.machine.me.item_import.data_stick.name=§oME Input Bus Configuration Data
gregtech.machine.me.fluid_import.data_stick.name=§oME Input Hatch Configuration Data

# Universal tooltips
gregtech.universal.tooltip.voltage_in=§aVoltage IN: §f%,d EU/t (%s§f)
Expand Down

0 comments on commit 111326a

Please sign in to comment.