mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-24 05:54:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b885b8eda | ||
|
|
318af9da8c | ||
|
|
152b8d4dc5 | ||
|
|
b2d44c9654 | ||
|
|
b5be252e9e | ||
|
|
0bb9b56e6d | ||
|
|
ad46bc37da | ||
|
|
e15b0f35ff | ||
|
|
a67d392b49 |
No files matched your search
@@ -29,7 +29,7 @@ TileEntityReplacementManager.tileEntityTransformer("GT_TileEntity_Ores", (tag, w
|
||||
#### b. We do the same here, but now we convert a Furnace TE to a chest TE. We also need to convert the underlying block to a chest. In addtion to this we can provide a transformer that can edit the NBTTagCompound of the TE. This is useful for converting NBT data from one format to another. Here we simply edit the NBT to place stone in the middle slot.
|
||||
|
||||
```java
|
||||
TileEntityReplacementManager.tileEntityTransformer("Furnace", (tag, world) -> {
|
||||
TileEntityReplacementManager.tileEntityTransformer("Chest", (tag, world) -> {
|
||||
return new BlockInfo(Blocks.chest, 0, Postea::chestTransformer);
|
||||
});
|
||||
|
||||
@@ -58,7 +58,7 @@ private static NBTTagCompound chestTransformer(NBTTagCompound oldTag) {
|
||||
#### a. The following example will replace all grass blocks with even x-coordinates with orange wool blocks.
|
||||
|
||||
```java
|
||||
Function<BlockConversionInfo, BlockConversionInfo> blockTransformer = (blockConversionInfoOld) -> {
|
||||
BiFunction<BlockConversionInfo, World, BlockConversionInfo> blockTransformer = (blockConversionInfoOld, world) -> {
|
||||
|
||||
BlockConversionInfo blockConversionInfoNew = new BlockConversionInfo();
|
||||
|
||||
|
||||
@@ -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,19 +1,36 @@
|
||||
package com.colen.postea;
|
||||
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import static com.colen.postea.API.BlockReplacementManager.blockReplacementMap;
|
||||
import static com.colen.postea.API.BlockReplacementManager.posteaMarkedIDs;
|
||||
|
||||
import com.colen.postea.checker.PosteaProcessedChunksWorldSavedData;
|
||||
import net.minecraft.block.Block;
|
||||
|
||||
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;
|
||||
|
||||
@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) {
|
||||
MinecraftForge.EVENT_BUS.register(new PosteaProcessedChunksWorldSavedData(""));
|
||||
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,6 +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;
|
||||
@@ -12,7 +16,6 @@ import net.minecraft.world.World;
|
||||
|
||||
import com.colen.postea.API.BlockReplacementManager;
|
||||
import com.colen.postea.API.TileEntityReplacementManager;
|
||||
import com.colen.postea.checker.PosteaProcessedChunksWorldSavedData;
|
||||
|
||||
import akka.japi.Pair;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
@@ -20,23 +23,36 @@ import cpw.mods.fml.common.registry.GameRegistry;
|
||||
public class ChunkFixerUtility {
|
||||
|
||||
public static void processChunkNBT(NBTTagCompound compound, World world) {
|
||||
NBTTagCompound level = compound.getCompoundTag("Level");
|
||||
int x = level.getInteger("xPos");
|
||||
int z = level.getInteger("zPos");
|
||||
NBTTagCompound levelCompoundTag = compound.getCompoundTag("Level");
|
||||
|
||||
if (PosteaProcessedChunksWorldSavedData.addChunk(x, z)) {
|
||||
transformTileEntities(compound, world);
|
||||
transformNormalBlocks(compound, world);
|
||||
if (processChunk(levelCompoundTag)) {
|
||||
transformTileEntities(levelCompoundTag, world);
|
||||
transformNormalBlocks(levelCompoundTag, world);
|
||||
levelCompoundTag.setInteger("POSTEA", POSTEA_UPDATE_CODE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void transformNormalBlocks(NBTTagCompound compound, World world) {
|
||||
NBTTagCompound level = compound.getCompoundTag("Level");
|
||||
NBTTagList sections = level.getTagList("Sections", 10);
|
||||
// This will not change between runs, unless a mod is updated or added.
|
||||
public static final int POSTEA_UPDATE_CODE = getModListHash();
|
||||
|
||||
int chunkXPos = level.getInteger("xPos") * 16;
|
||||
int chunkZPos = level.getInteger("zPos") * 16;
|
||||
// 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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int chunkXPos = levelCompoundTag.getInteger("xPos") * 16;
|
||||
int chunkZPos = levelCompoundTag.getInteger("zPos") * 16;
|
||||
|
||||
for (int i = 0; i < sections.tagCount(); i++) {
|
||||
NBTTagCompound section = sections.getCompoundTagAt(i);
|
||||
@@ -48,12 +64,21 @@ public class ChunkFixerUtility {
|
||||
byte sectionY = section.getByte("Y");
|
||||
|
||||
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);
|
||||
|
||||
// Skip air.
|
||||
if (blockId == AIR_ID) continue;
|
||||
// If this block has no registered Postea conversion, skip it.
|
||||
if (blockNotConvertible(blockId)) continue;
|
||||
|
||||
// Cache block names to improve performance, as findUniqueIdentifierFor is expensive.
|
||||
Block block = Block.getBlockById(blockId);
|
||||
String blockName = GameRegistry.findUniqueIdentifierFor(block)
|
||||
.toString();
|
||||
String blockName = loadedBlocks.computeIfAbsent(
|
||||
block,
|
||||
b -> GameRegistry.findUniqueIdentifierFor(b)
|
||||
.toString());
|
||||
|
||||
BlockConversionInfo blockConversionInfo = new BlockConversionInfo();
|
||||
blockConversionInfo.blockName = blockName;
|
||||
@@ -85,21 +110,22 @@ public class ChunkFixerUtility {
|
||||
}
|
||||
}
|
||||
|
||||
public static void transformTileEntities(NBTTagCompound chunkNBT, World world) {
|
||||
NBTTagCompound level = chunkNBT.getCompoundTag("Level");
|
||||
private static void transformTileEntities(NBTTagCompound levelCompoundTag, World world) {
|
||||
|
||||
Pair<List<ConversionInfo>, NBTTagList> output = adjustTileEntities(level.getTagList("TileEntities", 10), world);
|
||||
Pair<List<ConversionInfo>, NBTTagList> output = adjustTileEntities(
|
||||
levelCompoundTag.getTagList("TileEntities", 10),
|
||||
world);
|
||||
List<ConversionInfo> conversionInfoList = output.first();
|
||||
NBTTagList tileEntities = output.second();
|
||||
|
||||
if (tileEntities.tagCount() > 0) {
|
||||
level.setTag("TileEntities", tileEntities);
|
||||
levelCompoundTag.setTag("TileEntities", tileEntities);
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
NBTTagList sections = level.getTagList("Sections", 10);
|
||||
NBTTagList sections = levelCompoundTag.getTagList("Sections", 10);
|
||||
for (int i = 0; i < sections.tagCount(); i++) {
|
||||
NBTTagCompound section = sections.getCompoundTagAt(i);
|
||||
processSection(section, conversionInfoList, chunkXPos, chunkZPos);
|
||||
@@ -187,7 +213,7 @@ public class ChunkFixerUtility {
|
||||
return new Pair<>(conversionInfo, tileEntitiesCopy);
|
||||
}
|
||||
|
||||
public static class ConversionInfo {
|
||||
private static class ConversionInfo {
|
||||
|
||||
public final int x;
|
||||
public final int y;
|
||||
|
||||
@@ -2,9 +2,10 @@ package com.colen.postea.Utility;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
@@ -45,12 +46,23 @@ public abstract class PosteaUtilities {
|
||||
Collections.sort(modList);
|
||||
|
||||
// Step 2: Append all elements into one string
|
||||
StringBuilder combinedModStringSorted = new StringBuilder();
|
||||
StringBuilder combinedModStringSortedBuilder = new StringBuilder();
|
||||
for (String element : modList) {
|
||||
combinedModStringSorted.append(element);
|
||||
combinedModStringSortedBuilder.append(element);
|
||||
}
|
||||
|
||||
return Objects.hash(combinedModStringSorted);
|
||||
String combinedModStringSorted = combinedModStringSortedBuilder.toString();
|
||||
|
||||
return combinedModStringSorted.hashCode();
|
||||
}
|
||||
|
||||
public static void markChunkAsDirty(World world, int chunkX, int chunkZ) {
|
||||
if (!world.isRemote) { // Check to make sure we're on the server side.
|
||||
Chunk chunk = world.getChunkFromChunkCoords(chunkX, chunkZ);
|
||||
if (chunk == null) return;
|
||||
|
||||
chunk.setChunkModified();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
package com.colen.postea.checker;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashSet;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldSavedData;
|
||||
import net.minecraft.world.storage.MapStorage;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
|
||||
import akka.japi.Pair;
|
||||
import cpw.mods.fml.common.eventhandler.EventPriority;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
public class PosteaProcessedChunksWorldSavedData extends WorldSavedData {
|
||||
|
||||
private static final String DATA_NAME = "GregTech_GlobalIntValue";
|
||||
public static HashSet<Pair<Integer, Integer>> chunkCoordinates = new HashSet<>();
|
||||
public static PosteaProcessedChunksWorldSavedData INSTANCE;
|
||||
|
||||
// True if chunk has not been processed, else false.
|
||||
public static boolean addChunk(int x, int z) {
|
||||
boolean updateChunk = chunkCoordinates.add(new Pair<>(x, z));
|
||||
|
||||
if (updateChunk) {
|
||||
// Only mark the world NBT as dirty if we actually process a new chunk, since we will need to save that
|
||||
// info.
|
||||
INSTANCE.markDirty();
|
||||
}
|
||||
|
||||
return updateChunk;
|
||||
}
|
||||
|
||||
public PosteaProcessedChunksWorldSavedData() {
|
||||
super(DATA_NAME);
|
||||
}
|
||||
|
||||
public PosteaProcessedChunksWorldSavedData(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbtTagCompound) {
|
||||
try {
|
||||
byte[] ba = nbtTagCompound.getByteArray(DATA_NAME);
|
||||
InputStream byteArrayInputStream = new ByteArrayInputStream(ba);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
||||
Object data = objectInputStream.readObject();
|
||||
chunkCoordinates = (HashSet<Pair<Integer, Integer>>) data;
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
System.out.println(DATA_NAME + " FAILED");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbtTagCompound) {
|
||||
// Write the integer value to NBT
|
||||
try {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
|
||||
objectOutputStream.writeObject(chunkCoordinates);
|
||||
objectOutputStream.flush();
|
||||
byte[] data = byteArrayOutputStream.toByteArray();
|
||||
nbtTagCompound.setByteArray(DATA_NAME, data);
|
||||
} catch (IOException exception) {
|
||||
System.out.println(DATA_NAME + " SAVE FAILED");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void get(World world) {
|
||||
|
||||
chunkCoordinates.clear();
|
||||
|
||||
MapStorage storage = world.mapStorage;
|
||||
INSTANCE = (PosteaProcessedChunksWorldSavedData) storage
|
||||
.loadData(PosteaProcessedChunksWorldSavedData.class, DATA_NAME);
|
||||
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new PosteaProcessedChunksWorldSavedData();
|
||||
storage.setData(DATA_NAME, INSTANCE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Register event in a static block to ensure it's done as early as possible
|
||||
static {
|
||||
MinecraftForge.EVENT_BUS.register(new PosteaProcessedChunksWorldSavedData());
|
||||
}
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void onWorldLoad(WorldEvent.Load event) {
|
||||
if (!event.world.isRemote && event.world.provider.dimensionId == 0) {
|
||||
get(event.world);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading
|
||||
public List<String> getMixins(Set<String> loadedCoreMods) {
|
||||
final List<String> mixins = new ArrayList<>();
|
||||
mixins.add("MixinItemStack");
|
||||
mixins.add("MixinChunk");
|
||||
mixins.add("MixinAnvilChunkLoader");
|
||||
return mixins;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
package com.colen.postea.mixins.early;
|
||||
|
||||
import static com.colen.postea.Utility.ChunkFixerUtility.POSTEA_UPDATE_CODE;
|
||||
import static com.colen.postea.Utility.ChunkFixerUtility.processChunkNBT;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import com.colen.postea.mixins.interfaces.IChunkMixin;
|
||||
|
||||
@Mixin(AnvilChunkLoader.class)
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class MixinAnvilChunkLoader {
|
||||
|
||||
@Inject(method = "checkedReadChunkFromNBT__Async", at = @At("HEAD"), remap = false)
|
||||
@@ -21,4 +25,27 @@ public abstract class MixinAnvilChunkLoader {
|
||||
processChunkNBT(compound, world);
|
||||
}
|
||||
|
||||
@Inject(method = "readChunkFromNBT", at = @At("RETURN"))
|
||||
private Chunk onReadChunkFromNBT(World world, NBTTagCompound nbtTagCompound, CallbackInfoReturnable<Chunk> cir) {
|
||||
Chunk chunk = cir.getReturnValue();
|
||||
if (chunk instanceof IChunkMixin iChunkMixin) {
|
||||
if (nbtTagCompound.hasKey("POSTEA")) {
|
||||
long posteaCode = nbtTagCompound.getLong("POSTEA");
|
||||
iChunkMixin.Postea$setPosteaCode(posteaCode);
|
||||
|
||||
// Chunk has been updated with Postea, so we will mark it to save later.
|
||||
if (posteaCode != POSTEA_UPDATE_CODE) chunk.setChunkModified();
|
||||
}
|
||||
}
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
@Inject(method = "writeChunkToNBT", at = @At("HEAD"))
|
||||
private void onWriteChunkToNBT(Chunk chunk, World world, NBTTagCompound nbtTagCompound, CallbackInfo ci) {
|
||||
if (chunk instanceof IChunkMixin iChunkMixin) {
|
||||
nbtTagCompound.setLong("POSTEA", iChunkMixin.Postea$getPosteaCode());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.colen.postea.mixins.early;
|
||||
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
|
||||
import com.colen.postea.mixins.interfaces.IChunkMixin;
|
||||
|
||||
@Mixin(Chunk.class)
|
||||
public class MixinChunk implements IChunkMixin {
|
||||
|
||||
// This new field will store the hash of mod name + versions. We use this to check
|
||||
// if we need to reprocess this chunk.
|
||||
@Unique
|
||||
private long postea_GTNH$posteaID;
|
||||
|
||||
@Unique
|
||||
public long Postea$getPosteaCode() {
|
||||
return postea_GTNH$posteaID;
|
||||
}
|
||||
|
||||
@Unique
|
||||
public void Postea$setPosteaCode(long POSTEA_ID) {
|
||||
this.postea_GTNH$posteaID = POSTEA_ID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.colen.postea.mixins.interfaces;
|
||||
|
||||
public interface IChunkMixin {
|
||||
|
||||
long Postea$getPosteaCode();
|
||||
|
||||
void Postea$setPosteaCode(long posteaID);
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
"compatibilityLevel": "JAVA_8",
|
||||
"mixins": [
|
||||
"MixinAnvilChunkLoader",
|
||||
"MixinChunk",
|
||||
"MixinItemStack"
|
||||
],
|
||||
"client": [],
|
||||
|
||||
Reference in new issue
Block a user