Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08824ba045 |
+4
-4
@@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx2G
|
||||
|
||||
# Fabric Properties
|
||||
# check these on https://fabricmc.net/versions.html
|
||||
minecraft_version=1.20.1
|
||||
yarn_mappings=1.20.1+build.10
|
||||
minecraft_version=1.20.6
|
||||
yarn_mappings=1.20.6+build.3
|
||||
loader_version=0.15.11
|
||||
|
||||
# Fabric API
|
||||
fabric_version=0.92.2+1.20.1
|
||||
fabric_version=0.100.2+1.20.6
|
||||
|
||||
# Mod Properties
|
||||
mod_version=1.4.1
|
||||
mod_version=1.4.2
|
||||
maven_group=dev.draylar
|
||||
archives_base_name=omega-config
|
||||
modmenu_version=7.1.0
|
||||
@@ -5,14 +5,16 @@ import com.google.gson.GsonBuilder;
|
||||
import draylar.omegaconfig.api.Comment;
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfig.gson.SyncableExclusionStrategy;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.nbt.NbtList;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.network.codec.PacketCodec;
|
||||
import net.minecraft.network.codec.PacketCodecs;
|
||||
import net.minecraft.network.packet.CustomPayload;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -24,45 +26,15 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.*;
|
||||
|
||||
public class OmegaConfig implements ModInitializer {
|
||||
|
||||
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
public static final Identifier CONFIG_SYNC_PACKET = new Identifier("omegaconfig", "sync");
|
||||
public static final Gson SYNC_ONLY_GSON = new GsonBuilder().addSerializationExclusionStrategy(new SyncableExclusionStrategy()).setPrettyPrinting().create();
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final List<Config> REGISTERED_CONFIGURATIONS = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> server.execute(() -> {
|
||||
PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer());
|
||||
|
||||
// list of configurations that are synced
|
||||
NbtCompound root = new NbtCompound();
|
||||
NbtList configurations = new NbtList();
|
||||
|
||||
// Iterate over each configuration.
|
||||
// Find values that should be synced and send the value over.
|
||||
OmegaConfig.getRegisteredConfigurations().forEach(config -> {
|
||||
if (config.hasAnySyncable()) {
|
||||
configurations.add(config.writeSyncingTag());
|
||||
}
|
||||
});
|
||||
|
||||
// save to packet and send to user
|
||||
root.put("Configurations", configurations);
|
||||
packet.writeNbt(root);
|
||||
handler.sendPacket(ServerPlayNetworking.createS2CPacket(CONFIG_SYNC_PACKET, packet));
|
||||
}));
|
||||
}
|
||||
|
||||
public static <T extends Config> T register(Class<T> configClass) {
|
||||
try {
|
||||
// Attempt to instantiate a new instance of this class.
|
||||
@@ -96,7 +68,8 @@ public class OmegaConfig implements ModInitializer {
|
||||
}
|
||||
|
||||
return config;
|
||||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) {
|
||||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException exception) {
|
||||
exception.printStackTrace();
|
||||
throw new RuntimeException("No valid constructor found for: " + configClass.getName());
|
||||
}
|
||||
@@ -179,10 +152,8 @@ public class OmegaConfig implements ModInitializer {
|
||||
|
||||
Class<?>[] classes = aClass.getDeclaredClasses();
|
||||
|
||||
if (classes.length != 0) {
|
||||
for (Class<?> clazz : classes) {
|
||||
populateRecursively(list, clazz);
|
||||
}
|
||||
for (Class<?> clazz : classes) {
|
||||
populateRecursively(list, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,4 +212,38 @@ public class OmegaConfig implements ModInitializer {
|
||||
public static List<Config> getRegisteredConfigurations() {
|
||||
return REGISTERED_CONFIGURATIONS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
PayloadTypeRegistry.playS2C().register(OmegaConfig.SyncConfigPayload.ID, OmegaConfig.SyncConfigPayload.CODEC);
|
||||
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> server.execute(() -> {
|
||||
// list of configurations that are synced
|
||||
NbtCompound root = new NbtCompound();
|
||||
NbtList configurations = new NbtList();
|
||||
|
||||
// Iterate over each configuration.
|
||||
// Find values that should be synced and send the value over.
|
||||
OmegaConfig.getRegisteredConfigurations().forEach(config -> {
|
||||
if (config.hasAnySyncable()) {
|
||||
configurations.add(config.writeSyncingTag());
|
||||
}
|
||||
});
|
||||
|
||||
// save to packet and send to user
|
||||
root.put("Configurations", configurations);
|
||||
sender.sendPacket(new SyncConfigPayload(root));
|
||||
}));
|
||||
}
|
||||
|
||||
public record SyncConfigPayload(NbtCompound nbtCompound) implements CustomPayload {
|
||||
public static final Identifier CONFIG_SYNC_PACKET = new Identifier("omegaconfig", "sync");
|
||||
public static final CustomPayload.Id<SyncConfigPayload> ID = new Id<>(CONFIG_SYNC_PACKET);
|
||||
public static final PacketCodec<PacketByteBuf, SyncConfigPayload> CODEC = PacketCodecs.NBT_COMPOUND.xmap(SyncConfigPayload::new, SyncConfigPayload::nbtCompound).cast();
|
||||
|
||||
@Override
|
||||
public Id<? extends CustomPayload> getId() {
|
||||
return ID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,12 @@ public class ClientMixin {
|
||||
method = "<init>",
|
||||
at = @At("RETURN"))
|
||||
private void onReturn(RunArgs args, CallbackInfo ci) {
|
||||
ClientPlayNetworking.registerGlobalReceiver(OmegaConfig.CONFIG_SYNC_PACKET, (client, handler, buf, responseSender) -> {
|
||||
NbtCompound tag = buf.readNbt();
|
||||
ClientPlayNetworking.registerGlobalReceiver(OmegaConfig.SyncConfigPayload.ID, (payload, context) -> {
|
||||
|
||||
NbtCompound tag = payload.nbtCompound();
|
||||
savedClientConfig.clear();
|
||||
|
||||
client.execute(() -> {
|
||||
context.client().execute(() -> {
|
||||
if (tag != null && tag.contains("Configurations")) {
|
||||
NbtList list = tag.getList("Configurations", NbtType.COMPOUND);
|
||||
list.forEach(compound -> {
|
||||
|
||||
Reference in New Issue
Block a user