mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 08:38:17 +08:00
[v1.3.4] 配置文件相关更新
- [U] 采用Supplier方式获取指定配置文件源,防止static初始化时相关配置还未完成初始化。 - [A] 添加 ConfigItem ,快速获得简易的物品配置。
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
+9
-3
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
|
||||
|
||||
@@ -22,13 +23,19 @@ public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
|
||||
public ConfigSectionCast(@NotNull String sectionName,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, sectionName, valueCast, defaultValue);
|
||||
this((Supplier<FileConfig>) null, sectionName, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigSectionCast(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@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.defaultValue = defaultValue;
|
||||
}
|
||||
@@ -51,7 +58,6 @@ public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void set(ConfigurationSection section) {
|
||||
}
|
||||
|
||||
|
||||
+9
-2
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
|
||||
|
||||
@@ -21,13 +22,19 @@ public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
|
||||
public ConfigStringCast(@NotNull String configSection,
|
||||
@NotNull Function<String, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, configSection, valueCast, defaultValue);
|
||||
this((Supplier<FileConfig>) null, configSection, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigStringCast(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Function<String, V> valueCast,
|
||||
@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.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
+4
-2
@@ -3,6 +3,8 @@ package cc.carm.lib.easyplugin.configuration.file;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class FileConfigCachedValue<V> extends FileConfigValue {
|
||||
|
||||
protected V cachedValue;
|
||||
@@ -12,8 +14,8 @@ public abstract class FileConfigCachedValue<V> extends FileConfigValue {
|
||||
super(sectionName);
|
||||
}
|
||||
|
||||
public FileConfigCachedValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
||||
super(source, sectionName);
|
||||
public FileConfigCachedValue(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName) {
|
||||
super(provider, sectionName);
|
||||
}
|
||||
|
||||
public V updateCache(V value) {
|
||||
|
||||
+9
-4
@@ -6,18 +6,19 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class FileConfigValue {
|
||||
|
||||
protected @Nullable FileConfig source;
|
||||
protected @Nullable Supplier<FileConfig> provider;
|
||||
private final @NotNull String sectionName;
|
||||
|
||||
public FileConfigValue(@NotNull String sectionName) {
|
||||
this(null, sectionName);
|
||||
}
|
||||
|
||||
public FileConfigValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
||||
this.source = source;
|
||||
public FileConfigValue(@Nullable Supplier<FileConfig> provider, @NotNull String sectionName) {
|
||||
this.provider = provider;
|
||||
this.sectionName = sectionName;
|
||||
}
|
||||
|
||||
@@ -52,7 +53,7 @@ public abstract class FileConfigValue {
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
return provider == null || provider.get() == null ? defaultSource() : provider.get();
|
||||
}
|
||||
|
||||
public Optional<FileConfig> getSourceOptional() {
|
||||
@@ -71,4 +72,8 @@ public abstract class FileConfigValue {
|
||||
return clazz.isInstance(val) ? clazz.cast(val) : defaultValue;
|
||||
}
|
||||
|
||||
public FileConfig defaultSource() {
|
||||
return FileConfig.getPluginConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+152
@@ -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));
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
||||
|
||||
@@ -18,12 +19,20 @@ public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
||||
|
||||
public ConfigSound(@NotNull String configSection,
|
||||
@Nullable Sound defaultValue) {
|
||||
this(null, configSection, defaultValue);
|
||||
this((Supplier<FileConfig>) null, configSection, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigSound(@Nullable FileConfig source, @NotNull String configSection,
|
||||
@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) {
|
||||
|
||||
+69
-69
@@ -13,93 +13,93 @@ import java.util.Collections;
|
||||
|
||||
public class EasyMessage {
|
||||
|
||||
@Nullable ConfigValue<String> configValue;
|
||||
@Nullable ConfigValue<String> configValue;
|
||||
|
||||
@Nullable String defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
@Nullable String defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public EasyMessage() {
|
||||
this(null);
|
||||
}
|
||||
public EasyMessage() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public EasyMessage(@Nullable String defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
public EasyMessage(@Nullable String defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
|
||||
public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
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());
|
||||
}
|
||||
public void initialize(@NotNull FileConfig source, @NotNull String sectionName) {
|
||||
this.configValue = new ConfigValue<>(() -> source, sectionName, String.class, getDefaultValue());
|
||||
}
|
||||
|
||||
private @Nullable String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
private @Nullable String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
|
||||
private @NotNull String getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return "";
|
||||
else return getDefaultValue();
|
||||
}
|
||||
private @NotNull String getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return "";
|
||||
else return getDefaultValue();
|
||||
}
|
||||
|
||||
private @NotNull String getMessages() {
|
||||
if (configValue == null) {
|
||||
return getDefaultMessages();
|
||||
} else {
|
||||
return configValue.get();
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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 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 @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) {
|
||||
send(sender, null);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
send(sender, getMessageParams(), values);
|
||||
}
|
||||
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 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() {
|
||||
sendToAll(null);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable Object[] values) {
|
||||
sendToAll(messageParams, values);
|
||||
}
|
||||
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));
|
||||
}
|
||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+71
-71
@@ -15,96 +15,96 @@ import java.util.List;
|
||||
|
||||
public class EasyMessageList {
|
||||
|
||||
@Nullable ConfigValueList<String> configValue;
|
||||
@Nullable ConfigValueList<String> configValue;
|
||||
|
||||
@Nullable String[] defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
@Nullable String[] defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public EasyMessageList() {
|
||||
this((String[]) null);
|
||||
}
|
||||
public EasyMessageList() {
|
||||
this((String[]) null);
|
||||
}
|
||||
|
||||
public EasyMessageList(@Nullable String... defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
public EasyMessageList(@Nullable String... defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
|
||||
public EasyMessageList(@Nullable String[] defaultValue,
|
||||
@Nullable String[] messageParams) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
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());
|
||||
}
|
||||
public void initialize(FileConfig sourceConfig, String sectionName) {
|
||||
configValue = new ConfigValueList<>(() -> sourceConfig, sectionName, String.class, getDefaultValue());
|
||||
}
|
||||
|
||||
private @Nullable String[] getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
private @Nullable String[] getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
private @NotNull List<String> getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return new ArrayList<>();
|
||||
else return Arrays.asList(getDefaultValue());
|
||||
}
|
||||
@Unmodifiable
|
||||
private @NotNull List<String> getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return new ArrayList<>();
|
||||
else return Arrays.asList(getDefaultValue());
|
||||
}
|
||||
|
||||
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
|
||||
private @NotNull List<String> getMessages() {
|
||||
if (configValue == null) {
|
||||
return getDefaultMessages();
|
||||
} else {
|
||||
return configValue.get();
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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 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 @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) {
|
||||
send(sender, null);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
send(sender, getMessageParams(), values);
|
||||
}
|
||||
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 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() {
|
||||
sendToAll(null);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable Object[] values) {
|
||||
sendToAll(messageParams, values);
|
||||
}
|
||||
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));
|
||||
}
|
||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+65
-65
@@ -11,90 +11,90 @@ import java.util.Arrays;
|
||||
|
||||
public class MessagesInitializer {
|
||||
|
||||
private 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) {
|
||||
initialize(source, rootClazz, true);
|
||||
}
|
||||
|
||||
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) {
|
||||
MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class);
|
||||
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();
|
||||
}
|
||||
String rootSection = null;
|
||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
||||
rootSection = sectionAnnotation.value();
|
||||
}
|
||||
|
||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||
initSection(source, rootSection, innerClass, saveDefault);
|
||||
}
|
||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||
initSection(source, rootSection, innerClass, saveDefault);
|
||||
}
|
||||
|
||||
for (Field field : rootClazz.getFields()) {
|
||||
initMessage(source, rootSection, rootClazz, field, saveDefault);
|
||||
}
|
||||
for (Field field : rootClazz.getFields()) {
|
||||
initMessage(source, rootSection, rootClazz, field, saveDefault);
|
||||
}
|
||||
|
||||
if (saveDefault) {
|
||||
try {
|
||||
source.save();
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
}
|
||||
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;
|
||||
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));
|
||||
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);
|
||||
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));
|
||||
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);
|
||||
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 (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));
|
||||
}
|
||||
if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) {
|
||||
source.getConfig().set(section, Arrays.asList(messageList.defaultValue));
|
||||
}
|
||||
|
||||
messageList.initialize(source, section);
|
||||
}
|
||||
messageList.initialize(source, section);
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} 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 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("_", "-");
|
||||
}
|
||||
private static String getSectionName(String codeName) {
|
||||
return codeName.toLowerCase().replace("_", "-");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-8
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigMessage extends ConfigValue<String> {
|
||||
|
||||
@@ -23,14 +24,20 @@ public class ConfigMessage extends ConfigValue<String> {
|
||||
this(sectionName, defaultValue, null);
|
||||
}
|
||||
|
||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue, String[] messageParams) {
|
||||
super(null, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
public ConfigMessage(@NotNull String sectionName,
|
||||
@Nullable String defaultValue,
|
||||
String[] messageParams) {
|
||||
this((Supplier<FileConfig>) null, sectionName, defaultValue, messageParams);
|
||||
}
|
||||
|
||||
public ConfigMessage(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@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;
|
||||
}
|
||||
|
||||
@@ -84,9 +91,7 @@ public class ConfigMessage extends ConfigValue<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
||||
public FileConfig defaultSource() {
|
||||
return FileConfig.getMessageConfiguration();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+13
-7
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigMessageList extends ConfigValueList<String> {
|
||||
|
||||
@@ -23,14 +24,20 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
this(sectionName, defaultValue, null);
|
||||
}
|
||||
|
||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue, String[] messageParams) {
|
||||
super(null, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
public ConfigMessageList(@NotNull String sectionName,
|
||||
@Nullable String[] defaultValue,
|
||||
String[] messageParams) {
|
||||
this((Supplier<FileConfig>) null, sectionName, defaultValue, null);
|
||||
}
|
||||
|
||||
public ConfigMessageList(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@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;
|
||||
}
|
||||
|
||||
@@ -83,8 +90,7 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
||||
public FileConfig defaultSource() {
|
||||
return FileConfig.getMessageConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
-2
@@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfigValue<V> extends FileConfigValue {
|
||||
private final @NotNull Class<V> clazz;
|
||||
@@ -19,13 +20,19 @@ public class ConfigValue<V> extends FileConfigValue {
|
||||
public ConfigValue(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, sectionName, clazz, defaultValue);
|
||||
this((Supplier<FileConfig>) null, sectionName, clazz, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigValue(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@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.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
+7
-4
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConfigValueList<V> extends FileConfigValue {
|
||||
@@ -28,15 +29,17 @@ public class ConfigValueList<V> extends FileConfigValue {
|
||||
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) {
|
||||
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,
|
||||
@Nullable V[] defaultValue) {
|
||||
super(configuration, sectionName);
|
||||
super(provider, sectionName);
|
||||
this.clazz = clazz;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
+9
-2
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
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,
|
||||
@NotNull Class<V> valueClazz) {
|
||||
this(null, sectionName, keyCast, valueClazz);
|
||||
this((Supplier<FileConfig>) null, sectionName, keyCast, valueClazz);
|
||||
}
|
||||
|
||||
|
||||
public ConfigValueMap(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@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.valueClazz = valueClazz;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.3.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user