Some refactoring

This commit is contained in:
GTNH-Colen committed 2023-10-23 15:37:55 +01:00
1 parent a3d9aac95e
commit f80424f91d
6 files changed
+71 -31

No files matched your search

@@ -1,21 +1,25 @@
package com.colen.postea.API;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import java.util.HashMap;
import java.util.Map;
import static com.colen.postea.API.ItemStackReplacementManager.addItemReplacement;
public class BlockReplacementManager {
private static final Map<BlockKey, BlockKey> replacementMap = new HashMap<>();
private static final Map<BlockKey, BlockKey> blockReplacementMap = new HashMap<>();
@SuppressWarnings("unused")
public static void addBlockReplacement(Block blockIn, int metadataIn, Block blockOut, int metadataOut) {
replacementMap.put(new BlockKey(blockIn, metadataIn), new BlockKey(blockOut, metadataOut));
blockReplacementMap.put(new BlockKey(blockIn, metadataIn), new BlockKey(blockOut, metadataOut));
addItemReplacement(Item.getItemFromBlock(blockIn), metadataIn, Item.getItemFromBlock(blockOut), metadataOut);
}
@SuppressWarnings("unused")
public static BlockKey getBlockReplacement(Block blockIn, int metadataIn) {
return replacementMap.getOrDefault(new BlockKey(blockIn, metadataIn), null);
return blockReplacementMap.getOrDefault(new BlockKey(blockIn, metadataIn), null);
}
}
@@ -1,30 +1,11 @@
package com.colen.postea.mixins.early;
package com.colen.postea.API;
import com.colen.postea.API.BlockKey;
import com.colen.postea.API.BlockReplacementManager;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
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.CallbackInfoReturnable;
@Mixin(AnvilChunkLoader.class)
public abstract class MixinChunk {
@Inject(
method = "readChunkFromNBT(Lnet/minecraft/world/World;Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/world/chunk/Chunk;",
at = @At("RETURN"),
cancellable = true
)
private void modifyChunkData(World world, NBTTagCompound compound, CallbackInfoReturnable<Chunk> cir) {
Chunk chunk = cir.getReturnValue();
public class ChunkFixerUtility {
public static Chunk fixChunk(Chunk chunk) {
// Iterate over each ExtendedBlockStorage. Kinda ugly but does the job...
for (ExtendedBlockStorage storage : chunk.getBlockStorageArray()) {
if (storage != null) { // Always check if storage isn't null before using it
@@ -49,9 +30,6 @@ public abstract class MixinChunk {
}
}
}
cir.setReturnValue(chunk);
return chunk;
}
}
@@ -44,7 +44,8 @@ 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("MixinChunkProviderServer");
mixins.add("MixinAnvilChunkLoader");
return mixins;
}
@@ -0,0 +1,29 @@
package com.colen.postea.mixins.early;
import com.colen.postea.API.ChunkFixerUtility;
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.CallbackInfoReturnable;
@Mixin(AnvilChunkLoader.class)
public abstract class MixinAnvilChunkLoader {
@Inject(
method = "readChunkFromNBT(Lnet/minecraft/world/World;Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/world/chunk/Chunk;",
at = @At("RETURN"),
cancellable = true
)
private void onChunkReloadFromWorldSaveData(World world, NBTTagCompound compound, CallbackInfoReturnable<Chunk> cir) {
Chunk chunk = cir.getReturnValue();
Chunk fixedChunk = ChunkFixerUtility.fixChunk(chunk);
cir.setReturnValue(fixedChunk);
}
}
@@ -0,0 +1,27 @@
package com.colen.postea.mixins.early;
import com.colen.postea.API.ChunkFixerUtility;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
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.CallbackInfoReturnable;
@Mixin(ChunkProviderServer.class)
public class MixinChunkProviderServer {
@Inject(
method = "originalLoadChunk(II)Lnet/minecraft/world/chunk/Chunk;",
at = @At("RETURN"),
remap=false,
cancellable = true
)
public void onChunkCreation(int chunkX, int chunkZ, CallbackInfoReturnable<Chunk> cir) {
Chunk chunk = cir.getReturnValue();
Chunk fixedChunk = ChunkFixerUtility.fixChunk(chunk);
cir.setReturnValue(fixedChunk);
}
}