-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* push everything * fix mixin errors on fabric client * fix mixin errors on fabric client 2 * change tab to 4 spaces * Revert "change tab to 4 spaces" I think they use tabs This reverts commit a30576a. * Reapply "change tab to 4 spaces" Because Adubbz prefers spaces * try to adapt code style of Adubbz * trying to adapt code style of Adubbz * minimize the number of changes * minimize the number of changes 2 * minimize the number of changes 3 * minimize the number of changes 4 * minimize the number of changes 5 * minimize the number of changes 6
- Loading branch information
1 parent
5609ec2
commit ad7f28e
Showing
16 changed files
with
199 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,44 @@ | ||
package glitchcore.config; | ||
|
||
import glitchcore.core.GlitchCore; | ||
import glitchcore.network.PacketHandler; | ||
import glitchcore.network.SyncConfigPacket; | ||
import glitchcore.util.Environment; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import net.minecraft.resources.ResourceLocation; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class ConfigSync { | ||
private static final ResourceLocation CONFIG_SYNC_CHANNEL = new ResourceLocation("glitchcorefabric", "config_sync"); | ||
private static final Map<String, Config> CONFIGS_BY_PATH = new HashMap<>(); | ||
private static boolean inited = false; | ||
/** | ||
* Enables sync between server and client for your config. | ||
* NOTE: This function works only on fabric, you need to implement it yourself | ||
* if you want to use it on forge | ||
* | ||
* @param config your config. | ||
*/ | ||
public static void register(Config config) { | ||
if (!inited) { | ||
initSyncs(); | ||
inited = true; | ||
private static ResourceLocation configSyncChannel; | ||
public static PacketHandler packetHandler; | ||
public static final Map<String, Config> CONFIGS_BY_PATH = new HashMap<>(); | ||
private static AtomicBoolean inited = new AtomicBoolean(); | ||
|
||
/** | ||
* Enables sync between server and client for your config. | ||
* | ||
* @param config your config. | ||
*/ | ||
public static void register(Config config) { | ||
if (inited.compareAndSet(false, true)) | ||
{ | ||
configSyncChannel = new ResourceLocation(GlitchCore.MOD_ID, "config_sync"); | ||
packetHandler = new PacketHandler(configSyncChannel); | ||
packetHandler.register(new ResourceLocation(GlitchCore.MOD_ID, "config_sync_packet"), | ||
new SyncConfigPacket()); | ||
initFabric(); | ||
} | ||
String relative = Environment.getConfigPath().relativize(config.getPath()).toString(); | ||
CONFIGS_BY_PATH.put(relative, config); | ||
} | ||
|
||
private static void initFabric() {} | ||
|
||
public static void reload(String path, String toml) { | ||
var config = CONFIGS_BY_PATH.get(path); | ||
config.parse(toml); | ||
config.load(); | ||
} | ||
String relative = Environment.getConfigPath().relativize(config.getPath()).toString(); | ||
CONFIGS_BY_PATH.put(relative, config); | ||
} | ||
private static void initSyncs() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
private static void reload(String path, String toml) { | ||
var config = CONFIGS_BY_PATH.get(path); | ||
config.parse(toml); | ||
config.load(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
common/src/main/java/glitchcore/network/SyncConfigPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/******************************************************************************* | ||
* Copyright 2023, the Glitchfiend Team. | ||
* All rights reserved. | ||
******************************************************************************/ | ||
package glitchcore.network; | ||
|
||
import glitchcore.config.ConfigSync; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class SyncConfigPacket implements CustomPacket<SyncConfigPacket> | ||
{ | ||
private String path; | ||
private byte[] data; | ||
|
||
public SyncConfigPacket(String path, byte[] data) | ||
{ | ||
this.path = path; | ||
this.data = data; | ||
} | ||
|
||
public SyncConfigPacket() {} | ||
|
||
@Override | ||
public void encode(FriendlyByteBuf buf) | ||
{ | ||
buf.writeUtf(this.path); | ||
buf.writeByteArray(this.data); | ||
} | ||
|
||
@Override | ||
public SyncConfigPacket decode(FriendlyByteBuf buf) | ||
{ | ||
return new SyncConfigPacket(buf.readUtf(), buf.readByteArray()); | ||
} | ||
|
||
@Override | ||
public void handle(SyncConfigPacket data, Context context) | ||
{ | ||
if (context.isServerSide()) | ||
return; | ||
|
||
ConfigSync.reload(data.path, new String(data.data, StandardCharsets.UTF_8)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
forge/src/main/java/glitchcore/forge/handlers/PlayerLoggedInEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package glitchcore.forge.handlers; | ||
|
||
import glitchcore.config.ConfigSync; | ||
import glitchcore.network.SyncConfigPacket; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraftforge.event.entity.player.PlayerEvent; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE) | ||
public class PlayerLoggedInEventHandler | ||
{ | ||
|
||
@SubscribeEvent | ||
public static void onJoin(PlayerEvent.PlayerLoggedInEvent e) | ||
{ | ||
if (ConfigSync.CONFIGS_BY_PATH.isEmpty()) return; | ||
ConfigSync.CONFIGS_BY_PATH.forEach((path, config) -> { | ||
ConfigSync.packetHandler.sendToPlayer(new SyncConfigPacket(path, | ||
config.encode().getBytes(StandardCharsets.UTF_8)), (ServerPlayer) e.getEntity()); | ||
}); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
forge/src/main/java/glitchcore/forge/handlers/PlayerQuitClientEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package glitchcore.forge.handlers; | ||
|
||
import glitchcore.config.Config; | ||
import glitchcore.config.ConfigSync; | ||
import glitchcore.util.Environment; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.client.event.ClientPlayerNetworkEvent; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
|
||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT) | ||
public class PlayerQuitClientEventHandler | ||
{ | ||
@SubscribeEvent | ||
public static void onQuit(ClientPlayerNetworkEvent.LoggingOut e) | ||
{ | ||
if (!ConfigSync.CONFIGS_BY_PATH.isEmpty()) | ||
{ | ||
ConfigSync.CONFIGS_BY_PATH.forEach((path, config) -> { | ||
config.parse(Config.readToml(Environment.getConfigPath().resolve(path))); | ||
config.load(); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.