1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 16:48:16 +08:00

项目初始化

This commit is contained in:
2022-01-04 20:38:29 +08:00
commit d77b9f696d
43 changed files with 3407 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyplugin-configuration</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-Configuration</name>
<description>轻松插件配置,可以方便快捷的将配置文件作为静态参数使用。</description>
<url>https://github.com/CarmJos/EasyPlugin</url>
<developers>
<developer>
<id>CarmJos</id>
<name>Carm Jos</name>
<email>carm@carm.cc</email>
<url>https://www.carm.cc</url>
</developer>
</developers>
<licenses>
<license>
<name>GNU General Public License v3.0</name>
<url>https://opensource.org/licenses/GPL-3.0</url>
</license>
</licenses>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub Actions</system>
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
</ciManagement>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-main</artifactId>
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,91 @@
package cc.carm.lib.easyplugin.configuration.file;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.function.Supplier;
public class FileConfig {
public static Supplier<FileConfig> pluginConfiguration = null;
public static Supplier<FileConfig> messageConfiguration = null;
@Nullable
public static FileConfig getPluginConfiguration() {
return pluginConfiguration == null ? null : pluginConfiguration.get();
}
@Nullable
public static FileConfig getMessageConfiguration() {
return messageConfiguration == null ? null : messageConfiguration.get();
}
private long updateTime;
private final JavaPlugin plugin;
private final String fileName;
private File file;
private FileConfiguration config;
public FileConfig(final JavaPlugin plugin) {
this(plugin, "config.yml");
}
public FileConfig(final JavaPlugin plugin, final String name) {
this.plugin = plugin;
this.fileName = name;
initFile();
}
private void initFile() {
this.updateTime = System.currentTimeMillis();
this.file = new File(plugin.getDataFolder(), fileName);
if (!this.file.exists()) {
if (!this.file.getParentFile().exists()) {
boolean success = this.file.getParentFile().mkdirs();
}
plugin.saveResource(fileName, true);
}
this.config = YamlConfiguration.loadConfiguration(this.file);
}
public File getFile() {
return file;
}
public FileConfiguration getConfig() {
return config;
}
public void save() {
try {
getConfig().save(getFile());
} catch (IOException e) {
e.printStackTrace();
}
}
public void reload() {
this.updateTime = System.currentTimeMillis();
if (getFile().exists()) {
this.config = YamlConfiguration.loadConfiguration(getFile());
} else {
initFile();
}
}
public long getUpdateTime() {
return updateTime;
}
public boolean isExpired(long time) {
return getUpdateTime() > time;
}
}
@@ -0,0 +1,73 @@
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;
}
}
@@ -0,0 +1,110 @@
package cc.carm.lib.easyplugin.configuration.impl;
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
public ConfigSound(@NotNull String configSection) {
this(configSection, null);
}
public ConfigSound(@NotNull String configSection,
@Nullable Sound defaultValue) {
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 void set(@Nullable SoundData value) {
if (value == null) {
set((String) null);
} else if (value.pitch != 1) {
set(value.type, value.volume, value.pitch);
} else if (value.volume != 1) {
set(value.type, value.volume);
} else {
set(value.type);
}
}
public void set(Sound value) {
set(value.name());
}
public void set(Sound value, float volume) {
set(value.name() + (volume != 1 ? ":" + volume : ""));
}
public void set(Sound value, float volume, float pitch) {
set(value.name() + ":" + volume + (pitch != 1 ? ":" + pitch : ""));
}
public void play(Player player) {
SoundData data = get();
if (data != null) data.play(player);
}
public void save() {
if (getSource() != null) getSource().save();
}
public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getPluginConfiguration() : source;
}
public static class SoundData {
Sound type;
float volume;
float pitch;
public SoundData(Sound type) {
this(type, 1, 1);
}
public SoundData(Sound type, float volume) {
this(type, volume, 1);
}
public SoundData(Sound type, float volume, float pitch) {
this.type = type;
this.volume = volume;
this.pitch = pitch;
}
public void play(Player player) {
player.playSound(player.getLocation(), type, volume, pitch);
}
}
}
@@ -0,0 +1,76 @@
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;
}
}
@@ -0,0 +1,72 @@
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.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 configSection, @Nullable String defaultValue) {
this(configSection, defaultValue, null);
}
public ConfigMessage(@NotNull String configSection, @Nullable String defaultValue, String[] messageParams) {
super(null, configSection, String.class, defaultValue);
this.messageParams = messageParams;
}
public ConfigMessage(@Nullable FileConfig config, @NotNull String configSection,
@Nullable String defaultValue, String[] messageParams) {
super(config, configSection, String.class, defaultValue);
this.messageParams = messageParams;
}
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]);
}
}
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) : "";
}
public void send(CommandSender sender) {
MessageUtils.sendWithPlaceholders(sender, get());
}
public void send(CommandSender sender, Object[] values) {
if (messageParams != null) {
send(sender, messageParams, values);
} else {
send(sender, new String[0], new Object[0]);
}
}
public void send(CommandSender sender, String[] params, Object[] values) {
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(get()), params, values);
}
public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getMessageConfiguration() : source;
}
}
@@ -0,0 +1,73 @@
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.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class ConfigMessageList extends ConfigValueList<String> {
@Nullable String[] messageParams;
public ConfigMessageList(String configSection) {
this(configSection, new String[0]);
}
public ConfigMessageList(@NotNull String configSection, @Nullable String[] defaultValue) {
this(configSection, defaultValue, null);
}
public ConfigMessageList(@NotNull String configSection, @Nullable String[] defaultValue, String[] messageParams) {
super(null, configSection, String.class, defaultValue);
this.messageParams = messageParams;
}
public ConfigMessageList(@Nullable FileConfig config, @NotNull String configSection,
@Nullable String[] defaultValue, String[] messageParams) {
super(config, configSection, String.class, defaultValue);
this.messageParams = messageParams;
}
public @NotNull List<String> get(@Nullable CommandSender sender) {
return MessageUtils.setPlaceholders(sender, get());
}
public @NotNull List<String> get(@Nullable CommandSender sender, Object[] values) {
if (messageParams != null) {
return get(sender, messageParams, values);
} else {
return get(sender);
}
}
public @NotNull List<String> get(@Nullable CommandSender sender, String[] params, Object[] values) {
return MessageUtils.setPlaceholders(sender, get(), params, values);
}
public void send(@Nullable CommandSender sender) {
MessageUtils.sendWithPlaceholders(sender, get());
}
public void send(@Nullable CommandSender sender, Object[] values) {
if (messageParams != null) {
send(sender, messageParams, values);
} else {
send(sender);
}
}
public void send(@Nullable CommandSender sender, String[] params, Object[] values) {
MessageUtils.sendWithPlaceholders(sender, get(), params, values);
}
@Override
public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getMessageConfiguration() : source;
}
}
@@ -0,0 +1,64 @@
package cc.carm.lib.easyplugin.configuration.values;
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ConfigValue<V> {
protected @Nullable FileConfig source;
private final @NotNull String configSection;
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 configSection, @NotNull Class<V> clazz, @Nullable V defaultValue) {
this(null, configSection, clazz, defaultValue);
}
public ConfigValue(@Nullable FileConfig source, @NotNull String configSection,
@NotNull Class<V> clazz, @Nullable V defaultValue) {
this.source = source;
this.configSection = configSection;
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();
}
}
public void set(@Nullable V value) {
FileConfig source = getSource();
if (source == null) return;
source.getConfig().set(this.configSection, value);
source.save();
}
public void save() {
if (getSource() != null) getSource().save();
}
public V setDefault() {
set(this.defaultValue);
return this.defaultValue;
}
public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getPluginConfiguration() : source;
}
}
@@ -0,0 +1,79 @@
package cc.carm.lib.easyplugin.configuration.values;
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ConfigValueList<V> {
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 configSection, @NotNull Class<V> clazz, @Nullable V[] defaultValue) {
this(null, configSection, 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 configSection,
@NotNull Class<V> clazz, @Nullable V[] defaultValue) {
this.source = configuration;
this.configSection = configSection;
this.clazz = clazz;
this.defaultValue = defaultValue;
}
public @NotNull ArrayList<V> get() {
FileConfig source = getSource();
if (source == null) return new ArrayList<>();
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<>();
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();
}
public void save() {
if (getSource() != null) getSource().save();
}
public @Nullable FileConfig getSource() {
return this.source == null ? FileConfig.getPluginConfiguration() : this.source;
}
}
@@ -0,0 +1,88 @@
package cc.carm.lib.easyplugin.configuration.values;
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.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;
@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,
@NotNull Class<V> valueClazz) {
this(null, configSection, keyCast, valueClazz);
}
public ConfigValueMap(@Nullable FileConfig configuration, @NotNull String configSection,
@NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) {
this.source = configuration;
this.configSection = configSection;
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) {
K finalKey = keyCast.apply(key);
Object val = section.get(key);
V finalValue = this.valueClazz.isInstance(val) ? this.valueClazz.cast(val) : null;
if (finalKey != null && finalValue != null) {
result.put(finalKey, finalValue);
}
}
this.updateTime = System.currentTimeMillis();
this.valueCache = result;
return result;
}
}
public void set(@Nullable HashMap<K, V> valuesMap) {
FileConfig source = getSource();
if (source == null) return;
source.getConfig().createSection(this.configSection, valuesMap);
source.save();
}
public void save() {
if (getSource() != null) getSource().save();
}
public @Nullable FileConfig getSource() {
return source == null ? FileConfig.getPluginConfiguration() : source;
}
}