From df460264579b3acab6d7314affa4b8c731cf3d58 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 9 Mar 2023 18:57:09 +0100 Subject: [PATCH] Improves Json util class: ability to add TypeAdapterFactory on the fly. --- .../bungee/backup/BungeeWorkdirProcess.java | 6 +- .../commands/BungeeBrigadierDispatcher.java | 5 +- .../java/fr/pandacube/lib/core/json/Json.java | 61 ++++++++++++++++--- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java index 74b7007..3bdfd6e 100644 --- a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java @@ -1,12 +1,8 @@ package fr.pandacube.lib.bungee.backup; import fr.pandacube.lib.core.backup.BackupProcess; -import fr.pandacube.lib.util.Log; -import net.md_5.bungee.api.ChatColor; import java.io.File; -import java.text.DateFormat; -import java.util.Date; import java.util.function.BiPredicate; public class BungeeWorkdirProcess extends BackupProcess { @@ -22,7 +18,7 @@ public class BungeeWorkdirProcess extends BackupProcess { public BiPredicate getFilenameFilter() { - return new BiPredicate() { + return new BiPredicate<>() { @Override public boolean test(File file, String path) { if (new File(getSourceDir(), "logs").equals(file)) diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/commands/BungeeBrigadierDispatcher.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/commands/BungeeBrigadierDispatcher.java index 0c08416..40845ab 100644 --- a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/commands/BungeeBrigadierDispatcher.java +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/commands/BungeeBrigadierDispatcher.java @@ -1,6 +1,5 @@ package fr.pandacube.lib.bungee.commands; -import fr.pandacube.lib.bungee.PandaLibBungee; import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.commands.BrigadierDispatcher; import net.kyori.adventure.text.ComponentLike; @@ -21,6 +20,10 @@ public class BungeeBrigadierDispatcher extends BrigadierDispatcher b.serializeNulls().setPrettyPrinting()); @@ -52,8 +59,41 @@ public class Json { private static Gson build(Function builderModifier) { - return builderModifier - .apply(new GsonBuilder().registerTypeAdapterFactory(new RecordAdapterFactory()).setLenient()).create(); + GsonBuilder base = new GsonBuilder() + .registerTypeAdapterFactory(new CustomAdapterFactory()) + .setLenient(); + return builderModifier.apply(base).create(); + } + + + /** + * Adds the provided {@link TypeAdapterFactory} to all the static Gson instances of this class. + * @param factory the factory to add to the + */ + public static void registerTypeAdapterFactory(TypeAdapterFactory factory) { + synchronized (customTypeAdapterFactories) { + customTypeAdapterFactories.add(factory); + } + } + + + + private static final List customTypeAdapterFactories = new ArrayList<>(); + + + + private static class CustomAdapterFactory implements TypeAdapterFactory { + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + synchronized (customTypeAdapterFactories) { + for (TypeAdapterFactory actualFactory : customTypeAdapterFactories) { + TypeAdapter adapter = actualFactory.create(gson, type); + if (adapter != null) + return adapter; + } + } + return null; + } } @@ -62,6 +102,9 @@ public class Json { + static { + registerTypeAdapterFactory(new RecordAdapterFactory()); + } // from https://github.com/google/gson/issues/1794#issuecomment-812964421 private static class RecordAdapterFactory implements TypeAdapterFactory {