mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-05 00:58:17 +08:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ce1538003b | |||
| fa4a97d60d | |||
| 85b7179867 | |||
| 391f9b6389 | |||
| 0e74d6864c | |||
| f5d2022d8b | |||
| d7303719dd | |||
| 76b15f0cda | |||
| f97fd97a3c | |||
| 0c0249cab6 | |||
| 25eae114ad | |||
| edab1a822d | |||
| a9d26e179c | |||
| 3019216801 | |||
| 01250a65d1 |
@@ -26,7 +26,7 @@ jobs:
|
||||
server-password: MAVEN_TOKEN
|
||||
|
||||
- name: "Maven Deploy"
|
||||
run: mvn -B -Pgithub deploy --file pom.xml -DskipTests
|
||||
run: mvn -B -Pgithub deploy --file pom.xml -DskipTests -Dgpg.skip
|
||||
env:
|
||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
@@ -42,9 +42,8 @@ jobs:
|
||||
java-version: '8'
|
||||
distribution: 'adopt'
|
||||
cache: maven
|
||||
|
||||
- name: "Maven Deploy"
|
||||
run: mvn -B -Plocal deploy --file pom.xml -DskipTests
|
||||
run: mvn -B -Plocal deploy --file pom.xml -DskipTests -Dgpg.skip
|
||||
|
||||
- name: "Copy artifacts"
|
||||
run: |
|
||||
@@ -108,3 +107,27 @@ jobs:
|
||||
git add -A
|
||||
git commit -m "API Document generated."
|
||||
git push origin HEAD:gh-pages --force
|
||||
|
||||
central-deploy:
|
||||
name: "Deploy Project (Central)"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: "Set up JDK"
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: '11'
|
||||
distribution: 'adopt'
|
||||
cache: maven
|
||||
server-id: ossrh
|
||||
server-username: MAVEN_USERNAME
|
||||
server-password: MAVEN_PASSWORD
|
||||
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
|
||||
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase
|
||||
|
||||
- name: "Central Deploy"
|
||||
run: mvn -B -Possrh deploy --file pom.xml -DskipTests
|
||||
env:
|
||||
MAVEN_USERNAME: ${{ secrets.OSSRH_USER }}
|
||||
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASS }}
|
||||
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
|
||||
|
||||
@@ -25,7 +25,7 @@ jobs:
|
||||
server-username: MAVEN_USERNAME
|
||||
server-password: MAVEN_TOKEN
|
||||
- name: "Package"
|
||||
run: mvn -B package --file pom.xml -Dmaven.javadoc.skip=true
|
||||
run: mvn -B package --file pom.xml -Dmaven.javadoc.skip=true -Dgpg.skip
|
||||
env:
|
||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
@@ -36,45 +36,54 @@
|
||||
|
||||
### 主要部分 (`/base`)
|
||||
|
||||
- Color [`easyplugin-color`](base/color)
|
||||
- 颜色工具类模块,提供功能全面的MC颜色解析转换工具。
|
||||
- 支持 `&+颜色代码`(原版颜色)、`§(#XXXXXX)`(RGB颜色) 与 `&<#XXXXXX>`(前后标注RGB颜色渐变)。
|
||||
- Utils [`easyplugin-utils`](base/utils)
|
||||
- 通用工具类模块,该模块中的内容支持在Bungee、Bukkit使用。
|
||||
- 本模块提供
|
||||
- `ColorParser` 支持RGB颜色与RGB渐变色的颜色解析器。
|
||||
- `EasyCooldown` 快速创造一个冷却时间的管理器。
|
||||
- `JarResourceUtils` 快速读取Jar包内容的工具类。
|
||||
- 通用工具类模块,该模块中的内容支持在Bungee、Bukkit使用。
|
||||
- 本模块提供
|
||||
- `ColorParser` 支持RGB颜色与RGB渐变色的颜色解析器。
|
||||
- `EasyCooldown` 快速创造一个冷却时间的管理器。
|
||||
- `JarResourceUtils` 快速读取Jar包内容的工具类。
|
||||
- Main [`easyplugin-main`](base/main)
|
||||
- 主要接口模块,提供了方便的插件入口类与相关工具类。
|
||||
- 主要接口模块,提供了方便的插件入口类与相关工具类。
|
||||
- Command [`easyplugin-command`](base/command)
|
||||
- 指令接口模块,便于快速进行子指令的实现,并提供单独的TabComplete方法。
|
||||
- 随本项目提供了 `SimpleCompleter` 类,用于快速创建补全的内容列表。
|
||||
- 指令接口模块,便于快速进行子指令的实现,并提供单独的TabComplete方法。
|
||||
- 随本项目提供了 `SimpleCompleter` 类,用于快速创建补全的内容列表。
|
||||
- GUI [`easyplugin-gui`](base/main)
|
||||
- 简单便捷的箱子GUI接口,可以快速实现GUI中不同图标的点击功能。
|
||||
- 随本项目提供了 `AutoPagedGUI` 等翻页GUI抽象类。
|
||||
- 简单便捷的箱子GUI接口,可以快速实现GUI中不同图标的点击功能。
|
||||
- 随本项目提供了 `AutoPagedGUI` 等翻页GUI抽象类。
|
||||
- Storage [`easyplugin-storage`](base/storage)
|
||||
- 抽象存储管理器,便于实现不同的存储类型。
|
||||
- 随本项目提供了 `FileBasedStorage`、`FolderBasedStorage` 等常用存储抽象方法。
|
||||
- 抽象存储管理器,便于实现不同的存储类型。
|
||||
- 随本项目提供了 `FileBasedStorage`、`FolderBasedStorage` 等常用存储抽象方法。
|
||||
- Messages [`easyplugin-message`](base/messages)
|
||||
- 随本项目提供了基于 MineConfiguration 实现的 `EasyMessages` 类。
|
||||
- 支持多种消息配置,包括文本消息、ActionBar消息、Title消息、声音、粒子效果播放等。
|
||||
- 支持消息间的延迟发送。
|
||||
|
||||
> 以下项目均已独立出单独项目,仅仅是按照包名规则打包到EasyPlugin的子项目中。
|
||||
>
|
||||
> 如需使用,**强烈建议自行引用对应的项目**,以支持完整的Javadoc并获取源码内容!
|
||||
### 独立项目部分
|
||||
|
||||
- _Listener_ [`easyplugin-listener`](base/listener) (打包自 [**EasyListener**](https://github.com/CarmJos/EasyListener))
|
||||
- _Configuration_ [`easyplugin-conf`](base/conf) (打包自 [**MineConfiguration**](https://github.com/CarmJos/MineConfiguration))
|
||||
- _Database_ [`easyplugin-database`](base/database) (打包自 [**EasySQL**](https://github.com/CarmJos/EasySQL))
|
||||
- _Messages*_ [`easyplugin-message`](base/message) (打包自 [**EasyMessages**](https://github.com/CarmJos/EasyMessages))
|
||||
> 以下项目均已独立出单独项目,如需使用,**强烈建议自行引用对应的项目**,以支持完整的Javadoc并获取源码内容!
|
||||
|
||||
- _Listener_ -> [**EasyListener**](https://github.com/CarmJos/EasyListener)
|
||||
- _Configuration_ -> [**MineConfiguration**](https://github.com/CarmJos/MineConfiguration)
|
||||
- _Database_ -> [**EasySQL**](https://github.com/CarmJos/EasySQL)
|
||||
|
||||
### 附属部分 (`/extension`)
|
||||
|
||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/6245/)* [`easyplugin-placeholderapi`](extension/papi)
|
||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/6245/) [`easyplugin-placeholderapi`](extension/papi)
|
||||
- PlaceholderAPI 扩展模块,提供了方便的 PlaceholderAPI 变量注册方法。
|
||||
- [Vault](https://github.com/MilkBowl/VaultAPI)* [`easyplugin-vault`](extension/vault)
|
||||
|
||||
## 开发
|
||||
|
||||
详细开发介绍请 [点击这里](.documentation/README.md) , JavaDoc(最新Release) 请 [点击这里](https://carmjos.github.io/EasyPlugin) 。
|
||||
详细开发介绍请 [点击这里](.documentation/README.md) , JavaDoc(最新Release)
|
||||
请 [点击这里](https://carmjos.github.io/EasyPlugin) 。
|
||||
|
||||
### 示例代码
|
||||
|
||||
您可以 [点击这里](https://github.com/CarmJos/UltraDepository) 查看实例项目演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
您可以 [点击这里](https://github.com/CarmJos/UltraDepository)
|
||||
查看实例项目演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
|
||||
### 依赖方式
|
||||
|
||||
@@ -181,7 +190,8 @@ dependencies {
|
||||
|
||||
> MIT 协议可能是几大开源协议中最宽松的一个,核心条款是:
|
||||
>
|
||||
> 该软件及其相关文档对所有人免费,可以任意处置,包括使用,复制,修改,合并,发表,分发,再授权,或者销售。唯一的限制是,软件中必须包含上述版 权和许可提示。
|
||||
> 该软件及其相关文档对所有人免费,可以任意处置,包括使用,复制,修改,合并,发表,分发,再授权,或者销售。唯一的限制是,软件中必须包含上述版
|
||||
> 权和许可提示。
|
||||
>
|
||||
> 这意味着:
|
||||
> - 你可以自由使用,复制,修改,可以用于自己的项目。
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-color</artifactId>
|
||||
|
||||
<name>EasyPlugin-Color</name>
|
||||
<description>轻松插件颜色模块,支持简单便捷的颜色解析器,包括基本颜色、RGB颜色与RGB渐变颜色。</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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-command-alias</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasyPlugin-Command-Alias</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>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package cc.carm.lib.easyplugin.command.alias;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AliasCommand extends Command {
|
||||
|
||||
protected final AliasCommandManager aliasCommandManager;
|
||||
protected final String targetCommand;
|
||||
|
||||
public AliasCommand(String name, AliasCommandManager aliasCommandManager, String targetCommand) {
|
||||
super(name);
|
||||
this.aliasCommandManager = aliasCommandManager;
|
||||
this.targetCommand = targetCommand;
|
||||
}
|
||||
|
||||
protected SimpleCommandMap getCommandMap() {
|
||||
return this.aliasCommandManager.getCommandMap();
|
||||
}
|
||||
|
||||
protected String buildCommand(String[] args) {
|
||||
return this.targetCommand + " " + String.join(" ", args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
|
||||
return getCommandMap().dispatch(sender, buildCommand(args));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias,
|
||||
@NotNull String[] args, @Nullable Location location) throws IllegalArgumentException {
|
||||
return Optional.ofNullable(getCommandMap().tabComplete(sender, buildCommand(args))).orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
|
||||
return tabComplete(sender, alias, args, null);
|
||||
}
|
||||
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package cc.carm.lib.easyplugin.command.alias;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.plugin.SimplePluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 指令简化别名(简化映射)管理器
|
||||
* <br>支持将插件内复杂的子指令简化为一个单独的指令,方便玩家使用。
|
||||
*/
|
||||
public class AliasCommandManager {
|
||||
|
||||
protected final @NotNull JavaPlugin plugin;
|
||||
protected final @NotNull String prefix;
|
||||
|
||||
protected final @NotNull SimpleCommandMap commandMap;
|
||||
protected final @NotNull Field knownCommandsFiled;
|
||||
|
||||
protected final @NotNull Map<String, AliasCommand> registeredCommands = new HashMap<>();
|
||||
|
||||
public AliasCommandManager(@NotNull JavaPlugin plugin) throws Exception {
|
||||
this(plugin, plugin.getName());
|
||||
}
|
||||
|
||||
public AliasCommandManager(@NotNull JavaPlugin plugin, @NotNull String prefix) throws Exception {
|
||||
this.plugin = plugin;
|
||||
this.prefix = prefix.trim();
|
||||
|
||||
SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager();
|
||||
Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
|
||||
commandMapField.setAccessible(true);
|
||||
this.commandMap = (SimpleCommandMap) commandMapField.get(manager);
|
||||
|
||||
this.knownCommandsFiled = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
this.knownCommandsFiled.setAccessible(true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Command> getKnownCommands() {
|
||||
try {
|
||||
return (Map<String, Command>) knownCommandsFiled.get(commandMap);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
protected @NotNull SimpleCommandMap getCommandMap() {
|
||||
return commandMap;
|
||||
}
|
||||
|
||||
public String getCommandPrefix() {
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
public void register(@NotNull String alias, @NotNull String subCommand) {
|
||||
AliasCommand current = this.registeredCommands.get(alias);
|
||||
if (current != null) current.unregister(getCommandMap());
|
||||
|
||||
AliasCommand cmd = new AliasCommand(alias, this, getCommandPrefix() + " " + subCommand);
|
||||
this.registeredCommands.put(alias, cmd);
|
||||
getCommandMap().register(plugin.getName(), cmd);
|
||||
}
|
||||
|
||||
public void unregister(@NotNull String alias) {
|
||||
AliasCommand current = this.registeredCommands.remove(alias);
|
||||
if (current != null) {
|
||||
getKnownCommands().remove(alias);
|
||||
current.unregister(getCommandMap());
|
||||
}
|
||||
}
|
||||
|
||||
public void unregisterAll() {
|
||||
registeredCommands.forEach((k, v) -> {
|
||||
getKnownCommands().remove(k);
|
||||
v.unregister(getCommandMap());
|
||||
});
|
||||
registeredCommands.clear();
|
||||
}
|
||||
|
||||
}
|
||||
+11
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -48,4 +48,14 @@
|
||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
||||
</ciManagement>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -46,7 +46,8 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
public abstract Void noPermission(CommandSender sender);
|
||||
|
||||
public Void onException(CommandSender sender, SubCommand<?> cmd, Exception ex) {
|
||||
sender.sendMessage("Error occurred when executing " + cmd.getName() + ": " + ex.getLocalizedMessage());
|
||||
sender.sendMessage("Error occurred when executing " + cmd.getIdentifier() + ": " + ex.getLocalizedMessage());
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -56,18 +57,18 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
public @NotNull String getIdentifier() {
|
||||
return this.cmd;
|
||||
}
|
||||
|
||||
public void registerSubCommand(SubCommand<?> command) {
|
||||
String name = command.getName().toLowerCase();
|
||||
String name = command.getIdentifier().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();
|
||||
String name = handler.getIdentifier().toLowerCase();
|
||||
this.registeredHandlers.put(name, handler);
|
||||
handler.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
@@ -106,7 +107,6 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
sub.execute(this.plugin, sender, this.shortenArgs(args));
|
||||
} catch (Exception ex) {
|
||||
this.onException(sender, sub, ex);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
if (args.length == 1) {
|
||||
return getExecutors().stream()
|
||||
.filter(e -> e.hasPermission(sender))
|
||||
.map(NamedExecutor::getName)
|
||||
.map(NamedExecutor::getIdentifier)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
@@ -145,7 +145,7 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
executors.addAll(this.registeredHandlers.values());
|
||||
executors.addAll(this.registeredCommands.values());
|
||||
List<NamedExecutor> sortedExecutors = new ArrayList<>(executors);
|
||||
sortedExecutors.sort(Comparator.comparing(NamedExecutor::getName));
|
||||
sortedExecutors.sort(Comparator.comparing(NamedExecutor::getIdentifier));
|
||||
return sortedExecutors;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,16 +11,16 @@ import java.util.function.Function;
|
||||
|
||||
public interface NamedExecutor {
|
||||
|
||||
String getName();
|
||||
@NotNull String getIdentifier();
|
||||
|
||||
List<String> getAliases();
|
||||
@NotNull List<String> getAliases();
|
||||
|
||||
default boolean hasPermission(CommandSender sender) {
|
||||
default boolean hasPermission(@NotNull CommandSender sender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default Void sendMessage(@NotNull CommandSender sender, @NotNull String... messages) {
|
||||
return sendMessage(sender, (Function<String, String>) null, messages);
|
||||
return sendMessage(sender, Function.identity(), messages);
|
||||
}
|
||||
|
||||
default Void sendMessage(@NotNull CommandSender sender,
|
||||
|
||||
@@ -14,12 +14,12 @@ public abstract class SubCommand<C extends CommandHandler> implements NamedExecu
|
||||
|
||||
private final @NotNull C parent;
|
||||
|
||||
private final String name;
|
||||
private final String identifier;
|
||||
private final List<String> aliases;
|
||||
|
||||
public SubCommand(@NotNull C parent, String name, String... aliases) {
|
||||
public SubCommand(@NotNull C parent, String identifier, String... aliases) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.identifier = identifier;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
@@ -28,13 +28,13 @@ public abstract class SubCommand<C extends CommandHandler> implements NamedExecu
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
public @NotNull String getIdentifier() {
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Unmodifiable
|
||||
public List<String> getAliases() {
|
||||
public @NotNull List<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,81 +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.4.19</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-conf</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasyPlugin-Configuration</name>
|
||||
<description>轻松插件配置文件接口模块,基于 MineConfiguration 项目。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- https://github.com/CarmJos/MineConfiguration -->
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.0</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.configuration.core</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.conf</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>cc.carm.lib.mineconfiguration.common</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.conf</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>cc.carm.lib.mineconfiguration.bukkit</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.conf</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,73 +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.4.19</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-database</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasyPlugin-Database</name>
|
||||
<description>轻松插件数据库接口模块,基于 EasySQL 项目。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- https://github.com/CarmJos/EasySQL -->
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-beecp</artifactId>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.0</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>
|
||||
+10
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -59,4 +59,13 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -117,7 +117,7 @@ public class CommonPagedGUI extends PagedGUI {
|
||||
* @return 最后一页的页码
|
||||
*/
|
||||
public int getLastPageNumber() {
|
||||
return (this.container.size() / range.length) + 1;
|
||||
return (this.container.size() / range.length) + ((this.container.size() % range.length) == 0 ? 0 : 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,80 +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.4.19</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-listener</artifactId>
|
||||
|
||||
<name>EasyPlugin-Listener</name>
|
||||
<description>轻松插件监听器接口模块,基于 EasyListener 项目。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>GNU LESSER GENERAL PUBLIC LICENSE</name>
|
||||
<url>https://www.gnu.org/licenses/lgpl-3.0.html</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- https://github.com/CarmJos/EasyListener -->
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easylistener</artifactId>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.0</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.easylistener</pattern>
|
||||
<shadedPattern>cc.carm.lib.easyplugin.listener</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>
|
||||
+10
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -68,4 +68,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,208 @@
|
||||
package cc.carm.lib.easyplugin.utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 颜色解析器。
|
||||
* <br> 普通颜色 格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a}
|
||||
* <br> RGB颜色(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)}
|
||||
* <br> 渐变RBG颜色(版本需要≥1.14) 格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }
|
||||
* <p> 注意:当使用渐变RGB颜色时,普通颜色代码与RGB颜色代码将失效。
|
||||
*/
|
||||
public class ColorParser {
|
||||
|
||||
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([\\da-fA-F]{6})\\)");
|
||||
public static final Pattern GRADIENT_PATTERN = Pattern.compile("&<&?#([\\da-fA-F]{6})>");
|
||||
public static final Pattern COLOR_PATTERN = Pattern.compile("([&§][0-9a-fA-FrRxX])+"); // 会影响颜色的代码
|
||||
public static final Pattern FORMAT_PATTERN = Pattern.compile("([&§][0-9a-fA-Fk-oK-OrRxX])+"); // MC可用的格式化代码
|
||||
|
||||
/**
|
||||
* 清除一条消息中的全部颜色代码 (包括RGB颜色代码与渐变颜色代码)
|
||||
*
|
||||
* @param text 源消息内容
|
||||
* @return 清理颜色后的消息
|
||||
*/
|
||||
public static @NotNull String clear(@NotNull String text) {
|
||||
text = HEX_PATTERN.matcher(text).replaceAll("");
|
||||
text = GRADIENT_PATTERN.matcher(text).replaceAll("");
|
||||
text = COLOR_PATTERN.matcher(text).replaceAll("");
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对一条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。
|
||||
*
|
||||
* @param text 源消息内容
|
||||
* @return 解析后的消息
|
||||
*/
|
||||
public static @NotNull String parse(@NotNull String text) {
|
||||
return parseBaseColor(parseGradientColor(parseHexColor(text)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。
|
||||
*
|
||||
* @param texts 源消息内容
|
||||
* @return 解析后的消息
|
||||
*/
|
||||
public static @NotNull String[] parse(@NotNull String... texts) {
|
||||
return parse(Arrays.asList(texts)).toArray(new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对多条消息进行颜色解析,包括普通颜色代码、RGB颜色代码与RBG渐变代码。
|
||||
*
|
||||
* @param texts 源消息内容
|
||||
* @return 解析后的消息
|
||||
*/
|
||||
public static @NotNull List<String> parse(@NotNull Collection<String> texts) {
|
||||
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析消息中的基本颜色代码格式 {@code &+颜色代码 },如 {@literal &c} 、{@literal &a}
|
||||
*
|
||||
* @param text 消息内容
|
||||
* @return RGB处理后的消息
|
||||
* @see net.md_5.bungee.api.ChatColor
|
||||
*/
|
||||
public static String parseBaseColor(final String text) {
|
||||
return text.replaceAll("&", "§").replace("§§", "&");
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析消息中的RGB颜色代码(版本需要≥1.14) 格式 {@code &(#XXXXXX) },如 {@literal &(#aaaaaa)}
|
||||
*
|
||||
* @param text 消息内容
|
||||
* @return RGB处理后的消息
|
||||
*/
|
||||
public static String parseHexColor(String text) {
|
||||
Matcher matcher = HEX_PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase());
|
||||
matcher.reset(text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对一条消息进行RGB渐变处理(版本需要≥1.14),格式 {@code &<#XXXXXX>FOOBAR&<#XXXXXX> }。
|
||||
*
|
||||
* @param text 消息内容
|
||||
* @return RGB渐变处理后的消息
|
||||
*/
|
||||
public static @NotNull String parseGradientColor(@NotNull String text) {
|
||||
List<String> colors = new ArrayList<>();
|
||||
|
||||
Matcher matcher = ColorParser.GRADIENT_PATTERN.matcher(text);
|
||||
while (matcher.find()) colors.add(matcher.group(1));
|
||||
|
||||
if (colors.isEmpty()) return text; // 无渐变颜色,直接跳出
|
||||
|
||||
String[] parts = ColorParser.GRADIENT_PATTERN.split(text);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
String startHex = i - 1 >= 0 && colors.size() > i - 1 ? colors.get(i - 1) : null; // 本条消息的起始颜色
|
||||
String endHex = colors.size() > i ? colors.get(i) : null; // 本条消息的结束颜色
|
||||
builder.append(gradientText(parts[i], startHex, endHex));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static @NotNull String gradientText(@NotNull String text,
|
||||
@Nullable Color startColor, @Nullable Color endColor) {
|
||||
Objects.requireNonNull(text, "Text to be gradient should not be null!");
|
||||
if (startColor == null || endColor == null || text.isEmpty()) {
|
||||
// 起始颜色有任一为空,则不进行渐变上色。
|
||||
// 若有起始颜色,则代表其跟在某个渐变之后,应当添加"&r"阻断前面的渐变。
|
||||
return (startColor != null ? "&r" : "") + text;
|
||||
}
|
||||
|
||||
// 用于记录消息中特殊格式的位置
|
||||
// 在渐变中,允许使用格式字符与颜色字符来改变其中某个字的颜色/格式,以支持更多形式内容。
|
||||
LinkedHashMap<Integer, String> extraFormats = new LinkedHashMap<>();
|
||||
Matcher matcher = ColorParser.FORMAT_PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
extraFormats.put(matcher.start(), matcher.group());
|
||||
text = matcher.replaceFirst("");
|
||||
matcher.reset(text);
|
||||
}
|
||||
|
||||
if (text.length() == 1) {
|
||||
// 当只有一个实际字符时,无需进行渐变计算,直接返回 中间颜色+起始格式(如果有)+消息 即可。
|
||||
return colorText(text, extraFormats.get(0), buildHexColor(mediumHex(startColor, endColor)));
|
||||
}
|
||||
|
||||
String[] characters = text.split("");
|
||||
int step = characters.length; // 变换次数
|
||||
|
||||
// 决定每种颜色变换的方向
|
||||
int rDirection = startColor.getRed() < endColor.getRed() ? 1 : -1;
|
||||
int gDirection = startColor.getGreen() < endColor.getGreen() ? 1 : -1;
|
||||
int bDirection = startColor.getBlue() < endColor.getBlue() ? 1 : -1;
|
||||
|
||||
// 决定每种颜色每次变换的度
|
||||
int rStep = Math.abs(startColor.getRed() - endColor.getRed()) / (step - 1);
|
||||
int gStep = Math.abs(startColor.getGreen() - endColor.getGreen()) / (step - 1);
|
||||
int bStep = Math.abs(startColor.getBlue() - endColor.getBlue()) / (step - 1);
|
||||
|
||||
String[] hexes = IntStream.range(0, step).mapToObj(i -> colorToHex(
|
||||
startColor.getRed() + rStep * i * rDirection,
|
||||
startColor.getGreen() + gStep * i * gDirection,
|
||||
startColor.getBlue() + bStep * i * bDirection
|
||||
)).toArray(String[]::new);
|
||||
|
||||
return IntStream.range(0, characters.length)
|
||||
.mapToObj(i -> colorText(characters[i], extraFormats.get(i), buildHexColor(hexes[i])))
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
protected static String gradientText(@NotNull String text, @Nullable String startHex, @Nullable String endHex) {
|
||||
return gradientText(text,
|
||||
startHex == null ? null : Color.decode("0x" + startHex),
|
||||
endHex == null ? null : Color.decode("0x" + endHex)
|
||||
);
|
||||
}
|
||||
|
||||
private static String mediumHex(@NotNull Color start, @NotNull Color end) {
|
||||
return colorToHex(
|
||||
Math.abs(start.getRed() - end.getRed()) / 2,
|
||||
Math.abs(start.getGreen() - end.getGreen()) / 2,
|
||||
Math.abs(start.getBlue() - end.getBlue()) / 2
|
||||
);
|
||||
}
|
||||
|
||||
private static String colorText(String message, @Nullable String format, @Nullable String color) {
|
||||
if (format != null && COLOR_PATTERN.matcher(format).find()) {
|
||||
// format中存在影响颜色的内容,则当前消息的颜色会被覆盖。
|
||||
// 为了减少最终消息的长度,故直接返回键入的FORMAT和对应消息的内容。
|
||||
return format + message;
|
||||
}
|
||||
return (color == null ? "" : color) + (format == null ? "" : parseBaseColor(format)) + message;
|
||||
}
|
||||
|
||||
protected static String colorToHex(Color color) {
|
||||
return colorToHex(color.getRed(), color.getGreen(), color.getBlue());
|
||||
}
|
||||
|
||||
protected static String colorToHex(int r, int g, int b) {
|
||||
// 将R、G、B转换为16进制(若非2位则补0)输出
|
||||
return String.format("%02X%02X%02X", r, g, b);
|
||||
}
|
||||
|
||||
protected static String buildHexColor(String hexCode) {
|
||||
return Arrays.stream(hexCode.split("")).map(s -> '§' + s)
|
||||
.collect(Collectors.joining("", '§' + "x", ""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
<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>
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
</properties>
|
||||
<artifactId>easyplugin-messages</artifactId>
|
||||
|
||||
<name>EasyPlugin-Messages</name>
|
||||
<description>
|
||||
轻松插件消息配置模块,支持多种消息配置,包括文本消息、ActionBar消息、Title消息、声音、粒子效果播放等。
|
||||
</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>easyplugin-color</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -23,5 +23,13 @@
|
||||
<description>轻松插件存储接口模块,包含存储相关的统一接口。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,64 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
<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>
|
||||
<artifactId>easyplugin-user</artifactId>
|
||||
|
||||
<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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package cc.carm.lib.easyplugin.user;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class UserData<K> {
|
||||
|
||||
protected final @NotNull K key;
|
||||
|
||||
protected UserData(@NotNull K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public @NotNull K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
UserData<?> userData = (UserData<?>) o;
|
||||
return key.equals(userData.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
package cc.carm.lib.easyplugin.user;
|
||||
|
||||
import cc.carm.lib.easyplugin.EasyPlugin;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class UserDataManager<K, U extends UserData<K>> {
|
||||
|
||||
protected final @NotNull EasyPlugin plugin;
|
||||
|
||||
protected final @NotNull ExecutorService executor;
|
||||
protected final @NotNull Map<K, U> dataCache;
|
||||
|
||||
public UserDataManager(@NotNull EasyPlugin plugin) {
|
||||
this(plugin, Executors.newCachedThreadPool((r) -> {
|
||||
Thread t = new Thread(r);
|
||||
t.setDaemon(true);
|
||||
t.setName(plugin.getName() + "-UserManager");
|
||||
return t;
|
||||
}), new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
public UserDataManager(@NotNull EasyPlugin plugin, @NotNull ExecutorService executor) {
|
||||
this(plugin, executor, new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
public UserDataManager(@NotNull EasyPlugin plugin, @NotNull ExecutorService executor, @NotNull Map<K, U> cacheMap) {
|
||||
this.plugin = plugin;
|
||||
this.executor = executor;
|
||||
this.dataCache = cacheMap;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
this.executor.shutdown();
|
||||
}
|
||||
|
||||
protected @NotNull EasyPlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
protected @NotNull Logger getLogger() {
|
||||
return getPlugin().getLogger();
|
||||
}
|
||||
|
||||
public String serializeKey(@NotNull K key) {
|
||||
return key.toString();
|
||||
}
|
||||
|
||||
public abstract @NotNull U empty(@NotNull K key);
|
||||
|
||||
protected abstract @Nullable U loadData(@NotNull K key) throws Exception;
|
||||
|
||||
protected abstract void saveData(@NotNull U data) throws Exception;
|
||||
|
||||
public @NotNull CompletableFuture<U> load(@NotNull K key) {
|
||||
return load(key, false);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<U> load(@NotNull K key, boolean cache) {
|
||||
return load(key, () -> cache);
|
||||
}
|
||||
|
||||
public @NotNull Map<K, U> getDataCache() {
|
||||
return dataCache;
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
public @NotNull Set<U> list() {
|
||||
return ImmutableSet.copyOf(getDataCache().values());
|
||||
}
|
||||
|
||||
public @NotNull U get(@NotNull K key) {
|
||||
return Optional.ofNullable(getNullable(key)).orElseThrow(() -> new NullPointerException("User " + key + " not found."));
|
||||
}
|
||||
|
||||
public @Nullable U getNullable(@NotNull K key) {
|
||||
return getDataCache().get(key);
|
||||
}
|
||||
|
||||
public @NotNull Optional<@Nullable U> getOptional(@NotNull K key) {
|
||||
return Optional.ofNullable(getNullable(key));
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<U> load(@NotNull K key, @NotNull Supplier<Boolean> cacheCondition) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
String identifier = serializeKey(key);
|
||||
|
||||
try {
|
||||
long s1 = System.currentTimeMillis();
|
||||
getPlugin().debug("开始加载用户 " + identifier + " 的数据...");
|
||||
U data = loadData(key);
|
||||
if (data == null) {
|
||||
getPlugin().debug("数据库内不存在用户 " + identifier + " 的数据,视作新档案。");
|
||||
return empty(key);
|
||||
} else {
|
||||
getPlugin().debug("加载用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms.");
|
||||
return data;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
getPlugin().error("加载用户 " + serializeKey(key) + " 数据失败,请检查相关配置!");
|
||||
ex.printStackTrace();
|
||||
return empty(key);
|
||||
}
|
||||
|
||||
}, executor).thenApply((data) -> {
|
||||
if (cacheCondition.get()) dataCache.put(key, data);
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Boolean> save(@NotNull U user) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
String identifier = serializeKey(user.getKey());
|
||||
|
||||
try {
|
||||
long s1 = System.currentTimeMillis();
|
||||
getPlugin().debug("开始保存用户 " + identifier + " 的数据...");
|
||||
saveData(user);
|
||||
getPlugin().debug("保存用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms.");
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
getPlugin().error("保存用户 " + identifier + " 数据失败,请检查相关配置!");
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
}, executor);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Boolean> unload(@NotNull K key) {
|
||||
return unload(key, true);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Boolean> unload(@NotNull K key, boolean save) {
|
||||
U data = getNullable(key);
|
||||
if (data == null) return CompletableFuture.completedFuture(false);
|
||||
|
||||
if (save) {
|
||||
return save(data).thenApply(result -> {
|
||||
this.dataCache.remove(key);
|
||||
return result;
|
||||
});
|
||||
} else {
|
||||
this.dataCache.remove(key);
|
||||
return CompletableFuture.completedFuture(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Boolean> modify(@NotNull K key, @NotNull Consumer<U> consumer) {
|
||||
U cached = getNullable(key);
|
||||
if (cached != null) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
consumer.accept(cached);
|
||||
return true;
|
||||
}, executor);
|
||||
} else {
|
||||
return load(key, false).thenApply((data) -> {
|
||||
consumer.accept(data);
|
||||
return data;
|
||||
}).thenCompose(this::save);
|
||||
}
|
||||
}
|
||||
|
||||
public <V> @NotNull CompletableFuture<V> peek(@NotNull K key, @NotNull Function<U, V> function) {
|
||||
U cached = getNullable(key);
|
||||
if (cached != null) {
|
||||
return CompletableFuture.supplyAsync(() -> function.apply(cached), executor);
|
||||
} else {
|
||||
return load(key, false).thenApply(function);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Map<K, U>> loadOnline(@NotNull Function<Player, ? extends K> function) {
|
||||
return loadGroup(Bukkit.getOnlinePlayers(), function, OfflinePlayer::isOnline);
|
||||
}
|
||||
|
||||
public <T> @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<? extends T> users,
|
||||
@NotNull Function<? super T, ? extends K> function,
|
||||
@NotNull Predicate<T> cacheCondition) {
|
||||
CompletableFuture<Map<K, U>> task = CompletableFuture.completedFuture(new ConcurrentHashMap<>());
|
||||
if (users.isEmpty()) return task;
|
||||
|
||||
Map<K, T> usersMap = users.stream().collect(Collectors.toMap(function, Function.identity()));
|
||||
for (Map.Entry<K, T> entry : usersMap.entrySet()) {
|
||||
K key = entry.getKey();
|
||||
T user = entry.getValue();
|
||||
task = task.thenCombine(
|
||||
load(key, () -> cacheCondition.test(user)),
|
||||
(map, result) -> {
|
||||
map.put(key, result);
|
||||
return map;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return task.thenApply(Collections::unmodifiableMap);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<K> allKeys,
|
||||
@NotNull Predicate<K> cacheCondition) {
|
||||
return loadGroup(allKeys, Function.identity(), cacheCondition);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<K> allKeys) {
|
||||
return loadGroup(allKeys, (v) -> false);
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Integer> saveAll() {
|
||||
CompletableFuture<Integer> future = CompletableFuture.completedFuture(0);
|
||||
if (getDataCache().isEmpty()) return future;
|
||||
|
||||
for (U value : getDataCache().values()) {
|
||||
future = future.thenCombine(save(value), (before, result) -> before + (result ? 1 : 0));
|
||||
}
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
public @NotNull CompletableFuture<Integer> unloadAll() {
|
||||
return saveAll().thenApply(result -> {
|
||||
getDataCache().clear();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+20
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -22,6 +22,15 @@
|
||||
<description>轻松插件工具类模块,该模块中的内容支持在Bungee、Bukkit使用。</description>
|
||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>easyplugin-color</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>CarmJos</id>
|
||||
@@ -48,4 +57,14 @@
|
||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
||||
</ciManagement>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+11
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -62,4 +62,14 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+12
-18
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -75,23 +75,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-listener</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-conf</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyplugin-papi</artifactId>
|
||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -137,4 +121,14 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -68,10 +68,6 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-vault</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-database</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-storage</artifactId>
|
||||
@@ -82,4 +78,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--suppress VulnerableLibrariesLocal -->
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>githubreleases4j</artifactId>
|
||||
@@ -30,5 +31,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+9
-3
@@ -1,7 +1,9 @@
|
||||
package cc.carm.lib.easyplugin.updatechecker;
|
||||
|
||||
import cc.carm.lib.githubreleases4j.GithubReleases4J;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@@ -24,11 +26,15 @@ public class GHUpdateChecker {
|
||||
public static @NotNull Runnable runner(@NotNull Logger logger,
|
||||
@NotNull String owner, @NotNull String repo,
|
||||
@NotNull String currentVersion) {
|
||||
return of(logger, owner, repo).runner(currentVersion);
|
||||
return of(logger, owner, repo).createRunner(currentVersion);
|
||||
}
|
||||
|
||||
public static @NotNull Runnable runner(@NotNull Plugin plugin) {
|
||||
return of(plugin).runner(plugin.getDescription().getVersion());
|
||||
return of(plugin).createRunner(plugin.getDescription().getVersion());
|
||||
}
|
||||
|
||||
public static @NotNull BukkitTask run(@NotNull Plugin plugin) {
|
||||
return Bukkit.getScheduler().runTaskAsynchronously(plugin, runner(plugin));
|
||||
}
|
||||
|
||||
protected final @NotNull Logger logger;
|
||||
@@ -59,7 +65,7 @@ public class GHUpdateChecker {
|
||||
}
|
||||
}
|
||||
|
||||
public Runnable runner(@NotNull String currentVersion) {
|
||||
public Runnable createRunner(@NotNull String currentVersion) {
|
||||
return () -> checkUpdate(currentVersion);
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -21,6 +21,7 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasyPlugin-PlaceholderAPI</name>
|
||||
<description>EasyPlugin的 PlaceholderAPI 扩展模块,提供了方便的 PlaceholderAPI 变量注册方法。</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -32,4 +33,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,231 @@
|
||||
package cc.carm.lib.easyplugin.papi;
|
||||
|
||||
import cc.carm.lib.easyplugin.papi.expansion.SectionExpansion;
|
||||
import cc.carm.lib.easyplugin.papi.expansion.SubExpansion;
|
||||
import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class EasyPlaceholder extends PlaceholderExpansion {
|
||||
|
||||
protected final @NotNull String plugin;
|
||||
protected final @NotNull SectionExpansion rootExpansion;
|
||||
|
||||
protected final @NotNull String name;
|
||||
protected final @NotNull String author;
|
||||
protected final @NotNull String version;
|
||||
|
||||
protected final boolean persistent;
|
||||
|
||||
public EasyPlaceholder(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
|
||||
this(
|
||||
plugin.getName(), true, rootIdentifier,
|
||||
plugin.getName(), plugin.getDescription().getVersion(),
|
||||
String.join(", ", plugin.getDescription().getAuthors())
|
||||
);
|
||||
}
|
||||
|
||||
public EasyPlaceholder(@NotNull JavaPlugin plugin, @NotNull SectionExpansion rootExpansion) {
|
||||
this(
|
||||
plugin.getName(), true, rootExpansion,
|
||||
plugin.getName(), plugin.getDescription().getVersion(),
|
||||
String.join(", ", plugin.getDescription().getAuthors())
|
||||
);
|
||||
}
|
||||
|
||||
public EasyPlaceholder(@NotNull String plugin, boolean persistent, @NotNull String rootIdentifier,
|
||||
@NotNull String name, @NotNull String version, @NotNull String author) {
|
||||
super();
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
this.author = author;
|
||||
this.version = version;
|
||||
this.persistent = persistent;
|
||||
this.rootExpansion = new SectionExpansion(this, rootIdentifier);
|
||||
}
|
||||
|
||||
public EasyPlaceholder(@NotNull String plugin, boolean persistent,
|
||||
@NotNull SectionExpansion rootExpansion,
|
||||
@NotNull String name, @NotNull String version, @NotNull String author) {
|
||||
super();
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
this.author = author;
|
||||
this.version = version;
|
||||
this.persistent = persistent;
|
||||
this.rootExpansion = rootExpansion;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到根变量解析器
|
||||
*
|
||||
* @return 根变量解析器
|
||||
*/
|
||||
public @NotNull SectionExpansion getRootExpansion() {
|
||||
return rootExpansion;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当未找到对应变量处理器时调用
|
||||
*
|
||||
* @param player 变量解析时的目标玩家,可能为空或不在线。
|
||||
* @return 返回给玩家的内容
|
||||
*/
|
||||
public String onErrorParams(@Nullable OfflinePlayer player) {
|
||||
return "Wrong params";
|
||||
}
|
||||
|
||||
/**
|
||||
* 当变量处理器返回 NULL 值时调用
|
||||
*
|
||||
* @param player 变量解析时的目标玩家,可能为空或不在线。
|
||||
* @return 返回给玩家的内容
|
||||
*/
|
||||
public String onNullResponse(@Nullable OfflinePlayer player) {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 当变量处理器抛出异常时调用
|
||||
*
|
||||
* @param player 变量解析时的目标玩家,可能为空或不在线。
|
||||
* @param expansion 抛出异常的变量处理器
|
||||
* @param exception 异常内容
|
||||
* @return 返回给玩家的内容
|
||||
*/
|
||||
public String onException(@Nullable OfflinePlayer player, @NotNull SubExpansion<?> expansion, @NotNull Exception exception) {
|
||||
exception.printStackTrace();
|
||||
return "Error \"" + expansion.getIdentifier() + "\"";
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param aliases 该变量的别称
|
||||
* @return {@link EasyPlaceholder}
|
||||
*/
|
||||
public final EasyPlaceholder handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull String... aliases) {
|
||||
this.rootExpansion.handle(identifier, handler, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param paramsConsumer 用于提供该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
* @return {@link EasyPlaceholder}
|
||||
*/
|
||||
public final EasyPlaceholder handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull Consumer<ArrayList<String>> paramsConsumer, @NotNull String... aliases) {
|
||||
this.rootExpansion.handle(identifier, handler, paramsConsumer, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param availableParams 该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
* @return {@link EasyPlaceholder}
|
||||
*/
|
||||
public final EasyPlaceholder handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull List<String> availableParams, @NotNull String... aliases) {
|
||||
this.rootExpansion.handle(identifier, handler, availableParams, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param availableParams 该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
* @return {@link EasyPlaceholder}
|
||||
*/
|
||||
public final EasyPlaceholder handle(String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull Supplier<List<String>> availableParams, @NotNull String... aliases) {
|
||||
this.rootExpansion.handle(identifier, handler, availableParams, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理一组变量。
|
||||
*
|
||||
* @param section 该组变量的标识符
|
||||
* @param consumer 该组变量的处理器操作方法
|
||||
* <br> 在其中可调用 {@link SectionExpansion#handle(String, PlaceholderHandler, String...)} 方法处理子变量,
|
||||
* <br> 或者调用 {@link SectionExpansion#handleSection(String, Consumer, String...)} 方法处理下一层组变量
|
||||
* @param aliases 该变量的别称
|
||||
* @return {@link EasyPlaceholder}
|
||||
*/
|
||||
public final EasyPlaceholder handleSection(@NotNull String section,
|
||||
@NotNull Consumer<SectionExpansion> consumer,
|
||||
@NotNull String... aliases) {
|
||||
this.rootExpansion.handleSection(section, consumer, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onRequest(@Nullable OfflinePlayer player, @NotNull String params) {
|
||||
String[] args = params.split("_");
|
||||
if (args.length == 0) return onErrorParams(player);
|
||||
|
||||
Object response = rootExpansion.handleRequest(player, args);
|
||||
if (response == null) return onNullResponse(player);
|
||||
else return response.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getRequiredPlugin() {
|
||||
return this.plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return this.rootExpansion.getIdentifier();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return this.author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> getPlaceholders() {
|
||||
return this.rootExpansion.listPlaceholders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return this.persistent;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cc.carm.lib.easyplugin.papi.expansion;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EasyExpansion {
|
||||
|
||||
@NotNull String getIdentifier();
|
||||
|
||||
@NotNull List<String> getAliases();
|
||||
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
package cc.carm.lib.easyplugin.papi.expansion;
|
||||
|
||||
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
|
||||
import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SectionExpansion implements EasyExpansion {
|
||||
|
||||
protected final EasyPlaceholder root;
|
||||
|
||||
protected final @NotNull String identifier;
|
||||
protected final @NotNull List<String> aliases;
|
||||
|
||||
protected final @NotNull Map<String, SectionExpansion> registeredSections = new HashMap<>();
|
||||
protected final @NotNull Map<String, SubExpansion<?>> registeredExpansions = new HashMap<>();
|
||||
|
||||
protected final @NotNull Map<String, String> aliasesMap = new HashMap<>();
|
||||
|
||||
public SectionExpansion(@NotNull EasyPlaceholder root,
|
||||
@NotNull String identifier, @NotNull String... aliases) {
|
||||
this(root, identifier, Arrays.asList(aliases));
|
||||
}
|
||||
|
||||
public SectionExpansion(@NotNull EasyPlaceholder root,
|
||||
@NotNull String identifier, @NotNull List<String> aliases) {
|
||||
this.root = root;
|
||||
this.identifier = identifier;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
public EasyPlaceholder getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
|
||||
public void register(@NotNull SubExpansion<?> placeholder) {
|
||||
String name = placeholder.getIdentifier().toLowerCase();
|
||||
this.registeredExpansions.put(name, placeholder);
|
||||
placeholder.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
public final void register(@NotNull SectionExpansion placeholder) {
|
||||
String name = placeholder.getIdentifier().toLowerCase();
|
||||
this.registeredSections.put(name, placeholder);
|
||||
placeholder.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param aliases 该变量的别称
|
||||
*/
|
||||
public final void handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull String... aliases) {
|
||||
handle(identifier, handler, Collections.emptyList(), aliases);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param paramsConsumer 用于提供该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
*/
|
||||
public final void handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull Consumer<ArrayList<String>> paramsConsumer, @NotNull String... aliases) {
|
||||
handle(identifier, handler, () -> {
|
||||
ArrayList<String> params = new ArrayList<>();
|
||||
paramsConsumer.accept(params);
|
||||
return params;
|
||||
}, aliases);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param availableParams 该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
*/
|
||||
public final void handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull List<String> availableParams, @NotNull String... aliases) {
|
||||
handle(identifier, handler, () -> availableParams, aliases);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量并返回对应内容。
|
||||
*
|
||||
* @param identifier 该变量的标识符
|
||||
* @param handler 该变量的处理器,返回值将会被转换为字符串。
|
||||
* @param availableParams 该变量的可用参数
|
||||
* @param aliases 该变量的别称
|
||||
*/
|
||||
public final void handle(@NotNull String identifier, @NotNull PlaceholderHandler handler,
|
||||
@NotNull Supplier<List<String>> availableParams, @NotNull String... aliases) {
|
||||
register(new SubExpansion<SectionExpansion>(this, identifier, aliases) {
|
||||
@Override
|
||||
public Object handle(@Nullable OfflinePlayer player, @NotNull String[] args) throws Exception {
|
||||
return handler.handle(player, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listAvailableParams() {
|
||||
return availableParams.get();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理一组变量。
|
||||
*
|
||||
* @param section 该组变量的标识符
|
||||
* @param consumer 该组变量的处理器操作方法
|
||||
* <br> 在其中可调用 {@link SectionExpansion#handle(String, PlaceholderHandler, String...)} 方法处理子变量,
|
||||
* <br> 或者调用 {@link SectionExpansion#handleSection(String, Consumer, String...)} 方法处理下一层组变量
|
||||
* @param aliases 该变量的别称
|
||||
*/
|
||||
public final void handleSection(@NotNull String section, @NotNull Consumer<SectionExpansion> consumer,
|
||||
@NotNull String... aliases) {
|
||||
SectionExpansion sectionExpansion = new SectionExpansion(getRoot(), section, aliases);
|
||||
consumer.accept(sectionExpansion);
|
||||
register(sectionExpansion);
|
||||
}
|
||||
|
||||
public final @Nullable Object handleRequest(@Nullable OfflinePlayer player, @NotNull String[] args) {
|
||||
if (args.length == 0) return getRoot().onErrorParams(player);
|
||||
String input = args[0].toLowerCase();
|
||||
|
||||
SectionExpansion group = getHandler(input);
|
||||
if (group != null) return group.handleRequest(player, this.shortenArgs(args));
|
||||
|
||||
SubExpansion<?> sub = getSubPlaceholder(input);
|
||||
if (sub == null) return getRoot().onErrorParams(player);
|
||||
|
||||
try {
|
||||
return sub.handle(player, this.shortenArgs(args));
|
||||
} catch (Exception ex) {
|
||||
return getRoot().onException(player, sub, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> listPlaceholders() {
|
||||
List<String> placeholders = new ArrayList<>();
|
||||
|
||||
String currentID = getIdentifier();
|
||||
|
||||
for (SectionExpansion section : this.registeredSections.values()) {
|
||||
for (String groupID : section.listPlaceholders()) {
|
||||
placeholders.add(currentID + "_" + groupID);
|
||||
}
|
||||
}
|
||||
|
||||
for (SubExpansion<?> placeholder : this.registeredExpansions.values()) {
|
||||
String identifier = placeholder.getIdentifier();
|
||||
|
||||
List<String> params = placeholder.listAvailableParams();
|
||||
if (params.isEmpty()) {
|
||||
placeholders.add(currentID + "_" + identifier);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (String param : params) {
|
||||
placeholders.add(currentID + "_" + identifier + "_" + param);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return placeholders;
|
||||
}
|
||||
|
||||
|
||||
protected @Nullable SectionExpansion getHandler(@NotNull String name) {
|
||||
SectionExpansion fromName = this.registeredSections.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredSections.get(nameFromAlias);
|
||||
}
|
||||
|
||||
protected @Nullable SubExpansion<?> getSubPlaceholder(@NotNull String name) {
|
||||
SubExpansion<?> fromName = this.registeredExpansions.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredExpansions.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,44 @@
|
||||
package cc.carm.lib.easyplugin.papi.expansion;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SubExpansion<PARENT extends SectionExpansion> implements EasyExpansion {
|
||||
|
||||
protected final @NotNull PARENT parent;
|
||||
protected final @NotNull String identifier;
|
||||
protected final @NotNull List<String> aliases;
|
||||
|
||||
public SubExpansion(@NotNull PARENT parent,
|
||||
@NotNull String identifier, @NotNull String... aliases) {
|
||||
this.parent = parent;
|
||||
this.identifier = identifier;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
public abstract Object handle(@Nullable OfflinePlayer player, @NotNull String[] args) throws Exception;
|
||||
|
||||
/**
|
||||
* 用于提示此子变量的全部可用参数
|
||||
*
|
||||
* @return 该子变量的全部可用参数
|
||||
*/
|
||||
public List<String> listAvailableParams() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package cc.carm.lib.easyplugin.papi.handler;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PlaceholderHandler {
|
||||
|
||||
@Nullable Object handle(@Nullable OfflinePlayer player, @NotNull String[] args) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package cc.carm.tests.easyplugin.papi;
|
||||
|
||||
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
|
||||
import cc.carm.lib.easyplugin.papi.expansion.SubExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class DemoPlaceholder extends EasyPlaceholder {
|
||||
|
||||
public DemoPlaceholder() {
|
||||
super(
|
||||
"TestPlugin", true, "TestPlaceholder",
|
||||
"TestPlugin", "1.0.0", "Carm"
|
||||
);
|
||||
|
||||
handle("version", (player, args) -> "1.0.0", "ver", "v");
|
||||
handleSection("game", (section) -> {
|
||||
|
||||
// 即处理 %TestPlaceholder_game_items%
|
||||
section.handle("items", ((player, args) -> {
|
||||
if (player == null || !player.isOnline() || !(player instanceof Player)) return -1;
|
||||
PlayerInventory inv = ((Player) player).getInventory();
|
||||
return Arrays.stream(inv.getContents())
|
||||
.filter(Objects::nonNull)
|
||||
.mapToInt(ItemStack::getAmount)
|
||||
.count();
|
||||
}));
|
||||
|
||||
// 即处理 %TestPlaceholder_game_location% 或 %TestPlaceholder_game_loc%
|
||||
section.handle("location", (player, args) -> {
|
||||
if (player == null || !player.isOnline() || !(player instanceof Player)) return "Unknown";
|
||||
return ((Player) player).getLocation();
|
||||
}, "loc" /* 声明此变量的别称*/);
|
||||
|
||||
// 即处理 %TestPlaceholder_game_time%
|
||||
section.handle("time", (player, args) -> System.currentTimeMillis());
|
||||
|
||||
section.handle("random",
|
||||
(player, args) -> {
|
||||
if (args.length == 0) return Math.random();
|
||||
else if (args.length == 1) return Math.random() * Double.parseDouble(args[0]);
|
||||
else if (args.length == 2) {
|
||||
return Math.random() * (Double.parseDouble(args[1]) - Double.parseDouble(args[0])) + Double.parseDouble(args[0]);
|
||||
} else return onErrorParams(player);
|
||||
},
|
||||
(params) -> {
|
||||
params.add("<最小值>_<最大值>");
|
||||
params.add("<基础值(乘随机数)>");
|
||||
});
|
||||
|
||||
}, "g");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onErrorParams(@Nullable OfflinePlayer player) {
|
||||
return super.onErrorParams(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onNullResponse(@Nullable OfflinePlayer player) {
|
||||
return super.onNullResponse(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onException(@Nullable OfflinePlayer player, @NotNull SubExpansion<?> expansion, @NotNull Exception exception) {
|
||||
return super.onException(player, expansion, exception);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cc.carm.tests.easyplugin.papi;
|
||||
|
||||
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PlaceholderTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
DemoPlaceholder placeholder = new DemoPlaceholder();
|
||||
|
||||
System.out.println("# Placeholders");
|
||||
placeholder.getPlaceholders().forEach(s -> System.out.println(" -> " + s));
|
||||
|
||||
System.out.println();
|
||||
System.out.println("# Request tests");
|
||||
|
||||
test(placeholder, "game_items");
|
||||
test(placeholder, "g_time");
|
||||
test(placeholder, "game_loc");
|
||||
test(placeholder, "ver");
|
||||
|
||||
test(placeholder, "game_random");
|
||||
test(placeholder, "game_random_0_5");
|
||||
test(placeholder, "game_random_100");
|
||||
|
||||
System.out.println("# Error tests");
|
||||
test(placeholder, "game_random_try");
|
||||
}
|
||||
|
||||
private void test(EasyPlaceholder placeholder, String input) {
|
||||
System.out.println(" " + input + " -> " + placeholder.onRequest(null, input));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+11
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -33,4 +33,14 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -15,17 +15,18 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.4.19</version>
|
||||
<version>1.5.5</version>
|
||||
<modules>
|
||||
<module>base/color</module>
|
||||
<module>base/utils</module>
|
||||
|
||||
<module>base/main</module>
|
||||
<module>base/conf</module>
|
||||
<module>base/messages</module>
|
||||
<module>base/gui</module>
|
||||
<module>base/command</module>
|
||||
<module>base/command-alias</module>
|
||||
<module>base/storage</module>
|
||||
<module>base/listener</module>
|
||||
<module>base/database</module>
|
||||
<module>base/user</module>
|
||||
|
||||
<module>extension/papi</module>
|
||||
<module>extension/vault</module>
|
||||
@@ -206,6 +207,37 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<gpgArguments>
|
||||
<arg>--pinentry-mode</arg>
|
||||
<arg>loopback</arg>
|
||||
</gpgArguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<version>2.5.3</version>
|
||||
<configuration>
|
||||
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||
<useReleaseProfile>false</useReleaseProfile>
|
||||
<releaseProfiles>release</releaseProfiles>
|
||||
<goals>deploy</goals>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
|
||||
Reference in New Issue
Block a user