Files
Postea/src/main/java/com/colen/postea/Utility/ChunkFixerUtility.java
T

216 lines
9.0 KiB
Java
Raw Normal View History

2023-10-31 01:09:19 +00:00
package com.colen.postea.Utility;
2024-01-12 03:40:06 +00:00
import static com.colen.postea.Utility.PosteaUtilities.getModListHash;
2023-10-31 17:14:09 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import net.minecraft.block.Block;
2023-10-29 01:06:18 +01:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2023-10-30 20:02:08 +00:00
import net.minecraft.world.World;
2023-10-31 17:14:09 +00:00
import com.colen.postea.API.BlockReplacementManager;
import com.colen.postea.API.TileEntityReplacementManager;
import akka.japi.Pair;
import cpw.mods.fml.common.registry.GameRegistry;
2023-10-29 01:37:24 +00:00
2023-10-23 15:37:55 +01:00
public class ChunkFixerUtility {
2023-10-30 20:02:08 +00:00
public static void processChunkNBT(NBTTagCompound compound, World world) {
NBTTagCompound levelCompoundTag = compound.getCompoundTag("Level");
2024-01-05 15:40:11 +00:00
if (processChunk(levelCompoundTag)) {
transformTileEntities(levelCompoundTag, world);
transformNormalBlocks(levelCompoundTag, world);
levelCompoundTag.setInteger("POSTEA", POSTEA_UPDATE_CODE);
}
2023-10-30 01:28:47 +00:00
}
// This will not change between runs, unless a mod is updated or added.
public static final int POSTEA_UPDATE_CODE = getModListHash();
2024-01-11 16:32:13 +00:00
// If a chunk has the same postea code, then do not update it. Return false.
private static boolean processChunk(NBTTagCompound compound) {
// I'm minimising the string length here since this
// will be on every single chunk in every world... Rather not duplicate too much.
if (compound.hasKey("POSTEA")) {
return compound.getInteger("POSTEA") != POSTEA_UPDATE_CODE;
}
return true;
}
2024-01-05 15:40:11 +00:00
private static void transformNormalBlocks(NBTTagCompound levelCompoundTag, World world) {
NBTTagList sections = levelCompoundTag.getTagList("Sections", 10);
2023-10-31 00:27:30 +00:00
int chunkXPos = levelCompoundTag.getInteger("xPos") * 16;
int chunkZPos = levelCompoundTag.getInteger("zPos") * 16;
2023-10-31 00:27:30 +00:00
2023-10-30 01:28:47 +00:00
for (int i = 0; i < sections.tagCount(); i++) {
NBTTagCompound section = sections.getCompoundTagAt(i);
2023-12-20 02:00:48 +00:00
// Blocks16 and Data16 exist only because of NEID.
2023-10-30 01:28:47 +00:00
byte[] blockArray = section.getByteArray("Blocks16");
2023-12-20 02:00:48 +00:00
byte[] metadataArray = section.getByteArray("Data16");
2023-10-31 00:27:30 +00:00
byte sectionY = section.getByte("Y");
2023-10-30 01:28:47 +00:00
for (int index = 0; index < blockArray.length / 2; index++) {
int blockId = ((blockArray[index * 2] & 0xFF) << 8) | (blockArray[index * 2 + 1] & 0xFF);
2023-12-20 02:00:48 +00:00
int metadata = ((metadataArray[index * 2] & 0xFF) << 8) | (metadataArray[index * 2 + 1] & 0xFF);
2023-10-30 01:28:47 +00:00
2023-10-31 00:27:30 +00:00
Block block = Block.getBlockById(blockId);
2023-12-20 01:34:19 +00:00
String blockName = GameRegistry.findUniqueIdentifierFor(block)
.toString();
2023-10-31 00:27:30 +00:00
BlockConversionInfo blockConversionInfo = new BlockConversionInfo();
2023-12-20 01:11:18 +00:00
blockConversionInfo.blockName = blockName;
2023-10-31 00:27:30 +00:00
blockConversionInfo.blockID = blockId;
2023-12-20 01:11:18 +00:00
blockConversionInfo.metadata = (byte) metadata; // Updated
2023-10-31 00:27:30 +00:00
blockConversionInfo.world = world;
int x = index % 16;
2023-12-20 01:11:18 +00:00
int y = (index / 256) + (sectionY * 16);
2023-10-31 00:27:30 +00:00
int z = (index / 16) % 16;
2023-11-01 23:54:59 +00:00
blockConversionInfo.x = x + chunkXPos + 1;
2023-12-20 01:11:18 +00:00
blockConversionInfo.y = y;
2023-11-01 23:54:59 +00:00
blockConversionInfo.z = z + chunkZPos + 1;
2023-10-31 00:27:30 +00:00
2023-12-20 02:00:48 +00:00
BlockConversionInfo output = BlockReplacementManager.getBlockReplacement(blockConversionInfo, world);
2023-10-30 01:28:47 +00:00
if (output != null) {
2023-10-31 00:27:30 +00:00
int newBlockId = output.blockID;
int newMetadata = output.metadata;
2023-10-30 20:02:08 +00:00
blockArray[index * 2] = (byte) ((newBlockId >> 8) & 0xFF);
blockArray[index * 2 + 1] = (byte) (newBlockId & 0xFF);
2023-12-20 01:11:18 +00:00
metadataArray[index * 2] = (byte) ((newMetadata >> 8) & 0xFF);
metadataArray[index * 2 + 1] = (byte) (newMetadata & 0xFF);
2023-10-30 01:28:47 +00:00
}
}
}
2023-10-29 01:06:18 +01:00
}
private static void transformTileEntities(NBTTagCompound levelCompoundTag, World world) {
2023-10-29 23:53:12 +00:00
2024-01-12 03:40:06 +00:00
Pair<List<ConversionInfo>, NBTTagList> output = adjustTileEntities(
levelCompoundTag.getTagList("TileEntities", 10),
world);
2023-10-29 23:53:12 +00:00
List<ConversionInfo> conversionInfoList = output.first();
2023-12-24 01:30:13 +00:00
NBTTagList tileEntities = output.second();
2023-12-24 01:38:45 +00:00
if (tileEntities.tagCount() > 0) {
levelCompoundTag.setTag("TileEntities", tileEntities);
2023-12-24 01:38:45 +00:00
}
2023-10-29 01:37:24 +00:00
int chunkXPos = levelCompoundTag.getInteger("xPos") * 16; // Assuming each chunk is 16 blocks along x-axis
int chunkZPos = levelCompoundTag.getInteger("zPos") * 16; // Assuming each chunk is 16 blocks along z-axis
2023-10-29 18:40:08 +00:00
NBTTagList sections = levelCompoundTag.getTagList("Sections", 10);
2023-10-29 18:40:08 +00:00
for (int i = 0; i < sections.tagCount(); i++) {
NBTTagCompound section = sections.getCompoundTagAt(i);
2023-10-29 20:20:12 +00:00
processSection(section, conversionInfoList, chunkXPos, chunkZPos);
}
}
2023-10-29 18:40:08 +00:00
2023-10-31 17:14:09 +00:00
private static void processSection(NBTTagCompound section, List<ConversionInfo> conversionInfoList, int chunkXPos,
2023-12-20 01:34:19 +00:00
int chunkZPos) {
2023-10-29 20:20:12 +00:00
byte[] blockArray = section.getByteArray("Blocks16");
2023-12-20 01:32:20 +00:00
byte[] metadataArray = section.getByteArray("Data16"); // Updated to use Data16
2023-10-29 20:20:12 +00:00
byte y = section.getByte("Y");
List<ConversionInfo> filteredList = filterConversionInfosByY(conversionInfoList, y);
for (ConversionInfo info : filteredList) {
int[] localCoords = convertToChunkLocalCoordinates(info, y, chunkXPos, chunkZPos);
int index = computeBlockIndex(localCoords);
2023-10-29 20:40:26 +00:00
setBlockInfo(info.blockInfo.block, index, blockArray);
2023-12-20 01:32:20 +00:00
setMetadata(info.blockInfo.metadata, index, metadataArray); // This uses the updated setMetadata
2023-10-29 20:20:12 +00:00
}
}
private static List<ConversionInfo> filterConversionInfosByY(List<ConversionInfo> conversionInfoList, byte y) {
return conversionInfoList.stream()
.filter(info -> info.y >= y * 16 && info.y < (y + 1) * 16)
.collect(Collectors.toList());
}
private static int[] convertToChunkLocalCoordinates(ConversionInfo info, byte y, int chunkXPos, int chunkZPos) {
int tileX = info.x - chunkXPos;
int tileY = info.y - y * 16;
int tileZ = info.z - chunkZPos;
2023-10-31 17:14:09 +00:00
return new int[] { tileX, tileY, tileZ };
2023-10-29 20:20:12 +00:00
}
private static int computeBlockIndex(int[] localCoords) {
return localCoords[1] * 256 + localCoords[2] * 16 + localCoords[0];
}
2023-10-29 20:40:26 +00:00
private static void setBlockInfo(Block block, int index, byte[] blockArray) {
int blockID = Block.getIdFromBlock(block);
2023-10-29 20:20:12 +00:00
blockArray[index * 2] = (byte) ((blockID >> 8) & 0xFF);
blockArray[index * 2 + 1] = (byte) (blockID & 0xFF);
}
2023-10-29 20:40:26 +00:00
private static void setMetadata(int metadata, int index, byte[] metadataArray) {
2023-12-20 01:32:20 +00:00
// Since each metadata is now two bytes, multiply the index by 2 to get the correct position.
int metadataStartIndex = index * 2;
2023-10-29 18:40:08 +00:00
2023-12-20 01:32:20 +00:00
// Split the 16-bit metadata into two bytes and store them in the array.
metadataArray[metadataStartIndex] = (byte) ((metadata >> 8) & 0xFF); // Higher 8 bits
metadataArray[metadataStartIndex + 1] = (byte) (metadata & 0xFF); // Lower 8 bits
2023-10-29 18:40:08 +00:00
}
2023-10-30 20:02:08 +00:00
private static Pair<List<ConversionInfo>, NBTTagList> adjustTileEntities(NBTTagList tileEntities, World world) {
2023-10-29 18:40:08 +00:00
List<ConversionInfo> conversionInfo = new ArrayList<>();
2023-10-29 23:53:12 +00:00
NBTTagList tileEntitiesCopy = new NBTTagList();
2023-10-29 18:40:08 +00:00
2023-10-29 01:37:24 +00:00
for (int i = 0; i < tileEntities.tagCount(); i++) {
NBTTagCompound tileEntity = tileEntities.getCompoundTagAt(i);
String tileEntityId = tileEntity.getString("id");
2023-10-29 21:18:26 +00:00
// Check if we have a transformer registered for this tile entity ID
2023-10-31 17:14:09 +00:00
BiFunction<NBTTagCompound, World, BlockInfo> transformationFunction = TileEntityReplacementManager
.getTileEntityToNormalBlockTransformerFunction(tileEntityId);
2023-10-29 21:18:26 +00:00
if (transformationFunction != null) {
2023-10-29 01:37:24 +00:00
int x = tileEntity.getInteger("x");
int y = tileEntity.getInteger("y");
int z = tileEntity.getInteger("z");
2023-10-30 20:02:08 +00:00
BlockInfo blockInfo = transformationFunction.apply(tileEntity, world);
2023-10-29 01:37:24 +00:00
2023-10-29 23:53:12 +00:00
if (blockInfo.tileTransformer != null) {
tileEntitiesCopy.appendTag(blockInfo.tileTransformer.apply(tileEntity));
} // Otherwise they are removed, therefore not appended.
2023-10-29 18:40:08 +00:00
conversionInfo.add(new ConversionInfo(x, y, z, blockInfo));
2024-01-05 15:40:11 +00:00
} else {
tileEntitiesCopy.appendTag(tileEntity);
2023-10-29 01:37:24 +00:00
}
}
2023-10-29 18:40:08 +00:00
2023-10-29 23:53:12 +00:00
return new Pair<>(conversionInfo, tileEntitiesCopy);
2023-10-29 01:37:24 +00:00
}
2024-01-11 16:32:13 +00:00
private static class ConversionInfo {
2023-10-29 01:37:24 +00:00
public final int x;
public final int y;
public final int z;
public final BlockInfo blockInfo;
2023-10-29 01:37:24 +00:00
public ConversionInfo(int x, int y, int z, BlockInfo blockInfo) {
this.x = x;
this.y = y;
this.z = z;
this.blockInfo = blockInfo;
}
}
}