mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-05 00:58:17 +08:00
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c7190274d7 | |||
| be798a981a | |||
| bbdfe313d5 | |||
| 2acc81185a | |||
| 5886f162af | |||
| fbc153c9e9 | |||
| 3c7508c348 | |||
| 5fa255d7c7 | |||
| ce1122712d | |||
| 45426e947b | |||
| 7e3a2ea13e | |||
| 226121c1df | |||
| 94c9cc382d | |||
| 892371f702 | |||
| 12ff00e1d9 | |||
| 10f5961ccd | |||
| d8cbf2825d | |||
| f4292e761a | |||
| 47018fbf7d | |||
| dd277e99e8 | |||
| b3078553e7 | |||
| e67e23a24c | |||
| 30e57e3945 | |||
| acea995996 | |||
| 511d8d77c4 | |||
| 96ff3398c8 | |||
| 42860c332f | |||
| 5ea196b6df | |||
| fe34bbc17d | |||
| c575805c72 | |||
| 9c73fa81bc |
@@ -1,40 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>EasyPlugin Javadocs</title>
|
||||
<style>
|
||||
.container {
|
||||
width: 60%;
|
||||
margin: 10% auto 0;
|
||||
background-color: #f0f0f0;
|
||||
padding: 2% 5%;
|
||||
border-radius: 10px
|
||||
}
|
||||
|
||||
ul {
|
||||
padding-left: 20px;
|
||||
}
|
||||
|
||||
ul li {
|
||||
line-height: 2.3
|
||||
}
|
||||
|
||||
a {
|
||||
color: #7531c4
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>EasyPlugin Javadocs</h1>
|
||||
<ul>
|
||||
<li><a href="main">EasyPlugin-Main</a></li>
|
||||
<li><a href="command">EasyPlugin-Command</a></li>
|
||||
<li><a href="configuration">EasyPlugin-Configuration</a></li>
|
||||
<li><a href="gui">EasyPlugin-GUI</a></li>
|
||||
<li><a href="database">EasyPlugin-Database</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
- name: "Set up JDK"
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: '11'
|
||||
java-version: '8'
|
||||
distribution: 'adopt'
|
||||
cache: maven
|
||||
server-id: github
|
||||
@@ -33,9 +33,29 @@ jobs:
|
||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
|
||||
|
||||
javadoc-deploy:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: "Set up JDK"
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: '8'
|
||||
distribution: 'adopt'
|
||||
cache: maven
|
||||
|
||||
- name: "Generate Javadoc"
|
||||
run: mvn -B javadoc:aggregate --file pom.xml -DskipTests
|
||||
|
||||
- name: "Copy Javadoc"
|
||||
run: |
|
||||
bash .scripts/copy-javadoc.sh
|
||||
rm -rf docs
|
||||
mkdir -vp docs
|
||||
cp -vrf target/site/apidocs/* docs/
|
||||
cp -vrf .documentation/javadoc/JAVADOC-README.md docs/README.md
|
||||
|
||||
- name: "Generate Sitemap"
|
||||
id: sitemap
|
||||
|
||||
@@ -28,11 +28,4 @@ jobs:
|
||||
run: mvn -B package --file pom.xml -Dmaven.javadoc.skip=true
|
||||
env:
|
||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
- name: "Target Stage"
|
||||
run: mkdir staging && cp */target/*.jar staging
|
||||
- name: "Upload artifact"
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: Artifact
|
||||
path: staging
|
||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
@@ -1,21 +0,0 @@
|
||||
rm -rf docs
|
||||
mkdir -vp docs
|
||||
|
||||
DOC_URL="target/apidocs"
|
||||
|
||||
for FILE in easyplugin-*; do
|
||||
|
||||
if test -e "$FILE/$DOC_URL"; then
|
||||
|
||||
MODULE_FILE="docs/${FILE:11}/"
|
||||
|
||||
mkdir -vp "$MODULE_FILE"
|
||||
|
||||
cp -vrf "$FILE"/"$DOC_URL"/* "$MODULE_FILE"
|
||||
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
cp -vrf .documentation/javadoc/JAVADOC-README.md docs/README.md
|
||||
cp -vrf .documentation/javadoc/index.html docs/index.html
|
||||
@@ -23,30 +23,30 @@
|
||||
- 轻便独立的功能模块,按需使用,避免大量打包!
|
||||
- 详细的Javadoc与使用文档,轻松上手,方便使用!
|
||||
- 持续的更新与优化,需求不止,更新不止!
|
||||
- 如需新功能支持,请通过 [Issues](https://github.com/CarmJos/EasyPlugin/issues) 提交功能需求。
|
||||
- 如需新功能支持,请通过 [Issues](https://github.com/CarmJos/EasyPlugin/issues) 提交功能需求。
|
||||
|
||||
## 内容
|
||||
|
||||
项目初创不久,加 * 的仍在开发更新中...
|
||||
|
||||
### 集合部分
|
||||
### 集合部分 (`/collection`)
|
||||
|
||||
- All [`easyplugin-all`](easyplugin-all)
|
||||
- Common [`easyplugin-common`](easyplugin-common)
|
||||
- All [`easyplugin-all`](collection/all)
|
||||
- Common [`easyplugin-common`](collection/common)
|
||||
|
||||
### 主要部分
|
||||
### 主要部分 (`/base`)
|
||||
|
||||
- Main [`easyplugin-main`](easyplugin-main)
|
||||
- Command* [`easyplugin-command`](easyplugin-command)
|
||||
- Configuration [`easyplugin-configuration`](easyplugin-configuration)
|
||||
- Database* [`easyplugin-database`](easyplugin-database)
|
||||
- GUI [`easyplugin-gui`](easyplugin-gui)
|
||||
- Main [`easyplugin-main`](base/main)
|
||||
- Command [`easyplugin-command`](base/command)
|
||||
- ~~Messages*~~ (已独立项目到 [**EasyMessages**](https://github.com/CarmJos/EasyMessages))
|
||||
- ~~Configuration~~ (已独立项目到 [**MineConfiguration**](https://github.com/CarmJos/MineConfiguration))
|
||||
- ~~Database~~ (已独立项目到 [**EasySQL**](https://github.com/CarmJos/EasySQL))
|
||||
- GUI [`easyplugin-gui`](base/main)
|
||||
|
||||
### 附属部分
|
||||
### 附属部分 (`/extension`)
|
||||
|
||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/6245/)* [`easyplugin-placeholderapi`](easyplugin-placeholderapi)
|
||||
- [Vault](https://github.com/MilkBowl/VaultAPI)* [`easyplugin-vault`](easyplugin-vault)
|
||||
- [LuckPerms](https://www.spigotmc.org/resources/luckperms.28140/)* [`easyplugin-lp`](easyplugin-lp)
|
||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/6245/)* [`easyplugin-placeholderapi`](extension/papi)
|
||||
- [Vault](https://github.com/MilkBowl/VaultAPI)* [`easyplugin-vault`](extension/vault)
|
||||
|
||||
## 开发
|
||||
|
||||
|
||||
@@ -5,20 +5,16 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>easyplugin-command</artifactId>
|
||||
|
||||
<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>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<artifactId>easyplugin-command</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>12-EasyPlugin-Command</name>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,164 @@
|
||||
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
|
||||
protected final @NotNull JavaPlugin plugin;
|
||||
protected final @NotNull String cmd;
|
||||
protected final @NotNull List<String> aliases;
|
||||
|
||||
protected final @NotNull Map<String, SubCommand> registeredCommands = new HashMap<>();
|
||||
protected final @NotNull Map<String, CommandHandler> registeredHandlers = new HashMap<>();
|
||||
|
||||
protected final @NotNull Map<String, String> aliasesMap = new HashMap<>();
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin) {
|
||||
this(plugin, plugin.getName());
|
||||
}
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd) {
|
||||
this(plugin, cmd, new String[0]);
|
||||
}
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd, @NotNull String... aliases) {
|
||||
this.plugin = plugin;
|
||||
this.cmd = cmd;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
public abstract void noArgs(CommandSender sender);
|
||||
|
||||
public abstract void unknownCommand(CommandSender sender, String[] args);
|
||||
|
||||
public abstract void noPermission(CommandSender sender);
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.cmd;
|
||||
}
|
||||
|
||||
public void registerSubCommand(SubCommand command) {
|
||||
String name = command.getName().toLowerCase();
|
||||
this.registeredCommands.put(name, command);
|
||||
command.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
public void registerHandler(CommandHandler handler) {
|
||||
String name = handler.getName().toLowerCase();
|
||||
this.registeredHandlers.put(name, handler);
|
||||
handler.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
this.noArgs(sender);
|
||||
} else {
|
||||
String input = args[0].toLowerCase();
|
||||
|
||||
CommandHandler handler = getHandler(input);
|
||||
if (handler != null) {
|
||||
if (!handler.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
handler.onCommand(sender, command, label, this.shortenArgs(args));
|
||||
}
|
||||
}
|
||||
|
||||
SubCommand subCommand = getSubCommand(input);
|
||||
if (subCommand == null) {
|
||||
this.unknownCommand(sender, args);
|
||||
} else if (!subCommand.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
try {
|
||||
subCommand.execute(this.plugin, sender, this.shortenArgs(args));
|
||||
} catch (ArrayIndexOutOfBoundsException var9) {
|
||||
this.unknownCommand(sender, args);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||
if (args.length == 0) return Collections.emptyList();
|
||||
|
||||
String input = args[0].toLowerCase();
|
||||
if (args.length == 1) {
|
||||
return getExecutors().stream()
|
||||
.filter(e -> e.hasPermission(sender))
|
||||
.map(NamedExecutor::getName)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
|
||||
CommandHandler handler = getHandler(input);
|
||||
if (handler != null && handler.hasPermission(sender)) {
|
||||
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
||||
}
|
||||
|
||||
SubCommand subCommand = getSubCommand(input);
|
||||
if (subCommand != null && subCommand.hasPermission(sender)) {
|
||||
return subCommand.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<NamedExecutor> getExecutors() {
|
||||
Set<NamedExecutor> executors = new HashSet<>();
|
||||
executors.addAll(this.registeredHandlers.values());
|
||||
executors.addAll(this.registeredCommands.values());
|
||||
List<NamedExecutor> sortedExecutors = new ArrayList<>(executors);
|
||||
sortedExecutors.sort(Comparator.comparing(NamedExecutor::getName));
|
||||
return sortedExecutors;
|
||||
}
|
||||
|
||||
protected @Nullable CommandHandler getHandler(@NotNull String name) {
|
||||
CommandHandler fromName = this.registeredHandlers.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredHandlers.get(nameFromAlias);
|
||||
}
|
||||
|
||||
protected @Nullable SubCommand getSubCommand(@NotNull String name) {
|
||||
SubCommand fromName = this.registeredCommands.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredCommands.get(nameFromAlias);
|
||||
}
|
||||
|
||||
protected String[] shortenArgs(String[] args) {
|
||||
if (args.length == 0) {
|
||||
return args;
|
||||
} else {
|
||||
List<String> argList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
||||
return argList.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.permissions.Permissible;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface NamedExecutor {
|
||||
|
||||
String getName();
|
||||
|
||||
List<String> getAliases();
|
||||
|
||||
default boolean hasPermission(Permissible permissible) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SimpleCompleter {
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, List<?> objects) {
|
||||
return objects(input, objects.size(), objects);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, List<?> objects) {
|
||||
return objects(input, limit, objects.stream());
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, Stream<?> stream) {
|
||||
return stream.filter(Objects::nonNull).map(Object::toString)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.limit(Math.max(0, limit)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, String... texts) {
|
||||
return text(input, texts.length, texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, int limit, String... texts) {
|
||||
return text(input, limit, Arrays.asList(texts));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, List<String> texts) {
|
||||
return text(input, texts.size(), texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, int limit, List<String> texts) {
|
||||
return objects(input, limit, texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> onlinePlayers(@NotNull String input) {
|
||||
return onlinePlayers(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> onlinePlayers(@NotNull String input, int limit) {
|
||||
return objects(input, limit, Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> allPlayers(@NotNull String input) {
|
||||
return allPlayers(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> allPlayers(@NotNull String input, int limit) {
|
||||
return objects(input, limit, Arrays.stream(Bukkit.getOfflinePlayers()).map(OfflinePlayer::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input) {
|
||||
return worlds(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input, int limit) {
|
||||
return objects(input, limit, Bukkit.getWorlds().stream().map(World::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> materials(@NotNull String input) {
|
||||
return materials(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> materials(@NotNull String input, int limit) {
|
||||
return objects(input, limit, Arrays.stream(Material.values()).map(Enum::name));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> effects(@NotNull String input) {
|
||||
return effects(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> effects(@NotNull String input, int limit) {
|
||||
return objects(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public abstract class SubCommand implements NamedExecutor {
|
||||
|
||||
private final String name;
|
||||
private final List<String> aliases;
|
||||
|
||||
public SubCommand(String name) {
|
||||
this(name, new String[0]);
|
||||
}
|
||||
|
||||
public SubCommand(String name, String... aliases) {
|
||||
this.name = name;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
|
||||
public abstract Void execute(JavaPlugin plugin, CommandSender sender, String[] args);
|
||||
|
||||
public List<String> tabComplete(JavaPlugin plugin, CommandSender sender, String[] args) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,7 +20,7 @@
|
||||
<artifactId>easyplugin-gui</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>13-EasyPlugin-GUI</name>
|
||||
<name>EasyPlugin-GUI</name>
|
||||
<description>轻松插件GUI接口模块,方便快捷的创建箱子GUI界面。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
+13
-7
@@ -16,12 +16,13 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class GUI {
|
||||
|
||||
private static JavaPlugin plugin;
|
||||
private static final HashMap<Player, GUI> openedGUIs = new HashMap<>();
|
||||
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
|
||||
|
||||
public static void initialize(JavaPlugin plugin) {
|
||||
GUI.plugin = plugin;
|
||||
@@ -31,7 +32,7 @@ public class GUI {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public static HashMap<Player, GUI> getOpenedGUIs() {
|
||||
public static HashMap<UUID, GUI> getOpenedGUIs() {
|
||||
return openedGUIs;
|
||||
}
|
||||
|
||||
@@ -155,12 +156,17 @@ public class GUI {
|
||||
}
|
||||
|
||||
public void openGUI(Player player) {
|
||||
if (this.type == GUIType.CANCEL) throw new NullPointerException("被取消或不存在的GUI");
|
||||
if (this.type == GUIType.CANCEL) { throw new IllegalStateException("被取消或不存在的GUI"); }
|
||||
|
||||
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
|
||||
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
|
||||
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
|
||||
|
||||
GUI previous = getOpenedGUI(player);
|
||||
if(previous != null){
|
||||
previous.listener.close(player);
|
||||
}
|
||||
|
||||
setOpenedGUI(player, this);
|
||||
|
||||
this.inv = inv;
|
||||
@@ -187,19 +193,19 @@ public class GUI {
|
||||
|
||||
|
||||
public static void setOpenedGUI(Player player, GUI gui) {
|
||||
getOpenedGUIs().put(player, gui);
|
||||
getOpenedGUIs().put(player.getUniqueId(), gui);
|
||||
}
|
||||
|
||||
public static boolean hasOpenedGUI(Player player) {
|
||||
return getOpenedGUIs().containsKey(player);
|
||||
return getOpenedGUIs().containsKey(player.getUniqueId());
|
||||
}
|
||||
|
||||
public static GUI getOpenedGUI(Player player) {
|
||||
return getOpenedGUIs().get(player);
|
||||
return getOpenedGUIs().get(player.getUniqueId());
|
||||
}
|
||||
|
||||
public static void removeOpenedGUI(Player player) {
|
||||
getOpenedGUIs().remove(player);
|
||||
getOpenedGUIs().remove(player.getUniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
+6
-2
@@ -73,11 +73,15 @@ public class GUIListener implements Listener {
|
||||
if (!(event.getPlayer() instanceof Player)) return;
|
||||
if (!event.getInventory().equals(getCurrentGUI().inv)) return;
|
||||
|
||||
close((Player) event.getPlayer());
|
||||
|
||||
}
|
||||
|
||||
protected void close(Player p){
|
||||
HandlerList.unregisterAll(this);
|
||||
getCurrentGUI().listener = null;
|
||||
GUI.removeOpenedGUI((Player) event.getPlayer());
|
||||
GUI.removeOpenedGUI(p);
|
||||
getCurrentGUI().onClose();
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -20,7 +21,7 @@
|
||||
<artifactId>easyplugin-main</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>10-EasyPlugin-Main</name>
|
||||
<name>EasyPlugin-Main</name>
|
||||
<description>轻松插件主要接口模块,包含方便的插件入口类与相关工具类。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
@@ -33,7 +34,7 @@
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<licenses>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The MIT License</name>
|
||||
<url>https://opensource.org/licenses/MIT</url>
|
||||
@@ -50,6 +51,17 @@
|
||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
||||
</ciManagement>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.10.9</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -0,0 +1,151 @@
|
||||
package cc.carm.lib.easyplugin;
|
||||
|
||||
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
||||
import cc.carm.lib.easyplugin.utils.JarResourceUtils;
|
||||
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class EasyPlugin extends JavaPlugin {
|
||||
|
||||
protected EasyPluginMessageProvider messageProvider;
|
||||
|
||||
public EasyPlugin() {
|
||||
this(EasyPluginMessageProvider.ZH_CN);
|
||||
}
|
||||
|
||||
public EasyPlugin(EasyPluginMessageProvider messageProvider) {
|
||||
this.messageProvider = messageProvider;
|
||||
}
|
||||
|
||||
private SchedulerUtils scheduler;
|
||||
private boolean initialized = false;
|
||||
|
||||
@Override
|
||||
public final void onLoad() {
|
||||
scheduler = new SchedulerUtils(this);
|
||||
if (!hasOverride("load")) return;
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
log(messageProvider.loading(this));
|
||||
load();
|
||||
log(messageProvider.loaded(this, startTime));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onEnable() {
|
||||
outputInfo();
|
||||
|
||||
log(messageProvider.enabling(this));
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
if (!(this.initialized = initialize())) {
|
||||
setEnabled(false);
|
||||
log(messageProvider.enableFailure(this, startTime));
|
||||
return;
|
||||
}
|
||||
|
||||
log(messageProvider.enableSuccess(this, startTime));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void onDisable() {
|
||||
if (!hasOverride("shutdown") || !isInitialized()) return;
|
||||
outputInfo();
|
||||
|
||||
log(messageProvider.disabling(this));
|
||||
long startTime = System.currentTimeMillis();
|
||||
shutdown();
|
||||
log(messageProvider.disabled(this, startTime));
|
||||
}
|
||||
|
||||
protected void load() {
|
||||
}
|
||||
|
||||
protected abstract boolean initialize();
|
||||
|
||||
protected void shutdown() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
||||
*/
|
||||
public void outputInfo() {
|
||||
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public boolean isDebugging() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public SchedulerUtils getScheduler() {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
public void registerListener(@NotNull Listener... listeners) {
|
||||
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
||||
}
|
||||
|
||||
public void registerCommand(String commandName,
|
||||
@NotNull CommandExecutor executor) {
|
||||
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
||||
}
|
||||
|
||||
public void registerCommand(String commandName,
|
||||
@NotNull CommandExecutor executor,
|
||||
@Nullable TabCompleter tabCompleter) {
|
||||
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
||||
if (command == null) return;
|
||||
command.setExecutor(executor);
|
||||
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
||||
}
|
||||
|
||||
public void print(@Nullable String prefix, @Nullable String... messages) {
|
||||
messageProvider.print(this, prefix, messages);
|
||||
}
|
||||
|
||||
public void log(@Nullable String... messages) {
|
||||
print(null, messages);
|
||||
}
|
||||
|
||||
public void error(String... messages) {
|
||||
print("&c[ERROR] &r", messages);
|
||||
}
|
||||
|
||||
public void debug(@Nullable String... messages) {
|
||||
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
private boolean hasOverride(String methodName) {
|
||||
Map<Method, Method> methodMap = new HashMap<>();
|
||||
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
||||
.filter(method -> method.getName().equals(methodName))
|
||||
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
||||
.filter(extend -> extend.getName().equals(methodName))
|
||||
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
||||
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
||||
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
||||
);
|
||||
return !methodMap.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package cc.carm.lib.easyplugin.i18n;
|
||||
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public interface EasyPluginMessageProvider {
|
||||
|
||||
EasyPluginMessageProvider ZH_CN = new zh_CN();
|
||||
EasyPluginMessageProvider EN_US = new en_US();
|
||||
|
||||
String loading(Plugin plugin);
|
||||
|
||||
String loaded(Plugin plugin, long startMillis);
|
||||
|
||||
String enabling(Plugin plugin);
|
||||
|
||||
String enableSuccess(Plugin plugin, long startMillis);
|
||||
|
||||
String enableFailure(Plugin plugin, long startMillis);
|
||||
|
||||
String disabling(Plugin plugin);
|
||||
|
||||
String disabled(Plugin plugin, long startMillis);
|
||||
|
||||
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
||||
Arrays.stream(messages)
|
||||
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
||||
.map(ColorParser::parse)
|
||||
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
||||
}
|
||||
|
||||
class zh_CN implements EasyPluginMessageProvider {
|
||||
|
||||
@Override
|
||||
public String loading(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loaded(Plugin plugin, long startMillis) {
|
||||
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableFailure(Plugin plugin, long startMillis) {
|
||||
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabled(Plugin plugin, long startMillis) {
|
||||
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
}
|
||||
|
||||
class en_US implements EasyPluginMessageProvider {
|
||||
|
||||
@Override
|
||||
public String loading(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loaded(Plugin plugin, long startMillis) {
|
||||
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableFailure(Plugin plugin, long startMillis) {
|
||||
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabled(Plugin plugin, long startMillis) {
|
||||
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+14
-14
@@ -1,4 +1,3 @@
|
||||
|
||||
package cc.carm.lib.easyplugin.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -9,9 +8,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ColorParser {
|
||||
|
||||
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([0-9a-fA-F]{6})\\)");
|
||||
|
||||
public static String parse(String text) {
|
||||
text = parseHexColor(text);
|
||||
return parseColor(text);
|
||||
return parseBaseColor(parseHexColor(text));
|
||||
}
|
||||
|
||||
public static String[] parse(String... texts) {
|
||||
@@ -22,23 +22,23 @@ public class ColorParser {
|
||||
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String parseColor(final String text) {
|
||||
public static String parseBaseColor(final String text) {
|
||||
return text.replaceAll("&", "§").replace("§§", "&");
|
||||
}
|
||||
|
||||
public static String parseHexColor(String text) {
|
||||
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
Matcher matcher = HEX_PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
|
||||
hexColor = hexColor.replace("&", "");
|
||||
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
|
||||
for (int i = 1; i < hexColor.length(); i++) {
|
||||
bukkitColorCode.append('§').append(hexColor.charAt(i));
|
||||
}
|
||||
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
|
||||
text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase());
|
||||
matcher.reset(text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildHexColor(String hexCode) {
|
||||
return Arrays.stream(hexCode.split(""))
|
||||
.map(s -> '§' + s)
|
||||
.collect(Collectors.joining("", '§' + "x", ""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package cc.carm.lib.easyplugin.utils;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class JarResourceUtils {
|
||||
public static final char JAR_SEPARATOR = '/';
|
||||
|
||||
public static @Nullable String[] readResource(@Nullable InputStream resourceStream) {
|
||||
if (resourceStream == null) return null;
|
||||
try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) {
|
||||
List<String> contents = new ArrayList<>();
|
||||
while (scanner.hasNextLine()) {
|
||||
contents.add(scanner.nextLine());
|
||||
}
|
||||
return contents.toArray(new String[0]);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFolderFromJar(String folderName, File destFolder, CopyOption option)
|
||||
throws IOException {
|
||||
copyFolderFromJar(folderName, destFolder, option, null);
|
||||
}
|
||||
|
||||
public static void copyFolderFromJar(String folderName, File destFolder,
|
||||
CopyOption option, PathTrimmer trimmer) throws IOException {
|
||||
if (!destFolder.exists())
|
||||
destFolder.mkdirs();
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
File fullPath;
|
||||
String path = JarResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
||||
if (trimmer != null)
|
||||
path = trimmer.trim(path);
|
||||
try {
|
||||
if (!path.startsWith("file"))
|
||||
path = "file://" + path;
|
||||
|
||||
fullPath = new File(new URI(path));
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath));
|
||||
|
||||
ZipEntry entry;
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
if (!entry.getName().startsWith(folderName + JAR_SEPARATOR))
|
||||
continue;
|
||||
|
||||
String fileName = entry.getName();
|
||||
|
||||
if (fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) {
|
||||
File file = new File(destFolder + File.separator + fileName);
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
}
|
||||
file.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
File file = new File(destFolder + File.separator + fileName);
|
||||
if (option == CopyOption.COPY_IF_NOT_EXIST && file.exists())
|
||||
continue;
|
||||
|
||||
if (!file.getParentFile().exists())
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
if (!file.exists())
|
||||
file.createNewFile();
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
int len;
|
||||
while ((len = zis.read(buffer)) > 0) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
fos.close();
|
||||
}
|
||||
|
||||
zis.closeEntry();
|
||||
zis.close();
|
||||
}
|
||||
|
||||
public enum CopyOption {
|
||||
COPY_IF_NOT_EXIST, REPLACE_IF_EXIST
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PathTrimmer {
|
||||
String trim(String original);
|
||||
}
|
||||
}
|
||||
+43
-14
@@ -4,6 +4,8 @@ import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -18,7 +20,6 @@ public class MessageUtils {
|
||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||
}
|
||||
|
||||
|
||||
public static void send(@Nullable CommandSender sender, String... messages) {
|
||||
send(sender, Arrays.asList(messages));
|
||||
}
|
||||
@@ -29,7 +30,7 @@ public class MessageUtils {
|
||||
sender.sendMessage(ColorParser.parse(s));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
||||
sendWithPlaceholders(sender, Arrays.asList(messages));
|
||||
}
|
||||
@@ -47,7 +48,7 @@ public class MessageUtils {
|
||||
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
||||
}
|
||||
|
||||
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
|
||||
public static String setPlaceholders(@Nullable CommandSender sender, @Nullable String message) {
|
||||
if (message == null || sender == null) return message;
|
||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
||||
@@ -56,7 +57,11 @@ public class MessageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
||||
|
||||
@Nullable
|
||||
@Contract("_, !null -> !null")
|
||||
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||
@Nullable List<String> messages) {
|
||||
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
||||
@@ -65,20 +70,35 @@ public class MessageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static String setPlaceholders(@Nullable CommandSender sender, String message, String[] params, Object[] values) {
|
||||
public static String setPlaceholders(@Nullable CommandSender sender,
|
||||
@NotNull String message,
|
||||
@Nullable String[] params,
|
||||
@Nullable Object[] values) {
|
||||
return setPlaceholders(sender, setCustomParams(message, params, values));
|
||||
}
|
||||
|
||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
||||
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||
@NotNull List<String> messages,
|
||||
@Nullable String[] params,
|
||||
@Nullable Object[] values) {
|
||||
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
||||
}
|
||||
|
||||
public static String setCustomParams(String message, String param, Object value) {
|
||||
public static String setCustomParams(@NotNull String message,
|
||||
@NotNull String param,
|
||||
@NotNull Object value) {
|
||||
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
||||
}
|
||||
|
||||
public static String setCustomParams(String message, String[] params, Object[] values) {
|
||||
@Nullable
|
||||
@Contract("!null, _, _-> !null ; null, _, _->null ")
|
||||
public static String setCustomParams(@Nullable String message,
|
||||
@Nullable String[] params,
|
||||
@Nullable Object[] values) {
|
||||
if (message == null) return null;
|
||||
if (params == null || values == null) return message;
|
||||
if (params.length != values.length) return message;
|
||||
|
||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
paramsMap.put(params[i], values[i]);
|
||||
@@ -86,8 +106,8 @@ public class MessageUtils {
|
||||
return setCustomParams(message, paramsMap);
|
||||
}
|
||||
|
||||
|
||||
public static String setCustomParams(String message, HashMap<String, Object> params) {
|
||||
@NotNull
|
||||
public static String setCustomParams(@NotNull String message, @NotNull HashMap<String, Object> params) {
|
||||
String afterMessage = message;
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||
@@ -95,11 +115,18 @@ public class MessageUtils {
|
||||
return afterMessage;
|
||||
}
|
||||
|
||||
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
|
||||
@NotNull
|
||||
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||
@NotNull String param,
|
||||
@NotNull Object value) {
|
||||
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
||||
}
|
||||
|
||||
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
|
||||
@NotNull
|
||||
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||
@Nullable String[] params,
|
||||
@Nullable Object[] values) {
|
||||
if (params == null || values == null) return messages;
|
||||
if (params.length != values.length) return messages;
|
||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
@@ -108,9 +135,11 @@ public class MessageUtils {
|
||||
return setCustomParams(messages, paramsMap);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
||||
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList());
|
||||
return messages.stream()
|
||||
.map(message -> setCustomParams(message, params))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,7 +20,7 @@
|
||||
<artifactId>easyplugin-all</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>01-EasyPlugin-All</name>
|
||||
<name>EasyPlugin-All</name>
|
||||
<description>轻松插件全集,将打包全部工具类与工具接口。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,7 +20,7 @@
|
||||
<artifactId>easyplugin-bom</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>00-EasyPlugin-Bom</name>
|
||||
<name>EasyPlugin-Bom</name>
|
||||
<description>轻松插件汇总导入模块,允许快捷导入相关的接口并避免版本不一致问题。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
@@ -133,26 +134,11 @@
|
||||
<artifactId>easyplugin-main</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-configuration</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-database</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-gui</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-command</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||
@@ -163,11 +149,6 @@
|
||||
<artifactId>easyplugin-vault</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-lp</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,7 +20,7 @@
|
||||
<artifactId>easyplugin-common</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>02-EasyPlugin-Common</name>
|
||||
<name>EasyPlugin-Common</name>
|
||||
<description>轻松插件常用接口集,包含除附属插件模块外的所有模块。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
@@ -59,6 +60,10 @@
|
||||
<type>pom</type>
|
||||
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-github</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||
@@ -1,4 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
public class CommandsManager {
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
<?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.3.3</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>11-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>The MIT License</name>
|
||||
<url>https://opensource.org/licenses/MIT</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>
|
||||
|
||||
<dependency>
|
||||
<!--
|
||||
ConfigUpdater - to save the comments in default configuration.
|
||||
-> https://github.com/tchristofferson/Config-Updater
|
||||
-->
|
||||
<groupId>com.tchristofferson</groupId>
|
||||
<artifactId>ConfigUpdater</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>com.tchristofferson.configupdater</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.configuration.updater</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||
<exclude>META-INF/*.txt</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
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) {
|
||||
}
|
||||
|
||||
}
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
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) {
|
||||
setIfPresent(value, true);
|
||||
}
|
||||
|
||||
}
|
||||
-128
@@ -1,128 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.file;
|
||||
|
||||
|
||||
import com.tchristofferson.configupdater.ConfigUpdater;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
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();
|
||||
}
|
||||
|
||||
protected long updateTime;
|
||||
|
||||
protected final JavaPlugin plugin;
|
||||
protected final File fileFolder;
|
||||
protected final String fileName;
|
||||
protected final String resourcePath;
|
||||
|
||||
protected File file;
|
||||
protected FileConfiguration config;
|
||||
|
||||
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
|
||||
this(plugin, "config.yml");
|
||||
}
|
||||
|
||||
public FileConfig(@NotNull JavaPlugin plugin,
|
||||
@NotNull String fileName) throws IOException {
|
||||
this(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
||||
@NotNull String fileName) throws IOException {
|
||||
this(plugin, resourcePath, plugin.getDataFolder(), fileName);
|
||||
}
|
||||
|
||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
||||
@NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
||||
this.plugin = plugin;
|
||||
this.resourcePath = resourcePath;
|
||||
this.fileFolder = fileFolder;
|
||||
this.fileName = fileName;
|
||||
initFile();
|
||||
}
|
||||
|
||||
protected void initFile() throws IOException {
|
||||
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
||||
this.file = new File(getFileFolder(), fileName);
|
||||
|
||||
if (!file.exists()) {
|
||||
InputStream resourceStream = plugin.getResource(resourcePath);
|
||||
if (resourceStream == null) {
|
||||
throw new IOException("The resource " + resourcePath + " cannot find in " + plugin.getName() + " !");
|
||||
}
|
||||
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int readBytes;
|
||||
while ((readBytes = resourceStream.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, readBytes);
|
||||
}
|
||||
|
||||
out.close();
|
||||
resourceStream.close();
|
||||
|
||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
||||
}
|
||||
|
||||
this.updateTime = System.currentTimeMillis();
|
||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||
}
|
||||
|
||||
public File getFileFolder() {
|
||||
return fileFolder;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public JavaPlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
getConfig().save(getFile());
|
||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
||||
}
|
||||
|
||||
public void reload() throws IOException {
|
||||
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;
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
-74
@@ -1,74 +0,0 @@
|
||||
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.Map;
|
||||
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 -> {
|
||||
try {
|
||||
fileConfig.save();
|
||||
} catch (Exception ex) {
|
||||
fileConfig.getPlugin().getLogger().severe("Could not save the " + fileConfig.getFile() + " .");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setIfPresent(@Nullable Object value, boolean save) {
|
||||
getConfigOptional().ifPresent(configuration -> configuration.set(getSectionName(), value));
|
||||
if (save) save();
|
||||
}
|
||||
|
||||
public void createSection(Map<?, ?> values) {
|
||||
getConfigOptional().ifPresent(configuration -> {
|
||||
if (values == null) {
|
||||
configuration.set(getSectionName(), null);
|
||||
} else {
|
||||
configuration.createSection(getSectionName(), values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
-118
@@ -1,118 +0,0 @@
|
||||
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;
|
||||
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) {
|
||||
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, getSoundParser(), 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 playToAll() {
|
||||
SoundData data = get();
|
||||
if (data != null) {
|
||||
Bukkit.getOnlinePlayers().forEach(data::play);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-105
@@ -1,105 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class EasyMessage {
|
||||
|
||||
@Nullable ConfigValue<String> configValue;
|
||||
|
||||
@Nullable String defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public EasyMessage() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public EasyMessage(@Nullable String defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
|
||||
public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
public void initialize(@NotNull FileConfig sourceConfig, @NotNull String sectionName) {
|
||||
this.configValue = new ConfigValue<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
||||
}
|
||||
|
||||
private @Nullable String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
|
||||
private @NotNull String getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return "";
|
||||
else return getDefaultValue();
|
||||
}
|
||||
|
||||
private @NotNull String getMessages() {
|
||||
if (configValue == null) {
|
||||
return getDefaultMessages();
|
||||
} else {
|
||||
return configValue.get();
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull String get(@Nullable CommandSender sender) {
|
||||
return get(sender, null);
|
||||
}
|
||||
|
||||
public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
return get(sender, getMessageParams(), values);
|
||||
}
|
||||
|
||||
public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||
if (sender == null) return getMessages();
|
||||
if (params == null || values == null) {
|
||||
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages()));
|
||||
} else {
|
||||
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages(), params, values));
|
||||
}
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender) {
|
||||
send(sender, null);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
send(sender, getMessageParams(), values);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||
if (params == null || values == null) {
|
||||
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
||||
} else {
|
||||
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(getMessages()), params, values);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToAll() {
|
||||
sendToAll(null);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable Object[] values) {
|
||||
sendToAll(messageParams, values);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
|
||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class EasyMessageList {
|
||||
|
||||
@Nullable ConfigValueList<String> configValue;
|
||||
|
||||
@Nullable String[] defaultValue;
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public EasyMessageList() {
|
||||
this((String[]) null);
|
||||
}
|
||||
|
||||
public EasyMessageList(@Nullable String... defaultValue) {
|
||||
this(defaultValue, null);
|
||||
}
|
||||
|
||||
public EasyMessageList(@Nullable String[] defaultValue,
|
||||
@Nullable String[] messageParams) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.messageParams = messageParams;
|
||||
}
|
||||
|
||||
public void initialize(FileConfig sourceConfig, String sectionName) {
|
||||
configValue = new ConfigValueList<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
||||
}
|
||||
|
||||
private @Nullable String[] getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
private @NotNull List<String> getDefaultMessages() {
|
||||
if (getDefaultValue() == null) return new ArrayList<>();
|
||||
else return Arrays.asList(getDefaultValue());
|
||||
}
|
||||
|
||||
|
||||
private @Nullable String[] getMessageParams() {
|
||||
return messageParams;
|
||||
}
|
||||
|
||||
private @NotNull List<String> getMessages() {
|
||||
if (configValue == null) {
|
||||
return getDefaultMessages();
|
||||
} else {
|
||||
return configValue.get();
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull List<String> get(@Nullable CommandSender sender) {
|
||||
return get(sender, null);
|
||||
}
|
||||
|
||||
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
return get(sender, getMessageParams(), values);
|
||||
}
|
||||
|
||||
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||
if (sender == null) return getMessages();
|
||||
if (params == null || values == null) {
|
||||
return MessageUtils.setPlaceholders(sender, getMessages());
|
||||
} else {
|
||||
return MessageUtils.setPlaceholders(sender, getMessages(), params, values);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender) {
|
||||
send(sender, null);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
||||
send(sender, getMessageParams(), values);
|
||||
}
|
||||
|
||||
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
||||
if (params == null || values == null) {
|
||||
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
||||
} else {
|
||||
MessageUtils.sendWithPlaceholders(sender, getMessages(), params, values);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToAll() {
|
||||
sendToAll(null);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable Object[] values) {
|
||||
sendToAll(messageParams, values);
|
||||
}
|
||||
|
||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class MessagesConfig extends FileConfig {
|
||||
|
||||
public MessagesConfig(@NotNull JavaPlugin plugin) throws IOException {
|
||||
this(plugin, "messages.yml");
|
||||
}
|
||||
|
||||
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull String fileName) throws IOException {
|
||||
this(plugin, plugin.getDataFolder(), fileName);
|
||||
}
|
||||
|
||||
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
||||
super(plugin, fileName, fileFolder, fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initFile() throws IOException {
|
||||
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
||||
this.file = new File(getFileFolder(), fileName);
|
||||
if (!file.exists()) file.createNewFile();
|
||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
getConfig().save(getFile());
|
||||
}
|
||||
|
||||
}
|
||||
-100
@@ -1,100 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MessagesInitializer {
|
||||
|
||||
private MessagesInitializer() {
|
||||
// 静态方法类,不应当实例化。
|
||||
}
|
||||
|
||||
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz) {
|
||||
initialize(source, rootClazz, true);
|
||||
}
|
||||
|
||||
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) {
|
||||
MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class);
|
||||
|
||||
String rootSection = null;
|
||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
||||
rootSection = sectionAnnotation.value();
|
||||
}
|
||||
|
||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
||||
initSection(source, rootSection, innerClass, saveDefault);
|
||||
}
|
||||
|
||||
for (Field field : rootClazz.getFields()) {
|
||||
initMessage(source, rootSection, rootClazz, field, saveDefault);
|
||||
}
|
||||
|
||||
if (saveDefault) {
|
||||
try {
|
||||
source.save();
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void initSection(FileConfig source, String parentSection, Class<?> clazz, boolean saveDefault) {
|
||||
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
||||
|
||||
String section = getSection(clazz.getSimpleName(), parentSection, clazz.getAnnotation(MessagesSection.class));
|
||||
|
||||
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(source, section, innerClass, saveDefault);
|
||||
for (Field field : clazz.getFields()) initMessage(source, section, clazz, field, saveDefault);
|
||||
|
||||
}
|
||||
|
||||
private static void initMessage(FileConfig source, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
||||
try {
|
||||
String section = getSection(field.getName(), parentSection, field.getAnnotation(MessagesSection.class));
|
||||
|
||||
Object object = field.get(clazz);
|
||||
|
||||
if (object instanceof EasyMessage) {
|
||||
EasyMessage message = ((EasyMessage) object);
|
||||
if (saveDefault && message.defaultValue != null && !source.getConfig().contains(section)) {
|
||||
source.getConfig().set(section, message.defaultValue);
|
||||
}
|
||||
message.initialize(source, section);
|
||||
} else if (object instanceof EasyMessageList) {
|
||||
EasyMessageList messageList = ((EasyMessageList) object);
|
||||
|
||||
if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) {
|
||||
source.getConfig().set(section, Arrays.asList(messageList.defaultValue));
|
||||
}
|
||||
|
||||
messageList.initialize(source, section);
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String getSection(@NotNull String name,
|
||||
@Nullable String parentSection,
|
||||
@Nullable MessagesSection sectionAnnotation) {
|
||||
String parent = parentSection != null ? parentSection + "." : "";
|
||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 0) {
|
||||
return parent + sectionAnnotation.value();
|
||||
} else {
|
||||
return parent + getSectionName(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getSectionName(String codeName) {
|
||||
return codeName.toLowerCase().replace("_", "-");
|
||||
}
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
public abstract class MessagesRoot {
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.language;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagesSection {
|
||||
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
-92
@@ -1,92 +0,0 @@
|
||||
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;
|
||||
|
||||
public class ConfigMessage extends ConfigValue<String> {
|
||||
|
||||
String[] messageParams;
|
||||
|
||||
public ConfigMessage(@NotNull String sectionName) {
|
||||
this(sectionName, null);
|
||||
}
|
||||
|
||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue) {
|
||||
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(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@Nullable String defaultValue, String[] messageParams) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull String get(CommandSender sender, String[] params, Object[] values) {
|
||||
return MessageUtils.setPlaceholders(sender, get(), params, values);
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-90
@@ -1,90 +0,0 @@
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigMessageList extends ConfigValueList<String> {
|
||||
|
||||
@Nullable String[] messageParams;
|
||||
|
||||
public ConfigMessageList(String sectionName) {
|
||||
this(sectionName, new String[0]);
|
||||
}
|
||||
|
||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue) {
|
||||
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(@Nullable FileConfig source, @NotNull String sectionName,
|
||||
@Nullable String[] defaultValue, String[] messageParams) {
|
||||
super(source, sectionName, 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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileConfig getSource() {
|
||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
||||
}
|
||||
|
||||
}
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
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.Optional;
|
||||
|
||||
public class ConfigValue<V> extends FileConfigValue {
|
||||
private final @NotNull Class<V> clazz;
|
||||
@Nullable V defaultValue;
|
||||
|
||||
public ConfigValue(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz) {
|
||||
this(sectionName, clazz, null);
|
||||
}
|
||||
|
||||
public ConfigValue(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V defaultValue) {
|
||||
this(null, sectionName, clazz, defaultValue);
|
||||
}
|
||||
|
||||
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() {
|
||||
return getConfigOptional()
|
||||
.map(config -> {
|
||||
if (config.contains(getSectionName())) {
|
||||
return castValue(config.get(getSectionName()), clazz, this.defaultValue);
|
||||
} else {
|
||||
return setDefault(); // 如果没有默认值,就把配置写进去,便于配置
|
||||
}
|
||||
})
|
||||
.orElse(defaultValue);
|
||||
}
|
||||
|
||||
public @NotNull Optional<V> getOptional() {
|
||||
return Optional.ofNullable(get());
|
||||
}
|
||||
|
||||
public void set(@Nullable V value) {
|
||||
setIfPresent(value, true);
|
||||
}
|
||||
|
||||
public V setDefault() {
|
||||
if (this.defaultValue != null) set(this.defaultValue);
|
||||
return this.defaultValue;
|
||||
}
|
||||
}
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
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.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConfigValueList<V> extends FileConfigValue {
|
||||
|
||||
private final @NotNull Class<V> clazz;
|
||||
|
||||
@Nullable V[] defaultValue;
|
||||
|
||||
public ConfigValueList(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz) {
|
||||
this(sectionName, clazz, null);
|
||||
}
|
||||
|
||||
public ConfigValueList(@NotNull String sectionName,
|
||||
@NotNull Class<V> clazz,
|
||||
@Nullable V[] defaultValue) {
|
||||
this(null, sectionName, clazz, defaultValue);
|
||||
}
|
||||
|
||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName,
|
||||
Class<V> clazz) {
|
||||
this(configuration, sectionName, clazz, null);
|
||||
}
|
||||
|
||||
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() {
|
||||
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());
|
||||
}
|
||||
|
||||
public @NotNull ArrayList<V> getDefaultList() {
|
||||
return defaultValue == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(defaultValue));
|
||||
|
||||
}
|
||||
|
||||
public void set(@Nullable ArrayList<V> value) {
|
||||
setIfPresent(value, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.configuration.values;
|
||||
|
||||
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.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
|
||||
|
||||
@NotNull Function<String, K> keyCast;
|
||||
@NotNull Class<V> valueClazz;
|
||||
|
||||
public ConfigValueMap(@NotNull String sectionName, @NotNull Function<String, K> keyCast,
|
||||
@NotNull Class<V> valueClazz) {
|
||||
this(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.keyCast = keyCast;
|
||||
this.valueClazz = valueClazz;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public Map<K, V> get() {
|
||||
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);
|
||||
V finalValue = castValue(section.get(key), valueClazz);
|
||||
if (finalKey != null && finalValue != null) {
|
||||
result.put(finalKey, finalValue);
|
||||
}
|
||||
}
|
||||
return updateCache(result);
|
||||
}).orElse(new LinkedHashMap<>());
|
||||
}
|
||||
}
|
||||
|
||||
public void set(@Nullable Map<K, V> valuesMap) {
|
||||
createSection(valuesMap);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
<?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.3.3</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-database</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>14-EasyPlugin-Database</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>The MIT License</name>
|
||||
<url>https://opensource.org/licenses/MIT</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>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-beecp</artifactId>
|
||||
<version>0.2.7</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>cc.carm.lib.easysql</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.database</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||
<exclude>META-INF/*.txt</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,77 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.database;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
||||
import cc.carm.lib.easysql.api.builder.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseTable {
|
||||
|
||||
private final @NotNull String tableName;
|
||||
private final @NotNull String[] columns;
|
||||
|
||||
@Nullable String tableSettings;
|
||||
|
||||
public DatabaseTable(@NotNull String tableName, @NotNull String[] columns) {
|
||||
this(tableName, columns, null);
|
||||
}
|
||||
|
||||
public DatabaseTable(@NotNull String tableName, @NotNull String[] columns,
|
||||
@Nullable String tableSettings) {
|
||||
this.tableName = tableName;
|
||||
this.columns = columns;
|
||||
this.tableSettings = tableSettings;
|
||||
}
|
||||
|
||||
public @NotNull String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public @NotNull String[] getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public @Nullable String getTableSettings() {
|
||||
return tableSettings;
|
||||
}
|
||||
|
||||
public int createTable(SQLManager sqlManager) throws SQLException {
|
||||
TableCreateBuilder createAction = sqlManager.createTable(getTableName());
|
||||
createAction.setColumns(getColumns());
|
||||
if (getTableSettings() != null) createAction.setTableSettings(getTableSettings());
|
||||
return createAction.build().execute();
|
||||
}
|
||||
|
||||
public TableQueryBuilder createQuery(SQLManager sqlManager) {
|
||||
return sqlManager.createQuery().inTable(getTableName());
|
||||
}
|
||||
|
||||
public DeleteBuilder createDelete(SQLManager sqlManager) {
|
||||
return sqlManager.createDelete(getTableName());
|
||||
}
|
||||
|
||||
public UpdateBuilder createUpdate(SQLManager sqlManager) {
|
||||
return sqlManager.createUpdate(getTableName());
|
||||
}
|
||||
|
||||
public InsertBuilder<PreparedSQLUpdateAction> createInsert(SQLManager sqlManager) {
|
||||
return sqlManager.createInsert(getTableName());
|
||||
}
|
||||
|
||||
public InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(SQLManager sqlManager) {
|
||||
return sqlManager.createInsertBatch(getTableName());
|
||||
}
|
||||
|
||||
public ReplaceBuilder<PreparedSQLUpdateAction> createReplace(SQLManager sqlManager) {
|
||||
return sqlManager.createReplace(getTableName());
|
||||
}
|
||||
|
||||
public ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(SQLManager sqlManager) {
|
||||
return sqlManager.createReplaceBatch(getTableName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?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.3.3</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-lp</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>23-EasyPlugin-LuckPerms</name>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -1,149 +0,0 @@
|
||||
package cc.carm.lib.easyplugin;
|
||||
|
||||
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
||||
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class EasyPlugin extends JavaPlugin {
|
||||
|
||||
protected EasyPluginMessageProvider messageProvider;
|
||||
|
||||
public EasyPlugin() {
|
||||
this(new EasyPluginMessageProvider.en_US());
|
||||
}
|
||||
|
||||
public EasyPlugin(EasyPluginMessageProvider messageProvider) {
|
||||
this.messageProvider = messageProvider;
|
||||
}
|
||||
|
||||
private SchedulerUtils scheduler;
|
||||
private boolean initialized = false;
|
||||
|
||||
@Override
|
||||
public final void onLoad() {
|
||||
scheduler = new SchedulerUtils(this);
|
||||
if (!hasOverride("load")) return;
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
log(messageProvider.loading(this));
|
||||
load();
|
||||
log(messageProvider.loaded(this, startTime));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onEnable() {
|
||||
outputInfo();
|
||||
|
||||
log(messageProvider.enabling(this));
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
if (!(this.initialized = initialize())) {
|
||||
setEnabled(false);
|
||||
log(messageProvider.enableFailure(this, startTime));
|
||||
return;
|
||||
}
|
||||
|
||||
log(messageProvider.enableSuccess(this, startTime));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void onDisable() {
|
||||
if (!hasOverride("shutdown") || !isInitialized()) return;
|
||||
outputInfo();
|
||||
|
||||
log(messageProvider.disabling(this));
|
||||
long startTime = System.currentTimeMillis();
|
||||
shutdown();
|
||||
log(messageProvider.disabled(this, startTime));
|
||||
}
|
||||
|
||||
protected void load() {
|
||||
}
|
||||
|
||||
protected abstract boolean initialize();
|
||||
|
||||
protected void shutdown() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
||||
*/
|
||||
public void outputInfo() {
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public boolean isDebugging() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public SchedulerUtils getScheduler() {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
public void regListener(@NotNull Listener... listeners) {
|
||||
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
||||
}
|
||||
|
||||
public void registerCommand(String commandName,
|
||||
@NotNull CommandExecutor executor) {
|
||||
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
||||
}
|
||||
|
||||
public void registerCommand(String commandName,
|
||||
@NotNull CommandExecutor executor,
|
||||
@Nullable TabCompleter tabCompleter) {
|
||||
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
||||
if (command == null) return;
|
||||
command.setExecutor(executor);
|
||||
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
||||
}
|
||||
|
||||
public void print(@Nullable String prefix, @Nullable String... messages) {
|
||||
messageProvider.print(this, prefix, messages);
|
||||
}
|
||||
|
||||
public void log(@Nullable String... messages) {
|
||||
print(null, messages);
|
||||
}
|
||||
|
||||
public void error(String... messages) {
|
||||
print("&c[ERROR] &r", messages);
|
||||
}
|
||||
|
||||
public void debug(@Nullable String... messages) {
|
||||
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
private boolean hasOverride(String methodName) {
|
||||
Map<Method, Method> methodMap = new HashMap<>();
|
||||
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
||||
.filter(method -> method.getName().equals(methodName))
|
||||
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
||||
.filter(extend -> extend.getName().equals(methodName))
|
||||
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
||||
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
||||
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
||||
);
|
||||
return !methodMap.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
package cc.carm.lib.easyplugin.i18n;
|
||||
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public interface EasyPluginMessageProvider {
|
||||
|
||||
String loading(Plugin plugin);
|
||||
|
||||
String loaded(Plugin plugin, long startMillis);
|
||||
|
||||
String enabling(Plugin plugin);
|
||||
|
||||
String enableSuccess(Plugin plugin, long startMillis);
|
||||
|
||||
String enableFailure(Plugin plugin, long startMillis);
|
||||
|
||||
String disabling(Plugin plugin);
|
||||
|
||||
String disabled(Plugin plugin, long startMillis);
|
||||
|
||||
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
||||
Arrays.stream(messages)
|
||||
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
||||
.map(ColorParser::parse)
|
||||
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
||||
}
|
||||
|
||||
class zh_CN implements EasyPluginMessageProvider {
|
||||
|
||||
@Override
|
||||
public String loading(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loaded(Plugin plugin, long startMillis) {
|
||||
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableFailure(Plugin plugin, long startMillis) {
|
||||
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabled(Plugin plugin, long startMillis) {
|
||||
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||
}
|
||||
}
|
||||
|
||||
class en_US implements EasyPluginMessageProvider {
|
||||
|
||||
@Override
|
||||
public String loading(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loaded(Plugin plugin, long startMillis) {
|
||||
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String enableFailure(Plugin plugin, long startMillis) {
|
||||
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabling(Plugin plugin) {
|
||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String disabled(Plugin plugin, long startMillis) {
|
||||
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,6 +20,6 @@
|
||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>21-EasyPlugin-PlaceholderAPI</name>
|
||||
<name>EasyPlugin-PlaceholderAPI</name>
|
||||
|
||||
</project>
|
||||
@@ -5,13 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -19,7 +20,7 @@
|
||||
<artifactId>easyplugin-vault</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>22-EasyPlugin-Vault</name>
|
||||
<name>EasyPlugin-Vault</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.jdk.version>8</project.jdk.version>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
@@ -14,23 +15,19 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.3.3</version>
|
||||
<version>1.4.3</version>
|
||||
<modules>
|
||||
|
||||
<module>easyplugin-main</module>
|
||||
<module>base/main</module>
|
||||
<module>base/command</module>
|
||||
<module>base/gui</module>
|
||||
|
||||
<module>easyplugin-database</module>
|
||||
<module>easyplugin-gui</module>
|
||||
<module>easyplugin-configuration</module>
|
||||
<module>easyplugin-command</module>
|
||||
<module>extension/papi</module>
|
||||
<module>extension/vault</module>
|
||||
|
||||
<module>easyplugin-vault</module>
|
||||
<module>easyplugin-placeholderapi</module>
|
||||
<module>easyplugin-lp</module>
|
||||
|
||||
<module>easyplugin-bom</module>
|
||||
<module>easyplugin-all</module>
|
||||
<module>easyplugin-common</module>
|
||||
<module>collection/all</module>
|
||||
<module>collection/bom</module>
|
||||
<module>collection/common</module>
|
||||
|
||||
</modules>
|
||||
|
||||
@@ -75,12 +72,6 @@
|
||||
|
||||
<repositories>
|
||||
|
||||
<repository>
|
||||
<id>carm-repo</id>
|
||||
<name>Carm's Repo</name>
|
||||
<url>https://repo.carm.cc/repository/maven-public/</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<url>https://repo1.maven.org/maven2/</url>
|
||||
@@ -92,25 +83,20 @@
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>github</id>
|
||||
<name>GitHub Packages</name>
|
||||
<url>https://maven.pkg.github.com/CarmJos/*</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>carm-repo</id>
|
||||
<name>Carm's Repo</name>
|
||||
<url>https://repo.carm.cc/repository/maven-public/</url>
|
||||
</repository>
|
||||
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.13.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
@@ -118,17 +104,10 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.10.9</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>22.0.0</version>
|
||||
<version>23.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -160,9 +139,6 @@
|
||||
<version>3.2.0</version>
|
||||
<configuration>
|
||||
<classifier>javadoc</classifier>
|
||||
<links>
|
||||
<link>https://javadoc.io/doc/org.jetbrains/annotations/</link>
|
||||
</links>
|
||||
<detectJavaApiLink>false</detectJavaApiLink>
|
||||
<encoding>UTF-8</encoding>
|
||||
<charset>UTF-8</charset>
|
||||
|
||||
Reference in New Issue
Block a user