From c784c69eb3181a4bc18f450aba116f7f8b406df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 4 Jul 2020 18:53:03 +0200 Subject: [PATCH] progress --- Bukkit/build.gradle | 3 +- .../bukkit/player/BukkitPlayer.java | 9 ++- Core/build.gradle | 14 ++-- .../core/command/CommandCaller.java | 15 +++- .../core/configuration/Caption.java | 2 +- .../core/configuration/StaticCaption.java | 17 +++- .../configuration/caption/CaptionLoader.java | 2 + .../caption/LocalizedCaptionMap.java | 2 + .../caption/PerUserLocaleCaptionMap.java | 1 + .../caption/TranslatableCaption.java | 3 + .../caption/VariableReplacement.java | 79 +++++++++++++++++++ 11 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/configuration/caption/VariableReplacement.java diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index 6995d9034..afd240034 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation("net.luckperms:api:5.1") implementation("net.ess3:EssentialsX:2.17.2") implementation("net.alpenblock:BungeePerms:4.0-dev-106") + implementation("net.kyori:adventure-text-platform-bukkit:4.0.0-SNAPSHOT") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile('com.sk89q:squirrelid:1.0.0-SNAPSHOT'){ transitive = false } } @@ -91,7 +92,7 @@ shadowJar { dependencies { include(dependency(":PlotSquared-Core")) include(dependency("io.papermc:paperlib:1.0.2")) - include(dependency("net.kyori:text-adapter-bukkit:3.0.3")) + include(dependency("net.kyori:adventure-text-platform-bukkit:4.0.0-SNAPSHOT")) include(dependency("org.bstats:bstats-bukkit:1.7")) include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT")) include(dependency("com.sk89q:squirrelid:1.0.0-SNAPSHOT")) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index 643039600..94b77fd37 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -28,8 +28,10 @@ package com.plotsquared.bukkit.player; import com.google.common.base.Charsets; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Caption; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.VariableReplacement; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; @@ -225,7 +227,12 @@ public class BukkitPlayer extends PlotPlayer { return this.player.isPermissionSet(permission); } - @Override public void sendMessage(String message) { + @Override public void sendMessage(@NotNull final Caption caption, + @NotNull final VariableReplacement... replacements) { + + } + + @Deprecated @Override public void sendMessage(String message) { message = message.replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&'); if (!StringMan.isEqual(this.getMeta("lastMessage"), message) || ( System.currentTimeMillis() - this.getMeta("lastMessageTime") > 5000)) { diff --git a/Core/build.gradle b/Core/build.gradle index 51f56e187..e8a52b612 100644 --- a/Core/build.gradle +++ b/Core/build.gradle @@ -18,6 +18,9 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.72") implementation("org.jetbrains:annotations:19.0.0") implementation("org.khelekore:prtree:1.7.0-SNAPSHOT") + // Adventure related stuff + implementation('net.kyori:adventure-api:4.0.0-SNAPSHOT') + implementation('net.kyori:adventure-text-minimessage:3.0.0-SNAPSHOT') } sourceCompatibility = 1.8 @@ -71,11 +74,12 @@ task copyFiles { shadowJar { dependencies { - include(dependency("net.kyori:text-api:3.0.2")) - include(dependency("net.kyori:text-serializer-gson:3.0.2")) - include(dependency("net.kyori:text-serializer-legacy:3.0.2")) - include(dependency("net.kyori:text-serializer-plain:3.0.2")) - include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT")) + include(dependency('net.kyori:adventure-api:4.0.0-SNAPSHOT')) + include(dependency('net.kyori:adventure-gson:4.0.0-SNAPSHOT')) + include(dependency('net.kyori:adventure-legacy:4.0.0-SNAPSHOT')) + include(dependency('net.kyori:adventure-plain:4.0.0-SNAPSHOT')) + include(dependency('net.kyori:adventure-text-minimessage:3.0.0-SNAPSHOT')) + include(dependency('org.khelekore:prtree:1.7.0-SNAPSHOT')) } relocate('net.kyori.text', 'com.plotsquared.formatting.text') relocate("org.json", "com.plotsquared.json") { diff --git a/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java b/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java index b26eed517..60565c0e1 100644 --- a/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java +++ b/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java @@ -25,14 +25,27 @@ */ package com.plotsquared.core.command; +import com.plotsquared.core.configuration.Caption; +import com.plotsquared.core.configuration.caption.VariableReplacement; +import org.jetbrains.annotations.NotNull; + public interface CommandCaller { /** * Send the player a message. * * @param message the message to send + * @deprecated Use static captions instead */ - void sendMessage(String message); + @Deprecated void sendMessage(String message); + + /** + * Send a message to the command caller + * + * @param caption Caption to send + * @param replacements Variable replacements + */ + void sendMessage(@NotNull Caption caption, @NotNull VariableReplacement... replacements); /** * Check the player's permissions. Will be cached if permission caching is enabled. diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Caption.java b/Core/src/main/java/com/plotsquared/core/configuration/Caption.java index bb9cf098f..a70d77dab 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Caption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Caption.java @@ -33,7 +33,7 @@ public interface Caption { String getTranslated(); - default String formatted() { + @Deprecated default String formatted() { return StringMan.replaceFromMap(getTranslated(), Captions.replacements); } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/StaticCaption.java b/Core/src/main/java/com/plotsquared/core/configuration/StaticCaption.java index 403cc4c88..ee6df5a24 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/StaticCaption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/StaticCaption.java @@ -25,7 +25,9 @@ */ package com.plotsquared.core.configuration; +import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; +import org.jetbrains.annotations.NotNull; @RequiredArgsConstructor public final class StaticCaption implements Caption { @@ -33,10 +35,23 @@ public final class StaticCaption implements Caption { private final String value; private final boolean usePrefix; - public StaticCaption(final String value) { + /** + * @deprecated Use {@link #of(String)} + */ + @Deprecated public StaticCaption(final String value) { this(value, true); } + /** + * Create a new static caption from the given text + * + * @param text Text + * @return Created caption + */ + @NotNull public static StaticCaption of(@NotNull final String text) { + return new StaticCaption(Preconditions.checkNotNull(text, "Text may not be null")); + } + @Override public String getTranslated() { return this.value; } 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 14dfd0649..9850925c7 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 @@ -45,6 +45,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; public final class CaptionLoader { + private static final Gson GSON = new GsonBuilder().create(); private static final Pattern FILE_NAME_PATTERN = Pattern.compile("messages_(.*)\\.json"); @@ -82,4 +83,5 @@ public final class CaptionLoader { } return new LocalizedCaptionMap(locale, captions); } + } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java index ee2d2984e..b9e25e2a1 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java @@ -31,6 +31,7 @@ import java.util.Locale; import java.util.Map; public class LocalizedCaptionMap implements CaptionMap { + private final Locale locale; private final Map captions; @@ -54,4 +55,5 @@ public class LocalizedCaptionMap implements CaptionMap { @Override public Locale getLocale() { return this.locale; } + } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/PerUserLocaleCaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/PerUserLocaleCaptionMap.java index 7249dbbdb..c49d3863a 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/PerUserLocaleCaptionMap.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/PerUserLocaleCaptionMap.java @@ -32,6 +32,7 @@ import java.util.Locale; import java.util.Map; public class PerUserLocaleCaptionMap extends LocalizedCaptionMap { + private final Map localeMap; public PerUserLocaleCaptionMap(Map localeMap) { diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java index 72327b7af..ed8c90154 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java @@ -25,10 +25,12 @@ */ package com.plotsquared.core.configuration.caption; +import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Caption; import org.jetbrains.annotations.NotNull; public final class TranslatableCaption implements Caption { + @NotNull private final String key; private TranslatableCaption(@NotNull String key) { @@ -50,4 +52,5 @@ public final class TranslatableCaption implements Caption { @NotNull public String getKey() { return this.key; } + } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/VariableReplacement.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/VariableReplacement.java new file mode 100644 index 000000000..974acd5e1 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/VariableReplacement.java @@ -0,0 +1,79 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.configuration.caption; + +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +/** + * Key-value pair used as replacement of variables in {@link com.plotsquared.core.configuration.Caption captions} + */ +@ToString +@EqualsAndHashCode +public final class VariableReplacement { + + private final String key; + private final String value; + + private VariableReplacement(@NotNull final String key, @NotNull final String value) { + this.key = Objects.requireNonNull(key, "Key may not be null"); + this.value = Objects.requireNonNull(value, "Value may not be null"); + } + + /** + * Create a new variable replacement from a key-value pair + * + * @param key Replacement key + * @param value Replacement value + * @return Replacement instance + */ + @NotNull public static VariableReplacement keyed(@NotNull final String key, + @NotNull final String value) { + return new VariableReplacement(key, value); + } + + /** + * Get the replacement key + * + * @return Replacement key + */ + @NotNull public String getKey() { + return this.key; + } + + /** + * Get the replacement value + * + * @return Replacement value + */ + @NotNull public String getValue() { + return this.value; + } + +}