mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
[v1.3.0] 版本更新
- [U] 添加 MessagesInitializer 与 MessagesConfig 等相关消息配置,用于快捷创建消息静态类进行消文本消息的处理。
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
+87
-88
@@ -14,116 +14,115 @@ import java.util.function.Supplier;
|
|||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public class FileConfig {
|
public class FileConfig {
|
||||||
|
|
||||||
public static Supplier<FileConfig> pluginConfiguration = null;
|
public static Supplier<FileConfig> pluginConfiguration = null;
|
||||||
public static Supplier<FileConfig> messageConfiguration = null;
|
public static Supplier<FileConfig> messageConfiguration = null;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static FileConfig getPluginConfiguration() {
|
public static FileConfig getPluginConfiguration() {
|
||||||
return pluginConfiguration == null ? null : pluginConfiguration.get();
|
return pluginConfiguration == null ? null : pluginConfiguration.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static FileConfig getMessageConfiguration() {
|
public static FileConfig getMessageConfiguration() {
|
||||||
return messageConfiguration == null ? null : messageConfiguration.get();
|
return messageConfiguration == null ? null : messageConfiguration.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private long updateTime;
|
protected long updateTime;
|
||||||
|
|
||||||
private final JavaPlugin plugin;
|
protected final JavaPlugin plugin;
|
||||||
private final File fileFolder;
|
protected final File fileFolder;
|
||||||
private final String fileName;
|
protected final String fileName;
|
||||||
private final String resourcePath;
|
protected final String resourcePath;
|
||||||
|
|
||||||
private File file;
|
protected File file;
|
||||||
private FileConfiguration config;
|
protected FileConfiguration config;
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
|
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
|
||||||
this(plugin, "config.yml");
|
this(plugin, "config.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin,
|
public FileConfig(@NotNull JavaPlugin plugin,
|
||||||
@NotNull String fileName) throws IOException {
|
@NotNull String fileName) throws IOException {
|
||||||
this(plugin, fileName, fileName);
|
this(plugin, fileName, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
||||||
@NotNull String fileName) throws IOException {
|
@NotNull String fileName) throws IOException {
|
||||||
this(plugin, resourcePath, plugin.getDataFolder(), fileName);
|
this(plugin, resourcePath, plugin.getDataFolder(), fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
||||||
@NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
@NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.resourcePath = resourcePath;
|
this.resourcePath = resourcePath;
|
||||||
this.fileFolder = fileFolder;
|
this.fileFolder = fileFolder;
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
initFile();
|
||||||
|
}
|
||||||
|
|
||||||
initFile();
|
protected void initFile() throws IOException {
|
||||||
}
|
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
||||||
|
this.file = new File(getFileFolder(), fileName);
|
||||||
|
|
||||||
private void initFile() throws IOException {
|
if (!file.exists()) {
|
||||||
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
InputStream resourceStream = plugin.getResource(resourcePath);
|
||||||
this.file = new File(getFileFolder(), fileName);
|
if (resourceStream == null) {
|
||||||
|
throw new IOException("The resource " + resourcePath + " cannot find in " + plugin.getName() + " !");
|
||||||
|
}
|
||||||
|
|
||||||
if (!file.exists()) {
|
OutputStream out = new FileOutputStream(file);
|
||||||
InputStream resourceStream = plugin.getResource(resourcePath);
|
byte[] buffer = new byte[1024];
|
||||||
if (resourceStream == null) {
|
|
||||||
throw new IOException("The resource " + resourcePath + " cannot find in " + plugin.getName() + " !");
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputStream out = new FileOutputStream(file);
|
int readBytes;
|
||||||
byte[] buffer = new byte[1024];
|
while ((readBytes = resourceStream.read(buffer)) > 0) {
|
||||||
|
out.write(buffer, 0, readBytes);
|
||||||
|
}
|
||||||
|
|
||||||
int readBytes;
|
out.close();
|
||||||
while ((readBytes = resourceStream.read(buffer)) > 0) {
|
resourceStream.close();
|
||||||
out.write(buffer, 0, readBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
out.close();
|
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
||||||
resourceStream.close();
|
}
|
||||||
|
|
||||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
this.updateTime = System.currentTimeMillis();
|
||||||
}
|
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||||
|
}
|
||||||
|
|
||||||
this.updateTime = System.currentTimeMillis();
|
public File getFileFolder() {
|
||||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
return fileFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getFileFolder() {
|
public File getFile() {
|
||||||
return fileFolder;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getFile() {
|
public FileConfiguration getConfig() {
|
||||||
return file;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfiguration getConfig() {
|
public JavaPlugin getPlugin() {
|
||||||
return config;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JavaPlugin getPlugin() {
|
public void save() throws IOException {
|
||||||
return plugin;
|
getConfig().save(getFile());
|
||||||
}
|
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
||||||
|
}
|
||||||
|
|
||||||
public void save() throws IOException {
|
public void reload() throws IOException {
|
||||||
getConfig().save(getFile());
|
this.updateTime = System.currentTimeMillis();
|
||||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
if (getFile().exists()) {
|
||||||
}
|
this.config = YamlConfiguration.loadConfiguration(getFile());
|
||||||
|
} else {
|
||||||
|
initFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void reload() throws IOException {
|
public long getUpdateTime() {
|
||||||
this.updateTime = System.currentTimeMillis();
|
return updateTime;
|
||||||
if (getFile().exists()) {
|
}
|
||||||
this.config = YamlConfiguration.loadConfiguration(getFile());
|
|
||||||
} else {
|
|
||||||
initFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getUpdateTime() {
|
public boolean isExpired(long time) {
|
||||||
return updateTime;
|
return getUpdateTime() > time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isExpired(long time) {
|
|
||||||
return getUpdateTime() > time;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+105
@@ -0,0 +1,105 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||||
|
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class EasyMessage {
|
||||||
|
|
||||||
|
@Nullable ConfigValue<String> configValue;
|
||||||
|
|
||||||
|
@Nullable String defaultValue;
|
||||||
|
@Nullable String[] messageParams;
|
||||||
|
|
||||||
|
public EasyMessage() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyMessage(@Nullable String defaultValue) {
|
||||||
|
this(defaultValue, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
this.messageParams = messageParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize(@NotNull FileConfig sourceConfig, @NotNull String sectionName) {
|
||||||
|
this.configValue = new ConfigValue<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable String getDefaultValue() {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable String[] getMessageParams() {
|
||||||
|
return messageParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull String getDefaultMessages() {
|
||||||
|
if (getDefaultValue() == null) return "";
|
||||||
|
else return getDefaultValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull String getMessages() {
|
||||||
|
if (configValue == null) {
|
||||||
|
return getDefaultMessages();
|
||||||
|
} else {
|
||||||
|
return configValue.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String get(@Nullable CommandSender sender) {
|
||||||
|
return get(sender, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||||
|
return get(sender, getMessageParams(), values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
if (sender == null) return getMessages();
|
||||||
|
if (params == null || values == null) {
|
||||||
|
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages()));
|
||||||
|
} else {
|
||||||
|
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages(), params, values));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender) {
|
||||||
|
send(sender, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||||
|
send(sender, getMessageParams(), values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
if (params == null || values == null) {
|
||||||
|
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
||||||
|
} else {
|
||||||
|
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(getMessages()), params, values);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll() {
|
||||||
|
sendToAll(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll(@Nullable Object[] values) {
|
||||||
|
sendToAll(messageParams, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+110
@@ -0,0 +1,110 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||||
|
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
|
||||||
|
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EasyMessageList {
|
||||||
|
|
||||||
|
@Nullable ConfigValueList<String> configValue;
|
||||||
|
|
||||||
|
@Nullable String[] defaultValue;
|
||||||
|
@Nullable String[] messageParams;
|
||||||
|
|
||||||
|
public EasyMessageList() {
|
||||||
|
this((String[]) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyMessageList(@Nullable String... defaultValue) {
|
||||||
|
this(defaultValue, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyMessageList(@Nullable String[] defaultValue,
|
||||||
|
@Nullable String[] messageParams) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
this.messageParams = messageParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize(FileConfig sourceConfig, String sectionName) {
|
||||||
|
configValue = new ConfigValueList<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable String[] getDefaultValue() {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Unmodifiable
|
||||||
|
private @NotNull List<String> getDefaultMessages() {
|
||||||
|
if (getDefaultValue() == null) return new ArrayList<>();
|
||||||
|
else return Arrays.asList(getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private @Nullable String[] getMessageParams() {
|
||||||
|
return messageParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull List<String> getMessages() {
|
||||||
|
if (configValue == null) {
|
||||||
|
return getDefaultMessages();
|
||||||
|
} else {
|
||||||
|
return configValue.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull List<String> get(@Nullable CommandSender sender) {
|
||||||
|
return get(sender, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||||
|
return get(sender, getMessageParams(), values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
if (sender == null) return getMessages();
|
||||||
|
if (params == null || values == null) {
|
||||||
|
return MessageUtils.setPlaceholders(sender, getMessages());
|
||||||
|
} else {
|
||||||
|
return MessageUtils.setPlaceholders(sender, getMessages(), params, values);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender) {
|
||||||
|
send(sender, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||||
|
send(sender, getMessageParams(), values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
if (params == null || values == null) {
|
||||||
|
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
||||||
|
} else {
|
||||||
|
MessageUtils.sendWithPlaceholders(sender, getMessages(), params, values);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll() {
|
||||||
|
sendToAll(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll(@Nullable Object[] values) {
|
||||||
|
sendToAll(messageParams, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||||
|
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
public class MessagesConfig extends FileConfig {
|
||||||
|
|
||||||
|
public MessagesConfig(@NotNull JavaPlugin plugin) throws IOException {
|
||||||
|
this(plugin, "messages.yml");
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull String fileName) throws IOException {
|
||||||
|
this(plugin, plugin.getDataFolder(), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
||||||
|
super(plugin, fileName, fileFolder, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initFile() throws IOException {
|
||||||
|
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
||||||
|
this.file = new File(getFileFolder(), fileName);
|
||||||
|
if (!file.exists()) file.createNewFile();
|
||||||
|
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
getConfig().save(getFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MessagesInitializer {
|
||||||
|
|
||||||
|
private MessagesInitializer() {
|
||||||
|
// 静态方法类,不应当实例化。
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz) {
|
||||||
|
initialize(source, rootClazz, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) {
|
||||||
|
MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class);
|
||||||
|
|
||||||
|
String rootSection = null;
|
||||||
|
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
||||||
|
rootSection = sectionAnnotation.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||||
|
initSection(source, rootSection, innerClass, saveDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Field field : rootClazz.getFields()) {
|
||||||
|
initMessage(source, rootSection, rootClazz, field, saveDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (saveDefault) {
|
||||||
|
try {
|
||||||
|
source.save();
|
||||||
|
} catch (IOException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initSection(FileConfig source, String parentSection, Class<?> clazz, boolean saveDefault) {
|
||||||
|
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
||||||
|
|
||||||
|
String section = getSection(clazz.getSimpleName(), parentSection, clazz.getAnnotation(MessagesSection.class));
|
||||||
|
|
||||||
|
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(source, section, innerClass, saveDefault);
|
||||||
|
for (Field field : clazz.getFields()) initMessage(source, section, clazz, field, saveDefault);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initMessage(FileConfig source, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
||||||
|
try {
|
||||||
|
String section = getSection(field.getName(), parentSection, field.getAnnotation(MessagesSection.class));
|
||||||
|
|
||||||
|
Object object = field.get(clazz);
|
||||||
|
|
||||||
|
if (object instanceof EasyMessage) {
|
||||||
|
EasyMessage message = ((EasyMessage) object);
|
||||||
|
if (saveDefault && message.defaultValue != null && !source.getConfig().contains(section)) {
|
||||||
|
source.getConfig().set(section, message.defaultValue);
|
||||||
|
}
|
||||||
|
message.initialize(source, section);
|
||||||
|
} else if (object instanceof EasyMessageList) {
|
||||||
|
EasyMessageList messageList = ((EasyMessageList) object);
|
||||||
|
|
||||||
|
if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) {
|
||||||
|
source.getConfig().set(section, Arrays.asList(messageList.defaultValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
messageList.initialize(source, section);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String getSection(@NotNull String name,
|
||||||
|
@Nullable String parentSection,
|
||||||
|
@Nullable MessagesSection sectionAnnotation) {
|
||||||
|
String parent = parentSection != null ? parentSection + "." : "";
|
||||||
|
if (sectionAnnotation != null && sectionAnnotation.value().length() > 0) {
|
||||||
|
return parent + sectionAnnotation.value();
|
||||||
|
} else {
|
||||||
|
return parent + getSectionName(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getSectionName(String codeName) {
|
||||||
|
return codeName.toLowerCase().replace("_", "-");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
public abstract class MessagesRoot {
|
||||||
|
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package cc.carm.lib.easyplugin.configuration.language;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface MessagesSection {
|
||||||
|
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +1,44 @@
|
|||||||
|
|
||||||
package cc.carm.lib.easyplugin.utils;
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ColorParser {
|
public class ColorParser {
|
||||||
|
|
||||||
public static String parse(String text) {
|
public static String parse(String text) {
|
||||||
text = parseHexColor(text);
|
text = parseHexColor(text);
|
||||||
return parseColor(text);
|
return parseColor(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String parseColor(final String text) {
|
public static String[] parse(String... texts) {
|
||||||
return text.replaceAll("&", "§").replace("§§", "&");
|
return parse(Arrays.asList(texts)).toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String parseHexColor(String text) {
|
public static List<String> parse(List<String> texts) {
|
||||||
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
|
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
|
||||||
Matcher matcher = pattern.matcher(text);
|
}
|
||||||
while (matcher.find()) {
|
|
||||||
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
|
public static String parseColor(final String text) {
|
||||||
hexColor = hexColor.replace("&", "");
|
return text.replaceAll("&", "§").replace("§§", "&");
|
||||||
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
|
}
|
||||||
for (int i = 1; i < hexColor.length(); i++) {
|
|
||||||
bukkitColorCode.append('§').append(hexColor.charAt(i));
|
public static String parseHexColor(String text) {
|
||||||
}
|
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
|
||||||
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
|
Matcher matcher = pattern.matcher(text);
|
||||||
matcher.reset(text);
|
while (matcher.find()) {
|
||||||
}
|
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
|
||||||
return text;
|
hexColor = hexColor.replace("&", "");
|
||||||
}
|
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
|
||||||
|
for (int i = 1; i < hexColor.length(); i++) {
|
||||||
|
bukkitColorCode.append('§').append(hexColor.charAt(i));
|
||||||
|
}
|
||||||
|
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
|
||||||
|
matcher.reset(text);
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,103 +14,104 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class MessageUtils {
|
public class MessageUtils {
|
||||||
|
|
||||||
public static boolean hasPlaceholderAPI() {
|
public static boolean hasPlaceholderAPI() {
|
||||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void send(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return;
|
|
||||||
for (String s : messages) {
|
|
||||||
sender.sendMessage(ColorParser.parse(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void send(@Nullable CommandSender sender, String... messages) {
|
|
||||||
send(sender, Arrays.asList(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
|
||||||
sendWithPlaceholders(sender, Arrays.asList(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return;
|
|
||||||
send(sender, setPlaceholders(sender, messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
|
|
||||||
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
|
||||||
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
|
|
||||||
if (message == null || sender == null) return message;
|
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
|
||||||
} else {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
|
||||||
} else {
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender, String message, String[] params, Object[] values) {
|
|
||||||
return setPlaceholders(sender, setCustomParams(message, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
|
||||||
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setCustomParams(String message, String param, Object value) {
|
|
||||||
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setCustomParams(String message, String[] params, Object[] values) {
|
|
||||||
if (params.length != values.length) return message;
|
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
paramsMap.put(params[i], values[i]);
|
|
||||||
}
|
|
||||||
return setCustomParams(message, paramsMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static String setCustomParams(String message, HashMap<String, Object> params) {
|
public static void send(@Nullable CommandSender sender, String... messages) {
|
||||||
String afterMessage = message;
|
send(sender, Arrays.asList(messages));
|
||||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
}
|
||||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
|
||||||
}
|
|
||||||
return afterMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
|
public static void send(@Nullable CommandSender sender, List<String> messages) {
|
||||||
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
if (messages == null || messages.isEmpty() || sender == null) return;
|
||||||
}
|
for (String s : messages) {
|
||||||
|
sender.sendMessage(ColorParser.parse(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
|
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
||||||
if (params.length != values.length) return messages;
|
sendWithPlaceholders(sender, Arrays.asList(messages));
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
}
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
paramsMap.put(params[i], values[i]);
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
||||||
}
|
if (messages == null || messages.isEmpty() || sender == null) return;
|
||||||
return setCustomParams(messages, paramsMap);
|
send(sender, setPlaceholders(sender, messages));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
|
||||||
|
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
||||||
|
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
|
||||||
|
if (message == null || sender == null) return message;
|
||||||
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
|
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
||||||
|
} else {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
||||||
|
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
||||||
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
|
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
||||||
|
} else {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setPlaceholders(@Nullable CommandSender sender, String message, String[] params, Object[] values) {
|
||||||
|
return setPlaceholders(sender, setCustomParams(message, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
||||||
|
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setCustomParams(String message, String param, Object value) {
|
||||||
|
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setCustomParams(String message, String[] params, Object[] values) {
|
||||||
|
if (params.length != values.length) return message;
|
||||||
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
paramsMap.put(params[i], values[i]);
|
||||||
|
}
|
||||||
|
return setCustomParams(message, paramsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
public static String setCustomParams(String message, HashMap<String, Object> params) {
|
||||||
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList());
|
String afterMessage = message;
|
||||||
}
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||||
|
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||||
|
}
|
||||||
|
return afterMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
|
||||||
|
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
|
||||||
|
if (params.length != values.length) return messages;
|
||||||
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
paramsMap.put(params[i], values[i]);
|
||||||
|
}
|
||||||
|
return setCustomParams(messages, paramsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
||||||
|
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.2.0</version>
|
<version>1.3.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
|
|
||||||
<module>easyplugin-bom</module>
|
<module>easyplugin-bom</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user