Fix startup issues and make messages send properly

This commit is contained in:
Alexander Söderberg 2020-08-16 13:49:16 +02:00
parent 28d6d4db92
commit a4c9ed90b7
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
10 changed files with 55 additions and 46 deletions

View File

@ -1094,4 +1094,15 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
return (PlayerManager<BukkitPlayer, Player>) 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);
}
}
}
}

View File

@ -253,6 +253,11 @@ public interface PlotPlatform<P> extends LocaleHolder {
*/
@Nonnull Audience getConsoleAudience();
/**
* Load the caption maps
*/
void copyCaptionMaps();
/**
* Get the {@link PermissionHandler} implementation for the platform
*

View File

@ -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<String, HashMap<PlotId, Plot>> plots_tmp;
private YamlConfiguration config;
// Localization
private Map<String, CaptionMap> captionMaps;
private final Map<String, CaptionMap> 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());
}
/**

View File

@ -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 //

View File

@ -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"));
}

View File

@ -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<TranslatableCaption, String> captions = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
TranslatableCaption key = TranslatableCaption.of(entry.getKey());
captions.put(key, entry.getValue().getAsString());
final Map<String, String> object;
try {
object = GSON.fromJson(Files.newBufferedReader(file, StandardCharsets.UTF_8), new TypeToken<Map<String, String>>() {}.getType());
} catch (final Exception e) {
throw new RuntimeException(String.format("Failed to load caption file '%s'", file.getFileName().toString()), e);
}
final Map<TranslatableCaption, String> captions = new HashMap<>();
object.forEach((key, value) -> captions.put(TranslatableCaption.of(key), value));
return new LocalizedCaptionMap(locale, captions);
}

View File

@ -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()));
}
}

View File

@ -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<Actor> {
@ -145,15 +143,11 @@ public class ConsolePlayer extends PlotPlayer<Actor> {
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<Template> templates = Arrays.asList(replacements);
templates.add(Template.of("prefix", MINI_MESSAGE.parse(
TranslatableCaption.of("core.prefix").getComponent(this))));
message = CaptionUtility.format(this, message)
.replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&')
.replace("<prefix>", TranslatableCaption.of("core.prefix").getComponent(this));
// Parse the message
PlotSquared.platform().getConsoleAudience().sendMessage(MINI_MESSAGE.parse(message, templates));
PlotSquared.platform().getConsoleAudience().sendMessage(MINI_MESSAGE.parse(message, replacements));
}
@Override public void teleport(Location location, TeleportCause cause) {

View File

@ -32,12 +32,11 @@ import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.collection.ByteArrayUtilities;
import com.plotsquared.core.command.CommandCaller;
import com.plotsquared.core.command.RequiredType;
import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.configuration.caption.CaptionMap;
import com.plotsquared.core.configuration.caption.CaptionUtility;
import com.plotsquared.core.configuration.caption.LocaleHolder;
import com.plotsquared.core.configuration.caption.Templates;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.database.DBFunc;
import com.plotsquared.core.events.TeleportCause;
@ -79,13 +78,11 @@ import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@ -98,7 +95,6 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer, LocaleHolder {
private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
private static final String NON_EXISTENT_CAPTION = "<red>PlotSquared does not recognize the caption: ";
private static final Logger logger = LoggerFactory.getLogger("P2/" + PlotPlayer.class.getSimpleName());
@ -827,9 +823,9 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
public void sendTitle(@Nonnull final Caption title, @Nonnull final Caption subtitle,
final int fadeIn, final int stay, final int fadeOut,
@Nonnull final Template... replacements) {
final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements);
final Component titleComponent = MiniMessage.get().parse(title.getComponent(this), replacements);
final Component subtitleComponent =
MINI_MESSAGE.parse(subtitle.getComponent(this), replacements);
MiniMessage.get().parse(subtitle.getComponent(this), replacements);
getAudience().showTitle(Title
.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50, ChronoUnit.MILLIS),
Duration.of(stay * 50, ChronoUnit.MILLIS),
@ -851,14 +847,11 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
return;
}
// Replace placeholders, etc
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<Template> templates = Arrays.asList(replacements);
templates.add(Templates.of(this, "prefix", TranslatableCaption.of("core.prefix")));
message = CaptionUtility.format(this, message)
.replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&')
.replace("<prefix>", TranslatableCaption.of("core.prefix").getComponent(this));
// Parse the message
final Component component = MINI_MESSAGE.parse(message, templates);
final Component component = MiniMessage.get().parse(message, replacements);
if (!Objects.equal(component, this.getMeta("lastMessage"))
|| System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000) {
setMeta("lastMessage", component);

View File

@ -298,7 +298,7 @@
"errors.invalid_player": "<prefix><red>Player not found: </red><gray><value></gray><red>.</red>",
"errors.invalid_player_offline": "<prefix><gray><player></gray><red> must be online.</red>",
"errors.invalid_command_flag": "<prefix><red>Invalid command flag: </red><value></gray>",
"errors.error": "<prefix><red>An error occurred: </gray><value></gray></red>",
"errors.error": "<prefix><red>An error occurred: <gray><value></gray></red>",
"errors.error_create": "<prefix><red>An error occurred while creating the world: <gray><world></gray></red>",
"errors.error_console": "<prefix><red>An error occurred. See the console for more information.</red>",
"errors.command_went_wrong": "<prefix><red>Something went wrong when executing that command...</red>",