Some refactoring

This commit is contained in:
Connor Colenso committed 2023-10-31 00:27:30 +00:00
1 parent 40b70bfec3
commit 980f2c3ec2
7 files changed
+134 -64

No files matched your search

@@ -1,36 +1,29 @@
package com.colen.postea.API;
import akka.japi.Pair;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import com.colen.postea.Utility.BlockConversionInfo;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static com.colen.postea.API.ItemStackReplacementManager.addItemReplacement;
public class BlockReplacementManager {
private static final Map<BlockKey, Function<BlockKey, Pair<Integer, Integer>>> blockReplacementMap = new HashMap<>();
private static final Map<String, Function<BlockConversionInfo, BlockConversionInfo>> blockReplacementMap = new HashMap<>();
@SuppressWarnings("unused")
public static void addBlockReplacement(String blockNameIn, int metadataIn, Function<BlockKey, Pair<Integer, Integer>> transformer) {
blockReplacementMap.put(new BlockKey(blockNameIn, metadataIn), transformer);
public static void addBlockReplacement(String blockNameIn, Function<BlockConversionInfo, BlockConversionInfo> transformer) {
blockReplacementMap.put(blockNameIn, transformer);
}
@SuppressWarnings("unused")
public static Pair<Integer, Integer> getBlockReplacement(int blockID, int metadataIn) {
Block block = Block.getBlockById(blockID);
String blockName = GameRegistry.findUniqueIdentifierFor(block).toString();
public static BlockConversionInfo getBlockReplacement(BlockConversionInfo blockConversionInfo) {
Function<BlockKey, Pair<Integer, Integer>> transformer = blockReplacementMap.getOrDefault(new BlockKey(blockName, metadataIn), null);
Function<BlockConversionInfo, BlockConversionInfo> transformer = blockReplacementMap.getOrDefault(blockConversionInfo.blocKName, null);
if (transformer == null) {
return null;
} else {
return transformer.apply(new BlockKey(blockName, metadataIn));
return transformer.apply(blockConversionInfo);
}
}
@@ -1,6 +1,9 @@
package com.colen.postea.API;
import akka.japi.Pair;
import com.colen.postea.Utility.BlockConversionInfo;
import com.colen.postea.Utility.BlockInfo;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
@@ -9,7 +12,6 @@ import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ChunkFixerUtility {
@@ -22,19 +24,41 @@ public class ChunkFixerUtility {
private static void transformNormalBlocks(NBTTagCompound compound, World world) {
NBTTagCompound level = compound.getCompoundTag("Level");
NBTTagList sections = level.getTagList("Sections", 10);
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 sectionY = section.getByte("Y");
for (int index = 0; index < blockArray.length / 2; index++) {
int blockId = ((blockArray[index * 2] & 0xFF) << 8) | (blockArray[index * 2 + 1] & 0xFF);
byte metadata = (byte) ((index & 1) == 0 ? (metadataArray[index / 2] & 0x0F) : ((metadataArray[index / 2] & 0xF0) >> 4));
Pair<Integer, Integer> output = BlockReplacementManager.getBlockReplacement(blockId, metadata);
Block block = Block.getBlockById(blockId);
String blockName = GameRegistry.findUniqueIdentifierFor(block).toString();
BlockConversionInfo blockConversionInfo = new BlockConversionInfo();
blockConversionInfo.blocKName = blockName;
blockConversionInfo.blockID = blockId;
blockConversionInfo.metadata = metadata;
blockConversionInfo.world = world;
int x = index % 16;
int y = (index / 256) + (sectionY * 16); // Add the offset of the current section in Y direction
int z = (index / 16) % 16;
blockConversionInfo.x = x + chunkXPos;
blockConversionInfo.y = y; // Y remains unchanged as it's already global
blockConversionInfo.z = z + chunkZPos;
BlockConversionInfo output = BlockReplacementManager.getBlockReplacement(blockConversionInfo);
if (output != null) {
int newBlockId = output.first();
int newMetadata = output.second();
int newBlockId = output.blockID;
int newMetadata = output.metadata;
blockArray[index * 2] = (byte) ((newBlockId >> 8) & 0xFF);
blockArray[index * 2 + 1] = (byte) (newBlockId & 0xFF);
@@ -177,22 +201,5 @@ 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;
}
}
}
@@ -1,22 +1,21 @@
package com.colen.postea.API;
import com.colen.postea.Utility.BlockInfo;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
public class TileEntityReplacementManager {
private static final HashMap<String, BiFunction<NBTTagCompound, World, ChunkFixerUtility.BlockInfo>> tileEntityToNormalBlockTransformer = new HashMap<>();
private static final HashMap<String, BiFunction<NBTTagCompound, World, BlockInfo>> tileEntityToNormalBlockTransformer = new HashMap<>();
public static void tileEntityTransformer(String tileEntityId, BiFunction<NBTTagCompound, World, ChunkFixerUtility.BlockInfo> transformerFunction) {
public static void tileEntityTransformer(String tileEntityId, BiFunction<NBTTagCompound, World, BlockInfo> transformerFunction) {
tileEntityToNormalBlockTransformer.put(tileEntityId, transformerFunction);
}
public static BiFunction<NBTTagCompound, World, ChunkFixerUtility.BlockInfo> getTileEntityToNormalBlockTransformerFunction(String tileEntityId) {
public static BiFunction<NBTTagCompound, World, BlockInfo> getTileEntityToNormalBlockTransformerFunction(String tileEntityId) {
return tileEntityToNormalBlockTransformer.getOrDefault(tileEntityId, null);
}
}
+34 -24
View File
@@ -1,19 +1,21 @@
package com.colen.postea;
import akka.japi.Pair;
import com.colen.postea.API.BlockKey;
import com.colen.postea.API.BlockReplacementManager;
import com.colen.postea.API.ChunkFixerUtility;
import com.colen.postea.API.TileEntityReplacementManager;
import com.colen.postea.Utility.BlockConversionInfo;
import com.colen.postea.Utility.BlockInfo;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.util.function.Function;
import static com.colen.postea.Utility.PosteaUtilities.cleanseNBT;
@Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]")
@SuppressWarnings("unused")
public class Postea {
@@ -37,19 +39,17 @@ public class Postea {
//TileEntityReplacementManager.tileEntityFixer("GT_TileEntity_Ores", myTransformer);
TileEntityReplacementManager.tileEntityTransformer("GT_TileEntity_Ores", (tag, world) -> {
if (world.provider.dimensionId == -1) {
return new ChunkFixerUtility.BlockInfo(Blocks.wool, 1);
return new BlockInfo(Blocks.wool, 1);
}
if (world.provider.dimensionId == 1) {
return new ChunkFixerUtility.BlockInfo(Blocks.wool, 2);
return new BlockInfo(Blocks.wool, 2);
}
return new ChunkFixerUtility.BlockInfo(Blocks.wool, 3);
return new BlockInfo(Blocks.wool, 3);
});
/* TileEntityReplacementManager.tileEntityTransformer("Furnace", (tag) -> {
Function<NBTTagCompound, NBTTagCompound> chestTransformer = (oldTag) -> {
@@ -102,29 +102,39 @@ public class Postea {
return newTag;
};
return new ChunkFixerUtility.BlockInfo(Blocks.chest, 0, chestTransformer);
return new BlockInfo(Blocks.chest, 0, chestTransformer);
});
Function<BlockKey, Pair<Integer, Integer>> blockTransformer = (blockKey) -> {
return new Pair<>(7, 0);
Function<BlockConversionInfo, BlockConversionInfo> blockTransformer = (blockConversionInfoOld) -> {
BlockConversionInfo blockConversionInfoNew = new BlockConversionInfo();
if ((blockConversionInfoOld.x % 2 == 1) && (blockConversionInfoOld.y % 2 == 0)) {
blockConversionInfoNew.blockID = Block.getIdFromBlock(Blocks.wool);
blockConversionInfoNew.metadata = 1;
}
if ((blockConversionInfoOld.x % 2 == 1) && (blockConversionInfoOld.y % 2 == 1)) {
blockConversionInfoNew.blockID = Block.getIdFromBlock(Blocks.wool);
blockConversionInfoNew.metadata = 2;
}
if ((blockConversionInfoOld.x % 2 == 0) && (blockConversionInfoOld.y % 2 == 0)) {
blockConversionInfoNew.blockID = Block.getIdFromBlock(Blocks.wool);
blockConversionInfoNew.metadata = 3;
}
if ((blockConversionInfoOld.x % 2 == 0) && (blockConversionInfoOld.y % 2 == 1)) {
blockConversionInfoNew.blockID = Block.getIdFromBlock(Blocks.wool);
blockConversionInfoNew.metadata = 4;
}
return blockConversionInfoNew;
};
BlockReplacementManager.addBlockReplacement("minecraft:grass", 0, blockTransformer);
BlockReplacementManager.addBlockReplacement("minecraft:grass", blockTransformer);
}
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
public void postInit(FMLPostInitializationEvent event) {
//BlockReplacementManager.addBlockReplacement(GregTech_API.sBlockOres1, 0, Blocks.chest, 0);
@@ -0,0 +1,18 @@
package com.colen.postea.Utility;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
public final class BlockConversionInfo {
public int x;
public int y;
public int z;
public int blockID;
public int metadata;
public World world;
public NBTTagCompound tileEntityNBT;
public String blocKName;
}
@@ -0,0 +1,24 @@
package com.colen.postea.Utility;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import java.util.function.Function;
public final 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;
}
}
@@ -0,0 +1,19 @@
package com.colen.postea.Utility;
import net.minecraft.nbt.NBTTagCompound;
public abstract class PosteaUtilities {
@SuppressWarnings("unused")
public static 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;
}
}