Provide chunk to tile entity transformers (#17)

This commit is contained in:
SuperSoupr authored and GitHub committed 2025-12-06 19:20:09 +02:00
1 parent 85cacc679c
commit 2641e10174
4 files changed
+39 -8

No files matched your search

@@ -5,19 +5,40 @@ import java.util.function.BiFunction;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import com.gtnewhorizons.postea.utility.BlockInfo;
@SuppressWarnings("unused")
public class TileEntityReplacementManager {
private static final HashMap<String, BiFunction<NBTTagCompound, World, BlockInfo>> tileEntityToNormalBlockTransformer = new HashMap<>();
private static final HashMap<String, TriFunction<NBTTagCompound, World, Chunk, BlockInfo>> tileEntityToNormalBlockTransformer = new HashMap<>();
/**
* @deprecated Superseded by {@link #tileEntityTransformer(String, TriFunction)}.
*/
@Deprecated
public static void tileEntityTransformer(String tileEntityId,
BiFunction<NBTTagCompound, World, BlockInfo> transformerFunction) {
tileEntityToNormalBlockTransformer
.put(tileEntityId, (tag, world, chunk) -> transformerFunction.apply(tag, world));
}
/**
* Register a callback function to transform a tile entity before it's loaded.
* Callback returns a {@link BlockInfo} which the tile entity will be transformed into.</br>
* Note that the callback will be called before the world is fully loaded, and world.getBlock will throw an error.
* Use chunk.getBlock(x & 15, y, z & 15) instead.
*
* @param tileEntityId ID of the tile entity to be transformed
* @param transformerFunction Callback function to transforms the tile entity
*/
public static void tileEntityTransformer(String tileEntityId,
TriFunction<NBTTagCompound, World, Chunk, BlockInfo> transformerFunction) {
tileEntityToNormalBlockTransformer.put(tileEntityId, transformerFunction);
}
public static BiFunction<NBTTagCompound, World, BlockInfo> getTileEntityToNormalBlockTransformerFunction(
public static TriFunction<NBTTagCompound, World, Chunk, BlockInfo> getTileEntityToNormalBlockTransformerFunction(
String tileEntityId) {
return tileEntityToNormalBlockTransformer.getOrDefault(tileEntityId, null);
}
@@ -0,0 +1,7 @@
package com.gtnewhorizons.postea.api;
@FunctionalInterface
public interface TriFunction<T, U, O, R> {
R apply(T t, U u, O o);
}
@@ -6,7 +6,6 @@ import static com.gtnewhorizons.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;
import net.minecraft.block.Block;
@@ -18,6 +17,7 @@ import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import com.gtnewhorizons.postea.api.BlockReplacementManager;
import com.gtnewhorizons.postea.api.TileEntityReplacementManager;
import com.gtnewhorizons.postea.api.TriFunction;
import com.gtnewhorizons.postea.compat.Compat;
import com.gtnewhorizons.postea.compat.SubChunkAccess;
@@ -86,7 +86,8 @@ public class ChunkFixerUtility {
Pair<List<ConversionInfo>, NBTTagList> output = adjustTileEntities(
levelCompoundTag.getTagList("TileEntities", 10),
world);
world,
chunk);
List<ConversionInfo> conversionInfoList = output.first();
NBTTagList tileEntities = output.second();
@@ -116,7 +117,8 @@ public class ChunkFixerUtility {
}
}
private static Pair<List<ConversionInfo>, NBTTagList> adjustTileEntities(NBTTagList tileEntities, World world) {
private static Pair<List<ConversionInfo>, NBTTagList> adjustTileEntities(NBTTagList tileEntities, World world,
Chunk chunk) {
List<ConversionInfo> conversionInfo = new ArrayList<>();
NBTTagList tileEntitiesCopy = new NBTTagList();
@@ -126,7 +128,7 @@ public class ChunkFixerUtility {
String tileEntityId = tileEntity.getString("id");
// Check if we have a transformer registered for this tile entity ID
BiFunction<NBTTagCompound, World, BlockInfo> transformationFunction = TileEntityReplacementManager
TriFunction<NBTTagCompound, World, Chunk, BlockInfo> transformationFunction = TileEntityReplacementManager
.getTileEntityToNormalBlockTransformerFunction(tileEntityId);
if (transformationFunction != null) {
@@ -134,7 +136,7 @@ public class ChunkFixerUtility {
int y = tileEntity.getInteger("y");
int z = tileEntity.getInteger("z");
BlockInfo blockInfo = transformationFunction.apply(tileEntity, world);
BlockInfo blockInfo = transformationFunction.apply(tileEntity, world, chunk);
if (blockInfo == null) {
// Do nothing.
tileEntitiesCopy.appendTag(tileEntity);