diff --git a/src/main/java/kamkeel/npcs/controllers/SyncController.java b/src/main/java/kamkeel/npcs/controllers/SyncController.java index 100486761..56dc377f9 100644 --- a/src/main/java/kamkeel/npcs/controllers/SyncController.java +++ b/src/main/java/kamkeel/npcs/controllers/SyncController.java @@ -22,6 +22,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.server.MinecraftServer; import noppes.npcs.LogWriter; import noppes.npcs.client.ClientCacheHandler; +import noppes.npcs.client.gui.util.IGuiData; import noppes.npcs.controllers.data.PlayerData; import java.util.HashMap; @@ -291,6 +292,9 @@ public class SyncController { } ClientCacheHandler.updateClientRevision(syncType, revision); + + // New GUI data handling approach + IGuiData.notifyUpdate(compound, key, id); } @SideOnly(Side.CLIENT) @@ -305,6 +309,9 @@ public class SyncController { } ClientCacheHandler.updateClientRevision(syncType, revision); + + // New GUI data handling approach + IGuiData.notifyRemove(key, id); } // ═══════════════════════════════════════════════════════════════════════ diff --git a/src/main/java/kamkeel/npcs/network/packets/data/large/GuiDataPacket.java b/src/main/java/kamkeel/npcs/network/packets/data/large/GuiDataPacket.java index 631e32bf2..784d0fe1c 100644 --- a/src/main/java/kamkeel/npcs/network/packets/data/large/GuiDataPacket.java +++ b/src/main/java/kamkeel/npcs/network/packets/data/large/GuiDataPacket.java @@ -9,13 +9,9 @@ import kamkeel.npcs.network.PacketChannel; import kamkeel.npcs.network.PacketHandler; import kamkeel.npcs.network.enums.EnumDataPacket; import kamkeel.npcs.util.ByteBufUtils; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; -import noppes.npcs.client.gui.util.GuiContainerNPCInterface; -import noppes.npcs.client.gui.util.GuiNPCInterface; import noppes.npcs.client.gui.util.IGuiData; import java.io.IOException; @@ -59,15 +55,6 @@ public final class GuiDataPacket extends LargeAbstractPacket { @SideOnly(Side.CLIENT) @Override protected void handleCompleteData(ByteBuf data, EntityPlayer player) throws IOException { - GuiScreen gui = Minecraft.getMinecraft().currentScreen; - if (gui instanceof GuiNPCInterface && ((GuiNPCInterface) gui).hasSubGui()) { - gui = ((GuiNPCInterface) gui).getSubGui(); - } else if (gui instanceof GuiContainerNPCInterface && ((GuiContainerNPCInterface) gui).hasSubGui()) { - gui = ((GuiContainerNPCInterface) gui).getSubGui(); - } - if (gui instanceof IGuiData) { - NBTTagCompound nbt = ByteBufUtils.readBigNBT(data); - ((IGuiData) gui).setGuiData(nbt); - } + IGuiData.legacySetGuiData(ByteBufUtils.readBigNBT(data)); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/IGuiData.java b/src/main/java/noppes/npcs/client/gui/util/IGuiData.java index 36ba683ac..9f1796c8a 100644 --- a/src/main/java/noppes/npcs/client/gui/util/IGuiData.java +++ b/src/main/java/noppes/npcs/client/gui/util/IGuiData.java @@ -1,7 +1,108 @@ package noppes.npcs.client.gui.util; +import kamkeel.npcs.controllers.SyncController; +import kamkeel.npcs.controllers.sync.SyncHandler; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; import net.minecraft.nbt.NBTTagCompound; +/** + * OVERHAULED IGuiData FRAMEWORK! + * Based on the new {@link SyncController} and {@link SyncHandler} infrastructure + */ public interface IGuiData { - public void setGuiData(NBTTagCompound compound); + + /** + * Legacy data setter + * @Deprecated Use {@link #onDataUpdated(NBTTagCompound)} or {@link #onDataRemoved(String)} instead + */ + default void setGuiData(NBTTagCompound compound) { + + } + + // ═══════════════════════════════════════════════════════════════════════ + // USE THESE !!! + // ═══════════════════════════════════════════════════════════════════════ + + /** NOTE: + * No need for full object dataset onDataReload on GUI open, as these datasets are + * always freshly synced to their client's controllers thanks to {@link SyncController} + */ + + /** + * When a controller's object gets saved on server side, the updated object is synced to the client + * @param compound the compound of the updated object + */ + default void onDataUpdated(NBTTagCompound compound) { + } + + /** + * When a controller's object gets saved on server side, the updated object is synced to the client + * @param key key of updated object + * @param compound compound of updated object + */ + default void onDataUpdated(NBTTagCompound compound, String key) { + } + + /** + * When a controller's object gets saved on server side, the updated object is synced to the client + * @param id id of updated object + * @param compound compound of updated object + */ + default void onDataUpdated(NBTTagCompound compound, int id) { + } + + /** + * When a controller's object gets deleted on server side, the deleted object is removed from the client + * @param key key of removed object + */ + default void onDataRemoved(String key) { + } + + /** + * When a controller's object gets deleted on server side, the deleted object is removed from the client + * @param id id of removed object + */ + default void onDataRemoved(int id) { + } + + // ═══════════════════════════════════════════════════════════════════════ + // Static dispatchers + // ═══════════════════════════════════════════════════════════════════════ + + static void legacySetGuiData(NBTTagCompound nbt) { + GuiScreen gui = activeGui(); + if (gui instanceof IGuiData) + ((IGuiData) gui).setGuiData(nbt); + } + + static void notifyUpdate(NBTTagCompound nbt, String key, int id) { + GuiScreen gui = activeGui(); + if (gui instanceof IGuiData) { + ((IGuiData) gui).onDataUpdated(nbt); + ((IGuiData) gui).onDataUpdated(nbt, key); + ((IGuiData) gui).onDataUpdated(nbt, id); + } + } + + static void notifyRemove(String key, int id) { + GuiScreen gui = activeGui(); + if (gui instanceof IGuiData) { + ((IGuiData) gui).onDataRemoved(key); + ((IGuiData) gui).onDataRemoved(id); + } + } + + // ═══════════════════════════════════════════════════════════════════════ + // Active Gui fetcher + // ═══════════════════════════════════════════════════════════════════════ + + static GuiScreen activeGui() { + GuiScreen gui = Minecraft.getMinecraft().currentScreen; + if (gui instanceof GuiNPCInterface && ((GuiNPCInterface) gui).hasSubGui()) + return ((GuiNPCInterface) gui).getSubGui(); + if (gui instanceof GuiContainerNPCInterface && ((GuiContainerNPCInterface) gui).hasSubGui()) + return ((GuiContainerNPCInterface) gui).getSubGui(); + return gui; + } }