feat: 使用 MiniMessage 读取消息
This commit is contained in:
@@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "cn.hamster3.mc.plugin"
|
group = "cn.hamster3.mc.plugin"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
apply {
|
apply {
|
||||||
|
@@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@SuppressWarnings("CallToPrintStackTrace")
|
||||||
public class HamsterCorePlugin extends JavaPlugin {
|
public class HamsterCorePlugin extends JavaPlugin {
|
||||||
@Getter
|
@Getter
|
||||||
private static HamsterCorePlugin instance;
|
private static HamsterCorePlugin instance;
|
||||||
@@ -64,8 +65,13 @@ public class HamsterCorePlugin extends JavaPlugin {
|
|||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
logger.info("已读取配置文件");
|
logger.info("已读取配置文件");
|
||||||
CoreBukkitAPI.init();
|
try {
|
||||||
logger.info("已初始化 CoreAPI");
|
CoreBukkitAPI.init();
|
||||||
|
logger.info("已初始化 CoreAPI");
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warning("初始化 CoreAPI 出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
CoreMessage.init(this);
|
CoreMessage.init(this);
|
||||||
logger.info("已初始化语言文本");
|
logger.info("已初始化语言文本");
|
||||||
long time = System.currentTimeMillis() - start;
|
long time = System.currentTimeMillis() - start;
|
||||||
|
@@ -32,16 +32,33 @@ public final class CoreBukkitAPI extends CoreAPI {
|
|||||||
private final HikariDataSource datasource;
|
private final HikariDataSource datasource;
|
||||||
|
|
||||||
private CoreBukkitAPI() {
|
private CoreBukkitAPI() {
|
||||||
|
gson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||||
|
.create();
|
||||||
|
humanGson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||||
|
.serializeNulls()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||||
FileConfiguration config = plugin.getConfig();
|
FileConfiguration config = plugin.getConfig();
|
||||||
|
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("正在创建 redis 客户端");
|
||||||
redisClient = RedisClient.create(config.getString("redis-url"));
|
redisClient = RedisClient.create(config.getString("redis-url"));
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("redis 客户端创建完成");
|
||||||
|
|
||||||
ConfigurationSection datasourceConfig = config.getConfigurationSection("datasource");
|
ConfigurationSection datasourceConfig = config.getConfigurationSection("datasource");
|
||||||
if (datasourceConfig == null) {
|
if (datasourceConfig == null) {
|
||||||
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
|
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
|
||||||
}
|
}
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("正在创建数据库连接池");
|
||||||
HikariConfig hikariConfig = new HikariConfig();
|
HikariConfig hikariConfig = new HikariConfig();
|
||||||
hikariConfig.setDriverClassName(datasourceConfig.getString("driver"));
|
hikariConfig.setDriverClassName(datasourceConfig.getString("driver"));
|
||||||
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
||||||
@@ -58,21 +75,7 @@ public final class CoreBukkitAPI extends CoreAPI {
|
|||||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||||
hikariConfig.setPoolName("HamsterCore-Pool");
|
hikariConfig.setPoolName("HamsterCore-Pool");
|
||||||
datasource = new HikariDataSource(hikariConfig);
|
datasource = new HikariDataSource(hikariConfig);
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("数据库连接池创建完成");
|
||||||
gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
|
||||||
.create();
|
|
||||||
humanGson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
|
||||||
.serializeNulls()
|
|
||||||
.setPrettyPrinting()
|
|
||||||
.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CoreBukkitAPI getInstance() {
|
public static CoreBukkitAPI getInstance() {
|
||||||
|
@@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Getter
|
||||||
public enum CoreMessage {
|
public enum CoreMessage {
|
||||||
COMMAND_NOT_FOUND("§c未找到该指令"),
|
COMMAND_NOT_FOUND("§c未找到该指令"),
|
||||||
COMMAND_NOT_HAS_PERMISSION("§c你没有这个权限"),
|
COMMAND_NOT_HAS_PERMISSION("§c你没有这个权限"),
|
||||||
@@ -62,7 +63,6 @@ public enum CoreMessage {
|
|||||||
COMMAND_LORE_INSERT_INDEX_OUT_OF_RANGE("§c你的手持物品没有这么多行 lore 文本"),
|
COMMAND_LORE_INSERT_INDEX_OUT_OF_RANGE("§c你的手持物品没有这么多行 lore 文本"),
|
||||||
COMMAND_LORE_INSERT_SUCCESS("§a已成功设置 lore 文本");
|
COMMAND_LORE_INSERT_SUCCESS("§a已成功设置 lore 文本");
|
||||||
|
|
||||||
@Getter
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private DisplayMessage message;
|
private DisplayMessage message;
|
||||||
|
|
||||||
|
@@ -7,6 +7,7 @@ import de.tr7zw.changeme.nbtapi.NBTContainer;
|
|||||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import net.kyori.adventure.title.Title;
|
import net.kyori.adventure.title.Title;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@@ -212,18 +213,18 @@ public final class CoreBukkitUtils {
|
|||||||
DisplayMessage displayMessage = new DisplayMessage();
|
DisplayMessage displayMessage = new DisplayMessage();
|
||||||
String message = config.getString("message");
|
String message = config.getString("message");
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
displayMessage.setMessage(message);
|
displayMessage.setMessage(MiniMessage.miniMessage().deserialize(message));
|
||||||
}
|
}
|
||||||
String actionbar = config.getString("actionbar");
|
String actionbar = config.getString("actionbar");
|
||||||
if (actionbar != null) {
|
if (actionbar != null) {
|
||||||
displayMessage.setActionbar(actionbar);
|
displayMessage.setActionbar(MiniMessage.miniMessage().deserialize(actionbar));
|
||||||
}
|
}
|
||||||
String title = config.getString("title");
|
String title = config.getString("title");
|
||||||
String subtitle = config.getString("subtitle");
|
String subtitle = config.getString("subtitle");
|
||||||
if (title != null || subtitle != null) {
|
if (title != null || subtitle != null) {
|
||||||
displayMessage.setTitle(
|
displayMessage.setTitle(
|
||||||
title,
|
MiniMessage.miniMessage().escapeTags(title == null ? "" : title),
|
||||||
subtitle,
|
MiniMessage.miniMessage().escapeTags(subtitle == null ? "" : subtitle),
|
||||||
config.getInt("fade-in", 10),
|
config.getInt("fade-in", 10),
|
||||||
config.getInt("stay", 70),
|
config.getInt("stay", 70),
|
||||||
config.getInt("fade-out", 20)
|
config.getInt("fade-out", 20)
|
||||||
|
@@ -8,6 +8,7 @@ import net.md_5.bungee.api.plugin.Plugin;
|
|||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@SuppressWarnings("CallToPrintStackTrace")
|
||||||
public class HamsterCorePlugin extends Plugin {
|
public class HamsterCorePlugin extends Plugin {
|
||||||
@Getter
|
@Getter
|
||||||
private static HamsterCorePlugin instance;
|
private static HamsterCorePlugin instance;
|
||||||
@@ -20,8 +21,13 @@ public class HamsterCorePlugin extends Plugin {
|
|||||||
instance = this;
|
instance = this;
|
||||||
Logger logger = getLogger();
|
Logger logger = getLogger();
|
||||||
logger.info("仓鼠核心正在初始化");
|
logger.info("仓鼠核心正在初始化");
|
||||||
CoreBungeeAPI.init();
|
try {
|
||||||
logger.info("已初始化 CoreAPI");
|
CoreBungeeAPI.init();
|
||||||
|
logger.info("已初始化 CoreAPI");
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warning("初始化 CoreAPI 出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
long time = System.currentTimeMillis() - start;
|
long time = System.currentTimeMillis() - start;
|
||||||
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms");
|
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms");
|
||||||
}
|
}
|
||||||
@@ -53,5 +59,4 @@ public class HamsterCorePlugin extends Plugin {
|
|||||||
long time = System.currentTimeMillis() - start;
|
long time = System.currentTimeMillis() - start;
|
||||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms");
|
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -28,16 +28,29 @@ public final class CoreBungeeAPI extends CoreAPI {
|
|||||||
private final HikariDataSource datasource;
|
private final HikariDataSource datasource;
|
||||||
|
|
||||||
private CoreBungeeAPI() {
|
private CoreBungeeAPI() {
|
||||||
|
gson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||||
|
.create();
|
||||||
|
humanGson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||||
|
.serializeNulls()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||||
Configuration config = CoreBungeeCordUtils.getPluginConfig(plugin);
|
Configuration config = CoreBungeeCordUtils.getPluginConfig(plugin);
|
||||||
|
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("正在创建 redis 客户端");
|
||||||
redisClient = RedisClient.create(config.getString("redis-url"));
|
redisClient = RedisClient.create(config.getString("redis-url"));
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("redis 客户端创建完成");
|
||||||
|
|
||||||
Configuration datasourceConfig = config.getSection("datasource");
|
Configuration datasourceConfig = config.getSection("datasource");
|
||||||
if (datasourceConfig == null) {
|
if (datasourceConfig == null) {
|
||||||
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
|
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
|
||||||
}
|
}
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("正在创建数据库连接池");
|
||||||
HikariConfig hikariConfig = new HikariConfig();
|
HikariConfig hikariConfig = new HikariConfig();
|
||||||
hikariConfig.setDriverClassName(datasourceConfig.getString("driver"));
|
hikariConfig.setDriverClassName(datasourceConfig.getString("driver"));
|
||||||
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
||||||
@@ -54,17 +67,7 @@ public final class CoreBungeeAPI extends CoreAPI {
|
|||||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||||
hikariConfig.setPoolName("HamsterCore-Pool");
|
hikariConfig.setPoolName("HamsterCore-Pool");
|
||||||
datasource = new HikariDataSource(hikariConfig);
|
datasource = new HikariDataSource(hikariConfig);
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().info("数据库连接池创建完成");
|
||||||
gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
|
||||||
.create();
|
|
||||||
humanGson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
|
||||||
.serializeNulls()
|
|
||||||
.setPrettyPrinting()
|
|
||||||
.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CoreBungeeAPI getInstance() {
|
public static CoreBungeeAPI getInstance() {
|
||||||
|
@@ -73,5 +73,4 @@ public abstract class CoreAPI {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract Gson getHumanGson();
|
public abstract Gson getHumanGson();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ package cn.hamster3.mc.plugin.core.common.data;
|
|||||||
|
|
||||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import lombok.Data;
|
import lombok.Getter;
|
||||||
import net.kyori.adventure.audience.Audience;
|
import net.kyori.adventure.audience.Audience;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.sound.Sound;
|
import net.kyori.adventure.sound.Sound;
|
||||||
@@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@SuppressWarnings({"unused", "UnusedReturnValue", "PatternValidation"})
|
@SuppressWarnings({"unused", "UnusedReturnValue", "PatternValidation"})
|
||||||
@Data
|
@Getter
|
||||||
public class DisplayMessage {
|
public class DisplayMessage {
|
||||||
private Component message;
|
private Component message;
|
||||||
private Component actionbar;
|
private Component actionbar;
|
||||||
@@ -85,12 +85,24 @@ public class DisplayMessage {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public DisplayMessage setMessage(@Nullable Component message) {
|
||||||
|
this.message = message;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public DisplayMessage setActionbar(@NotNull String actionbar) {
|
public DisplayMessage setActionbar(@NotNull String actionbar) {
|
||||||
this.actionbar = Component.text(actionbar);
|
this.actionbar = Component.text(actionbar);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public DisplayMessage setActionbar(@NotNull Component actionbar) {
|
||||||
|
this.actionbar = actionbar;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public DisplayMessage setTitle(@Nullable String title, @Nullable String subtitle, int fadeIn, int stay, int fadeOut) {
|
public DisplayMessage setTitle(@Nullable String title, @Nullable String subtitle, int fadeIn, int stay, int fadeOut) {
|
||||||
this.title = Title.title(
|
this.title = Title.title(
|
||||||
@@ -105,6 +117,26 @@ public class DisplayMessage {
|
|||||||
return this;
|
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,
|
||||||
|
subtitle == null ? Component.empty() : subtitle,
|
||||||
|
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
|
@NotNull
|
||||||
public DisplayMessage setSound(@NotNull String sound, float volume, float pitch) {
|
public DisplayMessage setSound(@NotNull String sound, float volume, float pitch) {
|
||||||
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch);
|
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch);
|
||||||
@@ -123,6 +155,12 @@ public class DisplayMessage {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public DisplayMessage setSound(@Nullable Sound sound) {
|
||||||
|
this.sound = sound;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public DisplayMessage replace(@NotNull TextReplacementConfig... replacements) {
|
public DisplayMessage replace(@NotNull TextReplacementConfig... replacements) {
|
||||||
DisplayMessage copy = copy();
|
DisplayMessage copy = copy();
|
||||||
|
@@ -1,8 +1,11 @@
|
|||||||
package cn.hamster3.mc.plugin.core.common.util;
|
package cn.hamster3.mc.plugin.core.common.util;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Getter
|
||||||
@Deprecated
|
@Deprecated
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class Pair<K, V> implements Serializable {
|
public class Pair<K, V> implements Serializable {
|
||||||
@@ -14,14 +17,6 @@ public class Pair<K, V> implements Serializable {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public K getKey() {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public V getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
Reference in New Issue
Block a user