mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-12 00:17:47 +00:00
Kinda there for tile entities, need to change actual block
This commit is contained in:
1 parent
f80424f91d
commit
ce84d5a3e5
10 files changed
+173
-5
No files matched your search
@@ -0,0 +1,20 @@
|
||||
package com.colen.postea.API;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TileEntityReplacementManager {
|
||||
|
||||
private static final Map<String, TileTransformer> replacementMap = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void addItemReplacement(String tileName , TileTransformer tileTransformer) {
|
||||
replacementMap.put(tileName, tileTransformer);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static TileTransformer getTileReplacementTransformer(String tileName) {
|
||||
return replacementMap.getOrDefault(tileName, null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.colen.postea.API;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TileTransformer {
|
||||
|
||||
private final Class<? extends TileEntity> tileClass;
|
||||
private final Function<NBTTagCompound, NBTTagCompound> NBTModifierFunction;
|
||||
|
||||
public TileTransformer(Class<? extends TileEntity> tileClass, Function<NBTTagCompound, NBTTagCompound> NBTModifierFunction) {
|
||||
this.tileClass = tileClass;
|
||||
this.NBTModifierFunction = NBTModifierFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
if (o instanceof TileTransformer that) {
|
||||
return (this.tileClass == that.tileClass);
|
||||
} else return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(tileClass);
|
||||
}
|
||||
|
||||
public void modifyNBT(NBTTagCompound nbt) {
|
||||
NBTModifierFunction.apply(nbt);
|
||||
}
|
||||
|
||||
public Class<? extends TileEntity> getTileClass() {
|
||||
return tileClass;
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,19 @@ 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.API.TileTransformer;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.Item;
|
||||
import gregtech.api.GregTech_API;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntityChest;
|
||||
import net.minecraft.tileentity.TileEntityFurnace;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]")
|
||||
@SuppressWarnings("unused")
|
||||
@@ -13,7 +22,17 @@ 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(Blocks.bedrock, 0, Blocks.wool, 2);
|
||||
//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) -> {
|
||||
return tag;
|
||||
};
|
||||
|
||||
TileEntityReplacementManager.addItemReplacement("BlockController", new TileTransformer(TileEntityChest.class, myTransformer));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading
|
||||
mixins.add("MixinItemStack");
|
||||
mixins.add("MixinChunkProviderServer");
|
||||
mixins.add("MixinAnvilChunkLoader");
|
||||
mixins.add("MixinTileEntity");
|
||||
|
||||
return mixins;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(AnvilChunkLoader.class)
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class MixinAnvilChunkLoader {
|
||||
|
||||
@Inject(
|
||||
@@ -18,6 +19,7 @@ public abstract class MixinAnvilChunkLoader {
|
||||
at = @At("RETURN"),
|
||||
cancellable = true
|
||||
)
|
||||
@SuppressWarnings("unused")
|
||||
private void onChunkReloadFromWorldSaveData(World world, NBTTagCompound compound, CallbackInfoReturnable<Chunk> cir) {
|
||||
Chunk chunk = cir.getReturnValue();
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(ChunkProviderServer.class)
|
||||
@SuppressWarnings("unused")
|
||||
public class MixinChunkProviderServer {
|
||||
|
||||
@Inject(
|
||||
@@ -17,6 +18,7 @@ public class MixinChunkProviderServer {
|
||||
remap=false,
|
||||
cancellable = true
|
||||
)
|
||||
@SuppressWarnings("unused")
|
||||
public void onChunkCreation(int chunkX, int chunkZ, CallbackInfoReturnable<Chunk> cir) {
|
||||
Chunk chunk = cir.getReturnValue();
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.colen.postea.API.ItemStackKey;
|
||||
import com.colen.postea.API.ItemStackReplacementManager;
|
||||
|
||||
@Mixin(ItemStack.class)
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class MixinItemStack {
|
||||
|
||||
@Shadow
|
||||
@@ -23,6 +24,7 @@ public abstract class MixinItemStack {
|
||||
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);
|
||||
|
||||
@@ -34,13 +36,14 @@ public abstract class MixinItemStack {
|
||||
}
|
||||
|
||||
@Inject(method = "readFromNBT(Lnet/minecraft/nbt/NBTTagCompound;)V", at = @At("RETURN"))
|
||||
@SuppressWarnings("unused")
|
||||
private void onReadFromNBTReturn(NBTTagCompound p_77963_1_, CallbackInfo ci) {
|
||||
ItemStackKey replacement = ItemStackReplacementManager.getItemReplacement(field_151002_e, itemDamage);
|
||||
|
||||
if (replacement == null) return;
|
||||
|
||||
func_150996_a(replacement.getItem());
|
||||
itemDamage = replacement.getDamage();
|
||||
func_150996_a(replacement.getItem());
|
||||
}
|
||||
|
||||
@Unique
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
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 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user