Transform from TE -> TE working

This commit is contained in:
Connor Colenso committed 2023-10-29 23:53:12 +00:00
1 parent 036452b944
commit d112fd627f
3 files changed
+99 -19

No files matched your search

@@ -1,12 +1,11 @@
package com.colen.postea.API;
import akka.japi.Pair;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagByteArray;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -24,7 +23,14 @@ public class ChunkFixerUtility {
*/
public static void replaceTileEntitiesWithNormalBlock(NBTTagCompound chunkNBT) {
NBTTagCompound level = chunkNBT.getCompoundTag("Level");
List<ConversionInfo> conversionInfoList = removeTileEntities(level.getTagList("TileEntities", 10));
Pair<List<ConversionInfo>, NBTTagList> output = adjustTileEntities(level.getTagList("TileEntities", 10));
List<ConversionInfo> conversionInfoList = output.first();
NBTTagList tileEntities = output.second();
if (tileEntities.tagCount() > 0) {
level.setTag("TileEntities", tileEntities);
}
int chunkXPos = level.getInteger("xPos") * 16; // Assuming each chunk is 16 blocks along x-axis
int chunkZPos = level.getInteger("zPos") * 16; // Assuming each chunk is 16 blocks along z-axis
@@ -89,9 +95,10 @@ public class ChunkFixerUtility {
metadataArray[metadataIndex] = currentMetadataByte;
}
private static List<ConversionInfo> removeTileEntities(NBTTagList tileEntities) {
private static Pair<List<ConversionInfo>, NBTTagList> adjustTileEntities(NBTTagList tileEntities) {
List<ConversionInfo> conversionInfo = new ArrayList<>();
List<Integer> indicesToRemove = new ArrayList<>(); // List to store indices of tileEntities to be removed
NBTTagList tileEntitiesCopy = new NBTTagList();
for (int i = 0; i < tileEntities.tagCount(); i++) {
NBTTagCompound tileEntity = tileEntities.getCompoundTagAt(i);
@@ -107,18 +114,16 @@ public class ChunkFixerUtility {
BlockInfo blockInfo = transformationFunction.apply(tileEntity);
if (blockInfo.tileTransformer != null) {
tileEntitiesCopy.appendTag(blockInfo.tileTransformer.apply(tileEntity));
} // Otherwise they are removed, therefore not appended.
conversionInfo.add(new ConversionInfo(x, y, z, blockInfo));
indicesToRemove.add(i); // Add the index to the list for removal
}
}
// Remove the tile entities in reverse order to avoid index shifts
Collections.reverse(indicesToRemove);
for (int index : indicesToRemove) {
tileEntities.removeTag(index);
}
return conversionInfo;
return new Pair<>(conversionInfo, tileEntitiesCopy);
}
@@ -153,10 +158,18 @@ public class ChunkFixerUtility {
public static class BlockInfo {
public final Block block;
public final int metadata;
public final Function<NBTTagCompound, NBTTagCompound> tileTransformer;
public BlockInfo(Block block, int metadata, Function<NBTTagCompound, NBTTagCompound> tileTransformer) {
this.block = block;
this.metadata = metadata;
this.tileTransformer = tileTransformer;
}
public BlockInfo(Block block, int metadata) {
this.block = block;
this.metadata = metadata;
this.tileTransformer = null;
}
}
@@ -20,7 +20,7 @@ public class TileEntityReplacementManager {
return tileEntityFixMap.getOrDefault(tileName, null);
}
public static void tileEntityToNormalBlockTransformer(String tileEntityId, Function<NBTTagCompound, ChunkFixerUtility.BlockInfo> transformerFunction) {
public static void tileEntityTransformer(String tileEntityId, Function<NBTTagCompound, ChunkFixerUtility.BlockInfo> transformerFunction) {
tileEntityToNormalBlockTransformer.put(tileEntityId, transformerFunction);
}
+73 -6
View File
@@ -7,6 +7,7 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.util.function.Function;
@@ -31,7 +32,7 @@ public class Postea {
};*/
//TileEntityReplacementManager.tileEntityFixer("GT_TileEntity_Ores", myTransformer);
TileEntityReplacementManager.tileEntityToNormalBlockTransformer("GT_TileEntity_Ores", (tag) -> {
TileEntityReplacementManager.tileEntityTransformer("GT_TileEntity_Ores", (tag) -> {
if (tag.getInteger("m") == 86) {
return new ChunkFixerUtility.BlockInfo(Blocks.wool, 5);
}
@@ -39,13 +40,79 @@ public class Postea {
return new ChunkFixerUtility.BlockInfo(Blocks.wool, 1);
});
TileEntityReplacementManager.tileEntityToNormalBlockTransformer("Furnace", (tag) -> {
return new ChunkFixerUtility.BlockInfo(Blocks.planks, 3);
/* TileEntityReplacementManager.tileEntityTransformer("Furnace", (tag) -> {
Function<NBTTagCompound, NBTTagCompound> chestTransformer = (oldTag) -> {
NBTTagCompound newTag = cleanseNBT("Chest", oldTag);
NBTTagList tagList = new NBTTagList();
NBTTagCompound stoneAtSlot13 = new NBTTagCompound();
stoneAtSlot13.setByte("Count", (byte) 1);
stoneAtSlot13.setByte("Slot", (byte) 13);
stoneAtSlot13.setShort("Damage", (short) 0);
stoneAtSlot13.setShort("id", (short) 1);
tagList.appendTag(stoneAtSlot13);
newTag.setTag("Items", tagList);
return newTag;
};
return new ChunkFixerUtility.BlockInfo(Blocks.chest, 0, chestTransformer);
});*/
TileEntityReplacementManager.tileEntityTransformer("Chest", (tag) -> {
Function<NBTTagCompound, NBTTagCompound> chestTransformer = (oldTag) -> {
NBTTagCompound newTag = cleanseNBT("Chest", oldTag);
NBTTagList tagList = new NBTTagList();
NBTTagCompound stoneAtSlot12 = new NBTTagCompound();
stoneAtSlot12.setByte("Count", (byte) 2);
stoneAtSlot12.setByte("Slot", (byte) 12);
stoneAtSlot12.setShort("Damage", (short) 0);
stoneAtSlot12.setShort("id", (short) 1);
NBTTagCompound stoneAtSlot14 = new NBTTagCompound();
stoneAtSlot14.setByte("Count", (byte) 4);
stoneAtSlot14.setByte("Slot", (byte) 14);
stoneAtSlot14.setShort("Damage", (short) 3);
stoneAtSlot14.setShort("id", (short) 5);
tagList.appendTag(stoneAtSlot12);
tagList.appendTag(stoneAtSlot14);
newTag.setTag("Items", tagList);
return newTag;
};
return new ChunkFixerUtility.BlockInfo(Blocks.chest, 0, chestTransformer);
});
TileEntityReplacementManager.tileEntityToNormalBlockTransformer("Chest", (tag) -> {
return new ChunkFixerUtility.BlockInfo(Blocks.coal_block, 0);
});
// TileEntityReplacementManager.tileEntityToNormalBlockTransformer("Chest", (tag) -> {
// return new ChunkFixerUtility.BlockInfo(Blocks.coal_block, 0);
// });
}
private NBTTagCompound cleanseNBT(String newTileEntityID, NBTTagCompound tag) {
// Cleans the NBT, so that it replaces the ID and adds the x, y, z coordinates.
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setString("id", newTileEntityID);
tagCompound.setInteger("x", tag.getInteger("x"));
tagCompound.setInteger("y", tag.getInteger("y"));
tagCompound.setInteger("z", tag.getInteger("z"));
return tagCompound;
}
@Mod.EventHandler