diff --git a/build.gradle b/build.gradle index d793714..627494b 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ apply from: './gradle/scripts/mixins.gradle' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 -version = "1.5.0" +version = "1.6.0" group= "dev.tilera" archivesBaseName = "auracore" diff --git a/src/main/java/dev/tilera/auracore/api/research/IResearchTable.java b/src/main/java/dev/tilera/auracore/api/research/IResearchTable.java new file mode 100644 index 0000000..4fe2351 --- /dev/null +++ b/src/main/java/dev/tilera/auracore/api/research/IResearchTable.java @@ -0,0 +1,24 @@ +package dev.tilera.auracore.api.research; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +/** + * DO NOT IMPLEMENT THIS! + * Implemented by TileResearchTable. Can safely be casted to it. + */ +public interface IResearchTable { + + ResearchTableExtension getInternalExtension(); + + World getWorld(); + + int getXCoord(); + + int getYCoord(); + + int getZCoord(); + + void openGUI(EntityPlayer player); + +} diff --git a/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtension.java b/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtension.java new file mode 100644 index 0000000..567c0fe --- /dev/null +++ b/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtension.java @@ -0,0 +1,46 @@ +package dev.tilera.auracore.api.research; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public abstract class ResearchTableExtension { + + public World world; + public int xCoord; + public int yCoord; + public int zCoord; + + public ResearchTableExtension(IResearchTable researchTable) { + this.world = researchTable.getWorld(); + this.xCoord = researchTable.getXCoord(); + this.yCoord = researchTable.getYCoord(); + this.zCoord = researchTable.getZCoord(); + } + + public IResearchTable getResearchTable() { + TileEntity te = this.world.getTileEntity(this.xCoord, this.yCoord, this.zCoord); + if (te instanceof IResearchTable) { + return (IResearchTable) te; + } + return null; + } + + public abstract void writeToNBT(NBTTagCompound nbt); + + public abstract void readFromNBT(NBTTagCompound nbt); + + public abstract void writeToPacket(NBTTagCompound nbt); + + public abstract void readFromPacket(NBTTagCompound nbt); + + public abstract void onTick(); + + public abstract void markDirty(); + + public abstract boolean openGUI(EntityPlayer player); + + public abstract String getNBTKey(); + +} diff --git a/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtensionRegistry.java b/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtensionRegistry.java new file mode 100644 index 0000000..d4cf012 --- /dev/null +++ b/src/main/java/dev/tilera/auracore/api/research/ResearchTableExtensionRegistry.java @@ -0,0 +1,31 @@ +package dev.tilera.auracore.api.research; + +import java.lang.reflect.Constructor; + +public class ResearchTableExtensionRegistry { + + private static Class extension = null; + + public static boolean registerResearchTableExtension(Class ext, boolean force) { + if (extension == null || force) { + extension = ext; + return true; + } + return false; + } + + public static boolean hasActiveExtension() { + return extension != null; + } + + public static ResearchTableExtension createInstance(IResearchTable table) { + try { + Constructor constr = extension.getConstructor(IResearchTable.class); + return (ResearchTableExtension) constr.newInstance(table); + } catch (NullPointerException | ReflectiveOperationException | SecurityException | IllegalArgumentException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/java/dev/tilera/auracore/mixins/MixinBlockTable.java b/src/main/java/dev/tilera/auracore/mixins/MixinBlockTable.java index 1158c4f..b045119 100644 --- a/src/main/java/dev/tilera/auracore/mixins/MixinBlockTable.java +++ b/src/main/java/dev/tilera/auracore/mixins/MixinBlockTable.java @@ -4,6 +4,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import dev.tilera.auracore.AuraCore; +import dev.tilera.auracore.api.research.IResearchTable; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; @@ -48,12 +49,12 @@ public abstract class MixinBlockTable extends BlockContainer { return true; } else { if (tileEntity instanceof TileResearchTable) { - player.openGui(Thaumcraft.instance, 10, world, x, y, z); + ((IResearchTable)tileEntity).openGUI(player); } else { for(int a = 2; a < 6; ++a) { TileEntity tile = world.getTileEntity(x + ForgeDirection.getOrientation(a).offsetX, y + ForgeDirection.getOrientation(a).offsetY, z + ForgeDirection.getOrientation(a).offsetZ); - if (tile != null && tile instanceof TileResearchTable) { - player.openGui(Thaumcraft.instance, 10, world, x + ForgeDirection.getOrientation(a).offsetX, y + ForgeDirection.getOrientation(a).offsetY, z + ForgeDirection.getOrientation(a).offsetZ); + if (tile instanceof TileResearchTable) { + ((IResearchTable)tile).openGUI(player); break; } } diff --git a/src/main/java/dev/tilera/auracore/mixins/MixinTileResearchTable.java b/src/main/java/dev/tilera/auracore/mixins/MixinTileResearchTable.java new file mode 100644 index 0000000..1965cb5 --- /dev/null +++ b/src/main/java/dev/tilera/auracore/mixins/MixinTileResearchTable.java @@ -0,0 +1,135 @@ +package dev.tilera.auracore.mixins; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.At; + +import dev.tilera.auracore.api.research.IResearchTable; +import dev.tilera.auracore.api.research.ResearchTableExtension; +import dev.tilera.auracore.api.research.ResearchTableExtensionRegistry; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +import net.minecraft.network.play.server.S35PacketUpdateTileEntity; +import net.minecraft.world.World; +import thaumcraft.api.TileThaumcraft; +import thaumcraft.common.Thaumcraft; +import thaumcraft.common.tiles.TileResearchTable; + +@Mixin(TileResearchTable.class) +public abstract class MixinTileResearchTable extends TileThaumcraft implements IResearchTable { + + private ResearchTableExtension extension = null; + + @Inject(method = "()V", at = @At("TAIL"), remap = false) + public void constructorHead(CallbackInfo ci) { + if (ResearchTableExtensionRegistry.hasActiveExtension()) { + extension = ResearchTableExtensionRegistry.createInstance(this); + } + } + + @Override + public ResearchTableExtension getInternalExtension() { + return extension; + } + + @Override + public World getWorld() { + return this.worldObj; + } + + @Override + public int getXCoord() { + return this.xCoord; + } + + @Override + public int getYCoord() { + return this.yCoord; + } + + @Override + public int getZCoord() { + return this.zCoord; + } + + @Override + public void openGUI(EntityPlayer player) { + if (extension != null) { + if (extension.openGUI(player)) return; + } + player.openGui(Thaumcraft.instance, 10, this.worldObj, this.xCoord, this.yCoord, this.zCoord); + } + + @Inject(method = "markDirty", at = @At("TAIL")) + public void onMarkDirty(CallbackInfo ci) { + if (extension != null) { + extension.markDirty(); + } + } + + @Inject(method = "updateEntity", at = @At("TAIL")) + public void onUpdateEntity(CallbackInfo ci) { + if (extension != null) { + extension.onTick(); + } + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + super.readFromNBT(nbttagcompound); + if (extension != null) { + String key = extension.getNBTKey(); + if (!nbttagcompound.hasKey(key)) return; + NBTTagCompound nbt = nbttagcompound.getCompoundTag(key); + extension.readFromNBT(nbt); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + super.writeToNBT(nbttagcompound); + if (extension != null) { + String key = extension.getNBTKey(); + NBTTagCompound nbt = new NBTTagCompound(); + extension.writeToNBT(nbt); + nbttagcompound.setTag(key, nbt); + } + } + + @Override + public Packet getDescriptionPacket() { + NBTTagCompound nbttagcompound = new NBTTagCompound(); + this.writeCustomNBT(nbttagcompound); + if (extension != null) { + String key = extension.getNBTKey(); + NBTTagCompound nbt = new NBTTagCompound(); + extension.writeToPacket(nbt); + nbttagcompound.setTag(key, nbt); + } + return new S35PacketUpdateTileEntity(super.xCoord, super.yCoord, super.zCoord, -999, nbttagcompound); + } + + /** + * @author tilera + * @reason ResearchTable extensions + */ + @Overwrite(remap = false) + public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { + super.onDataPacket(net, pkt); + if (super.worldObj != null && super.worldObj.isRemote) { + super.worldObj.markBlockForUpdate(super.xCoord, super.yCoord, super.zCoord); + } + if (extension != null) { + NBTTagCompound nbttagcompound = pkt.func_148857_g(); + String key = extension.getNBTKey(); + if (!nbttagcompound.hasKey(key)) return; + NBTTagCompound nbt = nbttagcompound.getCompoundTag(key); + extension.readFromNBT(nbt); + } + } + +} diff --git a/src/main/resources/auracore.mixins.json b/src/main/resources/auracore.mixins.json index 96d9b53..0e6fa79 100644 --- a/src/main/resources/auracore.mixins.json +++ b/src/main/resources/auracore.mixins.json @@ -12,6 +12,7 @@ "MixinTileArcaneFurnace", "MixinTileJarFillable", "MixinTileAlembic", + "MixinTileResearchTable", "MixinBlockCustomPlant", "MixinBlockCustomOre", "MixinBlockTable",