Port to java 16 and MC 1.17
This commit is contained in:
@@ -10,8 +10,8 @@ import io.netty.buffer.Unpooled;
|
||||
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.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.nbt.NbtList;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -28,36 +28,33 @@ import java.util.*;
|
||||
|
||||
public class OmegaConfig {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final List<Config> REGISTERED_CONFIGURATIONS = new ArrayList<>();
|
||||
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();
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final List<Config> REGISTERED_CONFIGURATIONS = new ArrayList<>();
|
||||
|
||||
static {
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||
server.execute(() -> {
|
||||
PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer());
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> server.execute(() -> {
|
||||
PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer());
|
||||
|
||||
// list of configurations that are synced
|
||||
CompoundTag root = new CompoundTag();
|
||||
ListTag configurations = new ListTag();
|
||||
// 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.writeCompoundTag(root);
|
||||
handler.sendPacket(ServerPlayNetworking.createS2CPacket(CONFIG_SYNC_PACKET, packet));
|
||||
// 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) {
|
||||
@@ -69,7 +66,7 @@ public class OmegaConfig {
|
||||
// We want to provide access to the config as soon as it is created, so we:
|
||||
// 1. serialize to disk if the config does not already exist
|
||||
// 2. read from disk if it does exist
|
||||
if(!configExists(config)) {
|
||||
if (!configExists(config)) {
|
||||
writeConfig(configClass, config);
|
||||
REGISTERED_CONFIGURATIONS.add(config);
|
||||
} else {
|
||||
@@ -99,7 +96,7 @@ public class OmegaConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends Config> void writeConfig(Class<T> configClass, T instance) {
|
||||
public static <T extends Config> void writeConfig(Class<T> configClass, T instance) {
|
||||
// Write the config to disk with the default values.
|
||||
String json = GSON.toJson(instance);
|
||||
|
||||
@@ -109,14 +106,14 @@ public class OmegaConfig {
|
||||
Map<String, String> keyToComments = new HashMap<>();
|
||||
|
||||
// populate key -> comments map
|
||||
for(Field field : configClass.getDeclaredFields()) {
|
||||
for (Field field : configClass.getDeclaredFields()) {
|
||||
addFieldComments(field, keyToComments);
|
||||
}
|
||||
|
||||
// get inner-class fields
|
||||
// TODO: recursively get inner classes?
|
||||
for(Class<?> innerClass : configClass.getDeclaredClasses()) {
|
||||
for(Field field : innerClass.getDeclaredFields()) {
|
||||
for (Class<?> innerClass : configClass.getDeclaredClasses()) {
|
||||
for (Field field : innerClass.getDeclaredFields()) {
|
||||
addFieldComments(field, keyToComments);
|
||||
}
|
||||
}
|
||||
@@ -154,7 +151,7 @@ public class OmegaConfig {
|
||||
Files.write(configPath, res.toString().getBytes());
|
||||
} catch (IOException ioException) {
|
||||
LOGGER.error(ioException);
|
||||
LOGGER.info(String.format("Write error, using default values for config %s.", configClass.toString()));
|
||||
LOGGER.info(String.format("Write error, using default values for config %s.", configClass));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +161,7 @@ public class OmegaConfig {
|
||||
|
||||
// Find comment
|
||||
for (Annotation annotation : annotations) {
|
||||
if(annotation instanceof Comment) {
|
||||
if (annotation instanceof Comment) {
|
||||
keyToComments.put(fieldName, ((Comment) annotation).value());
|
||||
break;
|
||||
}
|
||||
@@ -179,23 +176,23 @@ public class OmegaConfig {
|
||||
* "p" -> ""
|
||||
* " p" -> " "
|
||||
*
|
||||
* @param input input to retrieve whitespaces from
|
||||
* @return starting whitespaces from the given input
|
||||
* @param input input to retrieve whitespaces from
|
||||
* @return starting whitespaces from the given input
|
||||
*/
|
||||
private static String getStartingWhitespace(String input) {
|
||||
int index = -1;
|
||||
|
||||
char[] chars = input.toCharArray();
|
||||
for(int i = 0; i < chars.length; i++) {
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char at = chars[i];
|
||||
|
||||
if(at != ' ') {
|
||||
if (at != ' ') {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(index != -1) {
|
||||
if (index != -1) {
|
||||
return input.substring(0, index);
|
||||
} else {
|
||||
return "";
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.lang.annotation.Target;
|
||||
*
|
||||
* <p>
|
||||
* When a configuration is serialized, any field elements with the {@link Comment} annotation
|
||||
* will be prefixed with a // comment on the previous line, with the value specified by this annotation.
|
||||
* will be prefixed with a // comment on the previous line, with the value specified by this annotation.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package draylar.omegaconfig.api;
|
||||
|
||||
import draylar.omegaconfig.OmegaConfig;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -16,12 +16,12 @@ public interface Config {
|
||||
OmegaConfig.writeConfig((Class<Config>) getClass(), this);
|
||||
}
|
||||
|
||||
default CompoundTag writeSyncingTag() {
|
||||
CompoundTag tag = new CompoundTag();
|
||||
default NbtCompound writeSyncingTag() {
|
||||
NbtCompound tag = new NbtCompound();
|
||||
tag.putString("ConfigName", getName());
|
||||
|
||||
// all config vs. individual fields
|
||||
if(Arrays.stream(getClass().getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
if (Arrays.stream(getClass().getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
// write ALL fields to tag
|
||||
String json = OmegaConfig.GSON.toJson(this);
|
||||
tag.putString("Serialized", json);
|
||||
@@ -37,7 +37,7 @@ public interface Config {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this {@link Config} has any values that should be synced to the client
|
||||
* @return true if this {@link Config} has any values that should be synced to the client
|
||||
*/
|
||||
default boolean hasAnySyncable() {
|
||||
boolean hasSyncingField = Arrays.stream(getClass().getDeclaredFields()).anyMatch(field -> Arrays.stream(field.getDeclaredAnnotations()).anyMatch(annotation -> annotation instanceof Syncing));
|
||||
@@ -55,7 +55,7 @@ public interface Config {
|
||||
* <li>No special characters ($, %, ^, etc.)
|
||||
* <li>No spaces
|
||||
*
|
||||
* @return the name of this config, which is used for the name of the config file saved to disk.
|
||||
* @return the name of this config, which is used for the name of the config file saved to disk.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
@@ -64,14 +64,14 @@ public interface Config {
|
||||
*
|
||||
* <p>
|
||||
* This functionality is used for libraries like ModMenu, which depend on
|
||||
* modids for configuration screen instances in their menu.
|
||||
* modids for configuration screen instances in their menu.
|
||||
* If you are intending for this config to have a ModMenu config
|
||||
* screen, this method should return the modid specified in your fabric.mod.json.
|
||||
*
|
||||
* screen, this method should return the modid specified in your fabric.mod.json.
|
||||
* <p>
|
||||
* If this method is not overridden, null will be returned, which
|
||||
* means this config is not explicitly associated with any particular mod.
|
||||
* means this config is not explicitly associated with any particular mod.
|
||||
*
|
||||
* @return the modid of the mod associated with this config, or null if none was specified
|
||||
* @return the modid of the mod associated with this config, or null if none was specified
|
||||
*/
|
||||
@Nullable
|
||||
default String getModid() {
|
||||
@@ -84,9 +84,9 @@ public interface Config {
|
||||
* <p>
|
||||
* The file extension is used while serializing this config to a local file.
|
||||
* The primary use-case of switching this would be supporting existing config files
|
||||
* when porting from other json5 config libraries.
|
||||
* when porting from other json5 config libraries.
|
||||
*
|
||||
* @return the file extension of this config
|
||||
* @return the file extension of this config
|
||||
*/
|
||||
default String getExtension() {
|
||||
return "json";
|
||||
@@ -98,11 +98,11 @@ public interface Config {
|
||||
* <p>
|
||||
* By default, a config such as 'my_config' will appear at /config/my_config.json.
|
||||
* If this method specifies a directory, such as 'configurations',
|
||||
* the config file will appear at /config/configurations/my_config.json.
|
||||
*
|
||||
* the config file will appear at /config/configurations/my_config.json.
|
||||
* <p>
|
||||
* Nested directories can be specified by using a string such as 'configurations/client'.
|
||||
*
|
||||
* @return the directory of this config
|
||||
* @return the directory of this config
|
||||
*/
|
||||
default String getDirectory() {
|
||||
return "";
|
||||
|
||||
@@ -3,13 +3,12 @@ package draylar.omegaconfig.mixin;
|
||||
import draylar.omegaconfig.OmegaConfig;
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfig.api.Syncing;
|
||||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.fabricmc.fabric.api.util.NbtType;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.RunArgs;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.nbt.NbtList;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
@@ -29,17 +28,17 @@ public class ClientMixin {
|
||||
|
||||
@Inject(
|
||||
method = "<init>",
|
||||
at = @At("RETURN"))
|
||||
at = @At("RETURN"))
|
||||
private void onReturn(RunArgs args, CallbackInfo ci) {
|
||||
ClientSidePacketRegistry.INSTANCE.register(OmegaConfig.CONFIG_SYNC_PACKET, (context, buffer) -> {
|
||||
CompoundTag tag = buffer.readCompoundTag();
|
||||
ClientPlayNetworking.registerGlobalReceiver(OmegaConfig.CONFIG_SYNC_PACKET, (client, handler, buf, responseSender) -> {
|
||||
NbtCompound tag = buf.readNbt();
|
||||
savedClientConfig.clear();
|
||||
|
||||
context.getTaskQueue().execute(() -> {
|
||||
if(tag != null && tag.contains("Configurations")) {
|
||||
ListTag list = tag.getList("Configurations", NbtType.COMPOUND);
|
||||
client.execute(() -> {
|
||||
if (tag != null && tag.contains("Configurations")) {
|
||||
NbtList list = tag.getList("Configurations", NbtType.COMPOUND);
|
||||
list.forEach(compound -> {
|
||||
CompoundTag syncedConfiguration = (CompoundTag) compound;
|
||||
NbtCompound syncedConfiguration = (NbtCompound) compound;
|
||||
String name = syncedConfiguration.getString("ConfigName");
|
||||
String json = syncedConfiguration.getString("Serialized");
|
||||
boolean allSync = syncedConfiguration.getBoolean("AllSync");
|
||||
@@ -56,7 +55,7 @@ public class ClientMixin {
|
||||
|
||||
// locate all fields that differ between the client and server, assign values from server to client (this will mutate the stored object)
|
||||
for (Field field : server.getClass().getDeclaredFields()) {
|
||||
if(allSync || Arrays.stream(field.getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
if (allSync || Arrays.stream(field.getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object serverValue = field.get(server);
|
||||
@@ -79,15 +78,15 @@ public class ClientMixin {
|
||||
@Inject(
|
||||
method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;)V",
|
||||
at = @At("RETURN"))
|
||||
private void restoreConfigurations(Screen screen, CallbackInfo ci) {
|
||||
for(Config config : savedClientConfig) {
|
||||
for(Config potentiallySynced : OmegaConfig.getRegisteredConfigurations()) {
|
||||
if(config.getName().equals(potentiallySynced.getName())) {
|
||||
private void restoreConfigurations(CallbackInfo ci) {
|
||||
for (Config config : savedClientConfig) {
|
||||
for (Config potentiallySynced : OmegaConfig.getRegisteredConfigurations()) {
|
||||
if (config.getName().equals(potentiallySynced.getName())) {
|
||||
boolean allConfigSyncs = Arrays.stream(config.getClass().getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing);
|
||||
|
||||
// mutate object in registered configurations
|
||||
for (Field field : config.getClass().getDeclaredFields()) {
|
||||
if(allConfigSyncs || Arrays.stream(field.getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
if (allConfigSyncs || Arrays.stream(field.getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object preSyncValue = field.get(config);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
"package": "draylar.omegaconfig.mixin",
|
||||
"compatibilityLevel": "JAVA_8",
|
||||
"mixins": [
|
||||
|
||||
],
|
||||
"client": [
|
||||
"ClientMixin"
|
||||
|
||||
Reference in New Issue
Block a user