Skip to content

Commit

Permalink
Expand syncing capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Oct 1, 2016
1 parent e328399 commit 339941c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AbstractConfigSyncPacket() {

@Override
public IMessage handleClient(NetHandlerPlayClient netHandler) {
AbstractConfig.syncConfig(getConfig(), config);
sync();
return null;
}

Expand All @@ -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<>();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/slimeknights/mantle/config/ExampleSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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;
}
}
}

0 comments on commit 339941c

Please sign in to comment.