1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-05 00:58:17 +08:00

Compare commits

..

7 Commits

Author SHA1 Message Date
carm 511d8d77c4 部署项目 2022-01-30 08:03:37 +08:00
carm 96ff3398c8 不打包javadoc 2022-01-30 07:45:48 +08:00
carm 42860c332f [v1.3.7] 版本更新
- [U] 更新 EasySQL 版本到 0.3.5 。
2022-01-30 07:36:35 +08:00
carm 5ea196b6df [v1.3.7] 版本更新
- [F] 修复Javadoc内容不继承的问题。
- [U] 更新 EasySQL 版本到 0.3.1 。
2022-01-26 04:22:49 +08:00
carm fe34bbc17d [v1.3.6] 更新 EasySQL 版本到 0.3.0 。 2022-01-26 02:34:43 +08:00
carm c575805c72 [v1.3.5] 消息配置文件相关更新
- [U] 优化消息发送逻辑,不再向玩家发送空消息。
2022-01-14 18:41:16 +08:00
carm 9c73fa81bc [v1.3.4] 配置文件相关更新
- [U] 采用Supplier方式获取指定配置文件源,防止static初始化时相关配置还未完成初始化。
- [A] 添加 ConfigItem ,快速获得简易的物品配置。
2022-01-14 12:16:35 +08:00
27 changed files with 674 additions and 404 deletions
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+5 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -59,6 +59,10 @@
<type>pom</type> <type>pom</type>
<exclusions> <exclusions>
<exclusion>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-github</artifactId>
</exclusion>
<exclusion> <exclusion>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-placeholderapi</artifactId> <artifactId>easyplugin-placeholderapi</artifactId>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
public class ConfigSectionCast<V> extends FileConfigCachedValue<V> { public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
@@ -22,13 +23,19 @@ public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
public ConfigSectionCast(@NotNull String sectionName, public ConfigSectionCast(@NotNull String sectionName,
@NotNull Function<ConfigurationSection, V> valueCast, @NotNull Function<ConfigurationSection, V> valueCast,
@Nullable V defaultValue) { @Nullable V defaultValue) {
this(null, sectionName, valueCast, defaultValue); this((Supplier<FileConfig>) null, sectionName, valueCast, defaultValue);
} }
public ConfigSectionCast(@Nullable FileConfig source, @NotNull String sectionName, public ConfigSectionCast(@Nullable FileConfig source, @NotNull String sectionName,
@NotNull Function<ConfigurationSection, V> valueCast, @NotNull Function<ConfigurationSection, V> valueCast,
@Nullable V defaultValue) { @Nullable V defaultValue) {
super(source, sectionName); this(source == null ? null : () -> source, sectionName, valueCast, defaultValue);
}
public ConfigSectionCast(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@NotNull Function<ConfigurationSection, V> valueCast,
@Nullable V defaultValue) {
super(provider, sectionName);
this.valueCast = valueCast; this.valueCast = valueCast;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@@ -51,7 +58,6 @@ public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
} }
public void set(ConfigurationSection section) { public void set(ConfigurationSection section) {
} }
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
public class ConfigStringCast<V> extends FileConfigCachedValue<V> { public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
@@ -21,13 +22,19 @@ public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
public ConfigStringCast(@NotNull String configSection, public ConfigStringCast(@NotNull String configSection,
@NotNull Function<String, V> valueCast, @NotNull Function<String, V> valueCast,
@Nullable V defaultValue) { @Nullable V defaultValue) {
this(null, configSection, valueCast, defaultValue); this((Supplier<FileConfig>) null, configSection, valueCast, defaultValue);
} }
public ConfigStringCast(@Nullable FileConfig source, @NotNull String sectionName, public ConfigStringCast(@Nullable FileConfig source, @NotNull String sectionName,
@NotNull Function<String, V> valueCast, @NotNull Function<String, V> valueCast,
@Nullable V defaultValue) { @Nullable V defaultValue) {
super(source, sectionName); this(source == null ? null : () -> source, sectionName, valueCast, defaultValue);
}
public ConfigStringCast(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@NotNull Function<String, V> valueCast,
@Nullable V defaultValue) {
super(provider, sectionName);
this.valueCast = valueCast; this.valueCast = valueCast;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@@ -3,6 +3,8 @@ package cc.carm.lib.easyplugin.configuration.file;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
public abstract class FileConfigCachedValue<V> extends FileConfigValue { public abstract class FileConfigCachedValue<V> extends FileConfigValue {
protected V cachedValue; protected V cachedValue;
@@ -12,10 +14,10 @@ public abstract class FileConfigCachedValue<V> extends FileConfigValue {
super(sectionName); super(sectionName);
} }
public FileConfigCachedValue(@Nullable FileConfig source, @NotNull String sectionName) { public FileConfigCachedValue(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName) {
super(source, sectionName); super(provider, sectionName);
} }
public V updateCache(V value) { public V updateCache(V value) {
this.updateTime = System.currentTimeMillis(); this.updateTime = System.currentTimeMillis();
this.cachedValue = value; this.cachedValue = value;
@@ -6,18 +6,19 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier;
public abstract class FileConfigValue { public abstract class FileConfigValue {
protected @Nullable FileConfig source; protected @Nullable Supplier<FileConfig> provider;
private final @NotNull String sectionName; private final @NotNull String sectionName;
public FileConfigValue(@NotNull String sectionName) { public FileConfigValue(@NotNull String sectionName) {
this(null, sectionName); this(null, sectionName);
} }
public FileConfigValue(@Nullable FileConfig source, @NotNull String sectionName) { public FileConfigValue(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName) {
this.source = source; this.provider = provider;
this.sectionName = sectionName; this.sectionName = sectionName;
} }
@@ -52,7 +53,7 @@ public abstract class FileConfigValue {
} }
public @Nullable FileConfig getSource() { public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getPluginConfiguration() : source; return provider == null || provider.get() == null ? defaultSource() : provider.get();
} }
public Optional<FileConfig> getSourceOptional() { public Optional<FileConfig> getSourceOptional() {
@@ -71,4 +72,8 @@ public abstract class FileConfigValue {
return clazz.isInstance(val) ? clazz.cast(val) : defaultValue; return clazz.isInstance(val) ? clazz.cast(val) : defaultValue;
} }
public FileConfig defaultSource() {
return FileConfig.getPluginConfiguration();
}
} }
@@ -0,0 +1,152 @@
package cc.carm.lib.easyplugin.configuration.impl;
import cc.carm.lib.easyplugin.configuration.cast.ConfigSectionCast;
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
public class ConfigItem extends ConfigSectionCast<ConfigItem.ItemConfiguration> {
@Nullable
String[] itemParams;
public ConfigItem(@NotNull String configSection) {
this(configSection, null, null);
}
public ConfigItem(@NotNull String configSection,
@Nullable String[] itemParams) {
this(configSection, itemParams, null);
}
public ConfigItem(@NotNull String configSection,
@Nullable ItemConfiguration defaultValue) {
this(configSection, null, defaultValue);
}
public ConfigItem(@NotNull String sectionName,
@Nullable String[] itemParams,
@Nullable ItemConfiguration defaultValue) {
this(null, sectionName, itemParams, defaultValue);
}
public ConfigItem(@Nullable Supplier<FileConfig> provider,
@NotNull String sectionName,
@Nullable String[] itemParams,
@Nullable ItemConfiguration defaultValue) {
this(provider, sectionName, ConfigItem::parseItemConfiguration, itemParams, defaultValue);
}
public ConfigItem(@Nullable Supplier<FileConfig> provider,
@NotNull String sectionName,
@NotNull Function<ConfigurationSection, ItemConfiguration> valueCast,
@Nullable String[] itemParams,
@Nullable ItemConfiguration defaultValue) {
super(provider, sectionName, valueCast, defaultValue);
this.itemParams = itemParams;
}
public ItemStack getItem(@NotNull Player player) {
return getItem(player, null);
}
public ItemStack getItem(@NotNull Player player,
@Nullable Object[] values) {
if (values != null && itemParams != null && itemParams.length > 0) {
return getItem(player, itemParams, values);
} else {
return getItem(player, null, null);
}
}
public ItemStack getItem(@NotNull Player player,
@Nullable String[] params,
@Nullable Object[] values) {
params = params == null ? new String[0] : params;
values = values == null ? new Object[0] : values;
ItemConfiguration configuration = get();
if (configuration == null) return null;
ItemStackFactory factory = new ItemStackFactory(configuration.getType());
if (configuration.getName() != null) {
factory.setDisplayName(MessageUtils.setCustomParams(configuration.getName(), params, values));
}
if (configuration.getLore() != null) {
factory.setLore(MessageUtils.setCustomParams(configuration.getLore(), params, values));
}
return factory.toItemStack();
}
public static class ItemConfiguration {
@NotNull Material type;
@Nullable String name;
@Nullable List<String> lore;
public ItemConfiguration(@NotNull Material type, @Nullable String name, @Nullable List<String> lore) {
this.type = type;
this.name = name;
this.lore = lore;
}
public @NotNull Material getType() {
return type;
}
public @Nullable String getName() {
return name;
}
public @Nullable List<String> getLore() {
return lore;
}
public static ItemConfiguration readFrom(@NotNull ConfigurationSection section) {
String typeName = section.getString("type");
if (typeName == null) return null;
Material type = Material.matchMaterial(typeName);
if (type == null) return null;
String name = section.getString("name");
List<String> lore = section.getStringList("lore");
return new ItemConfiguration(type, name, lore);
}
}
private static ItemConfiguration parseItemConfiguration(@NotNull ConfigurationSection section) {
return ItemConfiguration.readFrom(section);
}
@NotNull
public static ItemConfiguration create(@NotNull Material type) {
return create(type, null);
}
@NotNull
public static ItemConfiguration create(@NotNull Material type,
@Nullable String name) {
return create(type, name, null);
}
@NotNull
public static ItemConfiguration create(@NotNull Material type,
@Nullable String name,
@Nullable String[] lore) {
return new ItemConfiguration(type, name, lore == null ? null : Arrays.asList(lore));
}
}
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> { public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
@@ -18,12 +19,20 @@ public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
public ConfigSound(@NotNull String configSection, public ConfigSound(@NotNull String configSection,
@Nullable Sound defaultValue) { @Nullable Sound defaultValue) {
this(null, configSection, defaultValue); this((Supplier<FileConfig>) null, configSection, defaultValue);
} }
public ConfigSound(@Nullable FileConfig source, @NotNull String configSection, public ConfigSound(@Nullable FileConfig source, @NotNull String configSection,
@Nullable Sound defaultValue) { @Nullable Sound defaultValue) {
super(source, configSection, getSoundParser(), defaultValue == null ? null : new SoundData(defaultValue)); this(source == null ? null : () -> source, configSection, defaultValue);
}
public ConfigSound(@Nullable Supplier<FileConfig> provider, @NotNull String configSection,
@Nullable Sound defaultValue) {
super(provider, configSection,
getSoundParser(),
defaultValue == null ? null : new SoundData(defaultValue)
);
} }
public void set(@Nullable SoundData value) { public void set(@Nullable SoundData value) {
@@ -4,102 +4,116 @@ import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import cc.carm.lib.easyplugin.configuration.values.ConfigValue; import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
import cc.carm.lib.easyplugin.utils.ColorParser; import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.lib.easyplugin.utils.MessageUtils;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collections;
public class EasyMessage { public class EasyMessage {
@Nullable ConfigValue<String> configValue; @Nullable ConfigValue<String> configValue;
@Nullable String defaultValue; @Nullable String defaultValue;
@Nullable String[] messageParams; @Nullable String[] messageParams;
public EasyMessage() { public EasyMessage() {
this(null); this(null);
} }
public EasyMessage(@Nullable String defaultValue) { public EasyMessage(@Nullable String defaultValue) {
this(defaultValue, null); this(defaultValue, null);
} }
public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) { public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.messageParams = messageParams; this.messageParams = messageParams;
} }
public void initialize(@NotNull FileConfig sourceConfig, @NotNull String sectionName) { public void initialize(@NotNull FileConfig source, @NotNull String sectionName) {
this.configValue = new ConfigValue<>(sourceConfig, sectionName, String.class, getDefaultValue()); this.configValue = new ConfigValue<>(() -> source, sectionName, String.class, getDefaultValue());
} }
private @Nullable String getDefaultValue() { private @Nullable String getDefaultValue() {
return defaultValue; return defaultValue;
} }
private @Nullable String[] getMessageParams() { private @Nullable String[] getMessageParams() {
return messageParams; return messageParams;
} }
private @NotNull String getDefaultMessages() { private @NotNull String getDefaultMessages() {
if (getDefaultValue() == null) return ""; if (getDefaultValue() == null) return "";
else return getDefaultValue(); else return getDefaultValue();
} }
private @NotNull String getMessages() { private @NotNull String getMessages() {
if (configValue == null) { if (configValue == null) {
return getDefaultMessages(); return getDefaultMessages();
} else { } else {
return configValue.get(); return configValue.get();
} }
} }
public @NotNull String get(@Nullable CommandSender sender) { public @NotNull String get(@Nullable CommandSender sender) {
return get(sender, null); return get(sender, null);
} }
public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) { public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) {
return get(sender, getMessageParams(), values); return get(sender, getMessageParams(), values);
} }
public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) { public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
if (sender == null) return getMessages(); String messages = getMessages();
if (params == null || values == null) { if (sender == null || messages.length() < 1) return messages;
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages())); params = params == null ? new String[0] : params;
} else { values = values == null ? new Object[0] : values;
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages(), params, values)); return ColorParser.parse(MessageUtils.setPlaceholders(sender, messages, params, values));
} }
}
public void send(@Nullable CommandSender sender) { public void send(@Nullable CommandSender sender) {
send(sender, null); send(sender, null);
} }
public void send(@Nullable CommandSender sender, @Nullable Object[] values) { public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
send(sender, getMessageParams(), values); send(sender, getMessageParams(), values);
} }
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) { public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
if (params == null || values == null) { String message = get(sender, params, values);
MessageUtils.sendWithPlaceholders(sender, getMessages()); if (message.length() < 1) return;
} else { MessageUtils.send(sender, message);
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(getMessages()), params, values); }
}
}
public void sendToAll() { public void sendBar(@Nullable Player player) {
sendToAll(null); sendBar(player, null);
} }
public void sendToAll(@Nullable Object[] values) { public void sendBar(@Nullable Player player, @Nullable Object[] values) {
sendToAll(messageParams, values); sendBar(player, getMessageParams(), values);
} }
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) { public void sendBar(@Nullable Player player, @Nullable String[] params, @Nullable Object[] values) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values)); if (player == null) return;
} String message = get(player, params, values);
if (message.length() < 1) return;
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(get(player, 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));
}
} }
@@ -2,6 +2,7 @@ package cc.carm.lib.easyplugin.configuration.language;
import cc.carm.lib.easyplugin.configuration.file.FileConfig; import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList; import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -15,96 +16,93 @@ import java.util.List;
public class EasyMessageList { public class EasyMessageList {
@Nullable ConfigValueList<String> configValue; @Nullable ConfigValueList<String> configValue;
@Nullable String[] defaultValue; @Nullable String[] defaultValue;
@Nullable String[] messageParams; @Nullable String[] messageParams;
public EasyMessageList() { public EasyMessageList() {
this((String[]) null); this((String[]) null);
} }
public EasyMessageList(@Nullable String... defaultValue) { public EasyMessageList(@Nullable String... defaultValue) {
this(defaultValue, null); this(defaultValue, null);
} }
public EasyMessageList(@Nullable String[] defaultValue, public EasyMessageList(@Nullable String[] defaultValue,
@Nullable String[] messageParams) { @Nullable String[] messageParams) {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.messageParams = messageParams; this.messageParams = messageParams;
} }
public void initialize(FileConfig sourceConfig, String sectionName) { public void initialize(FileConfig sourceConfig, String sectionName) {
configValue = new ConfigValueList<>(sourceConfig, sectionName, String.class, getDefaultValue()); configValue = new ConfigValueList<>(() -> sourceConfig, sectionName, String.class, getDefaultValue());
} }
private @Nullable String[] getDefaultValue() { private @Nullable String[] getDefaultValue() {
return defaultValue; return defaultValue;
} }
@Unmodifiable @Unmodifiable
private @NotNull List<String> getDefaultMessages() { private @NotNull List<String> getDefaultMessages() {
if (getDefaultValue() == null) return new ArrayList<>(); if (getDefaultValue() == null) return new ArrayList<>();
else return Arrays.asList(getDefaultValue()); else return Arrays.asList(getDefaultValue());
} }
private @Nullable String[] getMessageParams() {
return messageParams;
}
private @Nullable String[] getMessageParams() { private @NotNull List<String> getMessages() {
return messageParams; if (configValue == null) {
} return getDefaultMessages();
} else {
return configValue.get();
}
}
private @NotNull List<String> getMessages() { public @NotNull List<String> get(@Nullable CommandSender sender) {
if (configValue == null) { return get(sender, null);
return getDefaultMessages(); }
} else {
return configValue.get();
}
}
public @NotNull List<String> get(@Nullable CommandSender sender) { public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) {
return get(sender, null); return get(sender, getMessageParams(), values);
} }
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) { public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
return get(sender, getMessageParams(), values); if (sender == null) return getMessages();
} params = params == null ? new String[0] : params;
values = values == null ? new Object[0] : values;
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) { return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages(), params, 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) { public void send(@Nullable CommandSender sender) {
send(sender, null); send(sender, null);
} }
public void send(@Nullable CommandSender sender, @Nullable Object[] values) { public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
send(sender, getMessageParams(), values); send(sender, getMessageParams(), values);
} }
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) { public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
if (params == null || values == null) { List<String> messages = get(sender, params, values);
MessageUtils.sendWithPlaceholders(sender, getMessages()); if (messages.isEmpty()) return;
} else { if (messages.size() == 1 && messages.get(0).length() == 0) return; //空消息不再发送
MessageUtils.sendWithPlaceholders(sender, getMessages(), params, values); MessageUtils.send(sender, messages);
} }
}
public void sendToAll() { public void sendToAll() {
sendToAll(null); sendToAll(null);
} }
public void sendToAll(@Nullable Object[] values) { public void sendToAll(@Nullable Object[] values) {
sendToAll(messageParams, values); sendToAll(messageParams, values);
} }
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) { public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values)); Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
} }
} }
@@ -11,90 +11,90 @@ import java.util.Arrays;
public class MessagesInitializer { public class MessagesInitializer {
private MessagesInitializer() { private MessagesInitializer() {
// 静态方法类,不应当实例化。 // 静态方法类,不应当实例化。
} }
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz) { public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz) {
initialize(source, rootClazz, true); initialize(source, rootClazz, true);
} }
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) { public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) {
MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class); MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class);
String rootSection = null; String rootSection = null;
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) { if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
rootSection = sectionAnnotation.value(); rootSection = sectionAnnotation.value();
} }
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) { for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
initSection(source, rootSection, innerClass, saveDefault); initSection(source, rootSection, innerClass, saveDefault);
} }
for (Field field : rootClazz.getFields()) { for (Field field : rootClazz.getFields()) {
initMessage(source, rootSection, rootClazz, field, saveDefault); initMessage(source, rootSection, rootClazz, field, saveDefault);
} }
if (saveDefault) { if (saveDefault) {
try { try {
source.save(); source.save();
} catch (IOException ignore) { } catch (IOException ignore) {
} }
} }
} }
private static void initSection(FileConfig source, String parentSection, Class<?> clazz, boolean saveDefault) { private static void initSection(FileConfig source, String parentSection, Class<?> clazz, boolean saveDefault) {
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return; if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
String section = getSection(clazz.getSimpleName(), parentSection, clazz.getAnnotation(MessagesSection.class)); String section = getSection(clazz.getSimpleName(), parentSection, clazz.getAnnotation(MessagesSection.class));
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(source, section, innerClass, saveDefault); for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(source, section, innerClass, saveDefault);
for (Field field : clazz.getFields()) initMessage(source, section, clazz, field, 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) { private static void initMessage(FileConfig source, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
try { try {
String section = getSection(field.getName(), parentSection, field.getAnnotation(MessagesSection.class)); String section = getSection(field.getName(), parentSection, field.getAnnotation(MessagesSection.class));
Object object = field.get(clazz); Object object = field.get(clazz);
if (object instanceof EasyMessage) { if (object instanceof EasyMessage) {
EasyMessage message = ((EasyMessage) object); EasyMessage message = ((EasyMessage) object);
if (saveDefault && message.defaultValue != null && !source.getConfig().contains(section)) { if (saveDefault && message.defaultValue != null && !source.getConfig().contains(section)) {
source.getConfig().set(section, message.defaultValue); source.getConfig().set(section, message.defaultValue);
} }
message.initialize(source, section); message.initialize(source, section);
} else if (object instanceof EasyMessageList) { } else if (object instanceof EasyMessageList) {
EasyMessageList messageList = ((EasyMessageList) object); EasyMessageList messageList = ((EasyMessageList) object);
if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) { if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) {
source.getConfig().set(section, Arrays.asList(messageList.defaultValue)); source.getConfig().set(section, Arrays.asList(messageList.defaultValue));
} }
messageList.initialize(source, section); messageList.initialize(source, section);
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static String getSection(@NotNull String name, private static String getSection(@NotNull String name,
@Nullable String parentSection, @Nullable String parentSection,
@Nullable MessagesSection sectionAnnotation) { @Nullable MessagesSection sectionAnnotation) {
String parent = parentSection != null ? parentSection + "." : ""; String parent = parentSection != null ? parentSection + "." : "";
if (sectionAnnotation != null && sectionAnnotation.value().length() > 0) { if (sectionAnnotation != null && sectionAnnotation.value().length() > 0) {
return parent + sectionAnnotation.value(); return parent + sectionAnnotation.value();
} else { } else {
return parent + getSectionName(name); return parent + getSectionName(name);
} }
} }
private static String getSectionName(String codeName) { private static String getSectionName(String codeName) {
return codeName.toLowerCase().replace("_", "-"); return codeName.toLowerCase().replace("_", "-");
} }
} }
@@ -3,13 +3,17 @@ package cc.carm.lib.easyplugin.configuration.message;
import cc.carm.lib.easyplugin.configuration.file.FileConfig; import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import cc.carm.lib.easyplugin.configuration.values.ConfigValue; import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.lib.easyplugin.utils.MessageUtils;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collections; import java.util.function.Supplier;
public class ConfigMessage extends ConfigValue<String> { public class ConfigMessage extends ConfigValue<String> {
@@ -23,70 +27,88 @@ public class ConfigMessage extends ConfigValue<String> {
this(sectionName, defaultValue, null); this(sectionName, defaultValue, null);
} }
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue, String[] messageParams) { public ConfigMessage(@NotNull String sectionName,
super(null, sectionName, String.class, defaultValue); @Nullable String defaultValue,
this.messageParams = messageParams; String[] messageParams) {
this((Supplier<FileConfig>) null, sectionName, defaultValue, messageParams);
} }
public ConfigMessage(@Nullable FileConfig source, @NotNull String sectionName, public ConfigMessage(@Nullable FileConfig source, @NotNull String sectionName,
@Nullable String defaultValue, String[] messageParams) { @Nullable String defaultValue, String[] messageParams) {
super(source, sectionName, String.class, defaultValue); this(source == null ? null : () -> source, sectionName, defaultValue, messageParams);
}
public ConfigMessage(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@Nullable String defaultValue, String[] messageParams) {
super(provider, sectionName, String.class, defaultValue);
this.messageParams = messageParams; this.messageParams = messageParams;
} }
public @NotNull String get(CommandSender sender) { public String[] getMessageParams() {
return MessageUtils.setPlaceholders(sender, get()); return messageParams;
} }
public @NotNull String get(CommandSender sender, Object[] values) { public @NotNull String get(@Nullable CommandSender sender) {
if (messageParams != null) { return get(sender, null);
return get(sender, messageParams, values);
} else {
return get(sender);
}
} }
public @NotNull String get(CommandSender sender, String[] params, Object[] values) { public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) {
return MessageUtils.setPlaceholders(sender, get(), params, values); return get(sender, getMessageParams(), values);
} }
public void send(CommandSender sender) { public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
MessageUtils.sendWithPlaceholders(sender, get()); String messages = get();
if (sender == null || messages.length() < 1) return messages;
params = params == null ? new String[0] : params;
values = values == null ? new Object[0] : values;
return ColorParser.parse(MessageUtils.setPlaceholders(sender, messages, params, values));
} }
public void send(CommandSender sender, Object[] values) { public void send(@Nullable CommandSender sender) {
if (messageParams != null) { send(sender, null);
send(sender, messageParams, values);
} else {
send(sender, new String[0], new Object[0]);
}
} }
public void send(CommandSender sender, String[] params, Object[] values) { public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(get()), params, values); send(sender, getMessageParams(), values);
}
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
String message = get(sender, params, values);
if (message.length() < 1) return;
MessageUtils.send(sender, message);
}
public void sendBar(@Nullable Player player) {
sendBar(player, null);
}
public void sendBar(@Nullable Player player, @Nullable Object[] values) {
sendBar(player, getMessageParams(), values);
}
public void sendBar(@Nullable Player player, @Nullable String[] params, @Nullable Object[] values) {
if (player == null) return;
String message = get(player, params, values);
if (message.length() < 1) return;
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(get(player, params, values)));
} }
public void sendToAll() { public void sendToAll() {
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, Collections.singletonList(get()))); sendToAll(null);
} }
public void sendToAll(Object[] values) { public void sendToAll(@Nullable Object[] values) {
if (messageParams != null) { sendToAll(messageParams, values);
sendToAll(messageParams, values);
} else {
sendToAll();
}
} }
public void sendToAll(String[] params, Object[] values) { public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, Collections.singletonList(get()), params, values)); Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
} }
@Override @Override
public @Nullable FileConfig getSource() { public FileConfig defaultSource() {
return source == null ? FileConfig.getMessageConfiguration() : source; return FileConfig.getMessageConfiguration();
} }
} }
@@ -3,6 +3,7 @@ package cc.carm.lib.easyplugin.configuration.message;
import cc.carm.lib.easyplugin.configuration.file.FileConfig; import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList; import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.MessageUtils; import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -10,6 +11,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
public class ConfigMessageList extends ConfigValueList<String> { public class ConfigMessageList extends ConfigValueList<String> {
@@ -23,68 +25,73 @@ public class ConfigMessageList extends ConfigValueList<String> {
this(sectionName, defaultValue, null); this(sectionName, defaultValue, null);
} }
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue, String[] messageParams) { public ConfigMessageList(@NotNull String sectionName,
super(null, sectionName, String.class, defaultValue); @Nullable String[] defaultValue,
this.messageParams = messageParams; String[] messageParams) {
this((Supplier<FileConfig>) null, sectionName, defaultValue, null);
} }
public ConfigMessageList(@Nullable FileConfig source, @NotNull String sectionName, public ConfigMessageList(@Nullable FileConfig source, @NotNull String sectionName,
@Nullable String[] defaultValue, String[] messageParams) { @Nullable String[] defaultValue, String[] messageParams) {
super(source, sectionName, String.class, defaultValue); this(source == null ? null : () -> source, sectionName, defaultValue, messageParams);
}
public ConfigMessageList(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@Nullable String[] defaultValue, String[] messageParams) {
super(provider, sectionName, String.class, defaultValue);
this.messageParams = messageParams; this.messageParams = messageParams;
} }
public String[] getMessageParams() {
return messageParams;
}
public @NotNull List<String> get(@Nullable CommandSender sender) { public @NotNull List<String> get(@Nullable CommandSender sender) {
return MessageUtils.setPlaceholders(sender, get()); return get(sender, null);
} }
public @NotNull List<String> get(@Nullable CommandSender sender, Object[] values) {
if (messageParams != null) { public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) {
return get(sender, messageParams, values); return get(sender, getMessageParams(), values);
} else {
return get(sender);
}
} }
public @NotNull List<String> get(@Nullable CommandSender sender, String[] params, Object[] values) { public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
return MessageUtils.setPlaceholders(sender, get(), params, values); if (sender == null) return get();
params = params == null ? new String[0] : params;
values = values == null ? new Object[0] : values;
return ColorParser.parse(MessageUtils.setPlaceholders(sender, get(), params, values));
} }
public void send(@Nullable CommandSender sender) { public void send(@Nullable CommandSender sender) {
MessageUtils.sendWithPlaceholders(sender, get()); send(sender, null);
} }
public void send(@Nullable CommandSender sender, Object[] values) { public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
if (messageParams != null) { send(sender, getMessageParams(), values);
send(sender, messageParams, values);
} else {
send(sender);
}
} }
public void send(@Nullable CommandSender sender, String[] params, Object[] values) { public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
MessageUtils.sendWithPlaceholders(sender, get(), params, values); List<String> messages = get(sender, params, values);
} if (messages.isEmpty()) return;
if (messages.size() == 1 && messages.get(0).length() == 0) return; //空消息不再发送
public void sendToAll(String[] params, Object[] values) { MessageUtils.send(sender, messages);
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, get(), params, values));
} }
public void sendToAll() { public void sendToAll() {
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, get())); sendToAll(null);
} }
public void sendToAll(Object[] values) { public void sendToAll(@Nullable Object[] values) {
if (messageParams != null) { sendToAll(messageParams, values);
sendToAll(messageParams, values); }
} else {
sendToAll(); public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
} Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
} }
@Override @Override
public @Nullable FileConfig getSource() { public FileConfig defaultSource() {
return source == null ? FileConfig.getMessageConfiguration() : source; return FileConfig.getMessageConfiguration();
} }
} }
@@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier;
public class ConfigValue<V> extends FileConfigValue { public class ConfigValue<V> extends FileConfigValue {
private final @NotNull Class<V> clazz; private final @NotNull Class<V> clazz;
@@ -19,13 +20,19 @@ public class ConfigValue<V> extends FileConfigValue {
public ConfigValue(@NotNull String sectionName, public ConfigValue(@NotNull String sectionName,
@NotNull Class<V> clazz, @NotNull Class<V> clazz,
@Nullable V defaultValue) { @Nullable V defaultValue) {
this(null, sectionName, clazz, defaultValue); this((Supplier<FileConfig>) null, sectionName, clazz, defaultValue);
} }
public ConfigValue(@Nullable FileConfig source, @NotNull String sectionName, public ConfigValue(@Nullable FileConfig source, @NotNull String sectionName,
@NotNull Class<V> clazz, @NotNull Class<V> clazz,
@Nullable V defaultValue) { @Nullable V defaultValue) {
super(source, sectionName); this(source == null ? null : () -> source, sectionName, clazz, defaultValue);
}
public ConfigValue(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@NotNull Class<V> clazz,
@Nullable V defaultValue) {
super(provider, sectionName);
this.clazz = clazz; this.clazz = clazz;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ConfigValueList<V> extends FileConfigValue { public class ConfigValueList<V> extends FileConfigValue {
@@ -28,15 +29,17 @@ public class ConfigValueList<V> extends FileConfigValue {
this(null, sectionName, clazz, defaultValue); this(null, sectionName, clazz, defaultValue);
} }
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName, public ConfigValueList(@Nullable Supplier<FileConfig> provider,
@NotNull String sectionName,
Class<V> clazz) { Class<V> clazz) {
this(configuration, sectionName, clazz, null); this(provider, sectionName, clazz, null);
} }
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName, public ConfigValueList(@Nullable Supplier<FileConfig> provider,
@NotNull String sectionName,
@NotNull Class<V> clazz, @NotNull Class<V> clazz,
@Nullable V[] defaultValue) { @Nullable V[] defaultValue) {
super(configuration, sectionName); super(provider, sectionName);
this.clazz = clazz; this.clazz = clazz;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> { public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
@@ -16,12 +17,18 @@ public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
public ConfigValueMap(@NotNull String sectionName, @NotNull Function<String, K> keyCast, public ConfigValueMap(@NotNull String sectionName, @NotNull Function<String, K> keyCast,
@NotNull Class<V> valueClazz) { @NotNull Class<V> valueClazz) {
this(null, sectionName, keyCast, valueClazz); this((Supplier<FileConfig>) null, sectionName, keyCast, valueClazz);
} }
public ConfigValueMap(@Nullable FileConfig source, @NotNull String sectionName, public ConfigValueMap(@Nullable FileConfig source, @NotNull String sectionName,
@NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) { @NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) {
super(source, sectionName); this(source == null ? null : () -> source, sectionName, keyCast, valueClazz);
}
public ConfigValueMap(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName,
@NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) {
super(provider, sectionName);
this.keyCast = keyCast; this.keyCast = keyCast;
this.valueClazz = valueClazz; this.valueClazz = valueClazz;
} }
+2 -2
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -54,7 +54,7 @@
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easysql-beecp</artifactId> <artifactId>easysql-beecp</artifactId>
<version>0.2.7</version> <version>0.3.5</version>
<optional>true</optional> <optional>true</optional>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -4,6 +4,8 @@ import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
@@ -14,104 +16,132 @@ 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, String... messages) { public static void send(@Nullable CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages)); send(sender, Arrays.asList(messages));
} }
public static void send(@Nullable CommandSender sender, List<String> messages) { public static void send(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return; if (messages == null || messages.isEmpty() || sender == null) return;
for (String s : messages) { for (String s : messages) {
sender.sendMessage(ColorParser.parse(s)); sender.sendMessage(ColorParser.parse(s));
} }
} }
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
sendWithPlaceholders(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) { public static void sendWithPlaceholders(CommandSender sender, String... messages) {
if (messages == null || messages.isEmpty() || sender == null) return; sendWithPlaceholders(sender, Arrays.asList(messages));
send(sender, setPlaceholders(sender, messages)); }
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) { public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value}); if (messages == null || messages.isEmpty() || sender == null) return;
} send(sender, setPlaceholders(sender, messages));
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) { public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values)); sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
} }
public static String setPlaceholders(@Nullable CommandSender sender, String message) { public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
if (message == null || sender == null) return message; sendWithPlaceholders(sender, setCustomParams(messages, params, values));
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) { public static String setPlaceholders(@Nullable CommandSender sender, @Nullable String message) {
if (messages == null || messages.isEmpty() || sender == null) return messages; if (message == null || sender == null) return message;
if (hasPlaceholderAPI() && sender instanceof Player) { if (hasPlaceholderAPI() && sender instanceof Player) {
return PlaceholderAPI.setPlaceholders((Player) sender, messages); return PlaceholderAPI.setPlaceholders((Player) sender, message);
} else { } else {
return messages; return message;
} }
} }
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) { @Nullable
String afterMessage = message; @Contract("_, !null -> !null")
for (Map.Entry<String, Object> entry : params.entrySet()) { public static List<String> setPlaceholders(@Nullable CommandSender sender,
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString()); @Nullable List<String> messages) {
} if (messages == null || messages.isEmpty() || sender == null) return messages;
return afterMessage; if (hasPlaceholderAPI() && sender instanceof Player) {
} return PlaceholderAPI.setPlaceholders((Player) sender, messages);
} else {
return messages;
}
}
public static List<String> setCustomParams(List<String> messages, String param, Object value) { public static String setPlaceholders(@Nullable CommandSender sender,
return setCustomParams(messages, new String[]{param}, new Object[]{value}); @NotNull String message,
} @Nullable String[] params,
@Nullable Object[] values) {
return setPlaceholders(sender, setCustomParams(message, params, values));
}
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) { public static List<String> setPlaceholders(@Nullable CommandSender sender,
if (params.length != values.length) return messages; @NotNull List<String> messages,
HashMap<String, Object> paramsMap = new HashMap<>(); @Nullable String[] params,
for (int i = 0; i < params.length; i++) { @Nullable Object[] values) {
paramsMap.put(params[i], values[i]); return setPlaceholders(sender, setCustomParams(messages, params, values));
} }
return setCustomParams(messages, paramsMap);
}
public static String setCustomParams(@NotNull String message,
@NotNull String param,
@NotNull Object value) {
return setCustomParams(message, new String[]{param}, new Object[]{value});
}
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) { @Nullable
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList()); @Contract("!null, _, _-> !null ; null, _, _->null ")
} public static String setCustomParams(@Nullable String message,
@Nullable String[] params,
@Nullable Object[] values) {
if (message == null) return null;
if (params == null || values == null) return message;
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);
}
@NotNull
public static String setCustomParams(@NotNull String message, @NotNull HashMap<String, Object> params) {
String afterMessage = message;
for (Map.Entry<String, Object> entry : params.entrySet()) {
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
}
return afterMessage;
}
@NotNull
public static List<String> setCustomParams(@NotNull List<String> messages,
@NotNull String param,
@NotNull Object value) {
return setCustomParams(messages, new String[]{param}, new Object[]{value});
}
@NotNull
public static List<String> setCustomParams(@NotNull List<String> messages,
@Nullable String[] params,
@Nullable Object[] values) {
if (params == null || values == null) return messages;
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);
}
@NotNull
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
return messages.stream()
.map(message -> setCustomParams(message, params))
.collect(Collectors.toList());
}
} }
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.3.3</version> <version>1.3.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -4
View File
@@ -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.3.3</version> <version>1.3.8</version>
<modules> <modules>
<module>easyplugin-main</module> <module>easyplugin-main</module>
@@ -160,9 +160,6 @@
<version>3.2.0</version> <version>3.2.0</version>
<configuration> <configuration>
<classifier>javadoc</classifier> <classifier>javadoc</classifier>
<links>
<link>https://javadoc.io/doc/org.jetbrains/annotations/</link>
</links>
<detectJavaApiLink>false</detectJavaApiLink> <detectJavaApiLink>false</detectJavaApiLink>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<charset>UTF-8</charset> <charset>UTF-8</charset>