aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/api')
-rw-r--r--src/main/java/gregtech/api/gui/modularui/GT_UIInfos.java3
-rw-r--r--src/main/java/gregtech/api/interfaces/IDataCopyable.java30
-rw-r--r--src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Hatch_OutputBus.java38
-rw-r--r--src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java30
-rw-r--r--src/main/java/gregtech/api/util/GT_Util.java153
5 files changed, 239 insertions, 15 deletions
diff --git a/src/main/java/gregtech/api/gui/modularui/GT_UIInfos.java b/src/main/java/gregtech/api/gui/modularui/GT_UIInfos.java
index 89a0835f13..ea9a39bdf8 100644
--- a/src/main/java/gregtech/api/gui/modularui/GT_UIInfos.java
+++ b/src/main/java/gregtech/api/gui/modularui/GT_UIInfos.java
@@ -7,6 +7,7 @@ import java.util.function.Function;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
+import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizons.modularui.api.UIInfos;
@@ -98,7 +99,7 @@ public class GT_UIInfos {
* Opens TileEntity UI, created by {@link ITileWithModularUI#createWindow}.
*/
public static void openGTTileEntityUI(IHasWorldObjectAndCoords aTileEntity, EntityPlayer aPlayer) {
- if (aTileEntity.isClientSide()) return;
+ if (aTileEntity.isClientSide() || aPlayer instanceof FakePlayer) return;
GTTileEntityDefaultUI.open(
aPlayer,
aTileEntity.getWorld(),
diff --git a/src/main/java/gregtech/api/interfaces/IDataCopyable.java b/src/main/java/gregtech/api/interfaces/IDataCopyable.java
new file mode 100644
index 0000000000..9994354fa7
--- /dev/null
+++ b/src/main/java/gregtech/api/interfaces/IDataCopyable.java
@@ -0,0 +1,30 @@
+package gregtech.api.interfaces;
+
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.nbt.NBTTagCompound;
+
+public interface IDataCopyable {
+
+ /**
+ * Implementation will set the type field. caller can assume the returned tag (if not null) always contains a "type"
+ * field
+ * with a value of {@link #getCopiedDataIdentifier(EntityPlayer)}
+ * Implementation should not try to alert player of situations that copy has failed.
+ *
+ * @return null if cannot copy (e.g. not properly configured yet) or the data to copy.
+ */
+ NBTTagCompound getCopiedData(EntityPlayer player);
+
+ /**
+ * Callee should check if the given tag is valid.
+ * Implementation should not try to alert player of situations that paste has failed.
+ *
+ * @return true if pasted. false otherwise.
+ */
+ boolean pasteCopiedData(EntityPlayer player, NBTTagCompound nbt);
+
+ /**
+ * @return the type identifier. this should be a constant for the given set of arguments.
+ */
+ String getCopiedDataIdentifier(EntityPlayer player);
+}
diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Hatch_OutputBus.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Hatch_OutputBus.java
index 5fdfa7899b..0bfe55cac1 100644
--- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Hatch_OutputBus.java
+++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_Hatch_OutputBus.java
@@ -22,6 +22,7 @@ import gregtech.GT_Mod;
import gregtech.api.enums.ItemList;
import gregtech.api.gui.modularui.GT_UIInfos;
import gregtech.api.gui.widgets.GT_PhantomItemButton;
+import gregtech.api.interfaces.IDataCopyable;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IItemLockable;
import gregtech.api.interfaces.modularui.IAddUIWidgets;
@@ -31,7 +32,8 @@ import gregtech.api.render.TextureFactory;
import gregtech.api.util.GT_Utility;
import gregtech.api.util.extensions.ArrayExt;
-public class GT_MetaTileEntity_Hatch_OutputBus extends GT_MetaTileEntity_Hatch implements IAddUIWidgets, IItemLockable {
+public class GT_MetaTileEntity_Hatch_OutputBus extends GT_MetaTileEntity_Hatch
+ implements IAddUIWidgets, IItemLockable, IDataCopyable {
private static final String DATA_STICK_DATA_TYPE = "outputBusFilter";
private static final String LOCKED_ITEM_NBT_KEY = "lockedItem";
@@ -127,17 +129,11 @@ public class GT_MetaTileEntity_Hatch_OutputBus extends GT_MetaTileEntity_Hatch i
return super.onRightclick(aBaseMetaTileEntity, aPlayer);
}
- if (!dataStick.hasTagCompound() || !DATA_STICK_DATA_TYPE.equals(dataStick.stackTagCompound.getString("type"))) {
+ if (!pasteCopiedData(aPlayer, dataStick.stackTagCompound)) {
aPlayer.addChatMessage(new ChatComponentTranslation("GT5U.machines.output_bus.invalid"));
return false;
}
- final NBTTagCompound nbt = dataStick.stackTagCompound;
- if (nbt.hasKey(LOCKED_ITEM_NBT_KEY)) {
- lockedItem = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag(LOCKED_ITEM_NBT_KEY));
- } else {
- lockedItem = null;
- }
aPlayer.addChatMessage(new ChatComponentTranslation("GT5U.machines.output_bus.loaded"));
return true;
@@ -153,15 +149,35 @@ public class GT_MetaTileEntity_Hatch_OutputBus extends GT_MetaTileEntity_Hatch i
return;
}
+ dataStick.stackTagCompound = getCopiedData(aPlayer);
+ dataStick.setStackDisplayName("Output Bus Configuration");
+ aPlayer.addChatMessage(new ChatComponentTranslation("GT5U.machines.output_bus.saved"));
+ }
+
+ @Override
+ public NBTTagCompound getCopiedData(EntityPlayer player) {
final NBTTagCompound nbt = new NBTTagCompound();
nbt.setString("type", DATA_STICK_DATA_TYPE);
if (lockedItem != null) {
nbt.setTag(LOCKED_ITEM_NBT_KEY, lockedItem.writeToNBT(new NBTTagCompound()));
}
+ return nbt;
+ }
- dataStick.stackTagCompound = nbt;
- dataStick.setStackDisplayName("Output Bus Configuration");
- aPlayer.addChatMessage(new ChatComponentTranslation("GT5U.machines.output_bus.saved"));
+ @Override
+ public boolean pasteCopiedData(EntityPlayer player, NBTTagCompound nbt) {
+ if (nbt == null || !DATA_STICK_DATA_TYPE.equals(nbt.getString("type"))) return false;
+ if (nbt.hasKey(LOCKED_ITEM_NBT_KEY)) {
+ lockedItem = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag(LOCKED_ITEM_NBT_KEY));
+ } else {
+ lockedItem = null;
+ }
+ return true;
+ }
+
+ @Override
+ public String getCopiedDataIdentifier(EntityPlayer player) {
+ return DATA_STICK_DATA_TYPE;
}
/**
diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java
index fb77abd8b2..709191f10c 100644
--- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java
+++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java
@@ -28,6 +28,7 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
+import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
@@ -84,6 +85,7 @@ import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_OverclockCalculator;
import gregtech.api.util.GT_ParallelHelper;
import gregtech.api.util.GT_Recipe;
+import gregtech.api.util.GT_Util;
import gregtech.api.util.GT_Utility;
import gregtech.api.util.GT_Waila;
import gregtech.api.util.OutputHatchWrapper;
@@ -344,8 +346,36 @@ public abstract class GT_MetaTileEntity_MultiBlockBase extends MetaTileEntity
return SingleRecipeCheck.tryLoad(getRecipeMap(), aNBT);
}
+ public boolean saveOtherHatchConfiguration(EntityPlayer player) {
+ return true;
+ }
+
+ public boolean loadOtherHatchConfiguration(EntityPlayer player) {
+ return true;
+ }
+
+ @Override
+ public void onLeftclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
+ if (aBaseMetaTileEntity.isServerSide() && GT_Util.saveMultiblockInputConfiguration(this, aPlayer)) {
+ aPlayer.addChatComponentMessage(new ChatComponentTranslation("GT5U.MULTI_MACHINE_CONFIG.SAVE"));
+ return;
+ }
+ super.onLeftclick(aBaseMetaTileEntity, aPlayer);
+ }
+
@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
+ if (GT_Util.hasMultiblockInputConfiguration(aPlayer.getHeldItem())) {
+ if (aBaseMetaTileEntity.isServerSide()) {
+ if (GT_Util.loadMultiblockInputConfiguration(this, aPlayer)) {
+ aPlayer.addChatComponentMessage(new ChatComponentTranslation("GT5U.MULTI_MACHINE_CONFIG.LOAD"));
+ } else {
+ aPlayer
+ .addChatComponentMessage(new ChatComponentTranslation("GT5U.MULTI_MACHINE_CONFIG.LOAD.FAIL"));
+ }
+ }
+ return true;
+ }
GT_UIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer);
return true;
}
diff --git a/src/main/java/gregtech/api/util/GT_Util.java b/src/main/java/gregtech/api/util/GT_Util.java
index 62ebcecd79..cdca6a1b9e 100644
--- a/src/main/java/gregtech/api/util/GT_Util.java
+++ b/src/main/java/gregtech/api/util/GT_Util.java
@@ -1,16 +1,29 @@
package gregtech.api.util;
+import static gregtech.api.util.GT_Utility.filterValidMTEs;
+
+import java.util.List;
+
import net.minecraft.block.Block;
+import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
+import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
+import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.Tuple;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
+import net.minecraftforge.common.util.Constants;
+import gregtech.api.enums.ItemList;
+import gregtech.api.interfaces.IDataCopyable;
+import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch;
+import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity;
+import gregtech.common.items.behaviors.Behaviour_DataOrb;
public class GT_Util {
@@ -99,7 +112,9 @@ public class GT_Util {
return null;
}
- /** Sets the TileEntity at the passed position, with the option of turning adjacent TileEntity updates off. */
+ /**
+ * Sets the TileEntity at the passed position, with the option of turning adjacent TileEntity updates off.
+ */
public static TileEntity setTileEntity(World world, int x, int y, int z, TileEntity aTileEntity,
boolean aCauseTileEntityUpdates) {
if (aCauseTileEntityUpdates) world.setTileEntity(x, y, z, aTileEntity);
@@ -128,7 +143,9 @@ public class GT_Util {
return getTileEntity(world, coords.posX, coords.posY, coords.posZ, loadUnloadedChunks);
}
- /** Marks a Chunk dirty so it is saved */
+ /**
+ * Marks a Chunk dirty so it is saved
+ */
public static boolean markChunkDirty(World world, int x, int z) {
if (world == null || world.isRemote) return false;
Chunk aChunk = world.getChunkFromBlockCoords(x, z);
@@ -145,7 +162,9 @@ public class GT_Util {
return true;
}
- /** Marks a Chunk dirty so it is saved */
+ /**
+ * Marks a Chunk dirty so it is saved
+ */
public static boolean markChunkDirty(Object maybeTile) {
return maybeTile instanceof TileEntity tileEntity
&& markChunkDirty(tileEntity.getWorldObj(), tileEntity.xCoord, tileEntity.zCoord);
@@ -197,4 +216,132 @@ public class GT_Util {
public static short getA(int aColors) {
return (short) ((aColors >>> 24) & 255);
}
+
+ public static boolean saveMultiblockInputConfiguration(GT_MetaTileEntity_MultiBlockBase controller,
+ EntityPlayer player) {
+ NBTTagCompound newTag = new NBTTagCompound();
+ ItemStack dataOrb = player.getHeldItem();
+ if (GT_Utility.isStackInvalid(dataOrb) || !ItemList.Tool_DataOrb.isStackEqual(dataOrb, false, true)) {
+ return false;
+ }
+ if (!controller.saveOtherHatchConfiguration(player)) {
+ return false;
+ }
+ newTag.setString("type", "MultiblockConfiguration");
+ int count = 0;
+ NBTTagList list = saveConfigurationToDataStick(player, controller.mInputBusses);
+ if (list == null) return false;
+ newTag.setTag("mInputBusses", list);
+ count += list.tagCount();
+ list = saveConfigurationToDataStick(player, controller.mInputHatches);
+ if (list == null) return false;
+ newTag.setTag("mInputHatches", list);
+ count += list.tagCount();
+ list = saveConfigurationToDataStick(player, controller.mOutputBusses);
+ if (list == null) return false;
+ newTag.setTag("mOutputBusses", list);
+ count += list.tagCount();
+ // Output hatch config currently cannot be copied, so we omit this part for now
+ // TODO this doesn't work for now
+ // newTag.setTag("mDualInputHatches", saveToDataStick(player, controller.mDualInputHatches));
+ dataOrb.setTagCompound(newTag);
+ Behaviour_DataOrb.setDataTitle(dataOrb, "Multiblock Hatch Configuration");
+ Behaviour_DataOrb.setDataName(dataOrb, String.format("%s configuration saved", count));
+ return true;
+ }
+
+ public static boolean hasMultiblockInputConfiguration(ItemStack dataOrb) {
+ return !GT_Utility.isStackInvalid(dataOrb) && ItemList.Tool_DataOrb.isStackEqual(dataOrb, false, true)
+ && dataOrb.getTagCompound() != null
+ && "MultiblockConfiguration".equals(
+ dataOrb.getTagCompound()
+ .getString("type"));
+ }
+
+ public static boolean loadMultiblockInputConfiguration(GT_MetaTileEntity_MultiBlockBase controller,
+ EntityPlayer player) {
+ ItemStack dataOrb = player.getHeldItem();
+ if (!hasMultiblockInputConfiguration(dataOrb)) {
+ return false;
+ }
+ if (!controller.loadOtherHatchConfiguration(player)) {
+ return false;
+ }
+ NBTTagCompound tag = dataOrb.getTagCompound();
+ if (!checkCanLoadConfigurationFromDataStick(
+ tag.getTagList("mInputBusses", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mInputBusses)
+ || !checkCanLoadConfigurationFromDataStick(
+ tag.getTagList("mInputHatches", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mInputHatches)
+ || !checkCanLoadConfigurationFromDataStick(
+ tag.getTagList("mOutputBusses", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mOutputBusses))
+ return false;
+
+ if (!loadConfigurationFromDataStick(
+ tag.getTagList("mInputBusses", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mInputBusses)) return false;
+ if (!loadConfigurationFromDataStick(
+ tag.getTagList("mInputHatches", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mInputHatches)) return false;
+ if (!loadConfigurationFromDataStick(
+ tag.getTagList("mOutputBusses", Constants.NBT.TAG_COMPOUND),
+ player,
+ controller.mOutputBusses)) return false;
+ return true;
+ }
+
+ private static NBTTagList saveConfigurationToDataStick(EntityPlayer player,
+ List<? extends GT_MetaTileEntity_Hatch> hatches) {
+ NBTTagList list = new NBTTagList();
+ for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(hatches)) {
+ if (!(tHatch instanceof IDataCopyable copyable)) {
+ list.appendTag(new NBTTagCompound());
+ continue;
+ }
+ NBTTagCompound tag = copyable.getCopiedData(player);
+ if (tag == null) return null;
+ list.appendTag(tag);
+ }
+ return list;
+ }
+
+ private static boolean loadConfigurationFromDataStick(NBTTagList list, EntityPlayer player,
+ List<? extends GT_MetaTileEntity_Hatch> hatches) {
+ if (list == null || list.tagList.isEmpty()) return false;
+ List<? extends GT_MetaTileEntity_Hatch> validMTEs = filterValidMTEs(hatches);
+ int end = Math.min(validMTEs.size(), list.tagCount());
+ for (int i = 0; i < end; i++) {
+ GT_MetaTileEntity_Hatch tHatch = validMTEs.get(i);
+ NBTTagCompound tag = list.getCompoundTagAt(i);
+ if (!(tHatch instanceof IDataCopyable copyable)) {
+ if (tag.hasNoTags()) continue;
+ return false;
+ }
+ if (tag.hasNoTags()) return false;
+ if (!copyable.pasteCopiedData(player, tag)) return false;
+ }
+ return true;
+ }
+
+ private static boolean checkCanLoadConfigurationFromDataStick(NBTTagList list, EntityPlayer player,
+ List<? extends GT_MetaTileEntity_Hatch> hatches) {
+ if (list == null || list.tagList.isEmpty()) return false;
+ List<? extends GT_MetaTileEntity_Hatch> validMTEs = filterValidMTEs(hatches);
+ int end = Math.min(validMTEs.size(), list.tagCount());
+ for (int i = 0; i < end; i++) {
+ GT_MetaTileEntity_Hatch tHatch = validMTEs.get(i);
+ NBTTagCompound tag = list.getCompoundTagAt(i);
+ if (tag.hasNoTags()) continue;
+ if (!(tHatch instanceof IDataCopyable copyable) || !copyable.getCopiedDataIdentifier(player)
+ .equals(tag.getString("type"))) return false;
+ }
+ return true;
+ }
}