mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
[v1.1.2] 版本更新
- [U] 对于可能为空的配置参数提供Optional方法 - [U] 将复用的方法提出为抽象类使用 - [U] 优化代码结构
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package cc.carm.lib.easyplugin.configuration.cast;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
|
||||
|
||||
@NotNull Function<ConfigurationSection, V> valueCast;
|
||||
@Nullable V defaultValue;
|
||||
|
||||
public ConfigSectionCast(@NotNull String configSection,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast) {
|
||||
this(configSection, valueCast, null);
|
||||
}
|
||||
|
||||
public ConfigSectionCast(@NotNull String sectionName,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, sectionName, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigSectionCast(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
super(source, sectionName);
|
||||
this.valueCast = valueCast;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
|
||||
public @Nullable V get() {
|
||||
V cached = getCachedValue();
|
||||
if (cached != null && !isExpired()) {
|
||||
return cached;
|
||||
} else {
|
||||
return getConfigOptional()
|
||||
.map(config -> config.getConfigurationSection(getSectionName()))
|
||||
.map(section -> updateCache(valueCast.apply(section)))
|
||||
.orElse(defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull Optional<V> getOptional() {
|
||||
return Optional.ofNullable(get());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void set(ConfigurationSection section) {
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package cc.carm.lib.easyplugin.configuration.cast;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
|
||||
|
||||
@NotNull Function<String, V> valueCast;
|
||||
@Nullable V defaultValue;
|
||||
|
||||
public ConfigStringCast(@NotNull String configSection,
|
||||
@NotNull Function<String, V> valueCast) {
|
||||
this(configSection, valueCast, null);
|
||||
}
|
||||
|
||||
public ConfigStringCast(@NotNull String configSection,
|
||||
@NotNull Function<String, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, configSection, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigStringCast(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Function<String, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
super(source, sectionName);
|
||||
this.valueCast = valueCast;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public @Nullable V get() {
|
||||
V cached = getCachedValue();
|
||||
if (cached != null && !isExpired()) {
|
||||
return cached;
|
||||
} else {
|
||||
return getConfigOptional()
|
||||
.map(config -> config.getString(getSectionName()))
|
||||
.map(s -> updateCache(valueCast.apply(s)))
|
||||
.orElse(defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull Optional<V> getOptional() {
|
||||
return Optional.ofNullable(get());
|
||||
}
|
||||
|
||||
public void set(@Nullable String value) {
|
||||
getSourceOptional().ifPresent(source -> {
|
||||
source.getConfig().set(getSectionName(), value);
|
||||
source.save();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package cc.carm.lib.easyplugin.configuration.file;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class FileConfigCachedValue<V> extends FileConfigValue {
|
||||
|
||||
protected V cachedValue;
|
||||
protected long updateTime;
|
||||
|
||||
public FileConfigCachedValue(@NotNull String sectionName) {
|
||||
super(sectionName);
|
||||
}
|
||||
|
||||
public FileConfigCachedValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
||||
super(source, sectionName);
|
||||
}
|
||||
|
||||
public V updateCache(V value) {
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
this.cachedValue = value;
|
||||
return getCachedValue();
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return getSource() == null || getSource().isExpired(this.updateTime);
|
||||
}
|
||||
|
||||
public long getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public V getCachedValue() {
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
this.cachedValue = null;
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package cc.carm.lib.easyplugin.configuration.file;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class FileConfigValue {
|
||||
|
||||
protected @Nullable FileConfig source;
|
||||
private final @NotNull String sectionName;
|
||||
|
||||
public FileConfigValue(@NotNull String sectionName) {
|
||||
this(null, sectionName);
|
||||
}
|
||||
|
||||
public FileConfigValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
||||
this.source = source;
|
||||
this.sectionName = sectionName;
|
||||
}
|
||||
|
||||
public @NotNull String getSectionName() {
|
||||
return sectionName;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
getSourceOptional().ifPresent(FileConfig::save);
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
}
|
||||
|
||||
public Optional<FileConfig> getSourceOptional() {
|
||||
return Optional.ofNullable(getSource());
|
||||
}
|
||||
|
||||
public Optional<FileConfiguration> getConfigOptional() {
|
||||
return getSourceOptional().map(FileConfig::getConfig);
|
||||
}
|
||||
|
||||
public static @Nullable <V> V castValue(@Nullable Object val, @NotNull Class<V> clazz) {
|
||||
return castValue(val, clazz, null);
|
||||
}
|
||||
|
||||
public static @Nullable <V> V castValue(@Nullable Object val, @NotNull Class<V> clazz, @Nullable V defaultValue) {
|
||||
return clazz.isInstance(val) ? clazz.cast(val) : defaultValue;
|
||||
}
|
||||
|
||||
}
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.impl;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigSectionCast<V> {
|
||||
|
||||
@Nullable FileConfig source;
|
||||
|
||||
@NotNull String configSection;
|
||||
@NotNull Function<ConfigurationSection, V> valueCast;
|
||||
@Nullable V defaultValue;
|
||||
|
||||
V valueCache;
|
||||
long updateTime;
|
||||
|
||||
public ConfigSectionCast(@NotNull String configSection, @NotNull Function<ConfigurationSection, V> valueCast) {
|
||||
this(configSection, valueCast, null);
|
||||
}
|
||||
|
||||
public ConfigSectionCast(@NotNull String configSection,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, configSection, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigSectionCast(@Nullable FileConfig source, @NotNull String configSection,
|
||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
||||
@Nullable V defaultValue) {
|
||||
this.source = source;
|
||||
this.configSection = configSection;
|
||||
this.valueCast = valueCast;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
|
||||
public @Nullable V get() {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return defaultValue;
|
||||
|
||||
if (valueCache != null && !source.isExpired(this.updateTime)) return valueCache;
|
||||
if (!source.getConfig().contains(this.configSection)) return defaultValue;
|
||||
try {
|
||||
V finalValue = this.valueCast.apply(source.getConfig().getConfigurationSection(this.configSection));
|
||||
if (finalValue != null) {
|
||||
this.valueCache = finalValue;
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
return finalValue;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public void set(ConfigurationSection section) {
|
||||
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) getSource().save();
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
}
|
||||
|
||||
}
|
||||
+36
-28
@@ -1,5 +1,6 @@
|
||||
package cc.carm.lib.easyplugin.configuration.impl;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.cast.ConfigStringCast;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
@@ -7,6 +8,8 @@ import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
||||
|
||||
public ConfigSound(@NotNull String configSection) {
|
||||
@@ -18,30 +21,9 @@ public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
||||
this(null, configSection, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigSound(@Nullable FileConfig source, @NotNull String configSection, @Nullable Sound defaultValue) {
|
||||
super(source, configSection, string -> {
|
||||
Sound finalSound = defaultValue;
|
||||
float volume = 1;
|
||||
float pitch = 1;
|
||||
|
||||
if (string != null) {
|
||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||
try {
|
||||
if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
|
||||
if (args.length >= 2) volume = Float.parseFloat(args[1]);
|
||||
if (args.length >= 3) pitch = Float.parseFloat(args[2]);
|
||||
} catch (Exception exception) {
|
||||
Bukkit.getLogger().severe("声音 " + configSection + " 配置错误,不存在 " + string + " ,请检查。");
|
||||
Bukkit.getLogger().severe("In " + configSection + " (" + string + ") doesn't match any sound name.");
|
||||
}
|
||||
}
|
||||
|
||||
if (finalSound != null) {
|
||||
return new SoundData(finalSound, volume, pitch);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}, defaultValue == null ? null : new SoundData(defaultValue));
|
||||
public ConfigSound(@Nullable FileConfig source, @NotNull String configSection,
|
||||
@Nullable Sound defaultValue) {
|
||||
super(source, configSection, getSoundParser(), defaultValue == null ? null : new SoundData(defaultValue));
|
||||
}
|
||||
|
||||
public void set(@Nullable SoundData value) {
|
||||
@@ -73,12 +55,38 @@ public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
||||
if (data != null) data.play(player);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) getSource().save();
|
||||
public void playToAll() {
|
||||
SoundData data = get();
|
||||
if (data != null) {
|
||||
Bukkit.getOnlinePlayers().forEach(data::play);
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
public static @NotNull Function<@Nullable String, @Nullable SoundData> getSoundParser() {
|
||||
return string -> {
|
||||
if (string == null) return null;
|
||||
|
||||
Sound finalSound = null;
|
||||
float volume = 1;
|
||||
float pitch = 1;
|
||||
|
||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||
try {
|
||||
if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
|
||||
if (args.length >= 2) volume = Float.parseFloat(args[1]);
|
||||
if (args.length >= 3) pitch = Float.parseFloat(args[2]);
|
||||
} catch (Exception exception) {
|
||||
Bukkit.getLogger().severe("声音 " + string + " 配置错误,不存在同名声音,请检查。");
|
||||
Bukkit.getLogger().severe("Sound " + string + " doesn't match any sound name.");
|
||||
}
|
||||
|
||||
|
||||
if (finalSound != null) {
|
||||
return new SoundData(finalSound, volume, pitch);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static class SoundData {
|
||||
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.impl;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigStringCast<V> {
|
||||
|
||||
@Nullable FileConfig source;
|
||||
|
||||
@NotNull String configSection;
|
||||
@NotNull Function<String, V> valueCast;
|
||||
|
||||
@Nullable V defaultValue;
|
||||
|
||||
V valueCache;
|
||||
long updateTime;
|
||||
|
||||
public ConfigStringCast(@NotNull String configSection, @NotNull Function<String, V> valueCast) {
|
||||
this(configSection, valueCast, null);
|
||||
}
|
||||
|
||||
public ConfigStringCast(@NotNull String configSection, @NotNull Function<String, V> valueCast, @Nullable V defaultValue) {
|
||||
this(null, configSection, valueCast, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigStringCast(@Nullable FileConfig source, @NotNull String configSection,
|
||||
@NotNull Function<String, V> valueCast, @Nullable V defaultValue) {
|
||||
this.source = source;
|
||||
this.configSection = configSection;
|
||||
this.valueCast = valueCast;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public @Nullable V get() {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return defaultValue;
|
||||
|
||||
if (valueCache != null && !source.isExpired(this.updateTime)) return valueCache;
|
||||
if (!source.getConfig().contains(this.configSection)) return defaultValue;
|
||||
try {
|
||||
V finalValue = this.valueCast.apply(source.getConfig().getString(this.configSection));
|
||||
if (finalValue != null) {
|
||||
this.valueCache = finalValue;
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
return finalValue;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public void set(@Nullable String value) {
|
||||
FileConfig source = getSource();
|
||||
if (source != null) {
|
||||
source.getConfig().set(this.configSection, value);
|
||||
source.save();
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) {
|
||||
getSource().save();
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+32
-12
@@ -4,47 +4,50 @@ package cc.carm.lib.easyplugin.configuration.message;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigMessage extends ConfigValue<String> {
|
||||
|
||||
String[] messageParams;
|
||||
|
||||
public ConfigMessage(@NotNull String configSection) {
|
||||
this(configSection, null);
|
||||
public ConfigMessage(@NotNull String sectionName) {
|
||||
this(sectionName, null);
|
||||
}
|
||||
|
||||
public ConfigMessage(@NotNull String configSection, @Nullable String defaultValue) {
|
||||
this(configSection, defaultValue, null);
|
||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue) {
|
||||
this(sectionName, defaultValue, null);
|
||||
}
|
||||
|
||||
public ConfigMessage(@NotNull String configSection, @Nullable String defaultValue, String[] messageParams) {
|
||||
super(null, configSection, String.class, defaultValue);
|
||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue, String[] messageParams) {
|
||||
super(null, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
public ConfigMessage(@Nullable FileConfig config, @NotNull String configSection,
|
||||
public ConfigMessage(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@Nullable String defaultValue, String[] messageParams) {
|
||||
super(config, configSection, String.class, defaultValue);
|
||||
super(source, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
public @NotNull String get(CommandSender sender) {
|
||||
return MessageUtils.setPlaceholders(sender, get());
|
||||
}
|
||||
|
||||
public @NotNull String get(CommandSender sender, Object[] values) {
|
||||
if (messageParams != null) {
|
||||
return get(sender, messageParams, values);
|
||||
} else {
|
||||
return get(sender, new String[0], new Object[0]);
|
||||
return get(sender);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull String get(CommandSender sender, String[] params, Object[] values) {
|
||||
List<String> messages = MessageUtils.setPlaceholders(sender, Collections.singletonList(get()), params, values);
|
||||
return messages != null && !messages.isEmpty() ? messages.get(0) : "";
|
||||
return MessageUtils.setPlaceholders(sender, get(), params, values);
|
||||
}
|
||||
|
||||
public void send(CommandSender sender) {
|
||||
@@ -64,6 +67,23 @@ public class ConfigMessage extends ConfigValue<String> {
|
||||
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(get()), params, values);
|
||||
}
|
||||
|
||||
public void sendToAll() {
|
||||
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, Collections.singletonList(get())));
|
||||
}
|
||||
|
||||
public void sendToAll(Object[] values) {
|
||||
if (messageParams != null) {
|
||||
sendToAll(messageParams, values);
|
||||
} else {
|
||||
sendToAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToAll(String[] params, Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, Collections.singletonList(get()), params, values));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
||||
}
|
||||
|
||||
+26
-9
@@ -4,6 +4,7 @@ package cc.carm.lib.easyplugin.configuration.message;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
|
||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -14,22 +15,22 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public ConfigMessageList(String configSection) {
|
||||
this(configSection, new String[0]);
|
||||
public ConfigMessageList(String sectionName) {
|
||||
this(sectionName, new String[0]);
|
||||
}
|
||||
|
||||
public ConfigMessageList(@NotNull String configSection, @Nullable String[] defaultValue) {
|
||||
this(configSection, defaultValue, null);
|
||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue) {
|
||||
this(sectionName, defaultValue, null);
|
||||
}
|
||||
|
||||
public ConfigMessageList(@NotNull String configSection, @Nullable String[] defaultValue, String[] messageParams) {
|
||||
super(null, configSection, String.class, defaultValue);
|
||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue, String[] messageParams) {
|
||||
super(null, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
public ConfigMessageList(@Nullable FileConfig config, @NotNull String configSection,
|
||||
public ConfigMessageList(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@Nullable String[] defaultValue, String[] messageParams) {
|
||||
super(config, configSection, String.class, defaultValue);
|
||||
super(source, sectionName, String.class, defaultValue);
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
@@ -45,7 +46,6 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public @NotNull List<String> get(@Nullable CommandSender sender, String[] params, Object[] values) {
|
||||
return MessageUtils.setPlaceholders(sender, get(), params, values);
|
||||
}
|
||||
@@ -62,6 +62,22 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToAll(String[] params, Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, get(), params, values));
|
||||
}
|
||||
|
||||
public void sendToAll() {
|
||||
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, get()));
|
||||
}
|
||||
|
||||
public void sendToAll(Object[] values) {
|
||||
if (messageParams != null) {
|
||||
sendToAll(messageParams, values);
|
||||
} else {
|
||||
sendToAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, String[] params, Object[] values) {
|
||||
MessageUtils.sendWithPlaceholders(sender, get(), params, values);
|
||||
}
|
||||
@@ -70,4 +86,5 @@ public class ConfigMessageList extends ConfigValueList<String> {
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+24
-34
@@ -1,64 +1,54 @@
|
||||
package cc.carm.lib.easyplugin.configuration.values;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ConfigValue<V> {
|
||||
import java.util.Optional;
|
||||
|
||||
protected @Nullable FileConfig source;
|
||||
|
||||
private final @NotNull String configSection;
|
||||
public class ConfigValue<V> extends FileConfigValue {
|
||||
private final @NotNull Class<V> clazz;
|
||||
@Nullable V defaultValue;
|
||||
|
||||
public ConfigValue(@NotNull String configSection, @NotNull Class<V> clazz) {
|
||||
this(configSection, clazz, null);
|
||||
public ConfigValue(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz) {
|
||||
this(sectionName, clazz, null);
|
||||
}
|
||||
|
||||
public ConfigValue(@NotNull String configSection, @NotNull Class<V> clazz, @Nullable V defaultValue) {
|
||||
this(null, configSection, clazz, defaultValue);
|
||||
public ConfigValue(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, sectionName, clazz, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigValue(@Nullable FileConfig source, @NotNull String configSection,
|
||||
@NotNull Class<V> clazz, @Nullable V defaultValue) {
|
||||
this.source = source;
|
||||
this.configSection = configSection;
|
||||
public ConfigValue(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V defaultValue) {
|
||||
super(source, sectionName);
|
||||
this.clazz = clazz;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public V get() {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return this.defaultValue;
|
||||
|
||||
if (source.getConfig().contains(this.configSection)) {
|
||||
Object val = source.getConfig().get(this.configSection, this.defaultValue);
|
||||
return this.clazz.isInstance(val) ? this.clazz.cast(val) : this.defaultValue;
|
||||
} else {
|
||||
// 如果没有默认值,就把配置写进去,便于配置
|
||||
return setDefault();
|
||||
return getConfigOptional()
|
||||
.map(config -> castValue(config.get(getSectionName()), clazz, this.defaultValue))
|
||||
.orElse(setDefault()); // 如果没有默认值,就把配置写进去,便于配置
|
||||
}
|
||||
|
||||
public @NotNull Optional<V> getOptional() {
|
||||
return Optional.ofNullable(get());
|
||||
}
|
||||
|
||||
public void set(@Nullable V value) {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return;
|
||||
|
||||
source.getConfig().set(this.configSection, value);
|
||||
getSourceOptional().ifPresent(source -> {
|
||||
source.getConfig().set(getSectionName(), value);
|
||||
source.save();
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) getSource().save();
|
||||
});
|
||||
}
|
||||
|
||||
public V setDefault() {
|
||||
set(this.defaultValue);
|
||||
if (this.defaultValue != null) set(this.defaultValue);
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
}
|
||||
}
|
||||
|
||||
+32
-44
@@ -2,78 +2,66 @@ package cc.carm.lib.easyplugin.configuration.values;
|
||||
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConfigValueList<V> {
|
||||
public class ConfigValueList<V> extends FileConfigValue {
|
||||
|
||||
protected @Nullable FileConfig source;
|
||||
|
||||
private final @NotNull String configSection;
|
||||
private final @NotNull Class<V> clazz;
|
||||
|
||||
@Nullable V[] defaultValue;
|
||||
|
||||
public ConfigValueList(@NotNull String configSection, @NotNull Class<V> clazz) {
|
||||
this(configSection, clazz, null);
|
||||
public ConfigValueList(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz) {
|
||||
this(sectionName, clazz, null);
|
||||
}
|
||||
|
||||
public ConfigValueList(@NotNull String configSection, @NotNull Class<V> clazz, @Nullable V[] defaultValue) {
|
||||
this(null, configSection, clazz, defaultValue);
|
||||
public ConfigValueList(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V[] defaultValue) {
|
||||
this(null, sectionName, clazz, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String configSection, Class<V> clazz) {
|
||||
this(configuration, configSection, clazz, null);
|
||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName,
|
||||
Class<V> clazz) {
|
||||
this(configuration, sectionName, clazz, null);
|
||||
}
|
||||
|
||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String configSection,
|
||||
@NotNull Class<V> clazz, @Nullable V[] defaultValue) {
|
||||
this.source = configuration;
|
||||
this.configSection = configSection;
|
||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V[] defaultValue) {
|
||||
super(configuration, sectionName);
|
||||
this.clazz = clazz;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public @NotNull ArrayList<V> get() {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return new ArrayList<>();
|
||||
return getConfigOptional()
|
||||
.map(configuration -> configuration.getList(getSectionName()))
|
||||
.map(list -> list.stream()
|
||||
.map(o -> castValue(o, this.clazz))
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toCollection(ArrayList::new)))
|
||||
.orElse(getDefaultList());
|
||||
}
|
||||
|
||||
List<?> list = source.getConfig().getList(this.configSection);
|
||||
if (list == null) {
|
||||
if (defaultValue != null) {
|
||||
return new ArrayList<>(Arrays.asList(defaultValue));
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} else {
|
||||
ArrayList<V> result = new ArrayList<>();
|
||||
public @NotNull ArrayList<V> getDefaultList() {
|
||||
return defaultValue == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(defaultValue));
|
||||
|
||||
for (Object object : list) {
|
||||
if (this.clazz.isInstance(object)) {
|
||||
result.add(this.clazz.cast(object));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void set(@Nullable ArrayList<V> value) {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return;
|
||||
source.getConfig().set(this.configSection, value);
|
||||
this.save();
|
||||
getSourceOptional().ifPresent(source -> {
|
||||
source.getConfig().set(getSectionName(), value);
|
||||
source.save();
|
||||
});
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) getSource().save();
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return this.source == null ? FileConfig.getPluginConfiguration() : this.source;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-51
@@ -1,88 +1,59 @@
|
||||
package cc.carm.lib.easyplugin.configuration.values;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigValueMap<K, V> {
|
||||
|
||||
@Nullable FileConfig source;
|
||||
|
||||
@NotNull String configSection;
|
||||
public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
|
||||
|
||||
@NotNull Function<String, K> keyCast;
|
||||
@NotNull Class<V> valueClazz;
|
||||
|
||||
@Nullable LinkedHashMap<K, V> valueCache;
|
||||
|
||||
long updateTime;
|
||||
|
||||
public ConfigValueMap(@NotNull String configSection, @NotNull Function<String, K> keyCast,
|
||||
public ConfigValueMap(@NotNull String sectionName, @NotNull Function<String, K> keyCast,
|
||||
@NotNull Class<V> valueClazz) {
|
||||
this(null, configSection, keyCast, valueClazz);
|
||||
this(null, sectionName, keyCast, valueClazz);
|
||||
}
|
||||
|
||||
public ConfigValueMap(@Nullable FileConfig configuration, @NotNull String configSection,
|
||||
public ConfigValueMap(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) {
|
||||
this.source = configuration;
|
||||
this.configSection = configSection;
|
||||
super(source, sectionName);
|
||||
this.keyCast = keyCast;
|
||||
this.valueClazz = valueClazz;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
this.valueCache = null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<K, V> get() {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return new HashMap<>();
|
||||
|
||||
if (valueCache != null && !getSource().isExpired(this.updateTime)) return valueCache;
|
||||
|
||||
ConfigurationSection section = source.getConfig().getConfigurationSection(this.configSection);
|
||||
if (section == null) return new LinkedHashMap<>();
|
||||
|
||||
Set<String> keys = section.getKeys(false);
|
||||
if (keys.isEmpty()) return new LinkedHashMap<>();
|
||||
|
||||
else {
|
||||
LinkedHashMap<K, V> result = new LinkedHashMap<>();
|
||||
for (String key : keys) {
|
||||
Map<K, V> cached = getCachedValue();
|
||||
if (cached != null && !isExpired()) {
|
||||
return cached;
|
||||
} else {
|
||||
return getConfigOptional()
|
||||
.map(config -> config.getConfigurationSection(getSectionName()))
|
||||
.map(section -> {
|
||||
Map<K, V> result = new LinkedHashMap<>();
|
||||
for (String key : section.getKeys(false)) {
|
||||
K finalKey = keyCast.apply(key);
|
||||
Object val = section.get(key);
|
||||
V finalValue = this.valueClazz.isInstance(val) ? this.valueClazz.cast(val) : null;
|
||||
V finalValue = castValue(section.get(key), valueClazz);
|
||||
if (finalKey != null && finalValue != null) {
|
||||
result.put(finalKey, finalValue);
|
||||
}
|
||||
}
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
this.valueCache = result;
|
||||
return result;
|
||||
return updateCache(result);
|
||||
}).orElse(new LinkedHashMap<>());
|
||||
}
|
||||
}
|
||||
|
||||
public void set(@Nullable HashMap<K, V> valuesMap) {
|
||||
FileConfig source = getSource();
|
||||
if (source == null) return;
|
||||
source.getConfig().createSection(this.configSection, valuesMap);
|
||||
public void set(@Nullable Map<K, V> valuesMap) {
|
||||
getSourceOptional().ifPresent(source -> {
|
||||
source.getConfig().createSection(getSectionName(), valuesMap);
|
||||
source.save();
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (getSource() != null) getSource().save();
|
||||
}
|
||||
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -71,12 +71,12 @@ public abstract class EasyPlugin extends JavaPlugin {
|
||||
log(messageProvider.disabled(this, startTime));
|
||||
}
|
||||
|
||||
public void load() {
|
||||
protected void load() {
|
||||
}
|
||||
|
||||
public abstract boolean initialize();
|
||||
protected abstract boolean initialize();
|
||||
|
||||
public void shutdown() {
|
||||
protected void shutdown() {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,11 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MessageUtils {
|
||||
|
||||
@@ -42,6 +46,15 @@ public class MessageUtils {
|
||||
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
||||
}
|
||||
|
||||
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
|
||||
if (message == null || sender == null) return message;
|
||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
||||
} else {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
||||
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||
@@ -51,10 +64,36 @@ public class MessageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
String afterMessage = message;
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
return afterMessage;
|
||||
}
|
||||
|
||||
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
|
||||
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
||||
}
|
||||
@@ -70,15 +109,7 @@ public class MessageUtils {
|
||||
|
||||
|
||||
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String message : messages) {
|
||||
String afterMessage = message;
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
list.add(afterMessage);
|
||||
}
|
||||
return list;
|
||||
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user