perf: 重写部分代码
This commit is contained in:
@@ -3,6 +3,7 @@ setArchivesBaseName("HamsterCore-Common")
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
//noinspection GradlePackageUpdate
|
||||
//noinspection VulnerableLibrariesLocal
|
||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
|
||||
|
@@ -1,20 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.common.constant;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreConstantObjects {
|
||||
public static final ExecutorService WORKER_EXECUTOR = CoreUtils.WORKER_EXECUTOR;
|
||||
public static final ScheduledExecutorService SCHEDULED_EXECUTOR = CoreUtils.SCHEDULED_EXECUTOR;
|
||||
public static Gson GSON = CoreUtils.GSON;
|
||||
public static Gson GSON_HUMAN = CoreUtils.GSON_HUMAN;
|
||||
|
||||
private CoreConstantObjects() {
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package cn.hamster3.mc.plugin.core.common.data;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.util.SerializeUtils;
|
||||
import com.google.gson.JsonElement;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.Data;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
@@ -14,7 +14,8 @@ import net.kyori.adventure.util.Ticks;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "UnusedReturnValue", "PatternValidation"})
|
||||
@Data
|
||||
public class DisplayMessage {
|
||||
private Component message;
|
||||
private Component actionbar;
|
||||
@@ -27,40 +28,22 @@ public class DisplayMessage {
|
||||
public DisplayMessage() {
|
||||
}
|
||||
|
||||
public static DisplayMessage message(@NotNull String message) {
|
||||
return new DisplayMessage().setMessage(message);
|
||||
}
|
||||
|
||||
public static DisplayMessage message(@NotNull Component message) {
|
||||
return new DisplayMessage().setMessage(message);
|
||||
}
|
||||
|
||||
public static DisplayMessage actionbar(@NotNull String actionbar) {
|
||||
return new DisplayMessage().setActionBar(actionbar);
|
||||
}
|
||||
|
||||
public static DisplayMessage actionbar(@NotNull Component actionbar) {
|
||||
return new DisplayMessage().setActionBar(actionbar);
|
||||
}
|
||||
|
||||
public static DisplayMessage title(@Nullable String title, @Nullable String subtitle) {
|
||||
return new DisplayMessage().setTitle(title, subtitle);
|
||||
}
|
||||
|
||||
public static DisplayMessage title(@Nullable String title, @Nullable String subtitle, int fadeIn, int stay, int fadeOut) {
|
||||
return new DisplayMessage().setTitle(title, subtitle, fadeIn, stay, fadeOut);
|
||||
}
|
||||
|
||||
public static DisplayMessage sound(@NotNull String sound) {
|
||||
return new DisplayMessage().setSound(sound);
|
||||
}
|
||||
|
||||
public static DisplayMessage sound(@NotNull String sound, float volume, float pitch) {
|
||||
return new DisplayMessage().setSound(sound, volume, pitch);
|
||||
}
|
||||
|
||||
public static DisplayMessage sound(@NotNull Sound sound) {
|
||||
return new DisplayMessage().setSound(sound);
|
||||
@NotNull
|
||||
public static DisplayMessage fromJson(@NotNull JsonObject object) {
|
||||
DisplayMessage displayMessage = new DisplayMessage();
|
||||
if (object.has("message")) {
|
||||
displayMessage.message = GsonComponentSerializer.gson().deserializeFromTree(object.get("message")).compact();
|
||||
}
|
||||
if (object.has("actionbar")) {
|
||||
displayMessage.actionbar = GsonComponentSerializer.gson().deserializeFromTree(object.get("actionbar")).compact();
|
||||
}
|
||||
if (object.has("title")) {
|
||||
displayMessage.title = CoreUtils.deserializeTitle(object.getAsJsonObject("title"));
|
||||
}
|
||||
if (object.has("sound")) {
|
||||
displayMessage.sound = CoreUtils.deserializeSound(object.getAsJsonObject("sound"));
|
||||
}
|
||||
return displayMessage;
|
||||
}
|
||||
|
||||
public void show(@NotNull Audience audience) {
|
||||
@@ -86,94 +69,15 @@ public class DisplayMessage {
|
||||
copy().replace(replacements).show(audience);
|
||||
}
|
||||
|
||||
public DisplayMessage replace(@NotNull TextReplacementConfig replacement) {
|
||||
if (message != null) {
|
||||
message = message.replaceText(replacement).compact();
|
||||
}
|
||||
if (actionbar != null) {
|
||||
actionbar = actionbar.replaceText(replacement).compact();
|
||||
}
|
||||
if (title != null) {
|
||||
title = Title.title(
|
||||
title.title().replaceText(replacement).compact(),
|
||||
title.subtitle().replaceText(replacement).compact(),
|
||||
title.times()
|
||||
);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DisplayMessage replace(@NotNull TextReplacementConfig... replacements) {
|
||||
if (message != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
message = message.replaceText(replacement).compact();
|
||||
}
|
||||
}
|
||||
if (actionbar != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
actionbar = actionbar.replaceText(replacement).compact();
|
||||
}
|
||||
}
|
||||
if (title != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
title = Title.title(
|
||||
title.title().replaceText(replacement).compact(),
|
||||
title.subtitle().replaceText(replacement).compact(),
|
||||
title.times()
|
||||
);
|
||||
}
|
||||
}
|
||||
@NotNull
|
||||
public DisplayMessage setMessage(@NotNull String message) {
|
||||
this.message = Component.text(message);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsonObject saveToJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
if (message != null) {
|
||||
object.add("message", GsonComponentSerializer.gson().serializeToTree(message));
|
||||
}
|
||||
if (actionbar != null) {
|
||||
object.add("actionbar", GsonComponentSerializer.gson().serializeToTree(actionbar));
|
||||
}
|
||||
if (title != null) {
|
||||
object.add("title", SerializeUtils.serializeTitle(title));
|
||||
}
|
||||
if (sound != null) {
|
||||
object.add("sound", SerializeUtils.serializeSound(sound));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setMessage(@Nullable String message) {
|
||||
this.message = message == null ? null : Component.text(message);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setMessage(@Nullable Component message) {
|
||||
this.message = message == null ? null : message.compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setActionBar(@Nullable String actionbar) {
|
||||
this.actionbar = actionbar == null ? null : Component.text(actionbar);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setActionBar(@Nullable Component actionbar) {
|
||||
this.actionbar = actionbar == null ? null : actionbar.compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setTitle(@Nullable String title, @Nullable String subtitle) {
|
||||
this.title = Title.title(
|
||||
title == null ? Component.empty() : Component.text(title),
|
||||
subtitle == null ? Component.empty() : Component.text(subtitle)
|
||||
);
|
||||
public DisplayMessage setActionbar(@NotNull String actionbar) {
|
||||
this.actionbar = Component.text(actionbar);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -192,108 +96,78 @@ public class DisplayMessage {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setTitle(@Nullable Component title, @Nullable Component subtitle) {
|
||||
this.title = Title.title(
|
||||
title == null ? Component.empty() : title.compact(),
|
||||
subtitle == null ? Component.empty() : subtitle.compact()
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setTitle(@Nullable Component title, @Nullable Component subtitle, int fadeIn, int stay, int fadeOut) {
|
||||
this.title = Title.title(
|
||||
title == null ? Component.empty() : title.compact(),
|
||||
subtitle == null ? Component.empty() : subtitle.compact(),
|
||||
Title.Times.times(
|
||||
Ticks.duration(fadeIn),
|
||||
Ticks.duration(stay),
|
||||
Ticks.duration(fadeOut)
|
||||
)
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setTitle(@Nullable Title title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DisplayMessage setSound(@Nullable String sound) {
|
||||
this.sound = sound == null ? null : Sound.sound(Key.key(sound), Sound.Source.MASTER, 1, 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DisplayMessage setSound(@NotNull String namespace, @NotNull String path) {
|
||||
this.sound = Sound.sound(Key.key(namespace, path), Sound.Source.MASTER, 1, 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DisplayMessage setSound(@NotNull String sound, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DisplayMessage setSound(@NotNull String namespace, @NotNull String value, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(namespace, value), Sound.Source.MASTER, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DisplayMessage setSound(@NotNull String namespace, @NotNull String value, @NotNull Sound.Source source, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(namespace, value), source, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage setSound(@Nullable Sound sound) {
|
||||
this.sound = sound;
|
||||
return this;
|
||||
public DisplayMessage replace(@NotNull TextReplacementConfig... replacements) {
|
||||
DisplayMessage copy = copy();
|
||||
if (copy.message != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
copy.message = copy.message.replaceText(replacement).compact();
|
||||
}
|
||||
}
|
||||
if (copy.actionbar != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
copy.actionbar = copy.actionbar.replaceText(replacement).compact();
|
||||
}
|
||||
}
|
||||
if (copy.title != null) {
|
||||
for (TextReplacementConfig replacement : replacements) {
|
||||
copy.title = Title.title(
|
||||
title.title().replaceText(replacement).compact(),
|
||||
title.subtitle().replaceText(replacement).compact(),
|
||||
title.times()
|
||||
);
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage fromJson(@NotNull JsonElement element) {
|
||||
if (!element.isJsonObject()) {
|
||||
message = Component.text(element.toString());
|
||||
return this;
|
||||
public JsonObject toJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
if (message != null) {
|
||||
object.add("message", GsonComponentSerializer.gson().serializeToTree(message));
|
||||
}
|
||||
JsonObject object = element.getAsJsonObject();
|
||||
if (object.has("message")) {
|
||||
message = GsonComponentSerializer.gson().deserializeFromTree(object.get("message")).compact();
|
||||
if (actionbar != null) {
|
||||
object.add("actionbar", GsonComponentSerializer.gson().serializeToTree(actionbar));
|
||||
}
|
||||
if (object.has("actionbar")) {
|
||||
actionbar = GsonComponentSerializer.gson().deserializeFromTree(object.get("actionbar")).compact();
|
||||
if (title != null) {
|
||||
object.add("title", CoreUtils.serializeTitle(title));
|
||||
}
|
||||
if (object.has("title")) {
|
||||
title = SerializeUtils.deserializeTitle(object.getAsJsonObject("title"));
|
||||
if (sound != null) {
|
||||
object.add("sound", CoreUtils.serializeSound(sound));
|
||||
}
|
||||
if (object.has("sound")) {
|
||||
sound = SerializeUtils.deserializeSound(object.getAsJsonObject("sound"));
|
||||
}
|
||||
return this;
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public DisplayMessage copy() {
|
||||
return new DisplayMessage()
|
||||
.setMessage(message)
|
||||
.setActionBar(actionbar)
|
||||
.setTitle(title)
|
||||
.setSound(sound);
|
||||
DisplayMessage copy = new DisplayMessage();
|
||||
copy.message = message;
|
||||
copy.actionbar = actionbar;
|
||||
copy.title = title;
|
||||
copy.sound = sound;
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return saveToJson().toString();
|
||||
return toJson().toString();
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package cn.hamster3.mc.plugin.core.common.thread;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -23,7 +23,7 @@ public abstract class CountdownThread implements Runnable {
|
||||
}
|
||||
|
||||
public void start(long initialDelay) {
|
||||
future = CoreConstantObjects.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
|
||||
future = CoreUtils.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package cn.hamster3.mc.plugin.core.common.misc;
|
||||
package cn.hamster3.mc.plugin.core.common.thread;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@@ -1,18 +1,21 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.misc.MessageTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.misc.NamedThreadFactory;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -72,135 +75,82 @@ public final class CoreUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换颜色代码
|
||||
* 将 adventure 中的 title 对象序列化为 json
|
||||
*
|
||||
* @param string 要替换的字符串
|
||||
* @return 替换后的字符串
|
||||
* @param title -
|
||||
* @return -
|
||||
*/
|
||||
@Nullable
|
||||
public static String replaceColorCode(@Nullable String string) {
|
||||
if (string == null) return null;
|
||||
return string.replace("&", "§");
|
||||
@NotNull
|
||||
public static JsonObject serializeTitle(@NotNull Title title) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.add("title", GsonComponentSerializer.gson().serializeToTree(title.title()));
|
||||
object.add("subtitle", GsonComponentSerializer.gson().serializeToTree(title.subtitle()));
|
||||
Title.Times times = title.times();
|
||||
if (times != null) {
|
||||
object.add("times", serializeTitleTimes(times));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换颜色代码
|
||||
* <p>
|
||||
* 添加这个方法是因为 ConfigurationSection 中的 getString 方法有 @Nullable 注解
|
||||
* <p>
|
||||
* 导致 idea 会弹出某些警告,让人非常不爽
|
||||
* 将 json 反序列化为 adventure 中的 title
|
||||
*
|
||||
* @param string 要替换的字符串
|
||||
* @param defaultValue 若 string 为空则使用该字符串
|
||||
* @return 替换后的字符串
|
||||
* @param object -
|
||||
* @return -
|
||||
*/
|
||||
@NotNull
|
||||
public static String replaceColorCode(@Nullable String string, @NotNull String defaultValue) {
|
||||
if (string == null) return defaultValue;
|
||||
return string.replace("&", "§");
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换颜色代码
|
||||
*
|
||||
* @param strings 要替换的字符串
|
||||
* @return 替换后的字符串
|
||||
*/
|
||||
@NotNull
|
||||
public static ArrayList<String> replaceColorCode(@Nullable Iterable<String> strings) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
if (strings == null) return list;
|
||||
for (String s : strings) {
|
||||
list.add(replaceColorCode(s));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换颜色代码
|
||||
*
|
||||
* @param strings 要替换的字符串
|
||||
*/
|
||||
public static void replaceColorCode(@NotNull String[] strings) {
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
strings[i] = strings[i].replace("&", "§");
|
||||
public static Title deserializeTitle(@NotNull JsonObject object) {
|
||||
if (object.has("times")) {
|
||||
return Title.title(
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("title")),
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("subtitle")),
|
||||
deserializeTitleTimes(object.getAsJsonObject("times"))
|
||||
);
|
||||
} else {
|
||||
return Title.title(
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("title")),
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("subtitle"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String[] replaceStringList(@NotNull String[] strings, @NotNull String key, @NotNull String value) {
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
strings[i] = strings[i].replace(key, value);
|
||||
}
|
||||
return strings;
|
||||
public static JsonObject serializeTitleTimes(@NotNull Title.Times times) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("fadeIn", times.fadeIn().toMillis());
|
||||
object.addProperty("stay", times.stay().toMillis());
|
||||
object.addProperty("fadeOut", times.fadeOut().toMillis());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> replaceStringList(@NotNull Iterable<String> strings, @NotNull String key, @NotNull String value) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String s : strings) {
|
||||
list.add(s.replace(key, value));
|
||||
}
|
||||
return list;
|
||||
public static Title.Times deserializeTitleTimes(@NotNull JsonObject object) {
|
||||
return Title.Times.times(
|
||||
Duration.ofMillis(object.get("fadeIn").getAsLong()),
|
||||
Duration.ofMillis(object.get("stay").getAsLong()),
|
||||
Duration.ofMillis(object.get("fadeOut").getAsLong())
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> replaceStringList(@NotNull List<String> strings, @NotNull String key, @NotNull String value) {
|
||||
strings.replaceAll(s -> s.replace(key, value));
|
||||
return strings;
|
||||
}
|
||||
|
||||
public static boolean startsWithIgnoreCase(@NotNull String string, @NotNull String prefix) {
|
||||
return string.regionMatches(true, 0, prefix, 0, prefix.length());
|
||||
}
|
||||
|
||||
public static boolean endsWithIgnoreCase(@NotNull String string, @NotNull String suffix) {
|
||||
int strOffset = string.length() - suffix.length();
|
||||
return string.regionMatches(true, strOffset, suffix, 0, suffix.length());
|
||||
public static JsonObject serializeSound(@NotNull Sound sound) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("key", sound.name().asString());
|
||||
object.addProperty("source", sound.source().name());
|
||||
object.addProperty("volume", sound.volume());
|
||||
object.addProperty("pitch", sound.pitch());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ArrayList<String> startsWith(@NotNull Iterable<String> strings, @NotNull String with) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String string : strings) {
|
||||
if (string.startsWith(with)) {
|
||||
list.add(string);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ArrayList<String> endsWith(@NotNull Iterable<String> strings, @NotNull String with) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String string : strings) {
|
||||
if (string.endsWith(with)) {
|
||||
list.add(string);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ArrayList<String> startsWithIgnoreCase(@NotNull Iterable<String> strings, @NotNull String start) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String string : strings) {
|
||||
if (startsWithIgnoreCase(string, start)) {
|
||||
list.add(string);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ArrayList<String> endsWithIgnoreCase(@NotNull Iterable<String> strings, @NotNull String end) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String string : strings) {
|
||||
if (endsWithIgnoreCase(string, end)) {
|
||||
list.add(string);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public static Sound deserializeSound(@NotNull JsonObject object) {
|
||||
return Sound.sound(
|
||||
Key.key(object.get("key").getAsString()),
|
||||
Sound.Source.valueOf(object.get("source").getAsString()),
|
||||
object.get("volume").getAsFloat(),
|
||||
object.get("pitch").getAsFloat()
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@@ -1,97 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 序列化相关工具
|
||||
*/
|
||||
public final class SerializeUtils {
|
||||
private SerializeUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 adventure 中的 title 对象序列化为 json
|
||||
*
|
||||
* @param title -
|
||||
* @return -
|
||||
*/
|
||||
@NotNull
|
||||
public static JsonObject serializeTitle(@NotNull Title title) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.add("title", GsonComponentSerializer.gson().serializeToTree(title.title()));
|
||||
object.add("subtitle", GsonComponentSerializer.gson().serializeToTree(title.subtitle()));
|
||||
Title.Times times = title.times();
|
||||
if (times != null) {
|
||||
object.add("times", serializeTitleTimes(times));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 json 反序列化为 adventure 中的 title
|
||||
*
|
||||
* @param object -
|
||||
* @return -
|
||||
*/
|
||||
@NotNull
|
||||
public static Title deserializeTitle(@NotNull JsonObject object) {
|
||||
if (object.has("times")) {
|
||||
return Title.title(
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("title")),
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("subtitle")),
|
||||
deserializeTitleTimes(object.getAsJsonObject("times"))
|
||||
);
|
||||
} else {
|
||||
return Title.title(
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("title")),
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("subtitle"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsonObject serializeTitleTimes(@NotNull Title.Times times) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("fadeIn", times.fadeIn().toMillis());
|
||||
object.addProperty("stay", times.stay().toMillis());
|
||||
object.addProperty("fadeOut", times.fadeOut().toMillis());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Title.Times deserializeTitleTimes(@NotNull JsonObject object) {
|
||||
return Title.Times.times(
|
||||
Duration.ofMillis(object.get("fadeIn").getAsLong()),
|
||||
Duration.ofMillis(object.get("stay").getAsLong()),
|
||||
Duration.ofMillis(object.get("fadeOut").getAsLong())
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsonObject serializeSound(@NotNull Sound sound) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("key", sound.name().asString());
|
||||
object.addProperty("source", sound.source().name());
|
||||
object.addProperty("volume", sound.volume());
|
||||
object.addProperty("pitch", sound.pitch());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public static Sound deserializeSound(@NotNull JsonObject object) {
|
||||
return Sound.sound(
|
||||
Key.key(object.get("key").getAsString()),
|
||||
Sound.Source.valueOf(object.get("source").getAsString()),
|
||||
object.get("volume").getAsFloat(),
|
||||
object.get("pitch").getAsFloat()
|
||||
);
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package cn.hamster3.mc.plugin.core.common.misc;
|
||||
package cn.hamster3.mc.plugin.core.common.util.serializer;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import com.google.gson.*;
|
||||
@@ -13,11 +13,11 @@ public class MessageTypeAdapter implements JsonSerializer<DisplayMessage>, JsonD
|
||||
|
||||
@Override
|
||||
public DisplayMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
return new DisplayMessage().fromJson(json);
|
||||
return DisplayMessage.fromJson(json.getAsJsonObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(DisplayMessage src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return src.saveToJson();
|
||||
return src.toJson();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user