Big cleanup

This commit is contained in:
GTNH-Colen committed 2023-10-31 16:10:35 +00:00
1 parent 08f5f74c9f
commit b3896b40a4
12 files changed
+78 -336

No files matched your search

@@ -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);
}
}
@@ -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<ItemStackKey, ItemStackKey> replacementMap = new HashMap<>();
private static final Map<String, Function<NBTTagCompound, NBTTagCompound>> 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<NBTTagCompound, NBTTagCompound> 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<NBTTagCompound, NBTTagCompound> getItemReplacement(String itemNameInternal) {
return replacementMap.get(itemNameInternal);
}
}
@@ -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<NBTTagCompound, NBTTagCompound> NBTModifierFunction;
private final String tileName;
public TileTransformer(String tileName, Function<NBTTagCompound, NBTTagCompound> 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;
}
}
+29 -25
View File
@@ -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<NBTTagCompound, NBTTagCompound> 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<NBTTagCompound, NBTTagCompound> 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<NBTTagCompound, NBTTagCompound> 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<BlockConversionInfo, BlockConversionInfo> 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<NBTTagCompound, NBTTagCompound> itemTransformer = (tag) -> {
tag.setShort("id", (short) Item.getIdFromItem(Items.gold_ingot));
return tag;
};
ItemStackReplacementManager.addItemReplacement("minecraft:iron_ingot", itemTransformer);
}
}
@@ -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<NBTTagCompound, NBTTagCompound> transformer = ItemStackReplacementManager.getItemReplacement(itemNameInternal);
if (transformer != null) {
transformer.apply(tag);
}
}
}
}
@@ -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}
}
}
@@ -44,10 +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("MixinChunkProviderServer");
mixins.add("MixinAnvilChunkLoader");
mixins.add("MixinTileEntity");
return mixins;
}
}
@@ -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<Object[]> cir) {
// Your injection code here
@@ -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<Chunk> cir) {
// Chunk chunk = cir.getReturnValue();
//
// Chunk fixedChunk = ChunkFixerUtility.fixChunk(chunk);
//
// cir.setReturnValue(fixedChunk);
// }
}
@@ -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 = "<init>(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_);
}
@@ -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;
// }
}