From ca78bd95066de7c7f3c0beb94253ddd5e2e6b090 Mon Sep 17 00:00:00 2001 From: GTNH-Colen <54497873+GTNH-Colen@users.noreply.github.com> Date: Sun, 29 Oct 2023 18:40:08 +0000 Subject: [PATCH] Seems to kinda work --- .../colen/postea/API/ChunkFixerUtility.java | 137 ++++++++++++++++-- 1 file changed, 127 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/colen/postea/API/ChunkFixerUtility.java b/src/main/java/com/colen/postea/API/ChunkFixerUtility.java index ed3052a..fd77ff5 100644 --- a/src/main/java/com/colen/postea/API/ChunkFixerUtility.java +++ b/src/main/java/com/colen/postea/API/ChunkFixerUtility.java @@ -1,6 +1,7 @@ package com.colen.postea.API; import net.minecraft.block.Block; +import net.minecraft.nbt.NBTTagByteArray; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.chunk.Chunk; @@ -9,6 +10,7 @@ import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import java.util.ArrayList; import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; public class ChunkFixerUtility { public static Chunk fixChunk(Chunk chunk) { @@ -42,26 +44,113 @@ public class ChunkFixerUtility { public static void processCode(NBTTagCompound compound) { System.out.println(compound.toString()); - fixTileEntities(compound); - replaceTileEntitiesWithDumbBlock(compound, "GT_TileEntity_Ores", (byte) 7, (byte) 0); + //fixTileEntities(compound); + replaceTileEntitiesWithNormalBlock(compound); + //setAllBlocksInNBTToID7(compound); + } + + public static void setAllBlocksInNBTToID7(NBTTagCompound compound) { + + int blockID = 469; // The block ID to replace with + + // Ensure that the "Level" tag compound exists + if (compound.hasKey("Level", 10)) { + NBTTagCompound levelTag = compound.getCompoundTag("Level"); + + // Ensure that the "Sections" tag list exists + if (levelTag.hasKey("Sections", 9)) { + NBTTagList sectionsList = levelTag.getTagList("Sections", 10); // 10 is the NBT compound ID + + for (int i = 0; i < sectionsList.tagCount(); i++) { + NBTTagCompound section = sectionsList.getCompoundTagAt(i); + + if (section.hasKey("Blocks16", 7)) { + byte[] blockArray = section.getByteArray("Blocks16"); + + // Extract high and low bytes from the blockID + byte highByte = (byte) ((blockID >> 8) & 0xFF); // Get the most significant 8 bits + byte lowByte = (byte) (blockID & 0xFF); // Get the least significant 8 bits + + // Set all entries to the specified block ID + for (int j = 0; j < blockArray.length; j += 2) { + blockArray[j] = highByte; + blockArray[j + 1] = lowByte; + } + + // Replace the old array with the new one + section.setTag("Blocks16", new NBTTagByteArray(blockArray)); + } + + } + } + } } /** * Replaces specified tile entities with a block in the chunk NBT data. * * @param chunkNBT The chunk's NBT data. - * @param targetTileEntityId The ID of the tile entity to be replaced. - * @param newBlockId The ID of the block to replace the tile entity with. - * @param newBlockMetadata The metadata of the new block. */ - public static void replaceTileEntitiesWithDumbBlock(NBTTagCompound chunkNBT, String targetTileEntityId, byte newBlockId, byte newBlockMetadata) { + public static void replaceTileEntitiesWithNormalBlock(NBTTagCompound chunkNBT) { - List conversionInfoList = new ArrayList<>(); + final NBTTagCompound level = chunkNBT.getCompoundTag("Level"); - NBTTagCompound level = chunkNBT.getCompoundTag("Level"); - NBTTagList tileEntities = level.getTagList("TileEntities", 10); + final List conversionInfoList = removeTileEntities(level.getTagList("TileEntities", 10)); NBTTagList sections = level.getTagList("Sections", 10); + List blockDataList = new ArrayList<>(); + + 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 + + for (int i = 0; i < sections.tagCount(); i++) { + NBTTagCompound section = sections.getCompoundTagAt(i); + byte[] blockArray = section.getByteArray("Blocks16"); + byte[] metadataArray = section.getByteArray("Data"); + byte y = section.getByte("Y"); + + List filteredList = conversionInfoList.stream() + .filter(info -> info.y >= y * 16 && info.y < (y + 1) * 16) + .collect(Collectors.toList()); + + for (ConversionInfo info : filteredList) { + int tileX = info.x - chunkXPos; // Convert global x-coordinate to local x-coordinate within the chunk + int tileY = info.y - y * 16; // Convert global y-coordinate to local y-coordinate within the section + int tileZ = info.z - chunkZPos; // Convert global z-coordinate to local z-coordinate within the chunk + + int index = tileY * 256 + tileZ * 16 + tileX; + + // Set block ID from ConversionInfo + int blockID = Block.getIdFromBlock(info.blockInfo.block); + blockArray[index * 2] = (byte) ((blockID >> 8) & 0xFF); + blockArray[index * 2 + 1] = (byte) (blockID & 0xFF); + + // Set metadata from ConversionInfo + int metadataIndex = index / 2; + byte currentMetadataByte = metadataArray[metadataIndex]; + + if ((index & 1) == 0) { // Even + currentMetadataByte &= 0xF0; + currentMetadataByte |= (info.blockInfo.metadata & 0x0F); + } else { // Odd + currentMetadataByte &= 0x0F; + currentMetadataByte |= ((info.blockInfo.metadata << 4) & 0xF0); + } + + metadataArray[metadataIndex] = currentMetadataByte; + } + + } + + } + + + + private static List removeTileEntities(NBTTagList tileEntities) { + + List conversionInfo = new ArrayList<>(); + String targetTileEntityId = "GT_TileEntity_Ores"; + // Loop through each tile entity to check for matches for (int i = 0; i < tileEntities.tagCount(); i++) { NBTTagCompound tileEntity = tileEntities.getCompoundTagAt(i); @@ -77,11 +166,13 @@ public class ChunkFixerUtility { BlockInfo blockInfo = transformationFunction.apply(tileEntity); - conversionInfoList.add(new ConversionInfo(x, y, z, blockInfo)); + conversionInfo.add(new ConversionInfo(x, y, z, blockInfo)); tileEntities.removeTag(i); } } + + return conversionInfo; } private static void fixTileEntities(NBTTagCompound compound) { @@ -121,4 +212,30 @@ public class ChunkFixerUtility { this.metadata = metadata; } } + + public static class ChunkBlockData { + private int blockID; // Block ID (2 bytes or 16 bits) + private byte metadata; // Metadata (4 bits) + + public ChunkBlockData(int blockID, byte metadata) { + this.blockID = blockID; + this.metadata = metadata; + } + + public int getBlockID() { + return blockID; + } + + public void setBlockID(int blockID) { + this.blockID = blockID; + } + + public byte getMetadata() { + return metadata; + } + + public void setMetadata(byte metadata) { + this.metadata = metadata; + } + } }