From 339941cac2651722335f2d0bf6fca40304657ca1 Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Sat, 1 Oct 2016 20:59:18 +0200 Subject: [PATCH] Expand syncing capabilities --- .../config/AbstractConfigSyncPacket.java | 7 ++++++- .../mantle/config/ExampleSync.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/slimeknights/mantle/config/AbstractConfigSyncPacket.java b/src/main/java/slimeknights/mantle/config/AbstractConfigSyncPacket.java index 74e619d4..d33c0858 100644 --- a/src/main/java/slimeknights/mantle/config/AbstractConfigSyncPacket.java +++ b/src/main/java/slimeknights/mantle/config/AbstractConfigSyncPacket.java @@ -21,7 +21,7 @@ public AbstractConfigSyncPacket() { @Override public IMessage handleClient(NetHandlerPlayClient netHandler) { - AbstractConfig.syncConfig(getConfig(), config); + sync(); return null; } @@ -31,6 +31,11 @@ public IMessage handleServer(NetHandlerPlayServer netHandler) { throw new UnsupportedOperationException("Trying to sync client configs to the server. You registered the packet for the wrong side."); } + protected boolean sync() { + return AbstractConfig.syncConfig(getConfig(), config); + } + + @Override public void fromBytes(ByteBuf buf) { config = new ArrayList<>(); diff --git a/src/main/java/slimeknights/mantle/config/ExampleSync.java b/src/main/java/slimeknights/mantle/config/ExampleSync.java index f6a74fe3..40007c86 100644 --- a/src/main/java/slimeknights/mantle/config/ExampleSync.java +++ b/src/main/java/slimeknights/mantle/config/ExampleSync.java @@ -6,6 +6,7 @@ import net.minecraft.util.text.TextComponentString; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent; @@ -20,10 +21,12 @@ class ExampleSync { static NetworkWrapper networkWrapper; + static ExampleSync INSTANCE; static void setup() { networkWrapper = new NetworkWrapper("mantle:example"); networkWrapper.registerPacketClient(ExampleSyncPacketImpl.class); + INSTANCE = new ExampleSync(); } @SideOnly(Side.CLIENT) @@ -59,6 +62,11 @@ static class ExampleConfig extends AbstractConfig { // call from preinit or something public void onPreInit(FMLPreInitializationEvent event) { exampleConfigFile = this.load(new ExampleConfigFile(event.getModConfigurationDirectory()), ExampleConfigFile.class); + + // register this serverside to sync + if(event.getSide().isServer()) { + MinecraftForge.EVENT_BUS.register(INSTANCE); + } } } @@ -80,5 +88,15 @@ static class ExampleSyncPacketImpl extends AbstractConfigSyncPacket { protected AbstractConfig getConfig() { return ExampleConfig.INSTANCE; } + + @Override + protected boolean sync() { + if(super.sync()) { + // clientside register only + MinecraftForge.EVENT_BUS.register(INSTANCE); + return true; + } + return false; + } } }