1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-05 09:01:47 +08:00

Compare commits

..

25 Commits

Author SHA1 Message Date
carm 429eb9c6ec chore(event): 为同步/异步事件唤起方法添加Javadoc 2022-07-27 14:41:20 +08:00
carm 05e2c8fad0 chore(deps): 补充新项目到bom中。 2022-07-27 14:37:23 +08:00
carm a58fe4abd6 docs(proj): 修改措辞 2022-07-27 14:34:58 +08:00
carm 79a18f2669 docs(proj): 提供更多关于子项目内容的介绍。 2022-07-27 14:30:39 +08:00
carm e5fc8acddc feat(proj): 提供更多子项目打包,便于快速导入对应依赖。 2022-07-27 14:29:13 +08:00
carm fb125ee9bd chore(gui): 为GUIItem的onClick方法提供viewer参数。
close #6
2022-07-27 13:53:30 +08:00
carm 376cde3529 chore(cmd): 令 hasPermission() 判断的参数为 CommandSender 。 2022-07-10 10:13:06 +08:00
carm 6e555a700f chore(cmd): 移除单String参数的SubCommand构造函数 2022-07-10 05:36:30 +08:00
carm b7e9295685 chore(gui): 为 GUIConfiguration 添加 serialize() 方法。 2022-07-10 04:58:59 +08:00
carm b5382a1cb3 chore(gui): 修改 GUIItemConfiguration 的读取与初始化方式 2022-07-01 20:01:50 +08:00
carm 77c035c2cb fix(main): 移除 isInitialized() 方法 (与部分服务端冲突) 2022-07-01 18:39:44 +08:00
carm fbf118436e feat(command): 指令部分新增基础方法 2022-06-26 10:34:06 +08:00
carm 572c22d2fc chore(pom): 移除无用条目 2022-06-18 05:38:13 +08:00
carm e416363751 fix(gui): 修复 GUIItemConfiguration 序列化错误的问题 2022-06-18 05:31:41 +08:00
carm 123e5463ad feat(main): 添加完整的构造方法用于测试 2022-06-18 04:40:35 +08:00
carm 65bc24d1e9 feat(storage): 为 StorageType 添加 DataStorage 泛型限制 2022-06-18 04:09:33 +08:00
carm 778235e8d1 feat(updater): 添加Github更新检查模块,方便获取基于Github发布的插件版本更新。 2022-06-18 03:04:30 +08:00
carm 120362f221 feat(updater): 添加Github更新检查模块,方便获取基于Github发布的插件版本更新。 2022-06-18 02:32:41 +08:00
carm 4c10a9798f feat(storage): 添加存储接口模块,包含存储相关的统一接口。 2022-06-18 02:32:08 +08:00
carm 558723ea82 chore(item): 添加序列化方法 2022-06-18 00:21:03 +08:00
carm b6bd4beda0 chore(item): 添加序列化方法 2022-06-18 00:16:02 +08:00
carm 7a06b39b31 feat(command): 新增可快捷return的消息方法。 2022-06-12 23:41:45 +08:00
carm 29fe7c3f76 feat(command): 新增可快捷return的消息方法。 2022-06-12 23:39:14 +08:00
carm 01b27e25bb feat(command): 新增可快捷return的消息方法。 2022-06-12 23:28:34 +08:00
carm 7e35f49e54 feat(main): 添加同步、异步与异步回调唤起事件的方法 2022-06-07 07:40:15 +08:00
46 changed files with 2117 additions and 1276 deletions
+1 -1
View File
@@ -14,6 +14,7 @@ assignees: ''
### **问题来源** ### **问题来源**
描述一下通过哪些操作才发现的问题,如: 描述一下通过哪些操作才发现的问题,如:
1. 使用了 ... 1. 使用了 ...
2. 输入了 ... 2. 输入了 ...
3. 出现了报错 ... 3. 出现了报错 ...
@@ -32,7 +33,6 @@ assignees: ''
- Java版本: `JDK11` / `OPENJDK8` / `JRE8` / `...` - Java版本: `JDK11` / `OPENJDK8` / `JRE8` / `...`
- 服务端版本: 请在后台输入 `version` 并复制相关输出。 - 服务端版本: 请在后台输入 `version` 并复制相关输出。
### **其他补充** ### **其他补充**
如有其他补充,可以在这里描述。 如有其他补充,可以在这里描述。
+4
View File
@@ -8,13 +8,17 @@ assignees: ''
--- ---
### **功能简述** ### **功能简述**
简单的描述一下你想要的功能 简单的描述一下你想要的功能
### **需求来源** ### **需求来源**
简单的描述一下为什么需要这个功能。 简单的描述一下为什么需要这个功能。
### **功能参考**(可选) ### **功能参考**(可选)
如果有相关功能的参考,如文本、截图,请提供给我们。 如果有相关功能的参考,如文本、截图,请提供给我们。
### **附加内容** ### **附加内容**
如果有什么小细节需要重点注意,请在这里告诉我们。 如果有什么小细节需要重点注意,请在这里告诉我们。
+18 -4
View File
@@ -27,7 +27,7 @@
## 内容 ## 内容
项目初创不久,加 * 的仍在开发更新中... 项目初创不久,加 * 的仍在开发更新中...欢迎各路大佬帮助提供本项目的开发文档~
### 集合部分 (`/collection`) ### 集合部分 (`/collection`)
@@ -37,11 +37,25 @@
### 主要部分 (`/base`) ### 主要部分 (`/base`)
- Main [`easyplugin-main`](base/main) - Main [`easyplugin-main`](base/main)
- 主要接口模块,提供了方便的插件入口类与相关工具类。
- Command [`easyplugin-command`](base/command) - Command [`easyplugin-command`](base/command)
- ~~Messages*~~ (已独立项目到 [**EasyMessages**](https://github.com/CarmJos/EasyMessages)) - 指令接口模块,便于快速进行子指令的实现,并提供单独的TabComplete方法。
- ~~Configuration~~ (已独立项目到 [**MineConfiguration**](https://github.com/CarmJos/MineConfiguration)) - 随本项目提供了 `SimpleCompleter` 类,用于快速创建补全的内容列表。
- ~~Database~~ (已独立项目到 [**EasySQL**](https://github.com/CarmJos/EasySQL))
- GUI [`easyplugin-gui`](base/main) - GUI [`easyplugin-gui`](base/main)
- 简单便捷的箱子GUI接口,可以快速实现GUI中不同图标的点击功能。
- 随本项目提供了 `AutoPagedGUI` 等翻页GUI抽象类。
- Storage [`easyplugin-storage`](base/storage)
- 抽象存储管理器,便于实现不同的存储类型。
- 随本项目提供了 `FileBasedStorage``FolderBasedStorage` 等常用存储抽象方法。
> 以下项目均已独立出单独项目,仅仅是按照包名规则打包到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))
### 附属部分 (`/extension`) ### 附属部分 (`/extension`)
+37 -6
View File
@@ -5,16 +5,47 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>easyplugin-command</artifactId>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <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> </properties>
<artifactId>easyplugin-command</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-Command</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>
</project> </project>
@@ -1,4 +1,3 @@
package cc.carm.lib.easyplugin.command; package cc.carm.lib.easyplugin.command;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@@ -39,7 +38,9 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
public abstract void noArgs(CommandSender sender); public abstract void noArgs(CommandSender sender);
public abstract void unknownCommand(CommandSender sender, String[] args); public void unknownCommand(CommandSender sender, String[] args) {
noArgs(sender);
}
public abstract void noPermission(CommandSender sender); public abstract void noPermission(CommandSender sender);
@@ -1,8 +1,13 @@
package cc.carm.lib.easyplugin.command; package cc.carm.lib.easyplugin.command;
import org.bukkit.permissions.Permissible; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.function.Function;
public interface NamedExecutor { public interface NamedExecutor {
@@ -10,8 +15,21 @@ public interface NamedExecutor {
List<String> getAliases(); List<String> getAliases();
default boolean hasPermission(Permissible permissible) { default boolean hasPermission(CommandSender sender) {
return true; return true;
} }
default Void sendMessage(@NotNull CommandSender sender, @NotNull String... messages) {
return sendMessage(sender, (Function<String, String>) null, messages);
}
default Void sendMessage(@NotNull CommandSender sender,
@Nullable Function<String, String> parser,
@NotNull String... messages) {
if (messages == null || messages.length == 0) return null;
Function<String, String> finalParser = Optional.ofNullable(parser).orElse(Function.identity());
Arrays.stream(messages).map(finalParser).forEach(sender::sendMessage);
return null;
}
} }
@@ -1,15 +1,18 @@
package cc.carm.lib.easyplugin.command; package cc.carm.lib.easyplugin.command;
import com.google.common.collect.ImmutableList;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -17,14 +20,22 @@ import java.util.stream.Stream;
public class SimpleCompleter { public class SimpleCompleter {
public static @NotNull List<String> objects(@NotNull String input, List<?> objects) { public static @NotNull List<String> none() {
return ImmutableList.of();
}
public static @NotNull List<String> objects(@NotNull String input, Collection<?> objects) {
return objects(input, objects.size(), objects); return objects(input, objects.size(), objects);
} }
public static @NotNull List<String> objects(@NotNull String input, int limit, List<?> objects) { public static @NotNull List<String> objects(@NotNull String input, int limit, Collection<?> objects) {
return objects(input, limit, objects.stream()); return objects(input, limit, objects.stream());
} }
public static @NotNull List<String> objects(@NotNull String input, Stream<?> stream) {
return objects(input, 20, stream);
}
public static @NotNull List<String> objects(@NotNull String input, int limit, Stream<?> stream) { public static @NotNull List<String> objects(@NotNull String input, int limit, Stream<?> stream) {
return stream.filter(Objects::nonNull).map(Object::toString) return stream.filter(Objects::nonNull).map(Object::toString)
.filter(s -> StringUtil.startsWithIgnoreCase(s, input)) .filter(s -> StringUtil.startsWithIgnoreCase(s, input))
@@ -39,11 +50,11 @@ public class SimpleCompleter {
return text(input, limit, Arrays.asList(texts)); return text(input, limit, Arrays.asList(texts));
} }
public static @NotNull List<String> text(@NotNull String input, List<String> texts) { public static @NotNull List<String> text(@NotNull String input, Collection<String> texts) {
return text(input, texts.size(), texts); return text(input, texts.size(), texts);
} }
public static @NotNull List<String> text(@NotNull String input, int limit, List<String> texts) { public static @NotNull List<String> text(@NotNull String input, int limit, Collection<String> texts) {
return objects(input, limit, texts); return objects(input, limit, texts);
} }
@@ -87,4 +98,13 @@ public class SimpleCompleter {
return objects(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName)); return objects(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName));
} }
public static @NotNull List<String> enchantments(@NotNull String input) {
return effects(input, 10);
}
@SuppressWarnings("deprecation")
public static @NotNull List<String> enchantments(@NotNull String input, int limit) {
return objects(input, limit, Arrays.stream(Enchantment.values()).map(Enchantment::getName));
}
} }
@@ -13,16 +13,11 @@ public abstract class SubCommand implements NamedExecutor {
private final String name; private final String name;
private final List<String> aliases; private final List<String> aliases;
public SubCommand(String name) {
this(name, new String[0]);
}
public SubCommand(String name, String... aliases) { public SubCommand(String name, String... aliases) {
this.name = name; this.name = name;
this.aliases = Arrays.asList(aliases); this.aliases = Arrays.asList(aliases);
} }
@Override @Override
public String getName() { public String getName() {
return this.name; return this.name;
+81
View File
@@ -0,0 +1,81 @@
<?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.13</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>
+73
View File
@@ -0,0 +1,73 @@
<?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.13</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>
+1 -25
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -59,28 +59,4 @@
</dependency> </dependency>
</dependencies> </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>
</plugin>
</plugins>
</build>
</project> </project>
@@ -13,10 +13,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class GUI { public class GUI {
@@ -56,9 +53,9 @@ public class GUI {
*/ */
boolean cancelOnOuter = true; boolean cancelOnOuter = true;
Map<String, Object> flags; protected final Map<String, Object> flags = new LinkedHashMap<>();
GUIListener listener; protected GUIListener listener;
public GUI(GUIType type, String name) { public GUI(GUIType type, String name) {
this.type = type; this.type = type;
@@ -131,24 +128,15 @@ public class GUI {
this.cancelOnOuter = b; this.cancelOnOuter = b;
} }
public void addFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>();
this.flags.put(flag, obj);
}
public Object getFlag(String flag) { public Object getFlag(String flag) {
if (this.flags == null) return null;
else
return this.flags.get(flag); return this.flags.get(flag);
} }
public void setFlag(String flag, Object obj) { public void setFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>(); this.flags.put(flag, obj);
this.flags.replace(flag, obj);
} }
public void removeFlag(String flag) { public void removeFlag(String flag) {
if (this.flags == null) this.flags = new HashMap<>();
this.flags.remove(flag); this.flags.remove(flag);
} }
@@ -156,7 +144,9 @@ public class GUI {
} }
public void openGUI(Player player) { public void openGUI(Player player) {
if (this.type == GUIType.CANCEL) { throw new IllegalStateException("被取消或不存在的GUI"); } if (this.type == GUIType.CANCEL) {
throw new IllegalStateException("被取消或不存在的GUI");
}
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name); Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR))); IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
@@ -191,6 +181,13 @@ public class GUI {
public void onClose() { public void onClose() {
} }
public GUIType getGUIType() {
return type;
}
public String getGUIName() {
return name;
}
public static void setOpenedGUI(Player player, GUI gui) { public static void setOpenedGUI(Player player, GUI gui) {
getOpenedGUIs().put(player.getUniqueId(), gui); getOpenedGUIs().put(player.getUniqueId(), gui);
@@ -37,12 +37,24 @@ public class GUIItem {
} }
/** /**
* 玩家点击GUI后执行的代码 * 玩家点击该物品后执行的代码
* 可以使用 {@link #onClick(Player, ClickType)} 操作点击者
* *
* @param type 点击的类型 * @param type 点击的类型
*/ */
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public void onClick(ClickType type) { public void onClick(ClickType type) {
}
/**
* 玩家点击GUI后执行的代码
*
* @param clicker 点击的玩家
* @param type 点击的类型
*/
public void onClick(Player clicker, ClickType type) {
this.onClick(type); // Deprecated method support
} }
public void addClickAction(GUIClickAction action) { public void addClickAction(GUIClickAction action) {
@@ -58,7 +70,7 @@ public class GUIItem {
} }
/** /**
* 玩家点击GUI后执行的代码 * 自定义点击事件代码 (须自行触发)
* *
* @param player 点击GUI的玩家 * @param player 点击GUI的玩家
*/ */
@@ -45,7 +45,7 @@ public class GUIListener implements Listener {
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot()); GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
if (clickedItem != null) { if (clickedItem != null) {
if (clickedItem.isActionActive()) { if (clickedItem.isActionActive()) {
clickedItem.onClick(event.getClick()); clickedItem.onClick(player, event.getClick());
clickedItem.rawClickAction(event); clickedItem.rawClickAction(event);
clickedItem.actions.forEach(action -> action.run(event.getClick(), player)); clickedItem.actions.forEach(action -> action.run(event.getClick(), player));
} }
@@ -14,8 +14,8 @@ public enum GUIType {
SIX_BY_NINE(6, 54), SIX_BY_NINE(6, 54),
CANCEL(0, 0); CANCEL(0, 0);
int lines; private final int lines;
int size; private final int size;
GUIType(int lines, int size) { GUIType(int lines, int size) {
this.lines = lines; this.lines = lines;
@@ -3,18 +3,39 @@ package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUIItem; import cc.carm.lib.easyplugin.gui.GUIItem;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class GUIActionConfiguration { public class GUIActionConfiguration {
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
@Nullable ClickType clickType,
@Nullable String actionContent) {
return new GUIActionConfiguration(actionType, clickType, actionContent);
}
@Nullable ClickType clickType; public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
final @NotNull GUIActionType actionType; @Nullable String actionContent) {
final @Nullable String actionContent; return of(actionType, null, actionContent);
}
public GUIActionConfiguration(@Nullable ClickType clickType, public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
@NotNull GUIActionType actionType, @Nullable ClickType clickType) {
return of(actionType, clickType, null);
}
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType) {
return of(actionType, null, null);
}
protected final @NotNull GUIActionType actionType;
protected final @Nullable ClickType clickType;
protected final @Nullable String actionContent;
public GUIActionConfiguration(@NotNull GUIActionType actionType,
@Nullable ClickType clickType,
@Nullable String actionContent) { @Nullable String actionContent) {
this.clickType = clickType; this.clickType = clickType;
this.actionType = actionType; this.actionType = actionType;
@@ -50,4 +71,36 @@ public class GUIActionConfiguration {
}; };
} }
@Nullable
@Contract("null->null")
public static GUIActionConfiguration deserialize(@Nullable String actionString) {
if (actionString == null) return null;
int prefixStart = actionString.indexOf("[");
int prefixEnd = actionString.indexOf("]");
if (prefixStart < 0 || prefixEnd < 0) return null;
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) return null;
String content = actionString.substring(prefixEnd + 1).trim();
return of(actionType, clickType, content);
}
public @NotNull String serialize() {
String prefix = "[" + getActionType().name() + (getClickType() == null ? "" : ":" + getClickType().name()) + "]";
String content = getActionContent() == null ? "" : " " + getActionContent();
return prefix + content;
}
} }
@@ -6,19 +6,26 @@ import cc.carm.lib.easyplugin.utils.ColorParser;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
public class GUIConfiguration { public class GUIConfiguration {
String title; protected String title;
int lines; protected int lines;
List<GUIItemConfiguration> guiItems; protected Map<String, GUIItemConfiguration> guiItems;
public GUIConfiguration(String title, int lines, List<GUIItemConfiguration> guiItems) { public GUIConfiguration(String title, int lines) {
this(title, lines, new LinkedHashMap<>(1));
}
public GUIConfiguration(String title, int lines, Map<String, GUIItemConfiguration> guiItems) {
this.title = title; this.title = title;
this.lines = lines; this.lines = lines;
this.guiItems = guiItems; this.guiItems = guiItems;
@@ -38,31 +45,50 @@ public class GUIConfiguration {
.get(); .get();
} }
public List<GUIItemConfiguration> getGuiItems() { public Map<String, GUIItemConfiguration> getGUIItems() {
return guiItems; return guiItems;
} }
public void setupItems(Player player, GUI gui) { public void setupItems(Player player, GUI gui) {
getGuiItems().forEach(itemConfiguration -> itemConfiguration.setupItems(player, gui)); getGUIItems().values().forEach(itemConfiguration -> itemConfiguration.setupItems(player, gui));
}
public @NotNull Map<String, Object> serialize() {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("title", this.title);
map.put("lines", this.lines);
if (!this.guiItems.isEmpty()) {
LinkedHashMap<String, Object> items = new LinkedHashMap<>();
this.guiItems.forEach((key, value) -> items.put(key, value.serialize()));
map.put("items", items);
}
return map;
} }
public static GUIConfiguration readConfiguration(@Nullable ConfigurationSection section) { public static GUIConfiguration readConfiguration(@Nullable ConfigurationSection section) {
if (section == null) return new GUIConfiguration("name", 6, new ArrayList<>()); if (section == null) return new GUIConfiguration("name", 6);
String title = section.getString("title", "");
int lines = section.getInt("lines", 6);
ConfigurationSection itemsSection = section.getConfigurationSection("items");
if (itemsSection == null) return new GUIConfiguration(title, lines, new ArrayList<>());
return new GUIConfiguration( return new GUIConfiguration(
title, lines, itemsSection.getKeys(false).stream() section.getString("title", ""),
.map(key -> GUIItemConfiguration.readFrom(itemsSection.getConfigurationSection(key))) section.getInt("lines", 6),
.filter(Objects::nonNull) readItems(section.getConfigurationSection("items"))
.collect(Collectors.toList())
); );
} }
public static Map<String, GUIItemConfiguration> readItems(ConfigurationSection itemsSection) {
Map<String, GUIItemConfiguration> items = new LinkedHashMap<>();
if (itemsSection == null) return items;
for (String key : itemsSection.getKeys(false)) {
GUIItemConfiguration item = GUIItemConfiguration.readFrom(itemsSection.getConfigurationSection(key));
if (item != null) items.put(key, item);
}
return items;
}
public static ClickType readClickType(String type) { public static ClickType readClickType(String type) {
return Arrays.stream(ClickType.values()) return Arrays.stream(ClickType.values())
.filter(click -> click.name().equalsIgnoreCase(type)) .filter(click -> click.name().equalsIgnoreCase(type))
@@ -7,30 +7,29 @@ import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.*;
import java.util.Collections; import java.util.stream.Collectors;
import java.util.List;
import java.util.Optional;
public class GUIItemConfiguration { public class GUIItemConfiguration {
Material material; @NotNull Material type;
int amount;
int data; int data;
String name; @Nullable String name;
@NotNull List<String> lore; @NotNull List<String> lore;
@NotNull List<Integer> slots; @NotNull List<Integer> slots;
@NotNull List<GUIActionConfiguration> actions; @NotNull List<GUIActionConfiguration> actions;
public GUIItemConfiguration(Material material, int data, public GUIItemConfiguration(@NotNull Material type, int amount, int data,
String name, @NotNull List<String> lore, @Nullable String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions, @NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) { @NotNull List<Integer> slots) {
this.material = material; this.type = type;
this.amount = amount;
this.data = data; this.data = data;
this.name = name; this.name = name;
this.lore = lore; this.lore = lore;
@@ -39,8 +38,7 @@ public class GUIItemConfiguration {
} }
public void setupItems(Player player, GUI gui) { public void setupItems(Player player, GUI gui) {
ItemStackFactory icon = new ItemStackFactory(this.material); ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data);
icon.setDurability(this.data);
if (this.name != null) icon.setDisplayName(this.name); if (this.name != null) icon.setDisplayName(this.name);
icon.setLore(MessageUtils.setPlaceholders(player, this.lore)); icon.setLore(MessageUtils.setPlaceholders(player, this.lore));
@@ -49,11 +47,32 @@ public class GUIItemConfiguration {
this.slots.forEach(slot -> gui.setItem(slot, item)); this.slots.forEach(slot -> gui.setItem(slot, item));
} }
public @NotNull Map<String, Object> serialize() {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("type", this.type.name());
if (this.name != null) map.put("name", this.name);
if (this.amount != 1) map.put("amount", this.amount);
if (this.data != 0) map.put("data", this.data);
if (!this.lore.isEmpty()) map.put("lore", this.lore);
if (this.slots.size() > 1) {
map.put("slots", this.slots);
} else if (slots.size() == 1) {
map.put("slot", this.slots.get(0));
}
if (!this.actions.isEmpty()) {
map.put("actions", this.actions.stream().map(GUIActionConfiguration::serialize).collect(Collectors.toList()));
}
return map;
}
@Nullable @Nullable
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) { public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
if (itemSection == null) return null; if (itemSection == null) return null;
Material material = Optional.ofNullable(Material.matchMaterial(itemSection.getString("material", "STONE"))).orElse(Material.STONE); String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE");
Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE);
int data = itemSection.getInt("data", 0); int data = itemSection.getInt("data", 0);
int amount = itemSection.getInt("amount", 1);
String name = itemSection.getString("name"); String name = itemSection.getString("name");
List<String> lore = itemSection.getStringList("lore"); List<String> lore = itemSection.getStringList("lore");
@@ -63,30 +82,15 @@ public class GUIItemConfiguration {
List<String> actionsString = itemSection.getStringList("actions"); List<String> actionsString = itemSection.getStringList("actions");
List<GUIActionConfiguration> actions = new ArrayList<>(); List<GUIActionConfiguration> actions = new ArrayList<>();
for (String actionString : actionsString) { for (String actionString : actionsString) {
int prefixStart = actionString.indexOf("["); GUIActionConfiguration action = GUIActionConfiguration.deserialize(actionString);
int prefixEnd = actionString.indexOf("]"); if (action == null) continue;
if (prefixStart < 0 || prefixEnd < 0) continue; actions.add(action);
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) continue;
actions.add(new GUIActionConfiguration(clickType, actionType, actionString.substring(prefixEnd + 1).trim()));
} }
return new GUIItemConfiguration( return new GUIItemConfiguration(
material, data, name, lore, actions, type, amount, data, name, lore, actions,
slots.size() > 0 ? slots : Collections.singletonList(slot) slots.size() > 0 ? slots : Collections.singletonList(slot)
); );
} }
} }
@@ -49,7 +49,7 @@ public class AutoPagedGUI extends CommonPagedGUI {
setItem(previousPageSlot, new GUIItem( setItem(previousPageSlot, new GUIItem(
previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) { previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) {
@Override @Override
public void onClick(ClickType type) { public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) { if (type == ClickType.RIGHT) {
goFirstPage(); goFirstPage();
} else { } else {
@@ -68,7 +68,7 @@ public class AutoPagedGUI extends CommonPagedGUI {
setItem(nextPageSlot, new GUIItem( setItem(nextPageSlot, new GUIItem(
nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) { nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) {
@Override @Override
public void onClick(ClickType type) { public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) { if (type == ClickType.RIGHT) {
goLastPage(); goLastPage();
} else { } else {
@@ -1,4 +1,3 @@
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType; import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration; import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
+80
View File
@@ -0,0 +1,80 @@
<?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.13</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>
+1 -23
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -56,31 +56,9 @@
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId> <artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </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>
</plugins>
</build>
</project> </project>
@@ -7,31 +7,46 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.event.Event;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public abstract class EasyPlugin extends JavaPlugin { public abstract class EasyPlugin extends JavaPlugin {
protected EasyPluginMessageProvider messageProvider; protected @NotNull EasyPluginMessageProvider messageProvider;
public EasyPlugin() { public EasyPlugin() {
this(EasyPluginMessageProvider.ZH_CN); this(EasyPluginMessageProvider.ZH_CN);
} }
public EasyPlugin(EasyPluginMessageProvider messageProvider) { public EasyPlugin(@NotNull EasyPluginMessageProvider messageProvider) {
this.messageProvider = messageProvider; this.messageProvider = messageProvider;
} }
private SchedulerUtils scheduler; protected EasyPlugin(JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, File file) {
private boolean initialized = false; this(EasyPluginMessageProvider.ZH_CN, loader, descriptionFile, dataFolder, file);
}
protected EasyPlugin(@NotNull EasyPluginMessageProvider messageProvider,
JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, File file) {
super(loader, descriptionFile, dataFolder, file);
this.messageProvider = messageProvider;
}
protected SchedulerUtils scheduler;
protected boolean initialized = false;
@Override @Override
public final void onLoad() { public final void onLoad() {
@@ -64,7 +79,7 @@ public abstract class EasyPlugin extends JavaPlugin {
@Override @Override
public final void onDisable() { public final void onDisable() {
if (!hasOverride("shutdown") || !isInitialized()) return; if (!hasOverride("shutdown") || !this.initialized) return;
outputInfo(); outputInfo();
log(messageProvider.disabling(this)); log(messageProvider.disabling(this));
@@ -88,10 +103,6 @@ public abstract class EasyPlugin extends JavaPlugin {
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log); Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
} }
public boolean isInitialized() {
return initialized;
}
public boolean isDebugging() { public boolean isDebugging() {
return false; return false;
} }
@@ -134,6 +145,42 @@ public abstract class EasyPlugin extends JavaPlugin {
if (isDebugging()) print("&8[DEBUG] &r", messages); if (isDebugging()) print("&8[DEBUG] &r", messages);
} }
/**
* 在主线程唤起一个事件,并支持获取事件的结果。
*
* @param event 同步事件 (isAsync=false)
* @param <T> 事件类型
* @return CompletableFuture
*/
public @NotNull <T extends Event> CompletableFuture<T> callSync(T event) {
CompletableFuture<T> future = new CompletableFuture<>();
getScheduler().run(() -> {
Bukkit.getPluginManager().callEvent(event);
future.complete(event);
});
return future;
}
/**
* 在异步线程中唤起一个事件,并支持获取事件的结果。
*
* @param event 异步事件 (isAsync=true)
* @param <T> 事件类型
* @return CompletableFuture
*/
public @NotNull <T extends Event> CompletableFuture<T> callAsync(T event) {
CompletableFuture<T> future = new CompletableFuture<>();
getScheduler().runAsync(() -> {
Bukkit.getPluginManager().callEvent(event);
future.complete(event);
});
return future;
}
protected void setMessageProvider(@NotNull EasyPluginMessageProvider provider) {
this.messageProvider = provider;
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted") @SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean hasOverride(String methodName) { private boolean hasOverride(String methodName) {
Map<Method, Method> methodMap = new HashMap<>(); Map<Method, Method> methodMap = new HashMap<>();
@@ -8,7 +8,7 @@ import java.util.stream.Collectors;
public class ColorParser { public class ColorParser {
public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([0-9a-fA-F]{6})\\)"); public static final Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([\\da-fA-F]{6})\\)");
public static String parse(String text) { public static String parse(String text) {
return parseBaseColor(parseHexColor(text)); return parseBaseColor(parseHexColor(text));
+27
View File
@@ -0,0 +1,27 @@
<?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.13</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-storage</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-Storage</name>
<description>轻松插件存储接口模块,包含存储相关的统一接口。</description>
<url>https://github.com/CarmJos/EasyPlugin</url>
</project>
@@ -0,0 +1,40 @@
package cc.carm.lib.easyplugin.storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface DataStorage<K, T> {
/**
* 在插件加载存储源时执行。
*
* @throws Exception 当出现任何错误时抛出
*/
void initialize() throws Exception;
/**
* 在插件被卸载时执行。
*/
void shutdown();
/**
* 用于加载数据的方法。<b>该方法应当异步运行!</b>
* <br>
* <br>若不存在对应数据,请返回 null 。
* <br>若加载出现任何错误,请抛出异常。
*
* @param key 数据主键
* @throws Exception 当出现任何错误时抛出
*/
@Nullable
T loadData(@NotNull K key) throws Exception;
/**
* 用于保存数据的方法。 <b>该方法应当被异步运行!</b>
*
* @param data 数据
* @throws Exception 当出现任何错误时抛出
*/
void saveData(@NotNull T data) throws Exception;
}
@@ -0,0 +1,19 @@
package cc.carm.lib.easyplugin.storage;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public interface StorageType<K, T, S extends DataStorage<K, T>> {
int getID();
@NotNull List<String> getAlias();
@NotNull Class<? extends S> getStorageClass();
default @NotNull S createStorage() throws Exception {
return getStorageClass().newInstance();
}
}
@@ -0,0 +1,31 @@
package cc.carm.lib.easyplugin.storage.file;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public abstract class FileBasedStorage<K, T> implements DataStorage<K, T> {
protected final String fileName;
protected File dataFile;
public FileBasedStorage(String fileName) {
this.fileName = fileName;
}
protected @NotNull File initializeFile(@NotNull File parentFolder) throws Exception {
this.dataFile = new File(parentFolder, fileName);
if (!dataFile.exists()) {
if (!dataFile.createNewFile()) throw new Exception("无法创建数据文件!");
} else if (dataFile.isDirectory()) {
throw new Exception("文件路径对应的不是一个文件!");
}
return dataFile;
}
public File getDataFile() {
return dataFile;
}
}
@@ -0,0 +1,46 @@
package cc.carm.lib.easyplugin.storage.file;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public abstract class FolderBasedStorage<K, T> implements DataStorage<K, T> {
protected final @NotNull String folderPath;
protected File dataFolder;
public FolderBasedStorage(@NotNull String folderPath) {
this.folderPath = folderPath;
}
protected @NotNull File initializeFolder(@NotNull File parentFolder) throws Exception {
this.dataFolder = new File(parentFolder, folderPath);
if (!dataFolder.exists()) {
if (!dataFolder.mkdir()) {
throw new Exception("无法创建数据文件夹!");
}
} else if (!dataFolder.isDirectory()) {
throw new Exception("数据文件夹路径对应的不是一个文件夹!");
}
return dataFolder;
}
protected @NotNull List<File> listFiles() {
if (this.dataFolder == null) return Collections.emptyList();
if (!this.dataFolder.isDirectory()) return Collections.emptyList();
File[] files = this.dataFolder.listFiles();
if (files == null) return Collections.emptyList();
return Arrays.asList(files);
}
public File getDataFolder() {
return dataFolder;
}
}
@@ -0,0 +1,24 @@
package cc.carm.lib.easyplugin.storage.file;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public abstract class YAMLBasedStorage<K, T> extends FileBasedStorage<K, T> {
protected FileConfiguration configuration;
public YAMLBasedStorage(String fileName) {
super(fileName);
}
protected @NotNull FileConfiguration initializeConfiguration(@NotNull File parentFolder) throws Exception {
return this.configuration = YamlConfiguration.loadConfiguration(initializeFile(parentFolder));
}
public FileConfiguration getConfiguration() {
return configuration;
}
}
@@ -0,0 +1,31 @@
package cc.carm.lib.easyplugin.storage.plugin;
import cc.carm.lib.easyplugin.storage.DataStorage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
public abstract class PluginBasedStorage<K, T> implements DataStorage<K, T> {
protected Plugin dependPlugin;
public PluginBasedStorage(String dependPluginName) {
this(Bukkit.getPluginManager().getPlugin(dependPluginName));
}
public PluginBasedStorage(Plugin dependPlugin) {
this.dependPlugin = dependPlugin;
}
@Override
public void initialize() throws NullPointerException {
if (dependPlugin == null) {
throw new NullPointerException("该存储类型依赖的插件不存在,请检查配置文件");
}
}
public Plugin getDependPlugin() {
return dependPlugin;
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+30 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -52,6 +52,7 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.parent.groupId}</groupId> <groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-main</artifactId> <artifactId>easyplugin-main</artifactId>
@@ -67,6 +68,27 @@
<artifactId>easyplugin-gui</artifactId> <artifactId>easyplugin-gui</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
</dependency> </dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-storage</artifactId>
<version>${project.parent.version}</version>
</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> <dependency>
<groupId>${project.parent.groupId}</groupId> <groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-papi</artifactId> <artifactId>easyplugin-papi</artifactId>
@@ -77,6 +99,13 @@
<artifactId>easyplugin-vault</artifactId> <artifactId>easyplugin-vault</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
</dependency> </dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-githubchecker</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
+9 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -68,6 +68,14 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-vault</artifactId> <artifactId>easyplugin-vault</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-database</artifactId>
</exclusion>
<exclusion>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-storage</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
+34
View File
@@ -0,0 +1,34 @@
<?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.13</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-githubchecker</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-GithubChecker</name>
<dependencies>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>githubreleases4j</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,84 @@
package cc.carm.lib.easyplugin.updatechecker;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GHUpdateChecker {
protected static final @NotNull Pattern GH_URL_PATTERN = Pattern.compile("^https?://github.com/([A-Za-z\\d-_]+)/([^/]*?)/?");
public static @NotNull GHUpdateChecker of(@NotNull Logger logger, @NotNull String owner, @NotNull String repo) {
return new GHUpdateChecker(logger, owner, repo);
}
public static @NotNull GHUpdateChecker of(@NotNull Plugin plugin) {
return new GHUpdateChecker(plugin.getLogger(), getGithubOwner(plugin), plugin.getName());
}
public static @NotNull Runnable runner(@NotNull Logger logger,
@NotNull String owner, @NotNull String repo,
@NotNull String currentVersion) {
return of(logger, owner, repo).runner(currentVersion);
}
public static @NotNull Runnable runner(@NotNull Plugin plugin) {
return of(plugin).runner(plugin.getDescription().getVersion());
}
protected final @NotNull Logger logger;
protected final @NotNull String owner;
protected final @NotNull String repo;
public GHUpdateChecker(@NotNull Logger logger, @NotNull String owner, @NotNull String repo) {
this.logger = logger;
this.owner = owner;
this.repo = repo;
}
public void checkUpdate(@NotNull String currentVersion) {
Integer behindVersions = GithubReleases4J.getVersionBehind(owner, repo, currentVersion);
String downloadURL = GithubReleases4J.getReleasesURL(owner, repo);
if (behindVersions == null) {
this.logger.severe("检查更新失败,请您定期查看插件是否更新,避免安全问题。");
this.logger.severe("下载地址 " + downloadURL);
} else if (behindVersions == 0) {
this.logger.info("检查完成,当前已是最新版本。");
} else if (behindVersions > 0) {
this.logger.info("发现新版本! 目前已落后 " + behindVersions + " 个版本。");
this.logger.info("最新版下载地址 " + downloadURL);
} else {
this.logger.severe("检查更新失败! 当前版本未知,请您使用原生版本以避免安全问题。");
this.logger.severe("最新版下载地址 " + downloadURL);
}
}
public Runnable runner(@NotNull String currentVersion) {
return () -> checkUpdate(currentVersion);
}
protected static String getGithubOwner(Plugin plugin) {
// 首先,尝试从插件提供的网址中获取OWNER
String websiteOwner = getGithubOwner(plugin.getDescription().getWebsite());
if (websiteOwner != null && !websiteOwner.isEmpty()) return websiteOwner;
// 如果插件提供的网址中没有,则尝试获取插件的首位作者名
List<String> authors = plugin.getDescription().getAuthors();
if (!authors.isEmpty()) return authors.get(0);
// 再没有的话只能返回插件的名称了
return plugin.getName();
}
protected static String getGithubOwner(String url) {
Matcher matcher = GH_URL_PATTERN.matcher(url);
return matcher.find() ? matcher.group(1) : null;
}
}
@@ -0,0 +1,16 @@
package cc.carm.lib.easyplugin.updatechecker;
import org.junit.Test;
public class RegexTest {
@Test
public void onTest() {
System.out.println(GHUpdateChecker.getGithubOwner("https://github.com/CarmJos/EasyPlugin"));
System.out.println(GHUpdateChecker.getGithubOwner("https://github.com/Ghost-chu/QuickShop-Hikari/pulls"));
System.out.println(GHUpdateChecker.getGithubOwner("https://github.com/QWQ123/QuickShop"));
}
}
+1 -2
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -27,7 +27,6 @@
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId> <artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
+2 -2
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.4.5</version> <version>1.4.13</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -24,10 +24,10 @@
<dependencies> <dependencies>
<!--suppress VulnerableLibrariesLocal -->
<dependency> <dependency>
<groupId>com.github.MilkBowl</groupId> <groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId> <artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
+75 -31
View File
@@ -15,15 +15,20 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>1.4.5</version> <version>1.4.13</version>
<modules> <modules>
<module>base/main</module> <module>base/main</module>
<module>base/command</module> <module>base/conf</module>
<module>base/gui</module> <module>base/gui</module>
<module>base/command</module>
<module>base/storage</module>
<module>base/listener</module>
<module>base/database</module>
<module>extension/papi</module> <module>extension/papi</module>
<module>extension/vault</module> <module>extension/vault</module>
<module>extension/gh-checker</module>
<module>collection/all</module> <module>collection/all</module>
<module>collection/bom</module> <module>collection/bom</module>
@@ -122,6 +127,41 @@
</dependencies> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>githubreleases4j</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easylistener</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-bukkit</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-beecp</artifactId>
<version>0.4.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@@ -132,6 +172,39 @@
<useSystemClassLoader>false</useSystemClassLoader> <useSystemClassLoader>false</useSystemClassLoader>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${project.jdk.version}</source>
<target>${project.jdk.version}</target>
<encoding>UTF-8</encoding>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins> </plugins>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
@@ -156,35 +229,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>