mirror of
https://github.com/sciwhiz12/Janitor.git
synced 2024-11-09 22:51:26 +00:00
Move from GSON to Jackson, cleanup code warnings, move to javax.annotation annotations
This commit is contained in:
parent
8e972ba96a
commit
44a55d3962
|
@ -35,7 +35,9 @@ dependencies {
|
||||||
implementation group: 'com.electronwill.night-config', name: 'toml', version: nightconfig_version
|
implementation group: 'com.electronwill.night-config', name: 'toml', version: nightconfig_version
|
||||||
implementation group: 'net.sf.jopt-simple', name: 'jopt-simple', version: jopt_version
|
implementation group: 'net.sf.jopt-simple', name: 'jopt-simple', version: jopt_version
|
||||||
implementation group: 'com.google.guava', name: 'guava', version: guava_version
|
implementation group: 'com.google.guava', name: 'guava', version: guava_version
|
||||||
implementation group: 'com.google.code.gson', name: 'gson', version: gson_version
|
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: jackson_version
|
||||||
|
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: jackson_version
|
||||||
|
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jackson_version
|
||||||
implementation group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: log4j_bridge_version
|
implementation group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: log4j_bridge_version
|
||||||
implementation group: 'ch.qos.logback', name: 'logback-classic', version: logback_version
|
implementation group: 'ch.qos.logback', name: 'logback-classic', version: logback_version
|
||||||
implementation group: 'com.mojang', name: 'brigadier', version: brigadier_version
|
implementation group: 'com.mojang', name: 'brigadier', version: brigadier_version
|
||||||
|
|
|
@ -5,7 +5,7 @@ jda_version=4.2.0_207
|
||||||
nightconfig_version=3.6.3
|
nightconfig_version=3.6.3
|
||||||
jopt_version=6.0-alpha-3
|
jopt_version=6.0-alpha-3
|
||||||
guava_version=29.0-jre
|
guava_version=29.0-jre
|
||||||
gson_version=2.8.6
|
jackson_version=2.11.2
|
||||||
log4j_bridge_version=2.13.3
|
log4j_bridge_version=2.13.3
|
||||||
logback_version=1.3.0-alpha5
|
logback_version=1.3.0-alpha5
|
||||||
brigadier_version=1.0.17
|
brigadier_version=1.0.17
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package sciwhiz12.janitor;
|
package sciwhiz12.janitor;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.OnlineStatus;
|
import net.dv8tion.jda.api.OnlineStatus;
|
||||||
import net.dv8tion.jda.api.entities.Activity;
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
|
@ -12,7 +13,6 @@ import sciwhiz12.janitor.config.BotOptions;
|
||||||
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
|
||||||
import static sciwhiz12.janitor.Logging.JANITOR;
|
import static sciwhiz12.janitor.Logging.JANITOR;
|
||||||
|
|
||||||
public class BotStartup {
|
public class BotStartup {
|
||||||
|
@ -21,7 +21,7 @@ public class BotStartup {
|
||||||
|
|
||||||
BotOptions options = new BotOptions(args);
|
BotOptions options = new BotOptions(args);
|
||||||
BotConfig config = new BotConfig(options);
|
BotConfig config = new BotConfig(options);
|
||||||
checkArgument(!config.getToken().isEmpty(), "Supply a client token through config or command line");
|
Preconditions.checkArgument(!config.getToken().isEmpty(), "Supply a client token through config or command line");
|
||||||
|
|
||||||
JANITOR.info("Building bot instance and connecting to Discord...");
|
JANITOR.info("Building bot instance and connecting to Discord...");
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package sciwhiz12.janitor;
|
package sciwhiz12.janitor;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import sciwhiz12.janitor.storage.IStorage;
|
import sciwhiz12.janitor.storage.IStorage;
|
||||||
|
|
||||||
|
@ -19,8 +17,6 @@ import java.util.function.Supplier;
|
||||||
import static java.nio.file.StandardOpenOption.*;
|
import static java.nio.file.StandardOpenOption.*;
|
||||||
|
|
||||||
public class GuildStorage {
|
public class GuildStorage {
|
||||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
|
|
||||||
|
|
||||||
private final JanitorBot bot;
|
private final JanitorBot bot;
|
||||||
private final Path mainFolder;
|
private final Path mainFolder;
|
||||||
private final Map<Guild, Map<String, IStorage>> guildStorage = new IdentityHashMap<>();
|
private final Map<Guild, Map<String, IStorage>> guildStorage = new IdentityHashMap<>();
|
||||||
|
@ -35,9 +31,9 @@ public class GuildStorage {
|
||||||
return bot;
|
return bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends IStorage> T getOrCreate(Guild guild, String key, Supplier<T> defaultSupplier) {
|
public <T extends IStorage> T getOrCreate(Guild guild, String key, Supplier<T> defaultSupplier) {
|
||||||
final Map<String, IStorage> storageMap = guildStorage.computeIfAbsent(guild, g -> new HashMap<>());
|
final Map<String, IStorage> storageMap = guildStorage.computeIfAbsent(guild, g -> new HashMap<>());
|
||||||
//noinspection unchecked
|
|
||||||
return (T) storageMap.computeIfAbsent(key, k -> load(guild, key, defaultSupplier.get()));
|
return (T) storageMap.computeIfAbsent(key, k -> load(guild, key, defaultSupplier.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.commands.BaseCommand;
|
import sciwhiz12.janitor.commands.BaseCommand;
|
||||||
import sciwhiz12.janitor.commands.CommandRegistry;
|
import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.commands.util.ModerationHelper;
|
import sciwhiz12.janitor.commands.util.ModerationHelper;
|
||||||
|
@ -16,6 +15,7 @@ import sciwhiz12.janitor.commands.util.ModerationHelper;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||||
|
|
|
@ -8,7 +8,6 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.commands.BaseCommand;
|
import sciwhiz12.janitor.commands.BaseCommand;
|
||||||
import sciwhiz12.janitor.commands.CommandRegistry;
|
import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.commands.util.CommandHelper;
|
import sciwhiz12.janitor.commands.util.CommandHelper;
|
||||||
|
@ -17,6 +16,7 @@ import sciwhiz12.janitor.commands.util.ModerationHelper;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
|
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
|
||||||
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
|
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
|
||||||
|
|
|
@ -9,7 +9,6 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.commands.BaseCommand;
|
import sciwhiz12.janitor.commands.BaseCommand;
|
||||||
import sciwhiz12.janitor.commands.CommandRegistry;
|
import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.moderation.notes.NoteEntry;
|
import sciwhiz12.janitor.moderation.notes.NoteEntry;
|
||||||
|
@ -22,6 +21,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||||
|
@ -160,10 +160,9 @@ public class NoteCommand extends BaseCommand {
|
||||||
case PERFORMER: {
|
case PERFORMER: {
|
||||||
predicate = predicate.and(e -> e.getValue().getPerformer().getIdLong() == performer.getIdLong());
|
predicate = predicate.and(e -> e.getValue().getPerformer().getIdLong() == performer.getIdLong());
|
||||||
}
|
}
|
||||||
|
case NONE: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.now();
|
|
||||||
|
|
||||||
if (!performer.hasPermission(NOTE_PERMISSION))
|
if (!performer.hasPermission(NOTE_PERMISSION))
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, NOTE_PERMISSION).build(getBot()))
|
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, NOTE_PERMISSION).build(getBot()))
|
||||||
|
@ -188,8 +187,6 @@ public class NoteCommand extends BaseCommand {
|
||||||
final Guild guild = ctx.getSource().getGuild();
|
final Guild guild = ctx.getSource().getGuild();
|
||||||
final Member performer = Objects.requireNonNull(ctx.getSource().getMember());
|
final Member performer = Objects.requireNonNull(ctx.getSource().getMember());
|
||||||
|
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.now();
|
|
||||||
|
|
||||||
if (!performer.hasPermission(NOTE_PERMISSION))
|
if (!performer.hasPermission(NOTE_PERMISSION))
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, NOTE_PERMISSION).build(getBot()))
|
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, NOTE_PERMISSION).build(getBot()))
|
||||||
|
|
|
@ -8,15 +8,14 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.commands.BaseCommand;
|
import sciwhiz12.janitor.commands.BaseCommand;
|
||||||
import sciwhiz12.janitor.commands.CommandRegistry;
|
import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
||||||
import sciwhiz12.janitor.moderation.warns.WarningStorage;
|
import sciwhiz12.janitor.moderation.warns.WarningStorage;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static sciwhiz12.janitor.commands.util.CommandHelper.argument;
|
import static sciwhiz12.janitor.commands.util.CommandHelper.argument;
|
||||||
import static sciwhiz12.janitor.commands.util.CommandHelper.literal;
|
import static sciwhiz12.janitor.commands.util.CommandHelper.literal;
|
||||||
|
@ -52,8 +51,6 @@ public class UnwarnCommand extends BaseCommand {
|
||||||
final Member performer = Objects.requireNonNull(ctx.getSource().getMember());
|
final Member performer = Objects.requireNonNull(ctx.getSource().getMember());
|
||||||
int caseID = IntegerArgumentType.getInteger(ctx, "caseId");
|
int caseID = IntegerArgumentType.getInteger(ctx, "caseId");
|
||||||
|
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.now();
|
|
||||||
|
|
||||||
if (!performer.hasPermission(WARN_PERMISSION))
|
if (!performer.hasPermission(WARN_PERMISSION))
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, WARN_PERMISSION).build(getBot()))
|
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, WARN_PERMISSION).build(getBot()))
|
||||||
|
|
|
@ -14,7 +14,6 @@ import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
||||||
import sciwhiz12.janitor.moderation.warns.WarningStorage;
|
import sciwhiz12.janitor.moderation.warns.WarningStorage;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -88,8 +87,6 @@ public class WarnListCommand extends BaseCommand {
|
||||||
predicate = predicate.and(e -> e.getValue().getPerformer().getIdLong() == mod.getIdLong());
|
predicate = predicate.and(e -> e.getValue().getPerformer().getIdLong() == mod.getIdLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.now();
|
|
||||||
|
|
||||||
if (!performer.hasPermission(WARN_PERMISSION))
|
if (!performer.hasPermission(WARN_PERMISSION))
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, WARN_PERMISSION).build(getBot()))
|
messages().MODERATION.ERRORS.performerInsufficientPermissions(performer, WARN_PERMISSION).build(getBot()))
|
||||||
|
|
|
@ -4,11 +4,11 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static sciwhiz12.janitor.utils.Util.nameFor;
|
import static sciwhiz12.janitor.utils.Util.nameFor;
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@ import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||||
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
|
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
|
||||||
import com.electronwill.nightconfig.core.file.FileWatcher;
|
import com.electronwill.nightconfig.core.file.FileWatcher;
|
||||||
import com.electronwill.nightconfig.toml.TomlFormat;
|
import com.electronwill.nightconfig.toml.TomlFormat;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static sciwhiz12.janitor.Logging.CONFIG;
|
import static sciwhiz12.janitor.Logging.CONFIG;
|
||||||
import static sciwhiz12.janitor.Logging.JANITOR;
|
import static sciwhiz12.janitor.Logging.JANITOR;
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package sciwhiz12.janitor.moderation.notes;
|
package sciwhiz12.janitor.moderation.notes;
|
||||||
|
|
||||||
import com.google.gson.JsonDeserializationContext;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.google.gson.JsonDeserializer;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.google.gson.JsonObject;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.JsonParseException;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import com.google.gson.JsonSerializationContext;
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
import com.google.gson.JsonSerializer;
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import sciwhiz12.janitor.JanitorBot;
|
import sciwhiz12.janitor.JanitorBot;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.io.IOException;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class NoteEntry {
|
public class NoteEntry {
|
||||||
private final User performer;
|
private final User performer;
|
||||||
|
@ -59,32 +60,42 @@ public class NoteEntry {
|
||||||
return Objects.hash(getPerformer(), getTarget(), getDateTime(), getContents());
|
return Objects.hash(getPerformer(), getTarget(), getDateTime(), getContents());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Serializer implements JsonDeserializer<NoteEntry>, JsonSerializer<NoteEntry> {
|
public static class Serializer extends StdSerializer<NoteEntry> {
|
||||||
private final JanitorBot bot;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public Serializer(JanitorBot bot) {
|
public Serializer() {
|
||||||
|
super(NoteEntry.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(NoteEntry value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||||
|
gen.writeStartObject();
|
||||||
|
gen.writeNumberField("performer", value.getPerformer().getIdLong());
|
||||||
|
gen.writeNumberField("target", value.getTarget().getIdLong());
|
||||||
|
gen.writeStringField("dateTime", value.getDateTime().toString());
|
||||||
|
gen.writeStringField("contents", value.getContents());
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Deserializer extends StdDeserializer<NoteEntry> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private final Supplier<JanitorBot> bot;
|
||||||
|
|
||||||
|
public Deserializer(Supplier<JanitorBot> bot) {
|
||||||
|
super(NoteEntry.class);
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NoteEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
public NoteEntry deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
|
||||||
throws JsonParseException {
|
final JsonNode obj = ctx.readTree(p);
|
||||||
final JsonObject obj = json.getAsJsonObject();
|
User performer = bot.get().getDiscord().retrieveUserById(obj.get("performer").asLong()).complete();
|
||||||
final User performer = bot.getDiscord().retrieveUserById(obj.get("performer").getAsLong()).complete();
|
User target = bot.get().getDiscord().retrieveUserById(obj.get("target").asLong()).complete();
|
||||||
final User target = bot.getDiscord().retrieveUserById(obj.get("target").getAsLong()).complete();
|
OffsetDateTime dateTime = OffsetDateTime.parse(obj.get("dateTime").asText());
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.parse(obj.get("dateTime").getAsString());
|
String contents = obj.get("contents").asText();
|
||||||
final String reason = obj.get("contents").getAsString();
|
return new NoteEntry(performer, target, dateTime, contents);
|
||||||
return new NoteEntry(performer, target, dateTime, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonElement serialize(NoteEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
|
||||||
final JsonObject obj = new JsonObject();
|
|
||||||
obj.addProperty("performer", src.getPerformer().getId());
|
|
||||||
obj.addProperty("target", src.getTarget().getId());
|
|
||||||
obj.addProperty("dateTime", src.getDateTime().toString());
|
|
||||||
obj.addProperty("contents", src.getContents());
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,36 @@
|
||||||
package sciwhiz12.janitor.moderation.notes;
|
package sciwhiz12.janitor.moderation.notes;
|
||||||
|
|
||||||
import com.electronwill.nightconfig.core.utils.ObservedMap;
|
import com.electronwill.nightconfig.core.utils.ObservedMap;
|
||||||
import com.google.gson.Gson;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.gson.JsonObject;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.GuildStorage;
|
import sciwhiz12.janitor.GuildStorage;
|
||||||
import sciwhiz12.janitor.JanitorBot;
|
import sciwhiz12.janitor.JanitorBot;
|
||||||
import sciwhiz12.janitor.storage.JsonStorage;
|
import sciwhiz12.janitor.storage.JsonStorage;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class NoteStorage extends JsonStorage {
|
public class NoteStorage extends JsonStorage {
|
||||||
private static final Type NOTE_MAP_TYPE = new TypeToken<Map<Integer, NoteEntry>>() {}.getType();
|
private static final TypeReference<Map<Integer, NoteEntry>> NOTE_MAP_TYPE = new TypeReference<>() {};
|
||||||
public static final String STORAGE_KEY = "notes";
|
public static final String STORAGE_KEY = "notes";
|
||||||
|
|
||||||
public static NoteStorage get(GuildStorage storage, Guild guild) {
|
public static NoteStorage get(GuildStorage storage, Guild guild) {
|
||||||
return storage.getOrCreate(guild, STORAGE_KEY, () -> new NoteStorage(storage.getBot()));
|
return storage.getOrCreate(guild, STORAGE_KEY, () -> new NoteStorage(storage.getBot()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Gson gson;
|
|
||||||
private final JanitorBot bot;
|
private final JanitorBot bot;
|
||||||
private int lastID = 1;
|
private int lastID = 1;
|
||||||
private final Map<Integer, NoteEntry> notes = new ObservedMap<>(new HashMap<>(), this::markDirty);
|
private final Map<Integer, NoteEntry> notes = new ObservedMap<>(new HashMap<>(), this::markDirty);
|
||||||
|
|
||||||
public NoteStorage(JanitorBot bot) {
|
public NoteStorage(JanitorBot bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
this.gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(NoteEntry.class, new NoteEntry.Serializer(bot))
|
|
||||||
.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JanitorBot getBot() {
|
public JanitorBot getBot() {
|
||||||
|
@ -52,8 +48,8 @@ public class NoteStorage extends JsonStorage {
|
||||||
return notes.get(noteID);
|
return notes.get(noteID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NoteEntry removeNote(int noteID) {
|
public void removeNote(int noteID) {
|
||||||
return notes.remove(noteID);
|
notes.remove(noteID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAmountOfNotes(User target) {
|
public int getAmountOfNotes(User target) {
|
||||||
|
@ -67,18 +63,27 @@ public class NoteStorage extends JsonStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement save() {
|
protected void initialize(ObjectMapper mapper) {
|
||||||
JsonObject obj = new JsonObject();
|
super.initialize(mapper);
|
||||||
obj.addProperty("lastNoteID", lastID);
|
mapper.registerModule(
|
||||||
obj.add("notes", gson.toJsonTree(notes));
|
new SimpleModule()
|
||||||
|
.addSerializer(NoteEntry.class, new NoteEntry.Serializer())
|
||||||
|
.addDeserializer(NoteEntry.class, new NoteEntry.Deserializer(this::getBot))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonNode save(ObjectMapper mapper) {
|
||||||
|
final ObjectNode obj = mapper.createObjectNode();
|
||||||
|
obj.put("lastNoteID", lastID);
|
||||||
|
obj.set("notes", mapper.valueToTree(notes));
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load(JsonElement in) {
|
public void load(JsonNode in, ObjectMapper mapper) throws IOException {
|
||||||
final JsonObject obj = in.getAsJsonObject();
|
lastID = in.get("lastNoteID").asInt();
|
||||||
lastID = obj.get("lastNoteID").getAsInt();
|
final Map<Integer, NoteEntry> loaded = mapper.readerFor(NOTE_MAP_TYPE).readValue(in.get("notes"));
|
||||||
final Map<Integer, NoteEntry> loaded = gson.fromJson(obj.get("notes"), NOTE_MAP_TYPE);
|
|
||||||
notes.clear();
|
notes.clear();
|
||||||
notes.putAll(loaded);
|
notes.putAll(loaded);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package sciwhiz12.janitor.moderation.warns;
|
package sciwhiz12.janitor.moderation.warns;
|
||||||
|
|
||||||
import com.google.gson.JsonDeserializationContext;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.google.gson.JsonDeserializer;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.google.gson.JsonObject;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.JsonParseException;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import com.google.gson.JsonSerializationContext;
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
import com.google.gson.JsonSerializer;
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import sciwhiz12.janitor.JanitorBot;
|
import sciwhiz12.janitor.JanitorBot;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.io.IOException;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class WarningEntry {
|
public class WarningEntry {
|
||||||
|
@ -62,35 +63,42 @@ public class WarningEntry {
|
||||||
return Objects.hash(getPerformer(), getWarned(), getDateTime(), getReason());
|
return Objects.hash(getPerformer(), getWarned(), getDateTime(), getReason());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Serializer implements JsonDeserializer<WarningEntry>, JsonSerializer<WarningEntry> {
|
public static class Serializer extends StdSerializer<WarningEntry> {
|
||||||
private final JanitorBot bot;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public Serializer(JanitorBot bot) {
|
public Serializer() {
|
||||||
|
super(WarningEntry.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(WarningEntry value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||||
|
gen.writeStartObject();
|
||||||
|
gen.writeNumberField("performer", value.getPerformer().getIdLong());
|
||||||
|
gen.writeNumberField("warned", value.getWarned().getIdLong());
|
||||||
|
gen.writeStringField("dateTime", value.getDateTime().toString());
|
||||||
|
gen.writeStringField("reason", value.getReason());
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Deserializer extends StdDeserializer<WarningEntry> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private final Supplier<JanitorBot> bot;
|
||||||
|
|
||||||
|
public Deserializer(Supplier<JanitorBot> bot) {
|
||||||
|
super(WarningEntry.class);
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WarningEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
public WarningEntry deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
|
||||||
throws JsonParseException {
|
final JsonNode obj = ctx.readTree(p);
|
||||||
final JsonObject obj = json.getAsJsonObject();
|
User performer = bot.get().getDiscord().retrieveUserById(obj.get("performer").asLong()).complete();
|
||||||
final User warned = bot.getDiscord().retrieveUserById(obj.get("warned").getAsLong()).complete();
|
User warned = bot.get().getDiscord().retrieveUserById(obj.get("warned").asLong()).complete();
|
||||||
final User performer = bot.getDiscord().retrieveUserById(obj.get("performer").getAsLong()).complete();
|
OffsetDateTime dateTime = OffsetDateTime.parse(obj.get("dateTime").asText());
|
||||||
final OffsetDateTime dateTime = OffsetDateTime.parse(obj.get("dateTime").getAsString());
|
String contents = obj.get("reason").asText();
|
||||||
@Nullable
|
return new WarningEntry(performer, warned, dateTime, contents);
|
||||||
final String reason = obj.has("reason") ? obj.get("reason").getAsString() : null;
|
|
||||||
return new WarningEntry(warned, performer, dateTime, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonElement serialize(WarningEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
|
||||||
final JsonObject obj = new JsonObject();
|
|
||||||
obj.addProperty("warned", src.getWarned().getId());
|
|
||||||
obj.addProperty("performer", src.getPerformer().getId());
|
|
||||||
obj.addProperty("dateTime", src.getDateTime().toString());
|
|
||||||
if (src.getReason() != null) {
|
|
||||||
obj.addProperty("reason", src.getReason());
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +1,35 @@
|
||||||
package sciwhiz12.janitor.moderation.warns;
|
package sciwhiz12.janitor.moderation.warns;
|
||||||
|
|
||||||
import com.electronwill.nightconfig.core.utils.ObservedMap;
|
import com.electronwill.nightconfig.core.utils.ObservedMap;
|
||||||
import com.google.gson.Gson;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.gson.JsonObject;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.GuildStorage;
|
import sciwhiz12.janitor.GuildStorage;
|
||||||
import sciwhiz12.janitor.JanitorBot;
|
import sciwhiz12.janitor.JanitorBot;
|
||||||
import sciwhiz12.janitor.storage.JsonStorage;
|
import sciwhiz12.janitor.storage.JsonStorage;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class WarningStorage extends JsonStorage {
|
public class WarningStorage extends JsonStorage {
|
||||||
private static final Type WARNING_MAP_TYPE = new TypeToken<Map<Integer, WarningEntry>>() {}.getType();
|
private static final TypeReference<Map<Integer, WarningEntry>> WARNING_MAP_TYPE = new TypeReference<>() {};
|
||||||
public static final String STORAGE_KEY = "warnings";
|
public static final String STORAGE_KEY = "warnings";
|
||||||
|
|
||||||
public static WarningStorage get(GuildStorage storage, Guild guild) {
|
public static WarningStorage get(GuildStorage storage, Guild guild) {
|
||||||
return storage.getOrCreate(guild, STORAGE_KEY, () -> new WarningStorage(storage.getBot()));
|
return storage.getOrCreate(guild, STORAGE_KEY, () -> new WarningStorage(storage.getBot()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Gson gson;
|
|
||||||
private final JanitorBot bot;
|
private final JanitorBot bot;
|
||||||
private int lastID = 1;
|
private int lastID = 1;
|
||||||
private final Map<Integer, WarningEntry> warnings = new ObservedMap<>(new HashMap<>(), this::markDirty);
|
private final Map<Integer, WarningEntry> warnings = new ObservedMap<>(new HashMap<>(), this::markDirty);
|
||||||
|
|
||||||
public WarningStorage(JanitorBot bot) {
|
public WarningStorage(JanitorBot bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
this.gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(WarningEntry.class, new WarningEntry.Serializer(bot))
|
|
||||||
.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JanitorBot getBot() {
|
public JanitorBot getBot() {
|
||||||
|
@ -51,8 +47,8 @@ public class WarningStorage extends JsonStorage {
|
||||||
return warnings.get(caseID);
|
return warnings.get(caseID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WarningEntry removeWarning(int caseID) {
|
public void removeWarning(int caseID) {
|
||||||
return warnings.remove(caseID);
|
warnings.remove(caseID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, WarningEntry> getWarnings() {
|
public Map<Integer, WarningEntry> getWarnings() {
|
||||||
|
@ -60,18 +56,27 @@ public class WarningStorage extends JsonStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement save() {
|
protected void initialize(ObjectMapper mapper) {
|
||||||
JsonObject obj = new JsonObject();
|
super.initialize(mapper);
|
||||||
obj.addProperty("lastCaseID", lastID);
|
mapper.registerModule(
|
||||||
obj.add("warnings", gson.toJsonTree(warnings));
|
new SimpleModule()
|
||||||
|
.addSerializer(WarningEntry.class, new WarningEntry.Serializer())
|
||||||
|
.addDeserializer(WarningEntry.class, new WarningEntry.Deserializer(this::getBot))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonNode save(ObjectMapper mapper) {
|
||||||
|
final ObjectNode obj = mapper.createObjectNode();
|
||||||
|
obj.put("lastCaseID", lastID);
|
||||||
|
obj.set("warnings", mapper.valueToTree(warnings));
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load(JsonElement in) {
|
public void load(JsonNode in, ObjectMapper mapper) throws IOException {
|
||||||
final JsonObject obj = in.getAsJsonObject();
|
lastID = in.get("lastCaseID").asInt();
|
||||||
lastID = obj.get("lastCaseID").getAsInt();
|
final Map<Integer, WarningEntry> loaded = mapper.convertValue(in.get("warnings"), WARNING_MAP_TYPE);
|
||||||
final Map<Integer, WarningEntry> loaded = gson.fromJson(obj.get("warnings"), WARNING_MAP_TYPE);
|
|
||||||
warnings.clear();
|
warnings.clear();
|
||||||
warnings.putAll(loaded);
|
warnings.putAll(loaded);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ public class MessageBuilder {
|
||||||
this(new EmbedBuilder(), new HashMap<>());
|
this(new EmbedBuilder(), new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("CopyConstructorMissesField")
|
|
||||||
public MessageBuilder(MessageBuilder copy) {
|
public MessageBuilder(MessageBuilder copy) {
|
||||||
this(new EmbedBuilder(copy.embedBuilder), new HashMap<>(copy.substitutions));
|
this(new EmbedBuilder(copy.embedBuilder), new HashMap<>(copy.substitutions));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package sciwhiz12.janitor.msg;
|
||||||
import net.dv8tion.jda.api.Permission;
|
import net.dv8tion.jda.api.Permission;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
import sciwhiz12.janitor.moderation.notes.NoteEntry;
|
import sciwhiz12.janitor.moderation.notes.NoteEntry;
|
||||||
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
import sciwhiz12.janitor.moderation.warns.WarningEntry;
|
||||||
|
|
||||||
|
@ -12,6 +11,7 @@ import java.time.ZoneOffset;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
|
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
package sciwhiz12.janitor.msg;
|
package sciwhiz12.janitor.msg;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.io.Resources;
|
import com.google.common.io.Resources;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
import sciwhiz12.janitor.JanitorBot;
|
import sciwhiz12.janitor.JanitorBot;
|
||||||
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -18,13 +16,13 @@ import static sciwhiz12.janitor.Logging.JANITOR;
|
||||||
import static sciwhiz12.janitor.Logging.TRANSLATIONS;
|
import static sciwhiz12.janitor.Logging.TRANSLATIONS;
|
||||||
|
|
||||||
public class Translations {
|
public class Translations {
|
||||||
private static final Gson GSON = new GsonBuilder().create();
|
|
||||||
private static final String DEFAULT_TRANSLATIONS_RESOURCE = "english.json";
|
private static final String DEFAULT_TRANSLATIONS_RESOURCE = "english.json";
|
||||||
private static final Type MAP_TYPE = new TypeToken<Map<String, String>>() {}.getType();
|
private static final TypeReference<Map<String, String>> MAP_TYPE = new TypeReference<>() {};
|
||||||
|
|
||||||
private final JanitorBot bot;
|
private final JanitorBot bot;
|
||||||
private final Path translationsFile;
|
private final Path translationsFile;
|
||||||
private final Map<String, String> translations = new HashMap<>();
|
private final Map<String, String> translations = new HashMap<>();
|
||||||
|
private final ObjectMapper jsonMapper = new ObjectMapper();
|
||||||
|
|
||||||
public Translations(JanitorBot bot, Path translationsFile) {
|
public Translations(JanitorBot bot, Path translationsFile) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
|
@ -40,7 +38,7 @@ public class Translations {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
JANITOR.debug(TRANSLATIONS, "Loading translations from file {}", translationsFile);
|
JANITOR.debug(TRANSLATIONS, "Loading translations from file {}", translationsFile);
|
||||||
Map<String, String> trans = GSON.fromJson(Files.newBufferedReader(translationsFile), MAP_TYPE);
|
Map<String, String> trans = jsonMapper.readValue(Files.newBufferedReader(translationsFile), MAP_TYPE);
|
||||||
translations.clear();
|
translations.clear();
|
||||||
translations.putAll(trans);
|
translations.putAll(trans);
|
||||||
JANITOR.info(TRANSLATIONS, "Loaded {} translations from file {}", translations.size(), translationsFile);
|
JANITOR.info(TRANSLATIONS, "Loaded {} translations from file {}", translations.size(), translationsFile);
|
||||||
|
@ -55,7 +53,7 @@ public class Translations {
|
||||||
try {
|
try {
|
||||||
JANITOR.debug(TRANSLATIONS, "Loading default english translations");
|
JANITOR.debug(TRANSLATIONS, "Loading default english translations");
|
||||||
// noinspection UnstableApiUsage
|
// noinspection UnstableApiUsage
|
||||||
Map<String, String> trans = GSON.fromJson(
|
Map<String, String> trans = jsonMapper.readValue(
|
||||||
new InputStreamReader(Resources.getResource(DEFAULT_TRANSLATIONS_RESOURCE).openStream()),
|
new InputStreamReader(Resources.getResource(DEFAULT_TRANSLATIONS_RESOURCE).openStream()),
|
||||||
MAP_TYPE);
|
MAP_TYPE);
|
||||||
translations.clear();
|
translations.clear();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package sciwhiz12.janitor.storage;
|
package sciwhiz12.janitor.storage;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
|
||||||
|
@ -7,7 +8,7 @@ public interface IStorage {
|
||||||
|
|
||||||
boolean dirty();
|
boolean dirty();
|
||||||
|
|
||||||
void write(Writer output);
|
void write(Writer output) throws IOException;
|
||||||
|
|
||||||
void read(Reader input);
|
void read(Reader input) throws IOException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,35 @@
|
||||||
package sciwhiz12.janitor.storage;
|
package sciwhiz12.janitor.storage;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
|
||||||
public abstract class JsonStorage extends AbstractStorage {
|
public abstract class JsonStorage extends AbstractStorage {
|
||||||
public static final Gson GSON = new GsonBuilder()
|
protected final ObjectMapper jsonMapper = new ObjectMapper()
|
||||||
.serializeNulls()
|
.enable(SerializationFeature.INDENT_OUTPUT)
|
||||||
.setPrettyPrinting()
|
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
|
||||||
.create();
|
|
||||||
|
|
||||||
public abstract JsonElement save();
|
protected JsonStorage() {
|
||||||
|
initialize(jsonMapper);
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void load(JsonElement object);
|
protected void initialize(ObjectMapper mapper) {}
|
||||||
|
|
||||||
|
public abstract JsonNode save(ObjectMapper mapper);
|
||||||
|
|
||||||
|
public abstract void load(JsonNode object, ObjectMapper mapper) throws IOException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(Writer input) {
|
public void write(Writer input) throws IOException {
|
||||||
GSON.toJson(save(), input);
|
jsonMapper.writeTree(jsonMapper.createGenerator(input), save(jsonMapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(Reader input) {
|
public void read(Reader input) throws IOException {
|
||||||
load(JsonParser.parseReader(input));
|
load(jsonMapper.readTree(input), jsonMapper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,8 +37,6 @@ public class StringReaderUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final char SYNTAX_ESCAPE = '\\';
|
private static final char SYNTAX_ESCAPE = '\\';
|
||||||
private static final char SYNTAX_DOUBLE_QUOTE = '"';
|
|
||||||
private static final char SYNTAX_SINGLE_QUOTE = '\'';
|
|
||||||
public static String readStringUntil(StringReader reader, char terminator) throws CommandSyntaxException {
|
public static String readStringUntil(StringReader reader, char terminator) throws CommandSyntaxException {
|
||||||
final StringBuilder result = new StringBuilder();
|
final StringBuilder result = new StringBuilder();
|
||||||
boolean escaped = false;
|
boolean escaped = false;
|
||||||
|
|
|
@ -55,8 +55,8 @@ public class Util {
|
||||||
return user.getName().concat("#").concat(user.getDiscriminator());
|
return user.getName().concat("#").concat(user.getDiscriminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <Success, Error> BiConsumer<Success, Error> handle(final Consumer<Success> success,
|
public static <Success, Err> BiConsumer<Success, Err> handle(final Consumer<Success> success,
|
||||||
final Consumer<Error> exceptionally) {
|
final Consumer<Err> exceptionally) {
|
||||||
return (suc, ex) -> {
|
return (suc, ex) -> {
|
||||||
if (ex == null) {
|
if (ex == null) {
|
||||||
success.accept(suc);
|
success.accept(suc);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user