From b3896b40a428ba0f5d01d6f3db4d2cb0da2d20e2 Mon Sep 17 00:00:00 2001 From: GTNH-Colen <54497873+GTNH-Colen@users.noreply.github.com> Date: Tue, 31 Oct 2023 16:10:35 +0000 Subject: [PATCH] Big cleanup --- .../java/com/colen/postea/API/BlockKey.java | 31 ------- .../API/ItemStackReplacementManager.java | 17 ++-- .../com/colen/postea/API/TileTransformer.java | 39 --------- src/main/java/com/colen/postea/Postea.java | 54 ++++++------ .../postea/Utility/ItemFixerUtility.java | 28 ++++++ .../colen/postea/Utility/PosteaUtilities.java | 18 ++-- .../postea/mixins/PosteaLoadingPlugin.java | 3 - .../mixins/early/MixinAnvilChunkLoader.java | 75 ---------------- .../early/MixinChunkProviderServer.java | 24 ------ .../postea/mixins/early/MixinItemStack.java | 36 +------- .../postea/mixins/early/MixinTileEntity.java | 85 ------------------- src/main/resources/mixins.postea.early.json | 4 +- 12 files changed, 78 insertions(+), 336 deletions(-) delete mode 100644 src/main/java/com/colen/postea/API/BlockKey.java delete mode 100644 src/main/java/com/colen/postea/API/TileTransformer.java create mode 100644 src/main/java/com/colen/postea/Utility/ItemFixerUtility.java delete mode 100644 src/main/java/com/colen/postea/mixins/early/MixinChunkProviderServer.java delete mode 100644 src/main/java/com/colen/postea/mixins/early/MixinTileEntity.java diff --git a/src/main/java/com/colen/postea/API/BlockKey.java b/src/main/java/com/colen/postea/API/BlockKey.java deleted file mode 100644 index 91c52da..0000000 --- a/src/main/java/com/colen/postea/API/BlockKey.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.colen.postea.API; - -import java.util.Objects; - -import net.minecraft.block.Block; -import net.minecraft.item.Item; - -public class BlockKey { - - public final String blockName; - public final int meta; - - public BlockKey(String blockName, int meta) { - this.blockName = blockName; - this.meta = meta; - } - - @Override - public boolean equals(Object o) { - if (o == null) return false; - if (o instanceof BlockKey that) { - return (this.meta == that.meta) && (this.blockName.equals(that.blockName)); - } else return false; - } - - @Override - public int hashCode() { - return Objects.hash(blockName, meta); - } - -} diff --git a/src/main/java/com/colen/postea/API/ItemStackReplacementManager.java b/src/main/java/com/colen/postea/API/ItemStackReplacementManager.java index aa6c2db..17a128e 100644 --- a/src/main/java/com/colen/postea/API/ItemStackReplacementManager.java +++ b/src/main/java/com/colen/postea/API/ItemStackReplacementManager.java @@ -1,23 +1,24 @@ package com.colen.postea.API; +import net.minecraft.nbt.NBTTagCompound; + import java.util.HashMap; import java.util.Map; - -import net.minecraft.item.Item; +import java.util.function.Function; public class ItemStackReplacementManager { - private static final Map replacementMap = new HashMap<>(); + private static final Map> replacementMap = new HashMap<>(); - // Public API for adding replacement ItemStacks. + // Public API for converting ItemStacks. @SuppressWarnings("unused") - public static void addItemReplacement(Item itemIn, int metaIn, Item itemOut, int metaOut) { - replacementMap.put(new ItemStackKey(itemIn, metaIn), new ItemStackKey(itemOut, metaOut)); + public static void addItemReplacement(String itemName, Function transformer) { + replacementMap.put(itemName, transformer); } @SuppressWarnings("unused") - public static ItemStackKey getItemReplacement(Item itemIn, int metaIn) { - return replacementMap.getOrDefault(new ItemStackKey(itemIn, metaIn), null); + public static Function getItemReplacement(String itemNameInternal) { + return replacementMap.get(itemNameInternal); } } diff --git a/src/main/java/com/colen/postea/API/TileTransformer.java b/src/main/java/com/colen/postea/API/TileTransformer.java deleted file mode 100644 index 741df49..0000000 --- a/src/main/java/com/colen/postea/API/TileTransformer.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.colen.postea.API; - -import net.minecraft.client.Minecraft; -import net.minecraft.init.Blocks; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; - -import java.util.Objects; -import java.util.function.Function; - -public class TileTransformer { - - - private final Function NBTModifierFunction; - private final String tileName; - - public TileTransformer(String tileName, Function NBTModifierFunction) { - this.tileName = tileName; - this.NBTModifierFunction = NBTModifierFunction; - } - - @Override - public boolean equals(Object o) { - if (o == null) return false; - if (o instanceof TileTransformer that) { - return (this.tileName.equals(that.tileName)); - } else return false; - } - - public void modifyNBT(NBTTagCompound nbt) { - NBTModifierFunction.apply(nbt); - } - - public String getTileName() { - return tileName; - } - -} diff --git a/src/main/java/com/colen/postea/Postea.java b/src/main/java/com/colen/postea/Postea.java index 2f4067f..ec25e69 100644 --- a/src/main/java/com/colen/postea/Postea.java +++ b/src/main/java/com/colen/postea/Postea.java @@ -1,6 +1,7 @@ package com.colen.postea; import com.colen.postea.API.BlockReplacementManager; +import com.colen.postea.API.ItemStackReplacementManager; import com.colen.postea.API.TileEntityReplacementManager; import com.colen.postea.Utility.BlockConversionInfo; import com.colen.postea.Utility.BlockInfo; @@ -9,6 +10,8 @@ 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.init.Items; +import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -22,21 +25,10 @@ public class Postea { @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { - //ItemStackReplacementManager.addItemReplacement(Item.getItemFromBlock(Blocks.grass), 0, Item.getItemFromBlock(Blocks.coal_ore), 10); -// BlockReplacementManager.addBlockReplacement(GregTech_API.sBlockOres1, 6, Blocks.emerald_ore, 0); -// BlockReplacementManager.addBlockReplacement(GregTech_API.sBlockOres1, 16022, Blocks.nether_brick, 0); -// BlockReplacementManager.addBlockReplacement(GregTech_API.sBlockOres1, 1018, Blocks.end_stone, 0); - // Define the transformation function -/* Function myTransformer = (tag) -> { - if (tag.getInteger("m") == 86) { - tag.setInteger("m", 323); - return tag; - } - return tag; - };*/ - - //TileEntityReplacementManager.tileEntityFixer("GT_TileEntity_Ores", myTransformer); + // This is an example of replacing GT ore blocks depending on the dimension. Because BlockInfo has no TileEntity + // transformer set, it will delete the underlying tile entity fully. This will essentially convert a TE block to a non-TE block. + // You can use the data in the tile to determine this conversion from the "tag" variable. TileEntityReplacementManager.tileEntityTransformer("GT_TileEntity_Ores", (tag, world) -> { if (world.provider.dimensionId == -1) { return new BlockInfo(Blocks.wool, 1); @@ -50,7 +42,9 @@ public class Postea { }); -/* TileEntityReplacementManager.tileEntityTransformer("Furnace", (tag) -> { + // This is an example of how to replace another tile entity and it's block. Here we are replacing furnaces with chests. + // It will then also replace the furnace block with a chest. Otherwise the Tile would not match the block. + TileEntityReplacementManager.tileEntityTransformer("Furnace", (tag, world) -> { Function chestTransformer = (oldTag) -> { @@ -71,9 +65,12 @@ public class Postea { return newTag; }; - return new ChunkFixerUtility.BlockInfo(Blocks.chest, 0, chestTransformer); - });*/ + return new BlockInfo(Blocks.chest, 0, chestTransformer); + }); + // This is an example of how to change the data of a specific tile entity. You provide the Tiles ID e.g. Chest + // and then provide a conversion function. In this instance we are changing the contents of a chest. + // This will replace the contents fully with stone and jungle planks at specific slots. TileEntityReplacementManager.tileEntityTransformer("Chest", (tag, world) -> { Function chestTransformer = (oldTag) -> { @@ -86,13 +83,13 @@ public class Postea { stoneAtSlot12.setByte("Count", (byte) 2); stoneAtSlot12.setByte("Slot", (byte) 12); stoneAtSlot12.setShort("Damage", (short) 0); - stoneAtSlot12.setShort("id", (short) 1); + stoneAtSlot12.setShort("id", (short) Block.getIdFromBlock(Blocks.stone)); NBTTagCompound stoneAtSlot14 = new NBTTagCompound(); stoneAtSlot14.setByte("Count", (byte) 4); stoneAtSlot14.setByte("Slot", (byte) 14); stoneAtSlot14.setShort("Damage", (short) 3); - stoneAtSlot14.setShort("id", (short) 5); + stoneAtSlot14.setShort("id", (short) Block.getIdFromBlock(Blocks.planks)); tagList.appendTag(stoneAtSlot12); tagList.appendTag(stoneAtSlot14); @@ -106,12 +103,13 @@ public class Postea { }); - + // Example of how to replace a non-TE block with another non-TE block, this replaces grass with wool. + // It converts every grass block with an even x coordinate to orange wool. Function blockTransformer = (blockConversionInfoOld) -> { BlockConversionInfo blockConversionInfoNew = new BlockConversionInfo(); - if ((blockConversionInfoOld.x == 334 && blockConversionInfoOld.y == 4 && blockConversionInfoOld.z == 73)) { + if ((blockConversionInfoOld.x % 2 == 0)) { blockConversionInfoNew.blockID = Block.getIdFromBlock(Blocks.wool); blockConversionInfoNew.metadata = 1; } else { @@ -124,10 +122,16 @@ public class Postea { BlockReplacementManager.addBlockReplacement("minecraft:grass", blockTransformer); - } - @Mod.EventHandler - public void postInit(FMLPostInitializationEvent event) { - //BlockReplacementManager.addBlockReplacement(GregTech_API.sBlockOres1, 0, Blocks.chest, 0); + // Example of how to replace an item, this replaces iron ingots with gold ingots. + // You can manipulate stack size but it isn't recommended. + // You can also use the tag associated with this ItemStack to determine the replacement. + Function itemTransformer = (tag) -> { + tag.setShort("id", (short) Item.getIdFromItem(Items.gold_ingot)); + return tag; + }; + + ItemStackReplacementManager.addItemReplacement("minecraft:iron_ingot", itemTransformer); + } } diff --git a/src/main/java/com/colen/postea/Utility/ItemFixerUtility.java b/src/main/java/com/colen/postea/Utility/ItemFixerUtility.java new file mode 100644 index 0000000..437c65c --- /dev/null +++ b/src/main/java/com/colen/postea/Utility/ItemFixerUtility.java @@ -0,0 +1,28 @@ +package com.colen.postea.Utility; + +import com.colen.postea.API.ItemStackReplacementManager; +import cpw.mods.fml.common.registry.GameRegistry; +import net.minecraft.item.Item; +import net.minecraft.nbt.NBTTagCompound; + +import java.util.function.Function; + +public abstract class ItemFixerUtility { + + public static void fixItemStack(NBTTagCompound tag) { + + if (tag.hasNoTags()) return; + + if (tag.hasKey("id")) { + short id = tag.getShort("id"); + Item item = Item.getItemById(id); + String itemNameInternal = GameRegistry.findUniqueIdentifierFor(item).modId + ":" + GameRegistry.findUniqueIdentifierFor(item).name; + + Function transformer = ItemStackReplacementManager.getItemReplacement(itemNameInternal); + + if (transformer != null) { + transformer.apply(tag); + } + } + } +} diff --git a/src/main/java/com/colen/postea/Utility/PosteaUtilities.java b/src/main/java/com/colen/postea/Utility/PosteaUtilities.java index fdb2f3c..884fd4e 100644 --- a/src/main/java/com/colen/postea/Utility/PosteaUtilities.java +++ b/src/main/java/com/colen/postea/Utility/PosteaUtilities.java @@ -1,5 +1,7 @@ package com.colen.postea.Utility; +import cpw.mods.fml.common.registry.GameRegistry; +import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; public abstract class PosteaUtilities { @@ -10,17 +12,15 @@ public abstract class PosteaUtilities { 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")); + + // This part is for tile entities only. + if (tag.hasKey("x") && tag.hasKey("y") && tag.hasKey("z")) { + tagCompound.setInteger("x", tag.getInteger("x")); + tagCompound.setInteger("y", tag.getInteger("y")); + tagCompound.setInteger("z", tag.getInteger("z")); + } return tagCompound; } - public static void fixItemStack(NBTTagCompound tag) { - - if (tag.hasNoTags()) return; - - // Todo {id:5s,Count:4b,Damage:3s} - } } diff --git a/src/main/java/com/colen/postea/mixins/PosteaLoadingPlugin.java b/src/main/java/com/colen/postea/mixins/PosteaLoadingPlugin.java index 36d36dd..766ac42 100644 --- a/src/main/java/com/colen/postea/mixins/PosteaLoadingPlugin.java +++ b/src/main/java/com/colen/postea/mixins/PosteaLoadingPlugin.java @@ -44,10 +44,7 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading public List getMixins(Set loadedCoreMods) { final List mixins = new ArrayList<>(); mixins.add("MixinItemStack"); - mixins.add("MixinChunkProviderServer"); mixins.add("MixinAnvilChunkLoader"); - mixins.add("MixinTileEntity"); - return mixins; } } diff --git a/src/main/java/com/colen/postea/mixins/early/MixinAnvilChunkLoader.java b/src/main/java/com/colen/postea/mixins/early/MixinAnvilChunkLoader.java index 65c48d0..8bde8cf 100644 --- a/src/main/java/com/colen/postea/mixins/early/MixinAnvilChunkLoader.java +++ b/src/main/java/com/colen/postea/mixins/early/MixinAnvilChunkLoader.java @@ -14,81 +14,6 @@ import static com.colen.postea.Utility.ChunkFixerUtility.processChunkNBT; @SuppressWarnings("unused") public abstract class MixinAnvilChunkLoader { -// @Unique -// private static Class PendingChunkClass; -// -// static { -// try { -// for (Class declaredClass : AnvilChunkLoader.class.getDeclaredClasses()) { -// if (declaredClass.getSimpleName().equals("PendingChunk")) { -// PendingChunkClass = declaredClass; -// break; -// } -// } -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -// -// /** -// * @author Colen -// * @reason Reasons. -// */ -// @Unique -// @Overwrite(remap = false) -// public Object[] loadChunk__Async(World p_75815_1_, int p_75815_2_, int p_75815_3_) throws IOException -// { -// NBTTagCompound nbttagcompound = null; -// ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(p_75815_2_, p_75815_3_); -// Object object = this.syncLockObject; -// -// synchronized (this.syncLockObject) -// { -// if (this.pendingAnvilChunksCoordinates.contains(chunkcoordintpair)) { -// Iterator iter = this.chunksToRemove.iterator(); -// while (iter.hasNext()) { -// Object potentialPendingChunk = iter.next(); -// if (PendingChunkClass.isInstance(potentialPendingChunk)) { -// try { -// Field chunkCoordinateField = PendingChunkClass.getDeclaredField("chunkCoordinate"); -// chunkCoordinateField.setAccessible(true); -// ChunkCoordIntPair chunkCoordinate = (ChunkCoordIntPair) chunkCoordinateField.get(potentialPendingChunk); -// -// Field nbtTagsField = PendingChunkClass.getDeclaredField("nbtTags"); -// nbtTagsField.setAccessible(true); -// NBTTagCompound nbtTags = (NBTTagCompound) nbtTagsField.get(potentialPendingChunk); -// -// if (chunkCoordinate.equals(chunkcoordintpair)) { -// nbttagcompound = nbtTags; -// break; -// } -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -// } -// } -// -// } -// -// if (nbttagcompound == null) -// { -// DataInputStream datainputstream = RegionFileCache.getChunkInputStream(this.chunkSaveLocation, p_75815_2_, p_75815_3_); -// -// if (datainputstream == null) -// { -// return null; -// } -// -// nbttagcompound = CompressedStreamTools.read(datainputstream); -// } -// -// return this.checkedReadChunkFromNBT__Async(p_75815_1_, p_75815_2_, p_75815_3_, nbttagcompound); -// } -// -// @Unique -// protected native Object[] checkedReadChunkFromNBT__Async(World p_75822_1_, int p_75822_2_, int p_75822_3_, NBTTagCompound p_75822_4_); - @Inject(method = "checkedReadChunkFromNBT__Async", at = @At("HEAD"), remap = false) private void onCheckedReadChunkFromNBT__Async(World world, int x, int z, NBTTagCompound compound, CallbackInfoReturnable cir) { // Your injection code here diff --git a/src/main/java/com/colen/postea/mixins/early/MixinChunkProviderServer.java b/src/main/java/com/colen/postea/mixins/early/MixinChunkProviderServer.java deleted file mode 100644 index eff6429..0000000 --- a/src/main/java/com/colen/postea/mixins/early/MixinChunkProviderServer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.colen.postea.mixins.early; - -import net.minecraft.world.gen.ChunkProviderServer; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(ChunkProviderServer.class) -@SuppressWarnings("unused") -public class MixinChunkProviderServer { - -// @Inject( -// method = "originalLoadChunk(II)Lnet/minecraft/world/chunk/Chunk;", -// at = @At("RETURN"), -// remap=false, -// cancellable = true -// ) -// @SuppressWarnings("unused") -// public void onChunkCreation(int chunkX, int chunkZ, CallbackInfoReturnable cir) { -// Chunk chunk = cir.getReturnValue(); -// -// Chunk fixedChunk = ChunkFixerUtility.fixChunk(chunk); -// -// cir.setReturnValue(fixedChunk); -// } -} diff --git a/src/main/java/com/colen/postea/mixins/early/MixinItemStack.java b/src/main/java/com/colen/postea/mixins/early/MixinItemStack.java index 4b59850..3e3a745 100644 --- a/src/main/java/com/colen/postea/mixins/early/MixinItemStack.java +++ b/src/main/java/com/colen/postea/mixins/early/MixinItemStack.java @@ -1,54 +1,22 @@ package com.colen.postea.mixins.early; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; - import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.colen.postea.API.ItemStackKey; -import com.colen.postea.API.ItemStackReplacementManager; - -import static com.colen.postea.Utility.PosteaUtilities.fixItemStack; +import static com.colen.postea.Utility.ItemFixerUtility.fixItemStack; @Mixin(ItemStack.class) @SuppressWarnings("unused") public abstract class MixinItemStack { - @Shadow - private int itemDamage; // Metadata - @Shadow - private Item field_151002_e; -// -// @Inject(method = "(Lnet/minecraft/item/Item;II)V", at = @At("RETURN")) -// @SuppressWarnings("unused") -// private void onConstructorReturn(Item item, int size, int metadata, CallbackInfo ci) { -// ItemStackKey replacement = ItemStackReplacementManager.getItemReplacement(item, metadata); -// -// if (replacement == null) return; -// -// // Edit private variables here. -// itemDamage = replacement.getDamage(); -// func_150996_a(replacement.getItem()); -// } -// - @Inject(method = "readFromNBT(Lnet/minecraft/nbt/NBTTagCompound;)V", at = @At("RETURN")) + @Inject(method = "readFromNBT(Lnet/minecraft/nbt/NBTTagCompound;)V", at = @At("HEAD")) @SuppressWarnings("unused") private void onReadFromNBTReturn(NBTTagCompound tag, CallbackInfo ci) { fixItemStack(tag); -// ItemStackKey replacement = ItemStackReplacementManager.getItemReplacement(field_151002_e, itemDamage); -// -// if (replacement == null) return; -// -// itemDamage = replacement.getDamage(); -// func_150996_a(replacement.getItem()); } - @Unique - public native void func_150996_a(Item p_150996_1_); } diff --git a/src/main/java/com/colen/postea/mixins/early/MixinTileEntity.java b/src/main/java/com/colen/postea/mixins/early/MixinTileEntity.java deleted file mode 100644 index 5eeba87..0000000 --- a/src/main/java/com/colen/postea/mixins/early/MixinTileEntity.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.colen.postea.mixins.early; - -import com.colen.postea.API.TileEntityReplacementManager; -import com.colen.postea.API.TileTransformer; -import cpw.mods.fml.common.FMLLog; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; -import org.spongepowered.asm.mixin.Shadow; - -import java.util.Map; - -@Mixin(TileEntity.class) -@SuppressWarnings("unused") -public class MixinTileEntity { - -// @Shadow -// private static Logger logger; -// -// @Shadow -// private static Map nameToClassMap; -// -// @Shadow -// protected World worldObj; - - - /** - * Creates a new entity and loads its data from the specified NBT. - */ -// @Overwrite -// @SuppressWarnings("unused") -// public static TileEntity createAndLoadEntity(NBTTagCompound nbt) { -// TileEntity tileentity = null; -// -// Class oclass = null; -// try -// { -// String tileEntityStringName = nbt.getString("id"); -// TileTransformer tileTransformer = TileEntityReplacementManager.getTileReplacementTransformer(tileEntityStringName); -// -// -// if (tileTransformer == null) { -// oclass = (Class)nameToClassMap.get(tileEntityStringName); -// } else { -// oclass = tileTransformer.getTileClass(); -// tileTransformer.modifyNBT(nbt); -// } -// -// if (oclass != null) -// { -// tileentity = (TileEntity)oclass.newInstance(); -// } -// } -// catch (Exception exception) -// { -// exception.printStackTrace(); -// } -// -// if (tileentity != null) -// { -// try -// { -// tileentity.readFromNBT(nbt); -// } -// catch (Exception ex) -// { -// FMLLog.log(Level.ERROR, ex, -// "A TileEntity %s(%s) has thrown an exception during loading, its state cannot be restored. Report this to the mod author", -// nbt.getString("id"), oclass.getName()); -// tileentity = null; -// } -// } -// else -// { -// logger.warn("Skipping BlockEntity with id " + nbt.getString("id")); -// } -// -// return tileentity; -// } - -} diff --git a/src/main/resources/mixins.postea.early.json b/src/main/resources/mixins.postea.early.json index ddc0558..48840ac 100644 --- a/src/main/resources/mixins.postea.early.json +++ b/src/main/resources/mixins.postea.early.json @@ -7,9 +7,7 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "MixinAnvilChunkLoader", - "MixinChunkProviderServer", - "MixinItemStack", - "MixinTileEntity" + "MixinItemStack" ], "client": [], "server": []