From a4c9ed90b704c11fc8290a0ffe0a475fce43e14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 16 Aug 2020 13:49:16 +0200 Subject: [PATCH] Fix startup issues and make messages send properly --- .../plotsquared/bukkit/BukkitPlatform.java | 11 ++++++++++ .../com/plotsquared/core/PlotPlatform.java | 5 +++++ .../com/plotsquared/core/PlotSquared.java | 21 ++++++++++++------- .../plotsquared/core/command/MainCommand.java | 4 ++-- .../com/plotsquared/core/command/Reload.java | 2 +- .../configuration/caption/CaptionLoader.java | 19 ++++++++--------- .../configuration/caption/CaptionMap.java | 2 +- .../core/player/ConsolePlayer.java | 14 ++++--------- .../plotsquared/core/player/PlotPlayer.java | 21 +++++++------------ Core/src/main/resources/lang/messages_en.json | 2 +- 10 files changed, 55 insertions(+), 46 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index e81955197..a76846fed 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -1094,4 +1094,15 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl return (PlayerManager) getInjector().getInstance(PlayerManager.class); } + @Override public void copyCaptionMaps() { + /* Make this prettier at some point */ + final String[] languages = new String[] { "en" }; + for (final String language : languages) { + if (!new File(new File(this.getDataFolder(), "lang"), String.format("messages_%s.json", language)).exists()) { + this.saveResource(String.format("lang/messages_%s.json", language), false); + logger.info("Copied language file 'messages_{}.json'", language); + } + } + } + } diff --git a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java index fdfd75a2a..c0265da15 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java +++ b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java @@ -253,6 +253,11 @@ public interface PlotPlatform

extends LocaleHolder { */ @Nonnull Audience getConsoleAudience(); + /** + * Load the caption maps + */ + void copyCaptionMaps(); + /** * Get the {@link PermissionHandler} implementation for the platform * diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 06a387dc4..c32de15dc 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -90,7 +90,6 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; -import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.sql.SQLException; import java.util.ArrayDeque; @@ -141,7 +140,7 @@ public class PlotSquared { public HashMap> plots_tmp; private YamlConfiguration config; // Localization - private Map captionMaps; + private final Map captionMaps = new HashMap<>(); // Platform / Version / Update URL private PlotVersion version; // Files and configuration @@ -174,6 +173,13 @@ public class PlotSquared { // ConfigurationSerialization.registerClass(BlockBucket.class, "BlockBucket"); + // Load caption map + try { + this.loadCaptionMap(); + } catch (final Exception e) { + logger.error("Failed to load caption map", e); + } + // Setup the global flag container GlobalFlagContainer.setup(); @@ -197,8 +203,6 @@ public class PlotSquared { return; } - this.captionMaps = new HashMap<>(); - this.worldedit = WorldEdit.getInstance(); // Create Event utility class @@ -224,16 +228,19 @@ public class PlotSquared { } } - public void loadCaptionMap() throws IOException { + public void loadCaptionMap() throws Exception { + this.platform.copyCaptionMaps(); // Setup localization CaptionMap captionMap; if (Settings.Enabled_Components.PER_USER_LOCALE) { - captionMap = CaptionLoader.loadAll(Paths.get("lang")); + captionMap = CaptionLoader.loadAll(new File(this.platform.getDirectory(), "lang").toPath()); } else { String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json"; - captionMap = CaptionLoader.loadSingle(Paths.get("lang", fileName)); + captionMap = CaptionLoader.loadSingle(new File(new File(this.platform.getDirectory(), "lang"), fileName).toPath()); } this.captionMaps.put(TranslatableCaption.DEFAULT_NAMESPACE, captionMap); + logger.info("Loaded caption map for namespace 'plotsquared': {}", + this.captionMaps.get(TranslatableCaption.DEFAULT_NAMESPACE).getClass().getCanonicalName()); } /** diff --git a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java index 428d52e14..ee93a2809 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -301,12 +301,12 @@ public class MainCommand extends Command { throw e; } catch (Throwable e) { e.printStackTrace(); - String message = e.getLocalizedMessage(); + String message = e.getMessage(); if (message != null) { player.sendMessage(TranslatableCaption.of("errors.error"), net.kyori.adventure.text.minimessage.Template.of("value", message)); } else { - player.sendMessage(TranslatableCaption.of("errors.error")); + player.sendMessage(TranslatableCaption.of("errors.error"), net.kyori.adventure.text.minimessage.Template.of("value", "")); } } // Reset command scope // diff --git a/Core/src/main/java/com/plotsquared/core/command/Reload.java b/Core/src/main/java/com/plotsquared/core/command/Reload.java index 927ef4afc..944fadf1c 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Reload.java +++ b/Core/src/main/java/com/plotsquared/core/command/Reload.java @@ -117,7 +117,7 @@ public class Reload extends SubCommand { }); this.worldConfiguration.save(this.worldFile); player.sendMessage(TranslatableCaption.of("reload.reloaded_configs")); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); player.sendMessage(TranslatableCaption.of("reload.reload_failed")); } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionLoader.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionLoader.java index cba84459d..cfde8ebfa 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionLoader.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionLoader.java @@ -25,10 +25,9 @@ */ package com.plotsquared.core.configuration.caption; +import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import javax.annotation.Nonnull; import java.io.IOException; @@ -64,7 +63,7 @@ public final class CaptionLoader { } } - public static CaptionMap loadSingle(@Nonnull final Path file) throws IOException { + @Nonnull public static CaptionMap loadSingle(@Nonnull final Path file) { final String fileName = file.getFileName().toString(); final Matcher matcher = FILE_NAME_PATTERN.matcher(fileName); final Locale locale; @@ -73,14 +72,14 @@ public final class CaptionLoader { } else { throw new IllegalArgumentException(fileName + " is an invalid message file (cannot extract locale)"); } - JsonObject object = GSON.fromJson( - Files.newBufferedReader(file, StandardCharsets.UTF_16), - JsonObject.class); - Map captions = new HashMap<>(); - for (Map.Entry entry : object.entrySet()) { - TranslatableCaption key = TranslatableCaption.of(entry.getKey()); - captions.put(key, entry.getValue().getAsString()); + final Map object; + try { + object = GSON.fromJson(Files.newBufferedReader(file, StandardCharsets.UTF_8), new TypeToken>() {}.getType()); + } catch (final Exception e) { + throw new RuntimeException(String.format("Failed to load caption file '%s'", file.getFileName().toString()), e); } + final Map captions = new HashMap<>(); + object.forEach((key, value) -> captions.put(TranslatableCaption.of(key), value)); return new LocalizedCaptionMap(locale, captions); } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java index 405ec2ebb..02538ce08 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java @@ -79,7 +79,7 @@ public interface CaptionMap { class NoSuchCaptionException extends IllegalArgumentException { public NoSuchCaptionException(@Nonnull final NamespacedCaption caption) { - super(String.format("No caption with the key '%s' exists in the map", caption.getKey())); + super(String.format("No caption with the key '%s:%s' exists in the map", caption.getNamespace(), caption.getKey())); } } diff --git a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java index ff28f5b1d..40cd70346 100644 --- a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java @@ -55,8 +55,6 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Arrays; -import java.util.List; import java.util.UUID; public class ConsolePlayer extends PlotPlayer { @@ -145,15 +143,11 @@ public class ConsolePlayer extends PlotPlayer { if (message.isEmpty()) { return; } - message = CaptionUtility.format(this, message). - /* Magic replacement characters */ - replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&'); - // Create the template list, and add the prefix as a replacement - final List