mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-21 04:32:19 +00:00
Optimise block conversion
This commit is contained in:
1 parent
b5be252e9e
commit
b2d44c9654
3 files changed
+86
-33
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.colen.postea.API;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@@ -10,7 +11,8 @@ import com.colen.postea.Utility.BlockConversionInfo;
|
||||
|
||||
public class BlockReplacementManager {
|
||||
|
||||
private static final Map<String, BiFunction<BlockConversionInfo, World, BlockConversionInfo>> blockReplacementMap = new HashMap<>();
|
||||
public static final Map<String, BiFunction<BlockConversionInfo, World, BlockConversionInfo>> blockReplacementMap = new HashMap<>();
|
||||
public static final HashSet<Integer> posteaMarkedIDs = new HashSet<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void addBlockReplacement(String blockNameIn,
|
||||
@@ -30,4 +32,9 @@ public class BlockReplacementManager {
|
||||
}
|
||||
}
|
||||
|
||||
// We need this to save reprocessing blocks.
|
||||
public static boolean blockNotConvertible(int blockID) {
|
||||
return !posteaMarkedIDs.contains(blockID);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,44 @@
|
||||
package com.colen.postea;
|
||||
|
||||
import com.colen.postea.API.BlockReplacementManager;
|
||||
import com.colen.postea.Utility.BlockConversionInfo;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.colen.postea.API.BlockReplacementManager.blockReplacementMap;
|
||||
import static com.colen.postea.API.BlockReplacementManager.posteaMarkedIDs;
|
||||
|
||||
@Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]")
|
||||
@SuppressWarnings("unused")
|
||||
public class Postea {
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {}
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
public void onServerStarting(FMLServerAboutToStartEvent event) {
|
||||
for (String name : blockReplacementMap.keySet()) {
|
||||
Block block = Block.getBlockFromName(name);
|
||||
if (block != null) {
|
||||
int id = Block.getIdFromBlock(block);
|
||||
posteaMarkedIDs.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
public void onServerStopping(FMLServerStoppingEvent event) {
|
||||
posteaMarkedIDs.clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.colen.postea.Utility;
|
||||
|
||||
import static com.colen.postea.API.BlockReplacementManager.blockNotConvertible;
|
||||
import static com.colen.postea.Utility.PosteaUtilities.getModListHash;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -43,6 +45,9 @@ public class ChunkFixerUtility {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final int AIR_ID = 0;
|
||||
private static final HashMap<Block, String> loadedBlocks = new HashMap<>();
|
||||
|
||||
private static void transformNormalBlocks(NBTTagCompound levelCompoundTag, World world) {
|
||||
NBTTagList sections = levelCompoundTag.getTagList("Sections", 10);
|
||||
|
||||
@@ -50,50 +55,60 @@ public class ChunkFixerUtility {
|
||||
int chunkZPos = levelCompoundTag.getInteger("zPos") * 16;
|
||||
|
||||
for (int i = 0; i < sections.tagCount(); i++) {
|
||||
NBTTagCompound section = sections.getCompoundTagAt(i);
|
||||
NBTTagCompound section = sections.getCompoundTagAt(i);
|
||||
|
||||
// Blocks16 and Data16 exist only because of NEID.
|
||||
byte[] blockArray = section.getByteArray("Blocks16");
|
||||
byte[] metadataArray = section.getByteArray("Data16");
|
||||
// Blocks16 and Data16 exist only because of NEID.
|
||||
byte[] blockArray = section.getByteArray("Blocks16");
|
||||
byte[] metadataArray = section.getByteArray("Data16");
|
||||
|
||||
byte sectionY = section.getByte("Y");
|
||||
byte sectionY = section.getByte("Y");
|
||||
System.out.println("Y: " + sectionY);
|
||||
System.out.println("i: " + i);
|
||||
System.out.println("sections.tagCount(): " + sections.tagCount());
|
||||
|
||||
for (int index = 0; index < blockArray.length / 2; index++) {
|
||||
int blockId = ((blockArray[index * 2] & 0xFF) << 8) | (blockArray[index * 2 + 1] & 0xFF);
|
||||
int metadata = ((metadataArray[index * 2] & 0xFF) << 8) | (metadataArray[index * 2 + 1] & 0xFF);
|
||||
for (int index = 0; index < blockArray.length / 2; index++) {
|
||||
// Horrible bit jank to recreate the actual IDs, because the array is just a byte array.
|
||||
int blockId = ((blockArray[index * 2] & 0xFF) << 8) | (blockArray[index * 2 + 1] & 0xFF);
|
||||
int metadata = ((metadataArray[index * 2] & 0xFF) << 8) | (metadataArray[index * 2 + 1] & 0xFF);
|
||||
|
||||
Block block = Block.getBlockById(blockId);
|
||||
String blockName = GameRegistry.findUniqueIdentifierFor(block)
|
||||
.toString();
|
||||
// Skip air.
|
||||
if (blockId == AIR_ID) continue;
|
||||
// If this block has no registered Postea conversion, skip it.
|
||||
if (blockNotConvertible(blockId)) continue;
|
||||
|
||||
BlockConversionInfo blockConversionInfo = new BlockConversionInfo();
|
||||
blockConversionInfo.blockName = blockName;
|
||||
blockConversionInfo.blockID = blockId;
|
||||
blockConversionInfo.metadata = (byte) metadata; // Updated
|
||||
blockConversionInfo.world = world;
|
||||
// Cache block names to improve performance, as findUniqueIdentifierFor is expensive.
|
||||
Block block = Block.getBlockById(blockId);
|
||||
String blockName = loadedBlocks.computeIfAbsent(block, b -> GameRegistry.findUniqueIdentifierFor(b).toString());
|
||||
|
||||
int x = index % 16;
|
||||
int y = (index / 256) + (sectionY * 16);
|
||||
int z = (index / 16) % 16;
|
||||
BlockConversionInfo blockConversionInfo = new BlockConversionInfo();
|
||||
blockConversionInfo.blockName = blockName;
|
||||
blockConversionInfo.blockID = blockId;
|
||||
blockConversionInfo.metadata = (byte) metadata; // Updated
|
||||
blockConversionInfo.world = world;
|
||||
|
||||
blockConversionInfo.x = x + chunkXPos + 1;
|
||||
blockConversionInfo.y = y;
|
||||
blockConversionInfo.z = z + chunkZPos + 1;
|
||||
int x = index % 16;
|
||||
int y = (index / 256) + (sectionY * 16);
|
||||
int z = (index / 16) % 16;
|
||||
|
||||
BlockConversionInfo output = BlockReplacementManager.getBlockReplacement(blockConversionInfo, world);
|
||||
blockConversionInfo.x = x + chunkXPos + 1;
|
||||
blockConversionInfo.y = y;
|
||||
blockConversionInfo.z = z + chunkZPos + 1;
|
||||
|
||||
if (output != null) {
|
||||
int newBlockId = output.blockID;
|
||||
int newMetadata = output.metadata;
|
||||
BlockConversionInfo output = BlockReplacementManager.getBlockReplacement(blockConversionInfo, world);
|
||||
|
||||
blockArray[index * 2] = (byte) ((newBlockId >> 8) & 0xFF);
|
||||
blockArray[index * 2 + 1] = (byte) (newBlockId & 0xFF);
|
||||
if (output != null) {
|
||||
int newBlockId = output.blockID;
|
||||
int newMetadata = output.metadata;
|
||||
|
||||
metadataArray[index * 2] = (byte) ((newMetadata >> 8) & 0xFF);
|
||||
metadataArray[index * 2 + 1] = (byte) (newMetadata & 0xFF);
|
||||
blockArray[index * 2] = (byte) ((newBlockId >> 8) & 0xFF);
|
||||
blockArray[index * 2 + 1] = (byte) (newBlockId & 0xFF);
|
||||
|
||||
metadataArray[index * 2] = (byte) ((newMetadata >> 8) & 0xFF);
|
||||
metadataArray[index * 2 + 1] = (byte) (newMetadata & 0xFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void transformTileEntities(NBTTagCompound levelCompoundTag, World world) {
|
||||
|
||||
Reference in new issue
Block a user