mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-05 00:58:17 +08:00
Compare commits
101 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d7303719dd | |||
| 76b15f0cda | |||
| f97fd97a3c | |||
| 0c0249cab6 | |||
| 25eae114ad | |||
| edab1a822d | |||
| a9d26e179c | |||
| 3019216801 | |||
| 01250a65d1 | |||
| 088cc62855 | |||
| 437d0ffb32 | |||
| d9b0689e63 | |||
| 62e7370622 | |||
| 3d52d5db15 | |||
| 9ad80b4916 | |||
| 9cff646226 | |||
| 479f4592d1 | |||
| d2b3224b61 | |||
| 7c72d910d9 | |||
| c822678043 | |||
| 98d9854d6f | |||
| 47b811dc33 | |||
| 0b6e1ad3e4 | |||
| 6863c02611 | |||
| 7fc0663e89 | |||
| b0c8091cb7 | |||
| 8f03c2a1b3 | |||
| 429eb9c6ec | |||
| 05e2c8fad0 | |||
| a58fe4abd6 | |||
| 79a18f2669 | |||
| e5fc8acddc | |||
| fb125ee9bd | |||
| 376cde3529 | |||
| 6e555a700f | |||
| b7e9295685 | |||
| b5382a1cb3 | |||
| 77c035c2cb | |||
| fbf118436e | |||
| 572c22d2fc | |||
| e416363751 | |||
| 123e5463ad | |||
| 65bc24d1e9 | |||
| 778235e8d1 | |||
| 120362f221 | |||
| 4c10a9798f | |||
| 558723ea82 | |||
| b6bd4beda0 | |||
| 7a06b39b31 | |||
| 29fe7c3f76 | |||
| 01b27e25bb | |||
| 7e35f49e54 | |||
| 4dbcdfb02c | |||
| 9448c21810 | |||
| e30f7ce795 | |||
| 1a9024ca40 | |||
| 0005ce13b4 | |||
| c7190274d7 | |||
| be798a981a | |||
| bbdfe313d5 | |||
| 2acc81185a | |||
| 5886f162af | |||
| fbc153c9e9 | |||
| 3c7508c348 | |||
| 5fa255d7c7 | |||
| ce1122712d | |||
| 45426e947b | |||
| 7e3a2ea13e | |||
| 226121c1df | |||
| 94c9cc382d | |||
| 892371f702 | |||
| 12ff00e1d9 | |||
| 10f5961ccd | |||
| d8cbf2825d | |||
| f4292e761a | |||
| 47018fbf7d | |||
| dd277e99e8 | |||
| b3078553e7 | |||
| e67e23a24c | |||
| 30e57e3945 | |||
| acea995996 | |||
| 511d8d77c4 | |||
| 96ff3398c8 | |||
| 42860c332f | |||
| 5ea196b6df | |||
| fe34bbc17d | |||
| c575805c72 | |||
| 9c73fa81bc | |||
| 5a61c2db14 | |||
| d15204a38e | |||
| dcbf1678ba | |||
| 116f3419f2 | |||
| 29321f2b25 | |||
| ef453d9481 | |||
| d0d36023c8 | |||
| 4fa3ffaa93 | |||
| 6ba1be457f | |||
| f170d654d6 | |||
| ff4da6b73e | |||
| d39951bf65 | |||
| 90a276c1c1 |
@@ -4,6 +4,6 @@
|
|||||||
|
|
||||||
## 如何实现?
|
## 如何实现?
|
||||||
|
|
||||||
若您也想通过 [Github Actions](https://docs.github.com/en/actions/learn-github-actions)
|
若您也想通过 [Github Actions](https://docs.github.com/en/actions/learn-github-actions)
|
||||||
自动部署项目的Javadoc到 [Github Pages](https://pages.github.com/) ,
|
自动部署项目的Javadoc到 [Github Pages](https://pages.github.com/) ,
|
||||||
可以参考我的文章 [《自动部署Javadoc到Github Pages》](https://pages.carm.cc/doc/javadoc-in-github.html) 。
|
可以参考我的文章 [《自动部署Javadoc到Github Pages》](https://pages.carm.cc/doc/javadoc-in-github.html) 。
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>EasyPlugin Javadocs</title>
|
|
||||||
<style>
|
|
||||||
.container {
|
|
||||||
width: 60%;
|
|
||||||
margin: 10% auto 0;
|
|
||||||
background-color: #f0f0f0;
|
|
||||||
padding: 2% 5%;
|
|
||||||
border-radius: 10px
|
|
||||||
}
|
|
||||||
|
|
||||||
ul {
|
|
||||||
padding-left: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
ul li {
|
|
||||||
line-height: 2.3
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
color: #20a53a
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<h1>EasyPlugin Javadocs</h1>
|
|
||||||
<ul>
|
|
||||||
<li><a href="main/index.html">EasyPlugin-Main</a></li>
|
|
||||||
<li><a href="command/index.html">EasyPlugin-Command</a></li>
|
|
||||||
<li><a href="configuration/index.html">EasyPlugin-Configuration</a></li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# EasyPlugin Repository
|
||||||
|
|
||||||
|
采用github的repo分支进行依赖,随项目发布而自动更新。
|
||||||
|
|
||||||
|
其他依赖方式见主页介绍。
|
||||||
|
|
||||||
|
## 依赖方式
|
||||||
|
|
||||||
|
### Maven
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>EasyPlugin</id>
|
||||||
|
<name>GitHub Branch Repository</name>
|
||||||
|
<url>https://github.com/CarmJos/EasyPlugin/blob/repo/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Gradle
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
repositories {
|
||||||
|
maven { url 'https://github.com/CarmJos/EasyPlugin/blob/repo/' }
|
||||||
|
}
|
||||||
|
```
|
||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
github: [ CarmJos ]
|
#github: [ CarmJos ]
|
||||||
custom: [ 'https://donate.carm.cc' ]
|
custom: [ 'https://donate.carm.cc' ]
|
||||||
@@ -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` 并复制相关输出。
|
||||||
|
|
||||||
|
|
||||||
### **其他补充**
|
### **其他补充**
|
||||||
|
|
||||||
如有其他补充,可以在这里描述。
|
如有其他补充,可以在这里描述。
|
||||||
|
|||||||
@@ -8,13 +8,17 @@ assignees: ''
|
|||||||
---
|
---
|
||||||
|
|
||||||
### **功能简述**
|
### **功能简述**
|
||||||
|
|
||||||
简单的描述一下你想要的功能
|
简单的描述一下你想要的功能
|
||||||
|
|
||||||
### **需求来源**
|
### **需求来源**
|
||||||
|
|
||||||
简单的描述一下为什么需要这个功能。
|
简单的描述一下为什么需要这个功能。
|
||||||
|
|
||||||
### **功能参考**(可选)
|
### **功能参考**(可选)
|
||||||
|
|
||||||
如果有相关功能的参考,如文本、截图,请提供给我们。
|
如果有相关功能的参考,如文本、截图,请提供给我们。
|
||||||
|
|
||||||
### **附加内容**
|
### **附加内容**
|
||||||
|
|
||||||
如果有什么小细节需要重点注意,请在这里告诉我们。
|
如果有什么小细节需要重点注意,请在这里告诉我们。
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
name: Deploy
|
name: Deploy & Publish
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# 支持手动触发构建
|
# 支持手动触发构建
|
||||||
@@ -11,10 +11,106 @@ on:
|
|||||||
types: [ published ]
|
types: [ published ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
packages-deploy:
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: "Set up JDK"
|
||||||
|
uses: actions/setup-java@v2
|
||||||
|
with:
|
||||||
|
java-version: '8'
|
||||||
|
distribution: 'adopt'
|
||||||
|
cache: maven
|
||||||
|
server-id: github
|
||||||
|
server-username: MAVEN_USERNAME
|
||||||
|
server-password: MAVEN_TOKEN
|
||||||
|
|
||||||
|
- name: "Maven Deploy"
|
||||||
|
run: mvn -B -Pgithub deploy --file pom.xml -DskipTests -Dgpg.skip
|
||||||
|
env:
|
||||||
|
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||||
|
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
|
||||||
|
|
||||||
|
github-deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: "Set up JDK"
|
||||||
|
uses: actions/setup-java@v2
|
||||||
|
with:
|
||||||
|
java-version: '8'
|
||||||
|
distribution: 'adopt'
|
||||||
|
cache: maven
|
||||||
|
- name: "Maven Deploy"
|
||||||
|
run: mvn -B -Plocal deploy --file pom.xml -DskipTests -Dgpg.skip
|
||||||
|
|
||||||
|
- name: "Copy artifacts"
|
||||||
|
run: |
|
||||||
|
rm -rf deploy
|
||||||
|
mkdir -vp deploy
|
||||||
|
cp -vrf $HOME/local-deploy/* deploy/
|
||||||
|
cp -vrf .documentation/repository/REPO-README.md deploy/README.md
|
||||||
|
|
||||||
|
- name: "Generate Javadoc"
|
||||||
|
run: mvn -B javadoc:aggregate --file pom.xml -DskipTests
|
||||||
|
|
||||||
|
- name: "Copy Javadoc"
|
||||||
|
run: |
|
||||||
|
rm -rf docs
|
||||||
|
mkdir -vp docs
|
||||||
|
cp -vrf target/site/apidocs/* docs/
|
||||||
|
cp -vrf .documentation/javadoc/JAVADOC-README.md docs/README.md
|
||||||
|
|
||||||
|
- name: "Generate Javadoc sitemap"
|
||||||
|
id: sitemap
|
||||||
|
uses: cicirello/generate-sitemap@v1
|
||||||
|
with:
|
||||||
|
base-url-path: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}
|
||||||
|
path-to-root: docs
|
||||||
|
|
||||||
|
- name: "Output Javadoc stats"
|
||||||
|
run: |
|
||||||
|
echo "sitemap-path = ${{ steps.sitemap.outputs.sitemap-path }}"
|
||||||
|
echo "url-count = ${{ steps.sitemap.outputs.url-count }}"
|
||||||
|
echo "excluded-count = ${{ steps.sitemap.outputs.excluded-count }}"
|
||||||
|
ls -l docs
|
||||||
|
|
||||||
|
- name: "Configure Git"
|
||||||
|
env:
|
||||||
|
DEPLOY_PRI: ${{secrets.DEPLOY_PRI}}
|
||||||
|
run: |
|
||||||
|
sudo timedatectl set-timezone "Asia/Shanghai"
|
||||||
|
mkdir -p ~/.ssh/
|
||||||
|
echo "$DEPLOY_PRI" > ~/.ssh/id_rsa
|
||||||
|
chmod 600 ~/.ssh/id_rsa
|
||||||
|
ssh-keyscan github.com >> ~/.ssh/known_hosts
|
||||||
|
git config --global user.name '${{ github.repository_owner }}'
|
||||||
|
git config --global user.email '${{ github.repository_owner }}@users.noreply.github.com'
|
||||||
|
|
||||||
|
- name: "Commit&Push repository files"
|
||||||
|
run: |
|
||||||
|
cd deploy
|
||||||
|
git init
|
||||||
|
git remote add origin git@github.com:${{ github.repository_owner }}/${{ github.event.repository.name }}.git
|
||||||
|
git checkout -b repo
|
||||||
|
git add -A
|
||||||
|
git commit -m "Maven project deployment."
|
||||||
|
git push origin HEAD:repo --force
|
||||||
|
|
||||||
|
- name: "Commit&Push API documentation"
|
||||||
|
run: |
|
||||||
|
cd docs
|
||||||
|
git init
|
||||||
|
git remote add origin git@github.com:${{ github.repository_owner }}/${{ github.event.repository.name }}.git
|
||||||
|
git checkout -b gh-pages
|
||||||
|
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:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: "Set up JDK"
|
- name: "Set up JDK"
|
||||||
@@ -23,11 +119,15 @@ jobs:
|
|||||||
java-version: '11'
|
java-version: '11'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
cache: maven
|
cache: maven
|
||||||
server-id: github
|
server-id: ossrh
|
||||||
server-username: MAVEN_USERNAME
|
server-username: MAVEN_USERNAME
|
||||||
server-password: MAVEN_TOKEN
|
server-password: MAVEN_PASSWORD
|
||||||
- name: "Deploy"
|
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
|
||||||
run: mvn -B deploy --file pom.xml -DskipTests
|
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:
|
env:
|
||||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
MAVEN_USERNAME: ${{ secrets.OSSRH_USER }}
|
||||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASS }}
|
||||||
|
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
|
||||||
|
|
||||||
name: Javadoc
|
|
||||||
|
|
||||||
on:
|
|
||||||
# 支持手动触发构建
|
|
||||||
workflow_dispatch:
|
|
||||||
release:
|
|
||||||
# 创建release的时候触发
|
|
||||||
types: [ published ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
api-website:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout the repo
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Set up the Java JDK
|
|
||||||
uses: actions/setup-java@v2
|
|
||||||
with:
|
|
||||||
cache: maven
|
|
||||||
java-version: '11'
|
|
||||||
distribution: 'adopt'
|
|
||||||
|
|
||||||
- name: Generate docs
|
|
||||||
run: mvn javadoc:javadoc -DskipTests
|
|
||||||
env:
|
|
||||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
|
||||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
|
||||||
|
|
||||||
- name: Copy to Location
|
|
||||||
run: |
|
|
||||||
bash .scripts/copy-javadoc.sh
|
|
||||||
|
|
||||||
- name: Generate the sitemap
|
|
||||||
id: sitemap
|
|
||||||
uses: cicirello/generate-sitemap@v1
|
|
||||||
with:
|
|
||||||
base-url-path: https://carmjos.github.io/EasyPlugin
|
|
||||||
path-to-root: docs
|
|
||||||
|
|
||||||
- name: Output stats
|
|
||||||
run: |
|
|
||||||
echo "sitemap-path = ${{ steps.sitemap.outputs.sitemap-path }}"
|
|
||||||
echo "url-count = ${{ steps.sitemap.outputs.url-count }}"
|
|
||||||
echo "excluded-count = ${{ steps.sitemap.outputs.excluded-count }}"
|
|
||||||
ls -l docs
|
|
||||||
|
|
||||||
- name: Configure Git
|
|
||||||
env:
|
|
||||||
DEPLOY_PRI: ${{secrets.DEPLOY_PRI}}
|
|
||||||
run: |
|
|
||||||
sudo timedatectl set-timezone "Asia/Shanghai"
|
|
||||||
mkdir -p ~/.ssh/
|
|
||||||
echo "$DEPLOY_PRI" > ~/.ssh/id_rsa
|
|
||||||
chmod 600 ~/.ssh/id_rsa
|
|
||||||
ssh-keyscan github.com >> ~/.ssh/known_hosts
|
|
||||||
git config --global user.name 'CarmJos'
|
|
||||||
git config --global user.email 'carm@carm.cc'
|
|
||||||
|
|
||||||
- name: Commit documentation changes
|
|
||||||
run: |
|
|
||||||
cd docs
|
|
||||||
git init
|
|
||||||
git remote add origin git@github.com:CarmJos/EasyPlugin.git
|
|
||||||
git checkout -b gh-pages
|
|
||||||
git add -A
|
|
||||||
git commit -m "API Document generated."
|
|
||||||
- name: Push javadocs
|
|
||||||
run: |
|
|
||||||
cd docs
|
|
||||||
git push origin HEAD:gh-pages --force
|
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
name: Build
|
name: Build & Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# 支持手动触发构建
|
# 支持手动触发构建
|
||||||
@@ -19,20 +19,13 @@ jobs:
|
|||||||
uses: actions/setup-java@v2
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
cache: maven
|
cache: maven
|
||||||
java-version: '11'
|
java-version: '8'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
server-id: github
|
server-id: github
|
||||||
server-username: MAVEN_USERNAME
|
server-username: MAVEN_USERNAME
|
||||||
server-password: MAVEN_TOKEN
|
server-password: MAVEN_TOKEN
|
||||||
- name: "Package"
|
- 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:
|
env:
|
||||||
MAVEN_USERNAME: ${{ github.repository_owner }}
|
MAVEN_USERNAME: ${{ github.repository_owner }}
|
||||||
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
- name: "Target Stage"
|
|
||||||
run: mkdir staging && cp */target/*.jar staging
|
|
||||||
- name: "Upload artifact"
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: Artifact
|
|
||||||
path: staging
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
rm -rf docs
|
|
||||||
mkdir -vp docs
|
|
||||||
|
|
||||||
DOC_URL="target/site/apidocs"
|
|
||||||
|
|
||||||
for FILE in easyplugin-*; do
|
|
||||||
|
|
||||||
if test -e "$FILE/$DOC_URL"; then
|
|
||||||
|
|
||||||
MODULE_FILE="docs/${FILE:11}/"
|
|
||||||
|
|
||||||
mkdir -vp "$MODULE_FILE"
|
|
||||||
|
|
||||||
cp -vrf "$FILE"/"$DOC_URL"/* "$MODULE_FILE"
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
|
|
||||||
cp -vrf .documentation/javadoc/JAVADOC-README.md docs/README.md
|
|
||||||
cp -vrf .documentation/javadoc/index.html docs/index.html
|
|
||||||
@@ -23,38 +23,63 @@
|
|||||||
- 轻便独立的功能模块,按需使用,避免大量打包!
|
- 轻便独立的功能模块,按需使用,避免大量打包!
|
||||||
- 详细的Javadoc与使用文档,轻松上手,方便使用!
|
- 详细的Javadoc与使用文档,轻松上手,方便使用!
|
||||||
- 持续的更新与优化,需求不止,更新不止!
|
- 持续的更新与优化,需求不止,更新不止!
|
||||||
- 如需新功能支持,请通过 [Issues](https://github.com/CarmJos/EasyPlugin/issues) 提交功能需求。
|
- 如需新功能支持,请通过 [Issues](https://github.com/CarmJos/EasyPlugin/issues) 提交功能需求。
|
||||||
|
|
||||||
## 内容
|
## 内容
|
||||||
|
|
||||||
项目初创不久,加 * 的仍在开发更新中...
|
项目初创不久,加 * 的仍在开发更新中...欢迎各路大佬帮助提供本项目的开发文档~
|
||||||
|
|
||||||
### 集合部分
|
### 集合部分 (`/collection`)
|
||||||
|
|
||||||
- All [`easyplugin-all`](easyplugin-all)
|
- All [`easyplugin-all`](collection/all)
|
||||||
- Common [`easyplugin-common`](easyplugin-common)
|
- Common [`easyplugin-common`](collection/common)
|
||||||
|
|
||||||
### 主要部分
|
### 主要部分 (`/base`)
|
||||||
|
|
||||||
- Main [`easyplugin-main`](easyplugin-main)
|
- Utils [`easyplugin-utils`](base/utils)
|
||||||
- Command* [`easyplugin-command`](easyplugin-command)
|
- 通用工具类模块,该模块中的内容支持在Bungee、Bukkit使用。
|
||||||
- Configuration [`easyplugin-configuration`](easyplugin-configuration)
|
- 本模块提供
|
||||||
- Database* [`easyplugin-database`](easyplugin-database)
|
- `ColorParser` 支持RGB颜色与RGB渐变色的颜色解析器。
|
||||||
- GUI [`easyplugin-gui`](easyplugin-gui)
|
- `EasyCooldown` 快速创造一个冷却时间的管理器。
|
||||||
|
- `JarResourceUtils` 快速读取Jar包内容的工具类。
|
||||||
|
- Main [`easyplugin-main`](base/main)
|
||||||
|
- 主要接口模块,提供了方便的插件入口类与相关工具类。
|
||||||
|
- Command [`easyplugin-command`](base/command)
|
||||||
|
- 指令接口模块,便于快速进行子指令的实现,并提供单独的TabComplete方法。
|
||||||
|
- 随本项目提供了 `SimpleCompleter` 类,用于快速创建补全的内容列表。
|
||||||
|
- GUI [`easyplugin-gui`](base/main)
|
||||||
|
- 简单便捷的箱子GUI接口,可以快速实现GUI中不同图标的点击功能。
|
||||||
|
- 随本项目提供了 `AutoPagedGUI` 等翻页GUI抽象类。
|
||||||
|
- Storage [`easyplugin-storage`](base/storage)
|
||||||
|
- 抽象存储管理器,便于实现不同的存储类型。
|
||||||
|
- 随本项目提供了 `FileBasedStorage`、`FolderBasedStorage` 等常用存储抽象方法。
|
||||||
|
- Messages [`easyplugin-message`](base/messages)
|
||||||
|
- 随本项目提供了基于 MineConfiguration 实现的 `EasyMessages` 类。
|
||||||
|
- 支持多种消息配置,包括文本消息、ActionBar消息、Title消息、声音、粒子效果播放等。
|
||||||
|
- 支持消息间的延迟发送。
|
||||||
|
|
||||||
### 附属部分
|
### 独立项目部分
|
||||||
|
|
||||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/6245/)* [`easyplugin-placeholderapi`](easyplugin-placeholderapi)
|
> 以下项目均已独立出单独项目,如需使用,**强烈建议自行引用对应的项目**,以支持完整的Javadoc并获取源码内容!
|
||||||
- [Vault](https://github.com/MilkBowl/VaultAPI)* [`easyplugin-vault`](easyplugin-vault)
|
|
||||||
- [LuckPerms](https://www.spigotmc.org/resources/luckperms.28140/)* [`easyplugin-lp`](easyplugin-lp)
|
- _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)
|
||||||
|
- [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) 。
|
||||||
|
|
||||||
### 依赖方式
|
### 依赖方式
|
||||||
|
|
||||||
@@ -67,10 +92,10 @@
|
|||||||
<repositories>
|
<repositories>
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
<!--采用github依赖库,安全稳定,但需要配置 (推荐)-->
|
<!--采用github-repo依赖库(推荐)-->
|
||||||
<id>EasyPlugin</id>
|
<id>EasyPlugin</id>
|
||||||
<name>GitHub Packages</name>
|
<name>GitHub Packages</name>
|
||||||
<url>https://maven.pkg.github.com/CarmJos/EasyPlugin</url>
|
<url>https://raw.githubusercontent.com/CarmJos/EasyPlugin/repo/</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
@@ -121,7 +146,7 @@
|
|||||||
```groovy
|
```groovy
|
||||||
repositories {
|
repositories {
|
||||||
// 采用github依赖库,安全稳定,但需要配置 (推荐)
|
// 采用github依赖库,安全稳定,但需要配置 (推荐)
|
||||||
maven { url 'https://maven.pkg.github.com/CarmJos/EasyPlugin' }
|
maven { url 'https://raw.githubusercontent.com/CarmJos/EasyPlugin/repo/' }
|
||||||
|
|
||||||
// 采用我的私人依赖库,简单方便,但可能因为变故而无法使用
|
// 采用我的私人依赖库,简单方便,但可能因为变故而无法使用
|
||||||
maven { url 'https://repo.carm.cc/repository/maven-public/' }
|
maven { url 'https://repo.carm.cc/repository/maven-public/' }
|
||||||
@@ -161,7 +186,8 @@ dependencies {
|
|||||||
|
|
||||||
> MIT 协议可能是几大开源协议中最宽松的一个,核心条款是:
|
> 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.2</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,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,50 @@
|
|||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import static cc.carm.lib.easyplugin.utils.ColorParser.*;
|
||||||
|
|
||||||
|
public class ColorParseTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
System.out.println(" ");
|
||||||
|
System.out.println(parseGradientColor("&<#AAAAAA>我真的&<#BBBBBB>爱死&<#111111>你&<#FFFFFF>!"));
|
||||||
|
|
||||||
|
// 测试穿插
|
||||||
|
System.out.println(parse("&<#AAAAAA>&l我&r真&b的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r!"));
|
||||||
|
System.out.println(parse("&<#AAAAAA>&l我&r真&(#666666)的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r!"));
|
||||||
|
System.out.println(parse("&r正常的颜色理应&c&l不受影响&r。"));
|
||||||
|
|
||||||
|
System.out.println(clear("&f测试&<#AAAAAA>清理颜色代码&<#111111> &&这样应该&(#666666)不被影响 &f。"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatReadTest() {
|
||||||
|
LinkedHashMap<Integer, String> formats = new LinkedHashMap<>();
|
||||||
|
String text = "&k&l &m&1我&k爱你爱你爱你&o吗?";
|
||||||
|
Matcher matcher = ColorParser.FORMAT_PATTERN.matcher(text);
|
||||||
|
while (matcher.find()) {
|
||||||
|
String code = matcher.group();
|
||||||
|
formats.put(matcher.start(), code);
|
||||||
|
text = matcher.replaceFirst("");
|
||||||
|
matcher.reset(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
formats.forEach((index, code) -> System.out.println(index + " -> " + code));
|
||||||
|
|
||||||
|
String[] parts = text.split("");
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = 0; i < parts.length; i++) {
|
||||||
|
String format = formats.get(i);
|
||||||
|
if (format != null) builder.append(ColorParser.parseBaseColor(format));
|
||||||
|
builder.append(parts[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
<version>1.5.2</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-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>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
package cc.carm.lib.easyplugin.command;
|
||||||
|
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.util.StringUtil;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
|
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||||
|
|
||||||
|
protected final @NotNull JavaPlugin plugin;
|
||||||
|
protected final @NotNull String cmd;
|
||||||
|
protected final @NotNull List<String> aliases;
|
||||||
|
|
||||||
|
protected final @NotNull Map<String, SubCommand<?>> registeredCommands = new HashMap<>();
|
||||||
|
protected final @NotNull Map<String, CommandHandler> registeredHandlers = new HashMap<>();
|
||||||
|
|
||||||
|
protected final @NotNull Map<String, String> aliasesMap = new HashMap<>();
|
||||||
|
|
||||||
|
public CommandHandler(@NotNull JavaPlugin plugin) {
|
||||||
|
this(plugin, plugin.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd) {
|
||||||
|
this(plugin, cmd, new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd, @NotNull String... aliases) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.cmd = cmd;
|
||||||
|
this.aliases = Arrays.asList(aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Void noArgs(CommandSender sender);
|
||||||
|
|
||||||
|
public Void unknownCommand(CommandSender sender, String[] args) {
|
||||||
|
return noArgs(sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Void noPermission(CommandSender sender);
|
||||||
|
|
||||||
|
public Void onException(CommandSender sender, SubCommand<?> cmd, Exception ex) {
|
||||||
|
sender.sendMessage("Error occurred when executing " + cmd.getIdentifier() + ": " + ex.getLocalizedMessage());
|
||||||
|
ex.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull List<String> getAliases() {
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getIdentifier() {
|
||||||
|
return this.cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerSubCommand(SubCommand<?> command) {
|
||||||
|
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.getIdentifier().toLowerCase();
|
||||||
|
this.registeredHandlers.put(name, handler);
|
||||||
|
handler.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
|
if (!this.hasPermission(sender)) {
|
||||||
|
noPermission(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
this.noArgs(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String input = args[0].toLowerCase();
|
||||||
|
|
||||||
|
CommandHandler handler = getHandler(input);
|
||||||
|
if (handler != null) {
|
||||||
|
if (!handler.hasPermission(sender)) {
|
||||||
|
this.noPermission(sender);
|
||||||
|
} else {
|
||||||
|
handler.onCommand(sender, command, label, this.shortenArgs(args));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
SubCommand<?> sub = getSubCommand(input);
|
||||||
|
if (sub == null) {
|
||||||
|
this.unknownCommand(sender, args);
|
||||||
|
} else if (!sub.hasPermission(sender)) {
|
||||||
|
this.noPermission(sender);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
sub.execute(this.plugin, sender, this.shortenArgs(args));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
this.onException(sender, sub, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
|
if (args.length == 0) return Collections.emptyList();
|
||||||
|
|
||||||
|
String input = args[0].toLowerCase();
|
||||||
|
if (args.length == 1) {
|
||||||
|
return getExecutors().stream()
|
||||||
|
.filter(e -> e.hasPermission(sender))
|
||||||
|
.map(NamedExecutor::getIdentifier)
|
||||||
|
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} else {
|
||||||
|
|
||||||
|
CommandHandler handler = getHandler(input);
|
||||||
|
if (handler != null && handler.hasPermission(sender)) {
|
||||||
|
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
SubCommand<?> sub = getSubCommand(input);
|
||||||
|
if (sub != null && sub.hasPermission(sender)) {
|
||||||
|
return sub.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<NamedExecutor> getExecutors() {
|
||||||
|
Set<NamedExecutor> executors = new HashSet<>();
|
||||||
|
executors.addAll(this.registeredHandlers.values());
|
||||||
|
executors.addAll(this.registeredCommands.values());
|
||||||
|
List<NamedExecutor> sortedExecutors = new ArrayList<>(executors);
|
||||||
|
sortedExecutors.sort(Comparator.comparing(NamedExecutor::getIdentifier));
|
||||||
|
return sortedExecutors;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected @Nullable CommandHandler getHandler(@NotNull String name) {
|
||||||
|
CommandHandler fromName = this.registeredHandlers.get(name);
|
||||||
|
if (fromName != null) return fromName;
|
||||||
|
|
||||||
|
String nameFromAlias = this.aliasesMap.get(name);
|
||||||
|
if (nameFromAlias == null) return null;
|
||||||
|
else return this.registeredHandlers.get(nameFromAlias);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected @Nullable SubCommand<?> getSubCommand(@NotNull String name) {
|
||||||
|
SubCommand<?> fromName = this.registeredCommands.get(name);
|
||||||
|
if (fromName != null) return fromName;
|
||||||
|
|
||||||
|
String nameFromAlias = this.aliasesMap.get(name);
|
||||||
|
if (nameFromAlias == null) return null;
|
||||||
|
else return this.registeredCommands.get(nameFromAlias);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String[] shortenArgs(String[] args) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
return args;
|
||||||
|
} else {
|
||||||
|
List<String> argList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
||||||
|
return argList.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package cc.carm.lib.easyplugin.command;
|
||||||
|
|
||||||
|
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.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public interface NamedExecutor {
|
||||||
|
|
||||||
|
@NotNull String getIdentifier();
|
||||||
|
|
||||||
|
@NotNull List<String> getAliases();
|
||||||
|
|
||||||
|
default boolean hasPermission(@NotNull CommandSender sender) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
default Void sendMessage(@NotNull CommandSender sender, @NotNull String... messages) {
|
||||||
|
return sendMessage(sender, Function.identity(), 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package cc.carm.lib.easyplugin.command;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.entity.HumanEntity;
|
||||||
|
import org.bukkit.potion.PotionEffectType;
|
||||||
|
import org.bukkit.util.StringUtil;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class SimpleCompleter {
|
||||||
|
|
||||||
|
public static @NotNull List<String> none() {
|
||||||
|
return ImmutableList.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> objects(@NotNull String input, Collection<?> objects) {
|
||||||
|
return objects(input, objects.size(), objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> objects(@NotNull String input, int limit, Collection<?> objects) {
|
||||||
|
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) {
|
||||||
|
return stream.filter(Objects::nonNull).map(Object::toString)
|
||||||
|
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||||
|
.limit(Math.max(0, limit)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> text(@NotNull String input, String... texts) {
|
||||||
|
return text(input, texts.length, texts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> text(@NotNull String input, int limit, String... texts) {
|
||||||
|
return text(input, limit, Arrays.asList(texts));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> text(@NotNull String input, Collection<String> texts) {
|
||||||
|
return text(input, texts.size(), texts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> text(@NotNull String input, int limit, Collection<String> texts) {
|
||||||
|
return objects(input, limit, texts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> onlinePlayers(@NotNull String input) {
|
||||||
|
return onlinePlayers(input, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> onlinePlayers(@NotNull String input, int limit) {
|
||||||
|
return objects(input, limit, Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> allPlayers(@NotNull String input) {
|
||||||
|
return allPlayers(input, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> allPlayers(@NotNull String input, int limit) {
|
||||||
|
return objects(input, limit, Arrays.stream(Bukkit.getOfflinePlayers()).map(OfflinePlayer::getName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> worlds(@NotNull String input) {
|
||||||
|
return worlds(input, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> worlds(@NotNull String input, int limit) {
|
||||||
|
return objects(input, limit, Bukkit.getWorlds().stream().map(World::getName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> materials(@NotNull String input) {
|
||||||
|
return materials(input, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> materials(@NotNull String input, int limit) {
|
||||||
|
return objects(input, limit, Arrays.stream(Material.values()).map(Enum::name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> effects(@NotNull String input) {
|
||||||
|
return effects(input, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull List<String> effects(@NotNull String input, int limit) {
|
||||||
|
return objects(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName));
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package cc.carm.lib.easyplugin.command;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
|
public abstract class SubCommand<C extends CommandHandler> implements NamedExecutor {
|
||||||
|
|
||||||
|
private final @NotNull C parent;
|
||||||
|
|
||||||
|
private final String identifier;
|
||||||
|
private final List<String> aliases;
|
||||||
|
|
||||||
|
public SubCommand(@NotNull C parent, String identifier, String... aliases) {
|
||||||
|
this.parent = parent;
|
||||||
|
this.identifier = identifier;
|
||||||
|
this.aliases = Arrays.asList(aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull C getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getIdentifier() {
|
||||||
|
return this.identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Unmodifiable
|
||||||
|
public @NotNull List<String> getAliases() {
|
||||||
|
return this.aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Void execute(JavaPlugin plugin, CommandSender sender, String[] args) throws Exception;
|
||||||
|
|
||||||
|
public List<String> tabComplete(JavaPlugin plugin, CommandSender sender, String[] args) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.2</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
<artifactId>easyplugin-gui</artifactId>
|
<artifactId>easyplugin-gui</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>13-EasyPlugin-GUI</name>
|
<name>EasyPlugin-GUI</name>
|
||||||
<description>轻松插件GUI接口模块,方便快捷的创建箱子GUI界面。</description>
|
<description>轻松插件GUI接口模块,方便快捷的创建箱子GUI界面。</description>
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>The MIT License</name>
|
<name>The MIT License</name>
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
<url>https://opensource.org/licenses/MIT</url>
|
||||||
@@ -58,5 +59,13 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.HumanEntity;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class GUI {
|
||||||
|
|
||||||
|
private static JavaPlugin plugin;
|
||||||
|
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
|
||||||
|
|
||||||
|
public static void initialize(JavaPlugin plugin) {
|
||||||
|
GUI.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JavaPlugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HashMap<UUID, GUI> getOpenedGUIs() {
|
||||||
|
return openedGUIs;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected GUIType type;
|
||||||
|
protected String name;
|
||||||
|
public HashMap<Integer, GUIItem> items;
|
||||||
|
public Inventory inv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当玩家点击目标GUI时是否取消
|
||||||
|
*/
|
||||||
|
boolean cancelOnTarget = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当玩家点击自己背包时是否取消
|
||||||
|
*/
|
||||||
|
boolean cancelOnSelf = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当玩家点击界面外时是否取消
|
||||||
|
*/
|
||||||
|
boolean cancelOnOuter = true;
|
||||||
|
|
||||||
|
protected final Map<String, Object> flags = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
protected GUIListener listener;
|
||||||
|
|
||||||
|
public GUI(GUIType type, String name) {
|
||||||
|
this.type = type;
|
||||||
|
this.name = ColorParser.parse(name);
|
||||||
|
this.items = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public HashMap<@NotNull Integer, @NotNull GUIItem> getItems() {
|
||||||
|
return new HashMap<>(items);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void setItem(int index, @Nullable GUIItem item) {
|
||||||
|
if (item == null) {
|
||||||
|
this.items.remove(index);
|
||||||
|
} else {
|
||||||
|
this.items.put(index, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItem(GUIItem item, int... index) {
|
||||||
|
for (int i : index) {
|
||||||
|
setItem(i, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUIItem getItem(int index) {
|
||||||
|
return this.items.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新玩家箱子的视图
|
||||||
|
*/
|
||||||
|
public void updateView() {
|
||||||
|
if (this.inv != null) {
|
||||||
|
List<HumanEntity> viewers = this.inv.getViewers();
|
||||||
|
IntStream.range(0, this.inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
|
||||||
|
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
|
||||||
|
viewers.forEach(p -> ((Player) p).updateInventory());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否取消点击GUI内物品的事件
|
||||||
|
* 如果不取消,玩家可以从GUI中拿取物品。
|
||||||
|
*
|
||||||
|
* @param b 是否取消
|
||||||
|
*/
|
||||||
|
public void setCancelOnTarget(boolean b) {
|
||||||
|
this.cancelOnTarget = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否取消点击自己背包内物品的事件
|
||||||
|
* 如果不取消,玩家可以从自己的背包中拿取物品。
|
||||||
|
*
|
||||||
|
* @param b 是否取消
|
||||||
|
*/
|
||||||
|
public void setCancelOnSelf(boolean b) {
|
||||||
|
this.cancelOnSelf = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否取消点击GUI外的事件
|
||||||
|
* 如果不取消,玩家可以把物品从GUI或背包中丢出去
|
||||||
|
*
|
||||||
|
* @param b 是否取消
|
||||||
|
*/
|
||||||
|
public void setCancelOnOuter(boolean b) {
|
||||||
|
this.cancelOnOuter = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getFlag(String flag) {
|
||||||
|
return this.flags.get(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFlag(String flag, Object obj) {
|
||||||
|
this.flags.put(flag, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeFlag(String flag) {
|
||||||
|
this.flags.remove(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rawClickListener(InventoryClickEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void openGUI(Player player) {
|
||||||
|
if (this.type == GUIType.CANCEL) {
|
||||||
|
throw new IllegalStateException("被取消或不存在的GUI");
|
||||||
|
}
|
||||||
|
|
||||||
|
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
|
||||||
|
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
|
||||||
|
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
|
||||||
|
|
||||||
|
GUI previous = getOpenedGUI(player);
|
||||||
|
if (previous != null) {
|
||||||
|
previous.listener.close(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
setOpenedGUI(player, this);
|
||||||
|
|
||||||
|
this.inv = inv;
|
||||||
|
player.openInventory(inv);
|
||||||
|
|
||||||
|
if (listener == null) {
|
||||||
|
Bukkit.getPluginManager().registerEvents(listener = new GUIListener(this), getPlugin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拖动GUI内物品是执行的代码
|
||||||
|
*
|
||||||
|
* @param event InventoryDragEvent
|
||||||
|
*/
|
||||||
|
public void onDrag(InventoryDragEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭GUI时执行的代码
|
||||||
|
*/
|
||||||
|
public void onClose() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUIType getGUIType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGUIName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setOpenedGUI(Player player, GUI gui) {
|
||||||
|
getOpenedGUIs().put(player.getUniqueId(), gui);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean hasOpenedGUI(Player player) {
|
||||||
|
return getOpenedGUIs().containsKey(player.getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GUI getOpenedGUI(Player player) {
|
||||||
|
return getOpenedGUIs().get(player.getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeOpenedGUI(Player player) {
|
||||||
|
getOpenedGUIs().remove(player.getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class GUIItem {
|
||||||
|
|
||||||
|
ItemStack display;
|
||||||
|
boolean actionActive = true;
|
||||||
|
|
||||||
|
public Set<GUIClickAction> actions = new HashSet<>();
|
||||||
|
public Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
|
||||||
|
|
||||||
|
public GUIItem(ItemStack display) {
|
||||||
|
this.display = display;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final ItemStack getDisplay() {
|
||||||
|
return this.display;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void setDisplay(ItemStack display) {
|
||||||
|
this.display = display;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final boolean isActionActive() {
|
||||||
|
return this.actionActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void setActionActive(boolean b) {
|
||||||
|
actionActive = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 玩家点击该物品后执行的代码
|
||||||
|
* 可以使用 {@link #onClick(Player, ClickType)} 操作点击者
|
||||||
|
*
|
||||||
|
* @param type 点击的类型
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@SuppressWarnings("DeprecatedIsStillUsed")
|
||||||
|
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) {
|
||||||
|
actions.add(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addActionIgnoreActive(GUIClickAction action) {
|
||||||
|
actionsIgnoreActive.add(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rawClickAction(InventoryClickEvent event) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义点击事件代码 (须自行触发)
|
||||||
|
*
|
||||||
|
* @param player 点击GUI的玩家
|
||||||
|
*/
|
||||||
|
public void customAction(Player player) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract static class GUIClickAction {
|
||||||
|
public abstract void run(ClickType type, Player player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
|
public class GUIListener implements Listener {
|
||||||
|
|
||||||
|
GUI currentGUI;
|
||||||
|
|
||||||
|
public GUIListener(GUI gui) {
|
||||||
|
this.currentGUI = gui;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUI getCurrentGUI() {
|
||||||
|
return currentGUI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInventoryClickEvent(InventoryClickEvent event) {
|
||||||
|
if (!(event.getWhoClicked() instanceof Player)) return;
|
||||||
|
Player player = (Player) event.getWhoClicked();
|
||||||
|
if (!GUI.hasOpenedGUI(player)) return;
|
||||||
|
if (GUI.getOpenedGUI(player) != getCurrentGUI()) return;
|
||||||
|
|
||||||
|
getCurrentGUI().rawClickListener(event);
|
||||||
|
|
||||||
|
if (event.getSlot() == -999 && getCurrentGUI().cancelOnOuter) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.getClickedInventory() == null) return;
|
||||||
|
|
||||||
|
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
|
||||||
|
|
||||||
|
if (getCurrentGUI().cancelOnTarget) event.setCancelled(true);
|
||||||
|
|
||||||
|
if (event.getSlot() != -999) {
|
||||||
|
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
|
||||||
|
if (clickedItem != null) {
|
||||||
|
if (clickedItem.isActionActive()) {
|
||||||
|
clickedItem.onClick(player, event.getClick());
|
||||||
|
clickedItem.rawClickAction(event);
|
||||||
|
clickedItem.actions.forEach(action -> action.run(event.getClick(), player));
|
||||||
|
}
|
||||||
|
clickedItem.actionsIgnoreActive.forEach(action -> action.run(event.getClick(), player));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (event.getClickedInventory().equals(player.getInventory()) && getCurrentGUI().cancelOnSelf) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onDrag(InventoryDragEvent e) {
|
||||||
|
if (!(e.getWhoClicked() instanceof Player)) return;
|
||||||
|
if (e.getInventory().equals(getCurrentGUI().inv)
|
||||||
|
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
|
||||||
|
getCurrentGUI().onDrag(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInventoryCloseEvent(InventoryCloseEvent event) {
|
||||||
|
if (!(event.getPlayer() instanceof Player)) return;
|
||||||
|
if (!event.getInventory().equals(getCurrentGUI().inv)) return;
|
||||||
|
|
||||||
|
close((Player) event.getPlayer());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void close(Player p) {
|
||||||
|
HandlerList.unregisterAll(this);
|
||||||
|
getCurrentGUI().listener = null;
|
||||||
|
GUI.removeOpenedGUI(p);
|
||||||
|
getCurrentGUI().onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerLeave(PlayerQuitEvent event) {
|
||||||
|
GUI.removeOpenedGUI(event.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public enum GUIType {
|
||||||
|
|
||||||
|
ONE_BY_NINE(1, 9),
|
||||||
|
TWO_BY_NINE(2, 18),
|
||||||
|
THREE_BY_NINE(3, 27),
|
||||||
|
FOUR_BY_NINE(4, 36),
|
||||||
|
FIVE_BY_NINE(5, 45),
|
||||||
|
SIX_BY_NINE(6, 54),
|
||||||
|
CANCEL(0, 0);
|
||||||
|
|
||||||
|
private final int lines;
|
||||||
|
private final int size;
|
||||||
|
|
||||||
|
GUIType(int lines, int size) {
|
||||||
|
this.lines = lines;
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLines() {
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static GUIType getBySize(int size) {
|
||||||
|
return Arrays.stream(values()).filter(type -> type.getSize() == size).findFirst().orElse(CANCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static GUIType getByLines(int lines) {
|
||||||
|
return Arrays.stream(values()).filter(type -> type.getLines() == lines).findFirst().orElse(CANCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static GUIType getByName(String name) {
|
||||||
|
return Arrays.stream(values()).filter(type -> type.name().equalsIgnoreCase(name)).findFirst().orElse(CANCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.configuration;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIItem;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public class GUIActionConfiguration {
|
||||||
|
|
||||||
|
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
|
||||||
|
@Nullable ClickType clickType,
|
||||||
|
@Nullable String actionContent) {
|
||||||
|
return new GUIActionConfiguration(actionType, clickType, actionContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull GUIActionConfiguration of(@NotNull GUIActionType actionType,
|
||||||
|
@Nullable String actionContent) {
|
||||||
|
return of(actionType, null, actionContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull GUIActionConfiguration of(@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) {
|
||||||
|
this.clickType = clickType;
|
||||||
|
this.actionType = actionType;
|
||||||
|
this.actionContent = actionContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable ClickType getClickType() {
|
||||||
|
return clickType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull GUIActionType getActionType() {
|
||||||
|
return actionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String getActionContent() {
|
||||||
|
return actionContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkAction(Player player, ClickType type) {
|
||||||
|
if (getClickType() == null || getClickType() == type) executeAction(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeAction(Player targetPlayer) {
|
||||||
|
getActionType().getExecutor().accept(targetPlayer, getActionContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUIItem.GUIClickAction toClickAction() {
|
||||||
|
return new GUIItem.GUIClickAction() {
|
||||||
|
@Override
|
||||||
|
public void run(ClickType type, Player player) {
|
||||||
|
checkAction(player, type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.configuration;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Sound;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
public enum GUIActionType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以玩家聊天的形式执行
|
||||||
|
* 若内容以 “/" 开头,则会以玩家身份执行命令。
|
||||||
|
*/
|
||||||
|
CHAT((player, string) -> {
|
||||||
|
if (string == null) return;
|
||||||
|
MessageUtils.setPlaceholders(player, Collections.singletonList(string)).forEach(player::chat);
|
||||||
|
}),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以后台的形式执行指令
|
||||||
|
* 指令内容不需要以“/”开头。
|
||||||
|
*/
|
||||||
|
CONSOLE((player, string) -> {
|
||||||
|
if (string == null) return;
|
||||||
|
MessageUtils.setPlaceholders(player, Collections.singletonList(string))
|
||||||
|
.forEach(message -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), message));
|
||||||
|
}),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向玩家发送消息。
|
||||||
|
*/
|
||||||
|
MESSAGE(MessageUtils::send),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向玩家发送声音。
|
||||||
|
* 允许配置音量与音调
|
||||||
|
* <ul>
|
||||||
|
* <li>SOUND_NAME</li>
|
||||||
|
* <li>SOUND_NAME:VOLUME</li>
|
||||||
|
* <li>SOUND_NAME:VOLUME:PITCH</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
SOUND((player, string) -> {
|
||||||
|
if (string == null) return;
|
||||||
|
try {
|
||||||
|
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||||
|
Sound sound = Arrays.stream(Sound.values())
|
||||||
|
.filter(s -> s.name().equals(args[0]))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
|
||||||
|
if (sound == null) return;
|
||||||
|
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
|
||||||
|
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
|
||||||
|
|
||||||
|
player.playSound(player.getLocation(), sound, volume, pitch);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为玩家关闭GUI。
|
||||||
|
*/
|
||||||
|
CLOSE((player, string) -> player.closeInventory());
|
||||||
|
|
||||||
|
BiConsumer<@NotNull Player, @Nullable String> executor;
|
||||||
|
|
||||||
|
|
||||||
|
GUIActionType(BiConsumer<@NotNull Player, @Nullable String> executor) {
|
||||||
|
this.executor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BiConsumer<@NotNull Player, @Nullable String> getExecutor() {
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GUIActionType readActionType(String string) {
|
||||||
|
return Arrays.stream(GUIActionType.values())
|
||||||
|
.filter(action -> action.name().equalsIgnoreCase(string))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.configuration;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUI;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIType;
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class GUIConfiguration {
|
||||||
|
|
||||||
|
protected String title;
|
||||||
|
protected int lines;
|
||||||
|
|
||||||
|
protected Map<String, 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.lines = lines;
|
||||||
|
this.guiItems = guiItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return ColorParser.parse(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLines() {
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUIType getGUIType() {
|
||||||
|
return Optional.of(GUIType.getByLines(lines))
|
||||||
|
.map(type -> type == GUIType.CANCEL ? GUIType.SIX_BY_NINE : type)
|
||||||
|
.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, GUIItemConfiguration> getGUIItems() {
|
||||||
|
return guiItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupItems(Player player, GUI 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) {
|
||||||
|
if (section == null) return new GUIConfiguration("name", 6);
|
||||||
|
|
||||||
|
return new GUIConfiguration(
|
||||||
|
section.getString("title", ""),
|
||||||
|
section.getInt("lines", 6),
|
||||||
|
readItems(section.getConfigurationSection("items"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return Arrays.stream(ClickType.values())
|
||||||
|
.filter(click -> click.name().equalsIgnoreCase(type))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+150
@@ -0,0 +1,150 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.configuration;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUI;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIItem;
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
|
||||||
|
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class GUIItemConfiguration {
|
||||||
|
|
||||||
|
@Nullable ItemStack original;
|
||||||
|
|
||||||
|
@NotNull Material type;
|
||||||
|
int amount;
|
||||||
|
int data;
|
||||||
|
@Nullable String name;
|
||||||
|
@NotNull List<String> lore;
|
||||||
|
|
||||||
|
@NotNull List<Integer> slots;
|
||||||
|
@NotNull List<GUIActionConfiguration> actions;
|
||||||
|
|
||||||
|
public GUIItemConfiguration(@NotNull Material type, int amount, int data,
|
||||||
|
@Nullable String name, @NotNull List<String> lore,
|
||||||
|
@NotNull List<GUIActionConfiguration> actions,
|
||||||
|
@NotNull List<Integer> slots) {
|
||||||
|
this(null, type, amount, data, name, lore, actions, slots);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GUIItemConfiguration(@Nullable ItemStack original,
|
||||||
|
@NotNull Material type, int amount, int data,
|
||||||
|
@Nullable String name, @NotNull List<String> lore,
|
||||||
|
@NotNull List<GUIActionConfiguration> actions,
|
||||||
|
@NotNull List<Integer> slots) {
|
||||||
|
this.original = original;
|
||||||
|
this.type = type;
|
||||||
|
this.amount = amount;
|
||||||
|
this.data = data;
|
||||||
|
this.name = name;
|
||||||
|
this.lore = lore;
|
||||||
|
this.slots = slots;
|
||||||
|
this.actions = actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupItems(Player player, GUI gui) {
|
||||||
|
ItemStack itemStack;
|
||||||
|
if (original != null) {
|
||||||
|
ItemStack tmp = original.clone();
|
||||||
|
ItemMeta originalMeta = original.getItemMeta();
|
||||||
|
if (originalMeta != null) {
|
||||||
|
if (originalMeta.hasDisplayName()) {
|
||||||
|
originalMeta.setDisplayName(parseText(player, originalMeta.getDisplayName()));
|
||||||
|
}
|
||||||
|
if (originalMeta.getLore() != null) {
|
||||||
|
originalMeta.setLore(parseTexts(player, originalMeta.getLore()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp.setItemMeta(originalMeta);
|
||||||
|
itemStack = tmp;
|
||||||
|
} else {
|
||||||
|
ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data);
|
||||||
|
if (this.name != null) {
|
||||||
|
icon.setDisplayName(parseText(player, this.name));
|
||||||
|
}
|
||||||
|
if (!this.lore.isEmpty()) {
|
||||||
|
icon.setLore(parseTexts(player, this.lore));
|
||||||
|
}
|
||||||
|
itemStack = icon.toItemStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GUIItem item = new GUIItem(itemStack);
|
||||||
|
this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction);
|
||||||
|
this.slots.forEach(slot -> gui.setItem(slot, item));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> parseTexts(Player player, List<String> lore) {
|
||||||
|
return ColorParser.parse(MessageUtils.setPlaceholders(player, lore));
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String parseText(Player player, @NotNull String name) {
|
||||||
|
return ColorParser.parse(MessageUtils.setPlaceholders(player, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Map<String, Object> serialize() {
|
||||||
|
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||||
|
if (original != null) map.put("original", original);
|
||||||
|
else {
|
||||||
|
map.put("type", this.type.name());
|
||||||
|
if (this.data != 0) map.put("data", this.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.name != null) map.put("name", this.name);
|
||||||
|
if (this.amount != 1) map.put("amount", this.amount);
|
||||||
|
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
|
||||||
|
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
|
||||||
|
if (itemSection == null) return null;
|
||||||
|
|
||||||
|
ItemStack original = null;
|
||||||
|
if (itemSection.contains("original")) original = itemSection.getItemStack("original");
|
||||||
|
|
||||||
|
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 amount = itemSection.getInt("amount", 1);
|
||||||
|
String name = itemSection.getString("name");
|
||||||
|
List<String> lore = itemSection.getStringList("lore");
|
||||||
|
|
||||||
|
List<Integer> slots = itemSection.getIntegerList("slots");
|
||||||
|
int slot = itemSection.getInt("slot", 0);
|
||||||
|
|
||||||
|
List<String> actionsString = itemSection.getStringList("actions");
|
||||||
|
List<GUIActionConfiguration> actions = new ArrayList<>();
|
||||||
|
for (String actionString : actionsString) {
|
||||||
|
GUIActionConfiguration action = GUIActionConfiguration.deserialize(actionString);
|
||||||
|
if (action == null) continue;
|
||||||
|
actions.add(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GUIItemConfiguration(
|
||||||
|
original, type, amount, data, name, lore, actions,
|
||||||
|
slots.size() > 0 ? slots : Collections.singletonList(slot)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.paged;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIItem;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIType;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class AutoPagedGUI extends CommonPagedGUI {
|
||||||
|
|
||||||
|
public static Function<Player, ItemStack> defaultPreviousPage = null;
|
||||||
|
public static Function<Player, ItemStack> defaultNextPage = null;
|
||||||
|
|
||||||
|
ItemStack previousPageUI;
|
||||||
|
ItemStack nextPageUI;
|
||||||
|
int previousPageSlot = -1;
|
||||||
|
int nextPageSlot = -1;
|
||||||
|
|
||||||
|
public AutoPagedGUI(GUIType type, String name, int[] range) {
|
||||||
|
super(type, name, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AutoPagedGUI(GUIType type, String name, int a, int b) {
|
||||||
|
super(type, name, a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreviousPageUI(ItemStack lastPageUI) {
|
||||||
|
this.previousPageUI = lastPageUI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextPageUI(ItemStack nextPageUI) {
|
||||||
|
this.nextPageUI = nextPageUI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreviousPageSlot(int slot) {
|
||||||
|
this.previousPageSlot = slot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextPageSlot(int slot) {
|
||||||
|
this.nextPageSlot = slot;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void openGUI(Player user) {
|
||||||
|
if (previousPageSlot >= 0) {
|
||||||
|
if (hasPreviousPage()) {
|
||||||
|
setItem(previousPageSlot, new GUIItem(
|
||||||
|
previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) {
|
||||||
|
@Override
|
||||||
|
public void onClick(Player clicker, ClickType type) {
|
||||||
|
if (type == ClickType.RIGHT) {
|
||||||
|
goFirstPage();
|
||||||
|
} else {
|
||||||
|
goPreviousPage();
|
||||||
|
}
|
||||||
|
openGUI(user);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setItem(previousPageSlot, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextPageSlot >= 0) {
|
||||||
|
if (hasNextPage()) {
|
||||||
|
setItem(nextPageSlot, new GUIItem(
|
||||||
|
nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) {
|
||||||
|
@Override
|
||||||
|
public void onClick(Player clicker, ClickType type) {
|
||||||
|
if (type == ClickType.RIGHT) {
|
||||||
|
goLastPage();
|
||||||
|
} else {
|
||||||
|
goNextPage();
|
||||||
|
}
|
||||||
|
openGUI(user);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setItem(nextPageSlot, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
super.openGUI(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ItemStack getDefaultNextPage(Player player) {
|
||||||
|
return defaultNextPage != null ? defaultNextPage.apply(player) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ItemStack getDefaultPreviousPage(Player player) {
|
||||||
|
return defaultPreviousPage != null ? defaultPreviousPage.apply(player) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.paged;
|
||||||
|
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIItem;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIType;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CommonPagedGUI extends PagedGUI {
|
||||||
|
|
||||||
|
private int[] range;
|
||||||
|
|
||||||
|
private CommonPagedGUI(GUIType type, String name) {
|
||||||
|
super(type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonPagedGUI(GUIType type, String Name, int a, int b) {
|
||||||
|
this(type, Name, toRange(type, a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonPagedGUI(GUIType type, String Name, int[] range) {
|
||||||
|
super(type, Name);
|
||||||
|
Arrays.sort(range);
|
||||||
|
this.range = range;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
int[] matrix = new int[]{
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8,
|
||||||
|
9, 10, 11, 12, 13, 14, 15, 16, 17,
|
||||||
|
18, 19, 20, 21, 22, 23, 24, 25, 26,
|
||||||
|
27, 28, 29, 30, 31, 32, 33, 34, 35,
|
||||||
|
36, 37, 38, 39, 40, 41, 42, 43, 44,
|
||||||
|
45, 46, 47, 48, 49, 50, 51, 52, 53
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
private static int[] toRange(GUIType type, int a, int b) {
|
||||||
|
if (a > b) {
|
||||||
|
a = a ^ b;
|
||||||
|
b = a ^ b;
|
||||||
|
a = a ^ b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lineA = getLine(a);
|
||||||
|
int columnA = getColumn(a);
|
||||||
|
int lineB = getLine(b);
|
||||||
|
int columnB = getColumn(b);
|
||||||
|
|
||||||
|
if (lineB > type.getLines())
|
||||||
|
throw new IndexOutOfBoundsException("页面内容范围超过了GUI的大小");
|
||||||
|
|
||||||
|
int[] range = new int[(lineB - lineA + 1) * (columnB - columnA + 1)];
|
||||||
|
|
||||||
|
for (int i = 0, l = 0; i < type.getSize(); i++) {
|
||||||
|
int li = getLine(i);
|
||||||
|
int ci = getColumn(i);
|
||||||
|
if (li >= lineA && li <= lineB && ci >= columnA && ci <= columnB) {
|
||||||
|
range[l] = i;
|
||||||
|
l++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getLine(int i) {
|
||||||
|
return i / 9 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getColumn(int i) {
|
||||||
|
return i % 9 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPreviousPage() {
|
||||||
|
return page > 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNextPage() {
|
||||||
|
return page < getLastPageNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前往第一页
|
||||||
|
*/
|
||||||
|
public void goFirstPage() {
|
||||||
|
if (hasPreviousPage())
|
||||||
|
this.page = 1;
|
||||||
|
else
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前往最后一页
|
||||||
|
*/
|
||||||
|
public void goLastPage() {
|
||||||
|
if (hasNextPage())
|
||||||
|
this.page = getLastPageNumber();
|
||||||
|
else
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到最后一页的页码
|
||||||
|
*
|
||||||
|
* @return 最后一页的页码
|
||||||
|
*/
|
||||||
|
public int getLastPageNumber() {
|
||||||
|
return (this.container.size() / range.length) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到第一页的页码
|
||||||
|
*
|
||||||
|
* @return 第一页页码(默认为1)
|
||||||
|
*/
|
||||||
|
public int getFirstPageNumber() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void openGUI(Player player) {
|
||||||
|
if (container.isEmpty()) {
|
||||||
|
super.openGUI(player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<GUIItem> list = new ArrayList<>();
|
||||||
|
int start = (page - 1) * range.length;
|
||||||
|
for (int i = start; i < start + range.length; i++) {
|
||||||
|
if (i < container.size()) {
|
||||||
|
list.add(container.get(i));
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
Arrays.stream(range).forEach(index -> setItem(index, null));
|
||||||
|
for (int index : range) {
|
||||||
|
if (i < list.size()) {
|
||||||
|
setItem(index, list.get(i));
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.openGUI(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package cc.carm.lib.easyplugin.gui.paged;
|
||||||
|
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUI;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIItem;
|
||||||
|
import cc.carm.lib.easyplugin.gui.GUIType;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class PagedGUI extends GUI {
|
||||||
|
|
||||||
|
List<GUIItem> container = new ArrayList<>();
|
||||||
|
public int page = 1;
|
||||||
|
|
||||||
|
public PagedGUI(GUIType type, String name) {
|
||||||
|
super(type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addItem(GUIItem i) {
|
||||||
|
container.add(i);
|
||||||
|
return container.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从GUI中移除一个物品
|
||||||
|
*
|
||||||
|
* @param item 物品
|
||||||
|
*/
|
||||||
|
public void removeItem(GUIItem item) {
|
||||||
|
container.remove(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从GUI中移除一个物品
|
||||||
|
*
|
||||||
|
* @param slot 物品格子数
|
||||||
|
*/
|
||||||
|
public void removeItem(int slot) {
|
||||||
|
container.remove(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GUIItem> getItemsContainer() {
|
||||||
|
return new ArrayList<>(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前往上一页
|
||||||
|
*/
|
||||||
|
public void goPreviousPage() {
|
||||||
|
if (hasPreviousPage())
|
||||||
|
page--;
|
||||||
|
else
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前往下一页
|
||||||
|
*/
|
||||||
|
public void goNextPage() {
|
||||||
|
if (hasNextPage())
|
||||||
|
page++;
|
||||||
|
else
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 是否有上一页
|
||||||
|
*/
|
||||||
|
public abstract boolean hasPreviousPage();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 是否有下一页
|
||||||
|
*/
|
||||||
|
public abstract boolean hasNextPage();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
|
||||||
|
import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ActionReadTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
List<String> actions = Arrays.asList(
|
||||||
|
"[CHAT] 123123",
|
||||||
|
"[SHIFT_LEFT:CHAT] /test qwq",
|
||||||
|
"[CONSOLE] say hello",
|
||||||
|
"[CLOSE]"
|
||||||
|
);
|
||||||
|
|
||||||
|
for (String actionString : actions) {
|
||||||
|
int prefixStart = actionString.indexOf("[");
|
||||||
|
int prefixEnd = actionString.indexOf("]");
|
||||||
|
if (prefixStart < 0 || prefixEnd < 0) continue;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
System.out.println("# " + actionString);
|
||||||
|
System.out.println("- actionType is Null");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("# " + actionType.name() + " " + (clickType == null ? "" : clickType.name()));
|
||||||
|
System.out.println("- " + actionString.substring(prefixEnd + 1).trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.2</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -20,8 +21,8 @@
|
|||||||
<artifactId>easyplugin-main</artifactId>
|
<artifactId>easyplugin-main</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>10-EasyPlugin-Main</name>
|
<name>EasyPlugin-Main</name>
|
||||||
<description>轻松插件主要接口模块,包含方便的插件入口类与相关工具类。</description>
|
<description>轻松插件主要接口模块,包含便捷的插件入口类与相关工具类。</description>
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||||
|
|
||||||
<developers>
|
<developers>
|
||||||
@@ -33,7 +34,7 @@
|
|||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>The MIT License</name>
|
<name>The MIT License</name>
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
<url>https://opensource.org/licenses/MIT</url>
|
||||||
@@ -50,24 +51,29 @@
|
|||||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
||||||
</ciManagement>
|
</ciManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>${project.groupId}</groupId>
|
||||||
|
<artifactId>easyplugin-utils</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>me.clip</groupId>
|
||||||
|
<artifactId>placeholderapi</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@@ -0,0 +1,221 @@
|
|||||||
|
package cc.carm.lib.easyplugin;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
||||||
|
import cc.carm.lib.easyplugin.utils.JarResourceUtils;
|
||||||
|
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public abstract class EasyPlugin extends JavaPlugin {
|
||||||
|
|
||||||
|
protected @NotNull EasyPluginMessageProvider messageProvider;
|
||||||
|
|
||||||
|
public EasyPlugin() {
|
||||||
|
this(EasyPluginMessageProvider.ZH_CN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyPlugin(@NotNull EasyPluginMessageProvider messageProvider) {
|
||||||
|
this.messageProvider = messageProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected EasyPlugin(JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, File file) {
|
||||||
|
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
|
||||||
|
public final void onLoad() {
|
||||||
|
scheduler = new SchedulerUtils(this);
|
||||||
|
if (!hasOverride("load")) return;
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
log(messageProvider.loading(this));
|
||||||
|
load();
|
||||||
|
log(messageProvider.loaded(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onEnable() {
|
||||||
|
outputInfo();
|
||||||
|
|
||||||
|
log(messageProvider.enabling(this));
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
if (!(this.initialized = initialize())) {
|
||||||
|
setEnabled(false);
|
||||||
|
log(messageProvider.enableFailure(this, startTime));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log(messageProvider.enableSuccess(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onDisable() {
|
||||||
|
if (!hasOverride("shutdown") || !this.initialized) return;
|
||||||
|
outputInfo();
|
||||||
|
|
||||||
|
log(messageProvider.disabling(this));
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
shutdown();
|
||||||
|
log(messageProvider.disabled(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void load() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract boolean initialize();
|
||||||
|
|
||||||
|
protected void shutdown() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
||||||
|
*/
|
||||||
|
public void outputInfo() {
|
||||||
|
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDebugging() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SchedulerUtils getScheduler() {
|
||||||
|
return scheduler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerListener(@NotNull Listener... listeners) {
|
||||||
|
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCommand(String commandName,
|
||||||
|
@NotNull CommandExecutor executor) {
|
||||||
|
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCommand(String commandName,
|
||||||
|
@NotNull CommandExecutor executor,
|
||||||
|
@Nullable TabCompleter tabCompleter) {
|
||||||
|
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
||||||
|
if (command == null) return;
|
||||||
|
command.setExecutor(executor);
|
||||||
|
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(@Nullable String prefix, @Nullable String... messages) {
|
||||||
|
messageProvider.print(this, prefix, messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log(@Nullable String... messages) {
|
||||||
|
print(null, messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error(String... messages) {
|
||||||
|
print("&c[ERROR] &r", messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void debug(@Nullable String... messages) {
|
||||||
|
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在主线程执行操作,并支持获取其结果。
|
||||||
|
*
|
||||||
|
* @param <T> 结果类型
|
||||||
|
* @param action 需要执行的内容
|
||||||
|
* @return CompletableFuture
|
||||||
|
*/
|
||||||
|
public @NotNull <T> CompletableFuture<T> supplySync(@NotNull Supplier<T> action) {
|
||||||
|
CompletableFuture<T> future = new CompletableFuture<>();
|
||||||
|
getScheduler().run(() -> future.complete(action.get()));
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在异步线程中执行一个操作,并获取操作的结果。
|
||||||
|
*
|
||||||
|
* @param <T> 事件类型
|
||||||
|
* @param action 需要执行的内容
|
||||||
|
* @return CompletableFuture
|
||||||
|
*/
|
||||||
|
public @NotNull <T> CompletableFuture<T> supplyAsync(@NotNull Supplier<T> action) {
|
||||||
|
CompletableFuture<T> future = new CompletableFuture<>();
|
||||||
|
getScheduler().runAsync(() -> future.complete(action.get()));
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在主线程唤起一个事件,并支持获取事件的结果。
|
||||||
|
*
|
||||||
|
* @param event 同步事件 (isAsync=false)
|
||||||
|
* @param <T> 事件类型
|
||||||
|
* @return CompletableFuture
|
||||||
|
*/
|
||||||
|
public @NotNull <T extends Event> CompletableFuture<T> callSync(T event) {
|
||||||
|
return supplySync(() -> {
|
||||||
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
return event;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在异步线程中唤起一个事件,并支持获取事件的结果。
|
||||||
|
*
|
||||||
|
* @param event 异步事件 (isAsync=true)
|
||||||
|
* @param <T> 事件类型
|
||||||
|
* @return CompletableFuture
|
||||||
|
*/
|
||||||
|
public @NotNull <T extends Event> CompletableFuture<T> callAsync(T event) {
|
||||||
|
return supplyAsync(() -> {
|
||||||
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
return event;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setMessageProvider(@NotNull EasyPluginMessageProvider provider) {
|
||||||
|
this.messageProvider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||||
|
private boolean hasOverride(String methodName) {
|
||||||
|
Map<Method, Method> methodMap = new HashMap<>();
|
||||||
|
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
||||||
|
.filter(method -> method.getName().equals(methodName))
|
||||||
|
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
||||||
|
.filter(extend -> extend.getName().equals(methodName))
|
||||||
|
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
||||||
|
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
||||||
|
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
||||||
|
);
|
||||||
|
return !methodMap.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package cc.carm.lib.easyplugin.i18n;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public interface EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
EasyPluginMessageProvider ZH_CN = new zh_CN();
|
||||||
|
EasyPluginMessageProvider EN_US = new en_US();
|
||||||
|
|
||||||
|
String loading(Plugin plugin);
|
||||||
|
|
||||||
|
String loaded(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String enabling(Plugin plugin);
|
||||||
|
|
||||||
|
String enableSuccess(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String enableFailure(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String disabling(Plugin plugin);
|
||||||
|
|
||||||
|
String disabled(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
||||||
|
Arrays.stream(messages)
|
||||||
|
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
||||||
|
.map(ColorParser::parse)
|
||||||
|
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
class zh_CN implements EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loading(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loaded(Plugin plugin, long startMillis) {
|
||||||
|
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||||
|
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableFailure(Plugin plugin, long startMillis) {
|
||||||
|
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabled(Plugin plugin, long startMillis) {
|
||||||
|
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class en_US implements EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loading(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loaded(Plugin plugin, long startMillis) {
|
||||||
|
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||||
|
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableFailure(Plugin plugin, long startMillis) {
|
||||||
|
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabled(Plugin plugin, long startMillis) {
|
||||||
|
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,144 @@
|
|||||||
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.inventory.ItemFlag;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.Damageable;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ItemStackFactory {
|
||||||
|
ItemStack item;
|
||||||
|
|
||||||
|
private ItemStackFactory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory(ItemStack is) {
|
||||||
|
this.item = is.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory(Material type) {
|
||||||
|
this(type, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory(Material type, int amount) {
|
||||||
|
this(type, amount, (short) 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory(Material type, int amount, short data) {
|
||||||
|
this.item = new ItemStack(type, amount, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory(Material type, int amount, int data) {
|
||||||
|
this(type, amount, (short) data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack toItemStack() {
|
||||||
|
return this.item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setType(Material type) {
|
||||||
|
this.item.setType(type);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setDurability(int i) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im instanceof Damageable) {
|
||||||
|
((Damageable) im).setDamage(i);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setAmount(int a) {
|
||||||
|
this.item.setAmount(a);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setDisplayName(@NotNull String name) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.setDisplayName(ColorParser.parse(name));
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setLore(@NotNull List<String> loreList) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.setLore(
|
||||||
|
loreList.stream()
|
||||||
|
.map(ColorParser::parse)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory addLore(@NotNull String s) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
List<String> lore = im.getLore() != null ? im.getLore() : new ArrayList<>();
|
||||||
|
lore.add(ColorParser.parse(s));
|
||||||
|
im.setLore(lore);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory addEnchant(@NotNull Enchantment enchant, int level, boolean ignoreLevelRestriction) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.addEnchant(enchant, level, ignoreLevelRestriction);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory removeEnchant(@NotNull Enchantment enchant) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.removeEnchant(enchant);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory addFlag(@NotNull ItemFlag flag) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.addItemFlags(flag);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory removeFlag(@NotNull ItemFlag flag) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.removeItemFlags(flag);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackFactory setUnbreakable(boolean unbreakable) {
|
||||||
|
ItemMeta im = this.item.getItemMeta();
|
||||||
|
if (im != null) {
|
||||||
|
im.setUnbreakable(unbreakable);
|
||||||
|
this.item.setItemMeta(im);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
+43
-14
@@ -4,6 +4,8 @@ import me.clip.placeholderapi.PlaceholderAPI;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -18,7 +20,6 @@ public class MessageUtils {
|
|||||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void send(@Nullable CommandSender sender, String... messages) {
|
public static void send(@Nullable CommandSender sender, String... messages) {
|
||||||
send(sender, Arrays.asList(messages));
|
send(sender, Arrays.asList(messages));
|
||||||
}
|
}
|
||||||
@@ -29,7 +30,7 @@ public class MessageUtils {
|
|||||||
sender.sendMessage(ColorParser.parse(s));
|
sender.sendMessage(ColorParser.parse(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
||||||
sendWithPlaceholders(sender, Arrays.asList(messages));
|
sendWithPlaceholders(sender, Arrays.asList(messages));
|
||||||
}
|
}
|
||||||
@@ -47,7 +48,7 @@ public class MessageUtils {
|
|||||||
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
|
public static String setPlaceholders(@Nullable CommandSender sender, @Nullable String message) {
|
||||||
if (message == null || sender == null) return message;
|
if (message == null || sender == null) return message;
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
||||||
@@ -56,7 +57,11 @@ public class MessageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
|
@Nullable
|
||||||
|
@Contract("_, !null -> !null")
|
||||||
|
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@Nullable List<String> messages) {
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
||||||
@@ -65,20 +70,35 @@ public class MessageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender, String message, String[] params, Object[] values) {
|
public static String setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@NotNull String message,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
return setPlaceholders(sender, setCustomParams(message, params, values));
|
return setPlaceholders(sender, setCustomParams(message, params, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@NotNull List<String> messages,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String setCustomParams(String message, String param, Object value) {
|
public static String setCustomParams(@NotNull String message,
|
||||||
|
@NotNull String param,
|
||||||
|
@NotNull Object value) {
|
||||||
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String setCustomParams(String message, String[] params, Object[] values) {
|
@Nullable
|
||||||
|
@Contract("!null, _, _-> !null ; null, _, _->null ")
|
||||||
|
public static String setCustomParams(@Nullable String message,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
if (message == null) return null;
|
||||||
|
if (params == null || values == null) return message;
|
||||||
if (params.length != values.length) return message;
|
if (params.length != values.length) return message;
|
||||||
|
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
for (int i = 0; i < params.length; i++) {
|
for (int i = 0; i < params.length; i++) {
|
||||||
paramsMap.put(params[i], values[i]);
|
paramsMap.put(params[i], values[i]);
|
||||||
@@ -86,8 +106,8 @@ public class MessageUtils {
|
|||||||
return setCustomParams(message, paramsMap);
|
return setCustomParams(message, paramsMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static String setCustomParams(String message, HashMap<String, Object> params) {
|
public static String setCustomParams(@NotNull String message, @NotNull HashMap<String, Object> params) {
|
||||||
String afterMessage = message;
|
String afterMessage = message;
|
||||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||||
@@ -95,11 +115,18 @@ public class MessageUtils {
|
|||||||
return afterMessage;
|
return afterMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
|
@NotNull
|
||||||
|
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||||
|
@NotNull String param,
|
||||||
|
@NotNull Object value) {
|
||||||
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
|
@NotNull
|
||||||
|
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
if (params == null || values == null) return messages;
|
||||||
if (params.length != values.length) return messages;
|
if (params.length != values.length) return messages;
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
for (int i = 0; i < params.length; i++) {
|
for (int i = 0; i < params.length; i++) {
|
||||||
@@ -108,9 +135,11 @@ public class MessageUtils {
|
|||||||
return setCustomParams(messages, paramsMap);
|
return setCustomParams(messages, paramsMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
||||||
return messages.stream().map(message -> setCustomParams(message, params)).collect(Collectors.toList());
|
return messages.stream()
|
||||||
|
.map(message -> setCustomParams(message, params))
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,355 @@
|
|||||||
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
public class SchedulerUtils {
|
||||||
|
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
|
||||||
|
public SchedulerUtils(JavaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JavaPlugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在服务端主线程中执行一个任务
|
||||||
|
*
|
||||||
|
* @param runnable 需要执行的任务
|
||||||
|
*/
|
||||||
|
public void run(Runnable runnable) {
|
||||||
|
Bukkit.getScheduler().runTask(getPlugin(), runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步执行一个任务。
|
||||||
|
*
|
||||||
|
* @param runnable 需要执行的任务
|
||||||
|
*/
|
||||||
|
public void runAsync(Runnable runnable) {
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在主线程延时执行一个任务。
|
||||||
|
*
|
||||||
|
* @param delay 延迟的ticks
|
||||||
|
* @param runnable 需要执行的任务
|
||||||
|
*/
|
||||||
|
public void runLater(long delay, Runnable runnable) {
|
||||||
|
Bukkit.getScheduler().runTaskLater(getPlugin(), runnable, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步延时执行一个任务。
|
||||||
|
*
|
||||||
|
* @param delay 延迟的ticks
|
||||||
|
* @param runnable 需要执行的任务
|
||||||
|
*/
|
||||||
|
public void runLaterAsync(long delay, Runnable runnable) {
|
||||||
|
Bukkit.getScheduler().runTaskLaterAsynchronously(getPlugin(), runnable, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 间隔一段时间按顺序执行列表中的任务
|
||||||
|
*
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param tasks 任务列表
|
||||||
|
*/
|
||||||
|
public void runAtInterval(long interval, Runnable... tasks) {
|
||||||
|
runAtInterval(0L, interval, tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 间隔一段时间按顺序执行列表中的任务
|
||||||
|
*
|
||||||
|
* @param delay 延迟时间
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param tasks 任务列表
|
||||||
|
*/
|
||||||
|
public void runAtInterval(long delay, long interval, Runnable... tasks) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (this.index >= tasks.length) {
|
||||||
|
this.cancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks[index].run();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}.runTaskTimer(getPlugin(), delay, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 间隔一段时间按顺序异步执行列表中的任务
|
||||||
|
*
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param tasks 任务列表
|
||||||
|
*/
|
||||||
|
public void runAtIntervalAsync(long interval, Runnable... tasks) {
|
||||||
|
runAtIntervalAsync(0L, interval, tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 间隔一段时间按顺序异步执行列表中的任务
|
||||||
|
*
|
||||||
|
* @param delay 延迟时间
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param tasks 任务列表
|
||||||
|
*/
|
||||||
|
public void runAtIntervalAsync(long delay, long interval, Runnable... tasks) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (this.index >= tasks.length) {
|
||||||
|
this.cancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks[index].run();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}.runTaskTimerAsynchronously(getPlugin(), delay, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重复执行一个任务。
|
||||||
|
*
|
||||||
|
* @param repetitions 重复次数
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param task 任务
|
||||||
|
* @param onComplete 结束时执行的任务
|
||||||
|
*/
|
||||||
|
public void repeat(int repetitions, long interval, Runnable task, Runnable onComplete) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
index++;
|
||||||
|
if (this.index >= repetitions) {
|
||||||
|
this.cancel();
|
||||||
|
if (onComplete == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onComplete.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.run();
|
||||||
|
}
|
||||||
|
}.runTaskTimer(getPlugin(), 0L, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重复执行一个任务。
|
||||||
|
*
|
||||||
|
* @param repetitions 重复次数
|
||||||
|
* @param interval 间隔时间
|
||||||
|
* @param task 任务
|
||||||
|
* @param onComplete 结束时执行的任务
|
||||||
|
*/
|
||||||
|
public void repeatAsync(int repetitions, long interval, Runnable task, Runnable onComplete) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
index++;
|
||||||
|
if (this.index >= repetitions) {
|
||||||
|
this.cancel();
|
||||||
|
if (onComplete == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onComplete.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.run();
|
||||||
|
}
|
||||||
|
}.runTaskTimerAsynchronously(getPlugin(), 0L, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在满足某个条件时,重复执行一个任务。
|
||||||
|
*
|
||||||
|
* @param interval 重复间隔时间
|
||||||
|
* @param predicate 条件
|
||||||
|
* @param task 任务
|
||||||
|
* @param onComplete 结束时执行的任务
|
||||||
|
*/
|
||||||
|
public void repeatWhile(long interval, Callable<Boolean> predicate, Runnable task, Runnable onComplete) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
if (!predicate.call()) {
|
||||||
|
this.cancel();
|
||||||
|
if (onComplete == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onComplete.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.run();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runTaskTimer(getPlugin(), 0L, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在满足某个条件时,重复执行一个任务。
|
||||||
|
*
|
||||||
|
* @param interval 重复间隔时间
|
||||||
|
* @param predicate 条件
|
||||||
|
* @param task 任务
|
||||||
|
* @param onComplete 结束时执行的任务
|
||||||
|
*/
|
||||||
|
public void repeatWhileAsync(long interval, Callable<Boolean> predicate, Runnable task, Runnable onComplete) {
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
if (!predicate.call()) {
|
||||||
|
this.cancel();
|
||||||
|
if (onComplete == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onComplete.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.run();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runTaskTimerAsynchronously(getPlugin(), 0L, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Task {
|
||||||
|
void start(Runnable onComplete);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TaskBuilder {
|
||||||
|
private final Queue<Task> taskList;
|
||||||
|
|
||||||
|
public TaskBuilder() {
|
||||||
|
this.taskList = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder append(TaskBuilder builder) {
|
||||||
|
this.taskList.addAll(builder.taskList);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendDelay(long delay) {
|
||||||
|
this.taskList.add(onComplete -> SchedulerUtils.this.runLater(delay, onComplete));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendTask(Runnable task) {
|
||||||
|
this.taskList.add(onComplete ->
|
||||||
|
{
|
||||||
|
task.run();
|
||||||
|
onComplete.run();
|
||||||
|
});
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendTask(Task task) {
|
||||||
|
this.taskList.add(task);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendDelayedTask(long delay, Runnable task) {
|
||||||
|
this.taskList.add(onComplete -> SchedulerUtils.this.runLater(delay, () ->
|
||||||
|
{
|
||||||
|
task.run();
|
||||||
|
onComplete.run();
|
||||||
|
}));
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendTasks(long delay, long interval, Runnable... tasks) {
|
||||||
|
this.taskList.add(onComplete ->
|
||||||
|
{
|
||||||
|
Runnable[] runnables = Arrays.copyOf(tasks, tasks.length + 1);
|
||||||
|
runnables[runnables.length - 1] = onComplete;
|
||||||
|
SchedulerUtils.this.runAtInterval(delay, interval, runnables);
|
||||||
|
});
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendRepeatingTask(int repetitions, long interval, Runnable task) {
|
||||||
|
this.taskList.add(onComplete -> SchedulerUtils.this.repeat(repetitions, interval, task, onComplete));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder appendConditionalRepeatingTask(long interval, Callable<Boolean> predicate, Runnable task) {
|
||||||
|
this.taskList.add(onComplete -> SchedulerUtils.this.repeatWhile(interval, predicate, task, onComplete));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskBuilder waitFor(Callable<Boolean> predicate) {
|
||||||
|
this.taskList.add(onComplete -> new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
if (!predicate.call()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cancel();
|
||||||
|
onComplete.run();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runTaskTimer(getPlugin(), 0L, 1L));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runTasks() {
|
||||||
|
this.startNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startNext() {
|
||||||
|
Task task = this.taskList.poll();
|
||||||
|
if (task == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.start(this::startNext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.2</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>
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?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.5.2</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>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+46
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
<?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.5.2</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-utils</artifactId>
|
||||||
|
|
||||||
|
<name>EasyPlugin-Utils</name>
|
||||||
|
<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>
|
||||||
|
<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,93 @@
|
|||||||
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.text.NumberFormat;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <a href="https://gist.github.com/CarmJos/402cb5aad0ec14ab25c2fa0d21571703">Easy cooldown time utils.</a>
|
||||||
|
*
|
||||||
|
* @param <P> Cooldown key provider
|
||||||
|
* @param <K> Cooldown key
|
||||||
|
* @author CarmJos
|
||||||
|
*/
|
||||||
|
public class EasyCooldown<P, K> {
|
||||||
|
|
||||||
|
protected final NumberFormat numberFormatter;
|
||||||
|
|
||||||
|
protected final @NotNull Map<K, Long> cooldown = new HashMap<>();
|
||||||
|
protected final @NotNull Function<P, K> providerToKey;
|
||||||
|
|
||||||
|
protected long defaultDuration;
|
||||||
|
|
||||||
|
public EasyCooldown(@NotNull Function<P, K> providerToKey) {
|
||||||
|
this(defaultFormatter(), providerToKey, 1000L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyCooldown(@NotNull NumberFormat numberFormatter,
|
||||||
|
@NotNull Function<P, K> providerToKey) {
|
||||||
|
this(numberFormatter, providerToKey, 1000L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyCooldown(@NotNull NumberFormat numberFormatter,
|
||||||
|
@NotNull Function<P, K> providerToKey,
|
||||||
|
long defaultDuration) {
|
||||||
|
this.numberFormatter = numberFormatter;
|
||||||
|
this.providerToKey = providerToKey;
|
||||||
|
this.defaultDuration = defaultDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCooldown(@NotNull P provider) {
|
||||||
|
Long time = this.cooldown.get(this.providerToKey.apply(provider));
|
||||||
|
if (time == null || time < 0) return 0;
|
||||||
|
|
||||||
|
long duration = getDuration(provider);
|
||||||
|
if (duration <= 0) return 0;
|
||||||
|
|
||||||
|
long past = System.currentTimeMillis() - time;
|
||||||
|
return duration - past;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String getCooldownSeconds(@NotNull P provider) {
|
||||||
|
return formatSeconds(getCooldown(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateTime(@NotNull P provider) {
|
||||||
|
this.cooldown.put(this.providerToKey.apply(provider), System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear(@NotNull P provider) {
|
||||||
|
clearCooldown(this.providerToKey.apply(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearCooldown(@NotNull K key) {
|
||||||
|
this.cooldown.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCoolingDown(@NotNull P provider) {
|
||||||
|
return getCooldown(provider) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDuration(@NotNull P provider) {
|
||||||
|
return this.defaultDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String formatSeconds(long cooldownMillis) {
|
||||||
|
return numberFormatter.format((double) cooldownMillis / 1000D);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NumberFormat createFormatter(@NotNull Consumer<NumberFormat> consumer) {
|
||||||
|
NumberFormat format = NumberFormat.getInstance();
|
||||||
|
consumer.accept(format);
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NumberFormat defaultFormatter() {
|
||||||
|
return createFormatter((f) -> f.setMaximumFractionDigits(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
public class JarResourceUtils {
|
||||||
|
public static final char JAR_SEPARATOR = '/';
|
||||||
|
|
||||||
|
public static @Nullable String[] readResource(@Nullable InputStream resourceStream) {
|
||||||
|
if (resourceStream == null) return null;
|
||||||
|
try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) {
|
||||||
|
List<String> contents = new ArrayList<>();
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
contents.add(scanner.nextLine());
|
||||||
|
}
|
||||||
|
return contents.toArray(new String[0]);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copyFolderFromJar(String folderName, File destFolder, CopyOption option)
|
||||||
|
throws IOException {
|
||||||
|
copyFolderFromJar(folderName, destFolder, option, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copyFolderFromJar(String folderName, File destFolder,
|
||||||
|
CopyOption option, PathTrimmer trimmer) throws IOException {
|
||||||
|
if (!destFolder.exists())
|
||||||
|
destFolder.mkdirs();
|
||||||
|
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
|
||||||
|
File fullPath;
|
||||||
|
String path = JarResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
||||||
|
if (trimmer != null)
|
||||||
|
path = trimmer.trim(path);
|
||||||
|
try {
|
||||||
|
if (!path.startsWith("file"))
|
||||||
|
path = "file://" + path;
|
||||||
|
|
||||||
|
fullPath = new File(new URI(path));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath));
|
||||||
|
|
||||||
|
ZipEntry entry;
|
||||||
|
while ((entry = zis.getNextEntry()) != null) {
|
||||||
|
if (!entry.getName().startsWith(folderName + JAR_SEPARATOR))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String fileName = entry.getName();
|
||||||
|
|
||||||
|
if (fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) {
|
||||||
|
File file = new File(destFolder + File.separator + fileName);
|
||||||
|
if (file.isFile()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
file.mkdirs();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = new File(destFolder + File.separator + fileName);
|
||||||
|
if (option == CopyOption.COPY_IF_NOT_EXIST && file.exists())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!file.getParentFile().exists())
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
|
||||||
|
if (!file.exists())
|
||||||
|
file.createNewFile();
|
||||||
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
|
|
||||||
|
int len;
|
||||||
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
|
fos.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
zis.closeEntry();
|
||||||
|
zis.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum CopyOption {
|
||||||
|
COPY_IF_NOT_EXIST, REPLACE_IF_EXIST
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface PathTrimmer {
|
||||||
|
String trim(String original);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.2</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
<artifactId>easyplugin-all</artifactId>
|
<artifactId>easyplugin-all</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>01-EasyPlugin-All</name>
|
<name>EasyPlugin-All</name>
|
||||||
<description>轻松插件全集,将打包全部工具类与工具接口。</description>
|
<description>轻松插件全集,将打包全部工具类与工具接口。</description>
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>The MIT License</name>
|
<name>The MIT License</name>
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
<url>https://opensource.org/licenses/MIT</url>
|
||||||
@@ -61,26 +62,14 @@
|
|||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.2</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -19,22 +20,44 @@
|
|||||||
<artifactId>easyplugin-bom</artifactId>
|
<artifactId>easyplugin-bom</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<name>00-EasyPlugin-Bom</name>
|
<name>EasyPlugin-Bom</name>
|
||||||
<description>轻松插件汇总导入模块,允许快捷导入相关的接口并避免版本不一致问题。</description>
|
<description>轻松插件汇总导入模块,允许快捷导入相关的接口并避免版本不一致问题。</description>
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
<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>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-main</artifactId>
|
<artifactId>easyplugin-main</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyplugin-configuration</artifactId>
|
|
||||||
<version>${project.parent.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-command</artifactId>
|
<artifactId>easyplugin-command</artifactId>
|
||||||
@@ -42,12 +65,12 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-database</artifactId>
|
<artifactId>easyplugin-gui</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-gui</artifactId>
|
<artifactId>easyplugin-storage</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -60,11 +83,13 @@
|
|||||||
<artifactId>easyplugin-vault</artifactId>
|
<artifactId>easyplugin-vault</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-lp</artifactId>
|
<artifactId>easyplugin-githubchecker</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
@@ -72,49 +97,38 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-main</artifactId>
|
<artifactId>easyplugin-main</artifactId>
|
||||||
</dependency>
|
<version>${project.parent.version}</version>
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyplugin-configuration</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyplugin-database</artifactId>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-gui</artifactId>
|
<artifactId>easyplugin-gui</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-command</artifactId>
|
<artifactId>easyplugin-command</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
<artifactId>easyplugin-vault</artifactId>
|
<artifactId>easyplugin-vault</artifactId>
|
||||||
</dependency>
|
<version>${project.parent.version}</version>
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyplugin-lp</artifactId>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.3.0</version>
|
<version>1.5.2</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
<artifactId>easyplugin-common</artifactId>
|
<artifactId>easyplugin-common</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>02-EasyPlugin-Common</name>
|
<name>EasyPlugin-Common</name>
|
||||||
<description>轻松插件常用接口集,包含除附属插件模块外的所有模块。</description>
|
<description>轻松插件常用接口集,包含除附属插件模块外的所有模块。</description>
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
<url>https://github.com/CarmJos/EasyPlugin</url>
|
||||||
|
|
||||||
@@ -65,11 +66,11 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-lp</artifactId>
|
<artifactId>easyplugin-vault</artifactId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-vault</artifactId>
|
<artifactId>easyplugin-storage</artifactId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
|
|
||||||
@@ -83,20 +84,7 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.3.0</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyplugin-command</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>12-EasyPlugin-Command</name>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,129 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.3.0</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyplugin-configuration</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
|
|
||||||
<name>11-EasyPlugin-Configuration</name>
|
|
||||||
<description>轻松插件配置模块,可以方便快捷的将配置文件作为静态参数使用。</description>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
|
||||||
|
|
||||||
<developers>
|
|
||||||
<developer>
|
|
||||||
<id>CarmJos</id>
|
|
||||||
<name>Carm Jos</name>
|
|
||||||
<email>carm@carm.cc</email>
|
|
||||||
<url>https://www.carm.cc</url>
|
|
||||||
</developer>
|
|
||||||
</developers>
|
|
||||||
|
|
||||||
<licenses>
|
|
||||||
<license>
|
|
||||||
<name>The MIT License</name>
|
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
|
||||||
</license>
|
|
||||||
</licenses>
|
|
||||||
|
|
||||||
<issueManagement>
|
|
||||||
<system>GitHub Issues</system>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
|
|
||||||
</issueManagement>
|
|
||||||
|
|
||||||
<ciManagement>
|
|
||||||
<system>GitHub Actions</system>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
|
||||||
</ciManagement>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyplugin-main</artifactId>
|
|
||||||
<version>${project.parent.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<!--
|
|
||||||
ConfigUpdater - to save the comments in default configuration.
|
|
||||||
-> https://github.com/tchristofferson/Config-Updater
|
|
||||||
-->
|
|
||||||
<groupId>com.tchristofferson</groupId>
|
|
||||||
<artifactId>ConfigUpdater</artifactId>
|
|
||||||
<version>2.0-SNAPSHOT</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
|
||||||
<version>3.2.3</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<phase>package</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>shade</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
<configuration>
|
|
||||||
<relocations>
|
|
||||||
<relocation>
|
|
||||||
<pattern>com.tchristofferson.configupdater</pattern>
|
|
||||||
<shadedPattern>cc.carm.lib.easyplugin.configuration.updater</shadedPattern>
|
|
||||||
</relocation>
|
|
||||||
</relocations>
|
|
||||||
<filters>
|
|
||||||
<filter>
|
|
||||||
<artifact>*:*</artifact>
|
|
||||||
<excludes>
|
|
||||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
|
||||||
<exclude>META-INF/*.txt</exclude>
|
|
||||||
</excludes>
|
|
||||||
</filter>
|
|
||||||
</filters>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
-58
@@ -1,58 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.cast;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class ConfigSectionCast<V> extends FileConfigCachedValue<V> {
|
|
||||||
|
|
||||||
@NotNull Function<ConfigurationSection, V> valueCast;
|
|
||||||
@Nullable V defaultValue;
|
|
||||||
|
|
||||||
public ConfigSectionCast(@NotNull String configSection,
|
|
||||||
@NotNull Function<ConfigurationSection, V> valueCast) {
|
|
||||||
this(configSection, valueCast, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigSectionCast(@NotNull String sectionName,
|
|
||||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
this(null, sectionName, valueCast, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigSectionCast(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@NotNull Function<ConfigurationSection, V> valueCast,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
super(source, sectionName);
|
|
||||||
this.valueCast = valueCast;
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public @Nullable V get() {
|
|
||||||
V cached = getCachedValue();
|
|
||||||
if (cached != null && !isExpired()) {
|
|
||||||
return cached;
|
|
||||||
} else {
|
|
||||||
return getConfigOptional()
|
|
||||||
.map(config -> config.getConfigurationSection(getSectionName()))
|
|
||||||
.map(section -> updateCache(valueCast.apply(section)))
|
|
||||||
.orElse(defaultValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Optional<V> getOptional() {
|
|
||||||
return Optional.ofNullable(get());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void set(ConfigurationSection section) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-55
@@ -1,55 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.cast;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
|
|
||||||
|
|
||||||
@NotNull Function<String, V> valueCast;
|
|
||||||
@Nullable V defaultValue;
|
|
||||||
|
|
||||||
public ConfigStringCast(@NotNull String configSection,
|
|
||||||
@NotNull Function<String, V> valueCast) {
|
|
||||||
this(configSection, valueCast, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigStringCast(@NotNull String configSection,
|
|
||||||
@NotNull Function<String, V> valueCast,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
this(null, configSection, valueCast, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigStringCast(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@NotNull Function<String, V> valueCast,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
super(source, sectionName);
|
|
||||||
this.valueCast = valueCast;
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable V get() {
|
|
||||||
V cached = getCachedValue();
|
|
||||||
if (cached != null && !isExpired()) {
|
|
||||||
return cached;
|
|
||||||
} else {
|
|
||||||
return getConfigOptional()
|
|
||||||
.map(config -> config.getString(getSectionName()))
|
|
||||||
.map(s -> updateCache(valueCast.apply(s)))
|
|
||||||
.orElse(defaultValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Optional<V> getOptional() {
|
|
||||||
return Optional.ofNullable(get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@Nullable String value) {
|
|
||||||
setIfPresent(value, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-128
@@ -1,128 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.file;
|
|
||||||
|
|
||||||
|
|
||||||
import com.tchristofferson.configupdater.ConfigUpdater;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
|
||||||
public class FileConfig {
|
|
||||||
|
|
||||||
public static Supplier<FileConfig> pluginConfiguration = null;
|
|
||||||
public static Supplier<FileConfig> messageConfiguration = null;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static FileConfig getPluginConfiguration() {
|
|
||||||
return pluginConfiguration == null ? null : pluginConfiguration.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static FileConfig getMessageConfiguration() {
|
|
||||||
return messageConfiguration == null ? null : messageConfiguration.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long updateTime;
|
|
||||||
|
|
||||||
protected final JavaPlugin plugin;
|
|
||||||
protected final File fileFolder;
|
|
||||||
protected final String fileName;
|
|
||||||
protected final String resourcePath;
|
|
||||||
|
|
||||||
protected File file;
|
|
||||||
protected FileConfiguration config;
|
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
|
|
||||||
this(plugin, "config.yml");
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin,
|
|
||||||
@NotNull String fileName) throws IOException {
|
|
||||||
this(plugin, fileName, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
|
||||||
@NotNull String fileName) throws IOException {
|
|
||||||
this(plugin, resourcePath, plugin.getDataFolder(), fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
|
|
||||||
@NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
|
||||||
this.plugin = plugin;
|
|
||||||
this.resourcePath = resourcePath;
|
|
||||||
this.fileFolder = fileFolder;
|
|
||||||
this.fileName = fileName;
|
|
||||||
initFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void initFile() throws IOException {
|
|
||||||
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
|
||||||
this.file = new File(getFileFolder(), fileName);
|
|
||||||
|
|
||||||
if (!file.exists()) {
|
|
||||||
InputStream resourceStream = plugin.getResource(resourcePath);
|
|
||||||
if (resourceStream == null) {
|
|
||||||
throw new IOException("The resource " + resourcePath + " cannot find in " + plugin.getName() + " !");
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputStream out = new FileOutputStream(file);
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
|
|
||||||
int readBytes;
|
|
||||||
while ((readBytes = resourceStream.read(buffer)) > 0) {
|
|
||||||
out.write(buffer, 0, readBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
resourceStream.close();
|
|
||||||
|
|
||||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
|
||||||
}
|
|
||||||
|
|
||||||
this.updateTime = System.currentTimeMillis();
|
|
||||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFileFolder() {
|
|
||||||
return fileFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfiguration getConfig() {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavaPlugin getPlugin() {
|
|
||||||
return plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save() throws IOException {
|
|
||||||
getConfig().save(getFile());
|
|
||||||
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reload() throws IOException {
|
|
||||||
this.updateTime = System.currentTimeMillis();
|
|
||||||
if (getFile().exists()) {
|
|
||||||
this.config = YamlConfiguration.loadConfiguration(getFile());
|
|
||||||
} else {
|
|
||||||
initFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getUpdateTime() {
|
|
||||||
return updateTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isExpired(long time) {
|
|
||||||
return getUpdateTime() > time;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-42
@@ -1,42 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.file;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public abstract class FileConfigCachedValue<V> extends FileConfigValue {
|
|
||||||
|
|
||||||
protected V cachedValue;
|
|
||||||
protected long updateTime;
|
|
||||||
|
|
||||||
public FileConfigCachedValue(@NotNull String sectionName) {
|
|
||||||
super(sectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfigCachedValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
|
||||||
super(source, sectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V updateCache(V value) {
|
|
||||||
this.updateTime = System.currentTimeMillis();
|
|
||||||
this.cachedValue = value;
|
|
||||||
return getCachedValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isExpired() {
|
|
||||||
return getSource() == null || getSource().isExpired(this.updateTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getUpdateTime() {
|
|
||||||
return updateTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public V getCachedValue() {
|
|
||||||
return cachedValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clearCache() {
|
|
||||||
this.cachedValue = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-74
@@ -1,74 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.file;
|
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public abstract class FileConfigValue {
|
|
||||||
|
|
||||||
protected @Nullable FileConfig source;
|
|
||||||
private final @NotNull String sectionName;
|
|
||||||
|
|
||||||
public FileConfigValue(@NotNull String sectionName) {
|
|
||||||
this(null, sectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileConfigValue(@Nullable FileConfig source, @NotNull String sectionName) {
|
|
||||||
this.source = source;
|
|
||||||
this.sectionName = sectionName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String getSectionName() {
|
|
||||||
return sectionName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save() {
|
|
||||||
getSourceOptional().ifPresent(fileConfig -> {
|
|
||||||
try {
|
|
||||||
fileConfig.save();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
fileConfig.getPlugin().getLogger().severe("Could not save the " + fileConfig.getFile() + " .");
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIfPresent(@Nullable Object value, boolean save) {
|
|
||||||
getConfigOptional().ifPresent(configuration -> configuration.set(getSectionName(), value));
|
|
||||||
if (save) save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createSection(Map<?, ?> values) {
|
|
||||||
getConfigOptional().ifPresent(configuration -> {
|
|
||||||
if (values == null) {
|
|
||||||
configuration.set(getSectionName(), null);
|
|
||||||
} else {
|
|
||||||
configuration.createSection(getSectionName(), values);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable FileConfig getSource() {
|
|
||||||
return source == null ? FileConfig.getPluginConfiguration() : source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<FileConfig> getSourceOptional() {
|
|
||||||
return Optional.ofNullable(getSource());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<FileConfiguration> getConfigOptional() {
|
|
||||||
return getSourceOptional().map(FileConfig::getConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @Nullable <V> V castValue(@Nullable Object val, @NotNull Class<V> clazz) {
|
|
||||||
return castValue(val, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @Nullable <V> V castValue(@Nullable Object val, @NotNull Class<V> clazz, @Nullable V defaultValue) {
|
|
||||||
return clazz.isInstance(val) ? clazz.cast(val) : defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-118
@@ -1,118 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.impl;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.cast.ConfigStringCast;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Sound;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class ConfigSound extends ConfigStringCast<ConfigSound.SoundData> {
|
|
||||||
|
|
||||||
public ConfigSound(@NotNull String configSection) {
|
|
||||||
this(configSection, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigSound(@NotNull String configSection,
|
|
||||||
@Nullable Sound defaultValue) {
|
|
||||||
this(null, configSection, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigSound(@Nullable FileConfig source, @NotNull String configSection,
|
|
||||||
@Nullable Sound defaultValue) {
|
|
||||||
super(source, configSection, getSoundParser(), defaultValue == null ? null : new SoundData(defaultValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@Nullable SoundData value) {
|
|
||||||
if (value == null) {
|
|
||||||
set((String) null);
|
|
||||||
} else if (value.pitch != 1) {
|
|
||||||
set(value.type, value.volume, value.pitch);
|
|
||||||
} else if (value.volume != 1) {
|
|
||||||
set(value.type, value.volume);
|
|
||||||
} else {
|
|
||||||
set(value.type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(Sound value) {
|
|
||||||
set(value.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(Sound value, float volume) {
|
|
||||||
set(value.name() + (volume != 1 ? ":" + volume : ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(Sound value, float volume, float pitch) {
|
|
||||||
set(value.name() + ":" + volume + (pitch != 1 ? ":" + pitch : ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void play(Player player) {
|
|
||||||
SoundData data = get();
|
|
||||||
if (data != null) data.play(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void playToAll() {
|
|
||||||
SoundData data = get();
|
|
||||||
if (data != null) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(data::play);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Function<@Nullable String, @Nullable SoundData> getSoundParser() {
|
|
||||||
return string -> {
|
|
||||||
if (string == null) return null;
|
|
||||||
|
|
||||||
Sound finalSound = null;
|
|
||||||
float volume = 1;
|
|
||||||
float pitch = 1;
|
|
||||||
|
|
||||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
|
||||||
try {
|
|
||||||
if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
|
|
||||||
if (args.length >= 2) volume = Float.parseFloat(args[1]);
|
|
||||||
if (args.length >= 3) pitch = Float.parseFloat(args[2]);
|
|
||||||
} catch (Exception exception) {
|
|
||||||
Bukkit.getLogger().severe("声音 " + string + " 配置错误,不存在同名声音,请检查。");
|
|
||||||
Bukkit.getLogger().severe("Sound " + string + " doesn't match any sound name.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (finalSound != null) {
|
|
||||||
return new SoundData(finalSound, volume, pitch);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SoundData {
|
|
||||||
Sound type;
|
|
||||||
float volume;
|
|
||||||
float pitch;
|
|
||||||
|
|
||||||
public SoundData(Sound type) {
|
|
||||||
this(type, 1, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SoundData(Sound type, float volume) {
|
|
||||||
this(type, volume, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SoundData(Sound type, float volume, float pitch) {
|
|
||||||
this.type = type;
|
|
||||||
this.volume = volume;
|
|
||||||
this.pitch = pitch;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void play(Player player) {
|
|
||||||
player.playSound(player.getLocation(), type, volume, pitch);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-105
@@ -1,105 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
|
||||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class EasyMessage {
|
|
||||||
|
|
||||||
@Nullable ConfigValue<String> configValue;
|
|
||||||
|
|
||||||
@Nullable String defaultValue;
|
|
||||||
@Nullable String[] messageParams;
|
|
||||||
|
|
||||||
public EasyMessage() {
|
|
||||||
this(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyMessage(@Nullable String defaultValue) {
|
|
||||||
this(defaultValue, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyMessage(@Nullable String defaultValue, @Nullable String[] messageParams) {
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initialize(@NotNull FileConfig sourceConfig, @NotNull String sectionName) {
|
|
||||||
this.configValue = new ConfigValue<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
private @Nullable String getDefaultValue() {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
private @Nullable String[] getMessageParams() {
|
|
||||||
return messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
private @NotNull String getDefaultMessages() {
|
|
||||||
if (getDefaultValue() == null) return "";
|
|
||||||
else return getDefaultValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private @NotNull String getMessages() {
|
|
||||||
if (configValue == null) {
|
|
||||||
return getDefaultMessages();
|
|
||||||
} else {
|
|
||||||
return configValue.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(@Nullable CommandSender sender) {
|
|
||||||
return get(sender, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
|
||||||
return get(sender, getMessageParams(), values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
if (sender == null) return getMessages();
|
|
||||||
if (params == null || values == null) {
|
|
||||||
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages()));
|
|
||||||
} else {
|
|
||||||
return ColorParser.parse(MessageUtils.setPlaceholders(sender, getMessages(), params, values));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender) {
|
|
||||||
send(sender, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
|
||||||
send(sender, getMessageParams(), values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
if (params == null || values == null) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
|
||||||
} else {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(getMessages()), params, values);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll() {
|
|
||||||
sendToAll(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(@Nullable Object[] values) {
|
|
||||||
sendToAll(messageParams, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-110
@@ -1,110 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.annotations.Unmodifiable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class EasyMessageList {
|
|
||||||
|
|
||||||
@Nullable ConfigValueList<String> configValue;
|
|
||||||
|
|
||||||
@Nullable String[] defaultValue;
|
|
||||||
@Nullable String[] messageParams;
|
|
||||||
|
|
||||||
public EasyMessageList() {
|
|
||||||
this((String[]) null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyMessageList(@Nullable String... defaultValue) {
|
|
||||||
this(defaultValue, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyMessageList(@Nullable String[] defaultValue,
|
|
||||||
@Nullable String[] messageParams) {
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initialize(FileConfig sourceConfig, String sectionName) {
|
|
||||||
configValue = new ConfigValueList<>(sourceConfig, sectionName, String.class, getDefaultValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
private @Nullable String[] getDefaultValue() {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Unmodifiable
|
|
||||||
private @NotNull List<String> getDefaultMessages() {
|
|
||||||
if (getDefaultValue() == null) return new ArrayList<>();
|
|
||||||
else return Arrays.asList(getDefaultValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private @Nullable String[] getMessageParams() {
|
|
||||||
return messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
private @NotNull List<String> getMessages() {
|
|
||||||
if (configValue == null) {
|
|
||||||
return getDefaultMessages();
|
|
||||||
} else {
|
|
||||||
return configValue.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender) {
|
|
||||||
return get(sender, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable Object[] values) {
|
|
||||||
return get(sender, getMessageParams(), values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
if (sender == null) return getMessages();
|
|
||||||
if (params == null || values == null) {
|
|
||||||
return MessageUtils.setPlaceholders(sender, getMessages());
|
|
||||||
} else {
|
|
||||||
return MessageUtils.setPlaceholders(sender, getMessages(), params, values);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender) {
|
|
||||||
send(sender, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, @Nullable Object[] values) {
|
|
||||||
send(sender, getMessageParams(), values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
if (params == null || values == null) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, getMessages());
|
|
||||||
} else {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, getMessages(), params, values);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll() {
|
|
||||||
sendToAll(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(@Nullable Object[] values) {
|
|
||||||
sendToAll(messageParams, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(@Nullable String[] params, @Nullable Object[] values) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-39
@@ -1,39 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
|
||||||
public class MessagesConfig extends FileConfig {
|
|
||||||
|
|
||||||
public MessagesConfig(@NotNull JavaPlugin plugin) throws IOException {
|
|
||||||
this(plugin, "messages.yml");
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull String fileName) throws IOException {
|
|
||||||
this(plugin, plugin.getDataFolder(), fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagesConfig(@NotNull JavaPlugin plugin, @NotNull File fileFolder, @NotNull String fileName) throws IOException {
|
|
||||||
super(plugin, fileName, fileFolder, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initFile() throws IOException {
|
|
||||||
if (!getFileFolder().exists()) getFileFolder().mkdirs();
|
|
||||||
this.file = new File(getFileFolder(), fileName);
|
|
||||||
if (!file.exists()) file.createNewFile();
|
|
||||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws IOException {
|
|
||||||
getConfig().save(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-100
@@ -1,100 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class MessagesInitializer {
|
|
||||||
|
|
||||||
private MessagesInitializer() {
|
|
||||||
// 静态方法类,不应当实例化。
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz) {
|
|
||||||
initialize(source, rootClazz, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void initialize(FileConfig source, Class<? extends MessagesRoot> rootClazz, boolean saveDefault) {
|
|
||||||
MessagesSection sectionAnnotation = rootClazz.getAnnotation(MessagesSection.class);
|
|
||||||
|
|
||||||
String rootSection = null;
|
|
||||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 1) {
|
|
||||||
rootSection = sectionAnnotation.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Class<?> innerClass : rootClazz.getDeclaredClasses()) {
|
|
||||||
initSection(source, rootSection, innerClass, saveDefault);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Field field : rootClazz.getFields()) {
|
|
||||||
initMessage(source, rootSection, rootClazz, field, saveDefault);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (saveDefault) {
|
|
||||||
try {
|
|
||||||
source.save();
|
|
||||||
} catch (IOException ignore) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initSection(FileConfig source, String parentSection, Class<?> clazz, boolean saveDefault) {
|
|
||||||
if (!Modifier.isStatic(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers())) return;
|
|
||||||
|
|
||||||
String section = getSection(clazz.getSimpleName(), parentSection, clazz.getAnnotation(MessagesSection.class));
|
|
||||||
|
|
||||||
for (Class<?> innerClass : clazz.getDeclaredClasses()) initSection(source, section, innerClass, saveDefault);
|
|
||||||
for (Field field : clazz.getFields()) initMessage(source, section, clazz, field, saveDefault);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initMessage(FileConfig source, String parentSection, Class<?> clazz, Field field, boolean saveDefault) {
|
|
||||||
try {
|
|
||||||
String section = getSection(field.getName(), parentSection, field.getAnnotation(MessagesSection.class));
|
|
||||||
|
|
||||||
Object object = field.get(clazz);
|
|
||||||
|
|
||||||
if (object instanceof EasyMessage) {
|
|
||||||
EasyMessage message = ((EasyMessage) object);
|
|
||||||
if (saveDefault && message.defaultValue != null && !source.getConfig().contains(section)) {
|
|
||||||
source.getConfig().set(section, message.defaultValue);
|
|
||||||
}
|
|
||||||
message.initialize(source, section);
|
|
||||||
} else if (object instanceof EasyMessageList) {
|
|
||||||
EasyMessageList messageList = ((EasyMessageList) object);
|
|
||||||
|
|
||||||
if (saveDefault && messageList.defaultValue != null && !source.getConfig().contains(section)) {
|
|
||||||
source.getConfig().set(section, Arrays.asList(messageList.defaultValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
messageList.initialize(source, section);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static String getSection(@NotNull String name,
|
|
||||||
@Nullable String parentSection,
|
|
||||||
@Nullable MessagesSection sectionAnnotation) {
|
|
||||||
String parent = parentSection != null ? parentSection + "." : "";
|
|
||||||
if (sectionAnnotation != null && sectionAnnotation.value().length() > 0) {
|
|
||||||
return parent + sectionAnnotation.value();
|
|
||||||
} else {
|
|
||||||
return parent + getSectionName(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getSectionName(String codeName) {
|
|
||||||
return codeName.toLowerCase().replace("_", "-");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-5
@@ -1,5 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
public abstract class MessagesRoot {
|
|
||||||
|
|
||||||
}
|
|
||||||
-14
@@ -1,14 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.language;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface MessagesSection {
|
|
||||||
|
|
||||||
String value() default "";
|
|
||||||
|
|
||||||
}
|
|
||||||
-92
@@ -1,92 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.message;
|
|
||||||
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValue;
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class ConfigMessage extends ConfigValue<String> {
|
|
||||||
|
|
||||||
String[] messageParams;
|
|
||||||
|
|
||||||
public ConfigMessage(@NotNull String sectionName) {
|
|
||||||
this(sectionName, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue) {
|
|
||||||
this(sectionName, defaultValue, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessage(@NotNull String sectionName, @Nullable String defaultValue, String[] messageParams) {
|
|
||||||
super(null, sectionName, String.class, defaultValue);
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessage(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@Nullable String defaultValue, String[] messageParams) {
|
|
||||||
super(source, sectionName, String.class, defaultValue);
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(CommandSender sender) {
|
|
||||||
return MessageUtils.setPlaceholders(sender, get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(CommandSender sender, Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
return get(sender, messageParams, values);
|
|
||||||
} else {
|
|
||||||
return get(sender);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String get(CommandSender sender, String[] params, Object[] values) {
|
|
||||||
return MessageUtils.setPlaceholders(sender, get(), params, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(CommandSender sender) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(CommandSender sender, Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
send(sender, messageParams, values);
|
|
||||||
} else {
|
|
||||||
send(sender, new String[0], new Object[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(CommandSender sender, String[] params, Object[] values) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, Collections.singletonList(get()), params, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll() {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, Collections.singletonList(get())));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
sendToAll(messageParams, values);
|
|
||||||
} else {
|
|
||||||
sendToAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(String[] params, Object[] values) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, Collections.singletonList(get()), params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable FileConfig getSource() {
|
|
||||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-90
@@ -1,90 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.message;
|
|
||||||
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.values.ConfigValueList;
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ConfigMessageList extends ConfigValueList<String> {
|
|
||||||
|
|
||||||
@Nullable String[] messageParams;
|
|
||||||
|
|
||||||
public ConfigMessageList(String sectionName) {
|
|
||||||
this(sectionName, new String[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue) {
|
|
||||||
this(sectionName, defaultValue, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessageList(@NotNull String sectionName, @Nullable String[] defaultValue, String[] messageParams) {
|
|
||||||
super(null, sectionName, String.class, defaultValue);
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigMessageList(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@Nullable String[] defaultValue, String[] messageParams) {
|
|
||||||
super(source, sectionName, String.class, defaultValue);
|
|
||||||
this.messageParams = messageParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender) {
|
|
||||||
return MessageUtils.setPlaceholders(sender, get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender, Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
return get(sender, messageParams, values);
|
|
||||||
} else {
|
|
||||||
return get(sender);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull List<String> get(@Nullable CommandSender sender, String[] params, Object[] values) {
|
|
||||||
return MessageUtils.setPlaceholders(sender, get(), params, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
send(sender, messageParams, values);
|
|
||||||
} else {
|
|
||||||
send(sender);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(@Nullable CommandSender sender, String[] params, Object[] values) {
|
|
||||||
MessageUtils.sendWithPlaceholders(sender, get(), params, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(String[] params, Object[] values) {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(pl -> MessageUtils.sendWithPlaceholders(pl, get(), params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll() {
|
|
||||||
Bukkit.getOnlinePlayers().forEach(player -> MessageUtils.sendWithPlaceholders(player, get()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendToAll(Object[] values) {
|
|
||||||
if (messageParams != null) {
|
|
||||||
sendToAll(messageParams, values);
|
|
||||||
} else {
|
|
||||||
sendToAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable FileConfig getSource() {
|
|
||||||
return source == null ? FileConfig.getMessageConfiguration() : source;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-57
@@ -1,57 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.values;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class ConfigValue<V> extends FileConfigValue {
|
|
||||||
private final @NotNull Class<V> clazz;
|
|
||||||
@Nullable V defaultValue;
|
|
||||||
|
|
||||||
public ConfigValue(@NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz) {
|
|
||||||
this(sectionName, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValue(@NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
this(null, sectionName, clazz, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValue(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
super(source, sectionName);
|
|
||||||
this.clazz = clazz;
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public V get() {
|
|
||||||
return getConfigOptional()
|
|
||||||
.map(config -> {
|
|
||||||
if (config.contains(getSectionName())) {
|
|
||||||
return castValue(config.get(getSectionName()), clazz, this.defaultValue);
|
|
||||||
} else {
|
|
||||||
return setDefault(); // 如果没有默认值,就把配置写进去,便于配置
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.orElse(defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Optional<V> getOptional() {
|
|
||||||
return Optional.ofNullable(get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@Nullable V value) {
|
|
||||||
setIfPresent(value, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V setDefault() {
|
|
||||||
if (this.defaultValue != null) set(this.defaultValue);
|
|
||||||
return this.defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-64
@@ -1,64 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.values;
|
|
||||||
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class ConfigValueList<V> extends FileConfigValue {
|
|
||||||
|
|
||||||
private final @NotNull Class<V> clazz;
|
|
||||||
|
|
||||||
@Nullable V[] defaultValue;
|
|
||||||
|
|
||||||
public ConfigValueList(@NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz) {
|
|
||||||
this(sectionName, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValueList(@NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz,
|
|
||||||
@Nullable V[] defaultValue) {
|
|
||||||
this(null, sectionName, clazz, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName,
|
|
||||||
Class<V> clazz) {
|
|
||||||
this(configuration, sectionName, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValueList(@Nullable FileConfig configuration, @NotNull String sectionName,
|
|
||||||
@NotNull Class<V> clazz,
|
|
||||||
@Nullable V[] defaultValue) {
|
|
||||||
super(configuration, sectionName);
|
|
||||||
this.clazz = clazz;
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ArrayList<V> get() {
|
|
||||||
return getConfigOptional()
|
|
||||||
.map(configuration -> configuration.getList(getSectionName()))
|
|
||||||
.map(list -> list.stream()
|
|
||||||
.map(o -> castValue(o, this.clazz))
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.toCollection(ArrayList::new)))
|
|
||||||
.orElse(getDefaultList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ArrayList<V> getDefaultList() {
|
|
||||||
return defaultValue == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(defaultValue));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@Nullable ArrayList<V> value) {
|
|
||||||
setIfPresent(value, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-56
@@ -1,56 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.values;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfig;
|
|
||||||
import cc.carm.lib.easyplugin.configuration.file.FileConfigCachedValue;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
|
|
||||||
|
|
||||||
@NotNull Function<String, K> keyCast;
|
|
||||||
@NotNull Class<V> valueClazz;
|
|
||||||
|
|
||||||
public ConfigValueMap(@NotNull String sectionName, @NotNull Function<String, K> keyCast,
|
|
||||||
@NotNull Class<V> valueClazz) {
|
|
||||||
this(null, sectionName, keyCast, valueClazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigValueMap(@Nullable FileConfig source, @NotNull String sectionName,
|
|
||||||
@NotNull Function<String, K> keyCast, @NotNull Class<V> valueClazz) {
|
|
||||||
super(source, sectionName);
|
|
||||||
this.keyCast = keyCast;
|
|
||||||
this.valueClazz = valueClazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Map<K, V> get() {
|
|
||||||
Map<K, V> cached = getCachedValue();
|
|
||||||
if (cached != null && !isExpired()) {
|
|
||||||
return cached;
|
|
||||||
} else {
|
|
||||||
return getConfigOptional()
|
|
||||||
.map(config -> config.getConfigurationSection(getSectionName()))
|
|
||||||
.map(section -> {
|
|
||||||
Map<K, V> result = new LinkedHashMap<>();
|
|
||||||
for (String key : section.getKeys(false)) {
|
|
||||||
K finalKey = keyCast.apply(key);
|
|
||||||
V finalValue = castValue(section.get(key), valueClazz);
|
|
||||||
if (finalKey != null && finalValue != null) {
|
|
||||||
result.put(finalKey, finalValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return updateCache(result);
|
|
||||||
}).orElse(new LinkedHashMap<>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@Nullable Map<K, V> valuesMap) {
|
|
||||||
createSection(valuesMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.3.0</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyplugin-database</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>14-EasyPlugin-Database</name>
|
|
||||||
<description>轻松插件数据库模块,包含快速实现数据库功能的工具。</description>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin</url>
|
|
||||||
|
|
||||||
<developers>
|
|
||||||
<developer>
|
|
||||||
<id>CarmJos</id>
|
|
||||||
<name>Carm Jos</name>
|
|
||||||
<email>carm@carm.cc</email>
|
|
||||||
<url>https://www.carm.cc</url>
|
|
||||||
</developer>
|
|
||||||
</developers>
|
|
||||||
|
|
||||||
<licenses>
|
|
||||||
<license>
|
|
||||||
<name>The MIT License</name>
|
|
||||||
<url>https://opensource.org/licenses/MIT</url>
|
|
||||||
</license>
|
|
||||||
</licenses>
|
|
||||||
|
|
||||||
<issueManagement>
|
|
||||||
<system>GitHub Issues</system>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
|
|
||||||
</issueManagement>
|
|
||||||
|
|
||||||
<ciManagement>
|
|
||||||
<system>GitHub Actions</system>
|
|
||||||
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
|
|
||||||
</ciManagement>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<artifactId>easysql-beecp</artifactId>
|
|
||||||
<version>0.2.6</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
|
||||||
<version>3.2.3</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<phase>package</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>shade</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
<configuration>
|
|
||||||
<relocations>
|
|
||||||
<relocation>
|
|
||||||
<pattern>cc.carm.lib.easysql</pattern>
|
|
||||||
<shadedPattern>cc.carm.lib.easyplugin.database</shadedPattern>
|
|
||||||
</relocation>
|
|
||||||
</relocations>
|
|
||||||
<filters>
|
|
||||||
<filter>
|
|
||||||
<artifact>*:*</artifact>
|
|
||||||
<excludes>
|
|
||||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
|
||||||
<exclude>META-INF/*.txt</exclude>
|
|
||||||
</excludes>
|
|
||||||
</filter>
|
|
||||||
</filters>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.database;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.*;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class DatabaseTable {
|
|
||||||
|
|
||||||
private final @NotNull String tableName;
|
|
||||||
private final @NotNull String[] columns;
|
|
||||||
|
|
||||||
@Nullable String tableSettings;
|
|
||||||
|
|
||||||
public DatabaseTable(@NotNull String tableName, @NotNull String[] columns) {
|
|
||||||
this(tableName, columns, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DatabaseTable(@NotNull String tableName, @NotNull String[] columns,
|
|
||||||
@Nullable String tableSettings) {
|
|
||||||
this.tableName = tableName;
|
|
||||||
this.columns = columns;
|
|
||||||
this.tableSettings = tableSettings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String getTableName() {
|
|
||||||
return tableName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String[] getColumns() {
|
|
||||||
return columns;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable String getTableSettings() {
|
|
||||||
return tableSettings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int createTable(SQLManager sqlManager) throws SQLException {
|
|
||||||
TableCreateBuilder createAction = sqlManager.createTable(getTableName());
|
|
||||||
createAction.setColumns(getColumns());
|
|
||||||
if (getTableSettings() != null) createAction.setTableSettings(getTableSettings());
|
|
||||||
return createAction.build().execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TableQueryBuilder createQuery(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createQuery().inTable(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public DeleteBuilder createDelete(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createDelete(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public UpdateBuilder createUpdate(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createUpdate(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public InsertBuilder<PreparedSQLUpdateAction> createInsert(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createInsert(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createInsertBatch(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReplaceBuilder<PreparedSQLUpdateAction> createReplace(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createReplace(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(SQLManager sqlManager) {
|
|
||||||
return sqlManager.createReplaceBatch(getTableName());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,206 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.HumanEntity;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
||||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
|
||||||
import org.bukkit.inventory.Inventory;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
public class GUI {
|
|
||||||
|
|
||||||
private static JavaPlugin plugin;
|
|
||||||
private static final HashMap<Player, GUI> openedGUIs = new HashMap<>();
|
|
||||||
|
|
||||||
public static void initialize(JavaPlugin plugin) {
|
|
||||||
GUI.plugin = plugin;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JavaPlugin getPlugin() {
|
|
||||||
return plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HashMap<Player, GUI> getOpenedGUIs() {
|
|
||||||
return openedGUIs;
|
|
||||||
}
|
|
||||||
|
|
||||||
GUIType type;
|
|
||||||
String name;
|
|
||||||
public HashMap<Integer, GUIItem> items;
|
|
||||||
public Inventory inv;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当玩家点击目标GUI时是否取消
|
|
||||||
*/
|
|
||||||
boolean cancelOnTarget = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当玩家点击自己背包时是否取消
|
|
||||||
*/
|
|
||||||
boolean cancelOnSelf = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当玩家点击界面外时是否取消
|
|
||||||
*/
|
|
||||||
boolean cancelOnOuter = true;
|
|
||||||
|
|
||||||
Map<String, Object> flags;
|
|
||||||
|
|
||||||
GUIListener listener;
|
|
||||||
|
|
||||||
public GUI(GUIType type, String name) {
|
|
||||||
this.type = type;
|
|
||||||
this.name = ColorParser.parse(name);
|
|
||||||
this.items = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public HashMap<@NotNull Integer, @NotNull GUIItem> getItems() {
|
|
||||||
return new HashMap<>(items);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setItem(int index, @Nullable GUIItem item) {
|
|
||||||
if (item == null) {
|
|
||||||
this.items.remove(index);
|
|
||||||
} else {
|
|
||||||
this.items.put(index, item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setItem(GUIItem item, int... index) {
|
|
||||||
for (int i : index) {
|
|
||||||
setItem(i, item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public GUIItem getItem(int index) {
|
|
||||||
return this.items.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新玩家箱子的视图
|
|
||||||
*/
|
|
||||||
public void updateView() {
|
|
||||||
if (this.inv != null) {
|
|
||||||
List<HumanEntity> viewers = this.inv.getViewers();
|
|
||||||
IntStream.range(0, this.inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
|
|
||||||
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
|
|
||||||
viewers.forEach(p -> ((Player) p).updateInventory());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置是否取消点击GUI内物品的事件
|
|
||||||
* 如果不取消,玩家可以从GUI中拿取物品。
|
|
||||||
*
|
|
||||||
* @param b 是否取消
|
|
||||||
*/
|
|
||||||
public void setCancelOnTarget(boolean b) {
|
|
||||||
this.cancelOnTarget = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置是否取消点击自己背包内物品的事件
|
|
||||||
* 如果不取消,玩家可以从自己的背包中拿取物品。
|
|
||||||
*
|
|
||||||
* @param b 是否取消
|
|
||||||
*/
|
|
||||||
public void setCancelOnSelf(boolean b) {
|
|
||||||
this.cancelOnSelf = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置是否取消点击GUI外的事件
|
|
||||||
* 如果不取消,玩家可以把物品从GUI或背包中丢出去
|
|
||||||
*
|
|
||||||
* @param b 是否取消
|
|
||||||
*/
|
|
||||||
public void setCancelOnOuter(boolean 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) {
|
|
||||||
if (this.flags == null) return null;
|
|
||||||
else
|
|
||||||
return this.flags.get(flag);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFlag(String flag, Object obj) {
|
|
||||||
if (this.flags == null) this.flags = new HashMap<>();
|
|
||||||
this.flags.replace(flag, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeFlag(String flag) {
|
|
||||||
if (this.flags == null) this.flags = new HashMap<>();
|
|
||||||
this.flags.remove(flag);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rawClickListener(InventoryClickEvent event) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public void openGUI(Player player) {
|
|
||||||
if (this.type == GUIType.CANCEL) throw new NullPointerException("被取消或不存在的GUI");
|
|
||||||
|
|
||||||
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
|
|
||||||
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
|
|
||||||
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
|
|
||||||
|
|
||||||
setOpenedGUI(player, this);
|
|
||||||
|
|
||||||
this.inv = inv;
|
|
||||||
player.openInventory(inv);
|
|
||||||
|
|
||||||
if (listener == null) {
|
|
||||||
Bukkit.getPluginManager().registerEvents(listener = new GUIListener(this), getPlugin());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拖动GUI内物品是执行的代码
|
|
||||||
*
|
|
||||||
* @param event InventoryDragEvent
|
|
||||||
*/
|
|
||||||
public void onDrag(InventoryDragEvent event) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关闭GUI时执行的代码
|
|
||||||
*/
|
|
||||||
public void onClose() {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void setOpenedGUI(Player player, GUI gui) {
|
|
||||||
getOpenedGUIs().put(player, gui);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasOpenedGUI(Player player) {
|
|
||||||
return getOpenedGUIs().containsKey(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GUI getOpenedGUI(Player player) {
|
|
||||||
return getOpenedGUIs().get(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void removeOpenedGUI(Player player) {
|
|
||||||
getOpenedGUIs().remove(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui;
|
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class GUIItem {
|
|
||||||
|
|
||||||
ItemStack display;
|
|
||||||
boolean actionActive = true;
|
|
||||||
|
|
||||||
public Set<GUIClickAction> actions = new HashSet<>();
|
|
||||||
public Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
|
|
||||||
|
|
||||||
public GUIItem(ItemStack display) {
|
|
||||||
this.display = display;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final ItemStack getDisplay() {
|
|
||||||
return this.display;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setDisplay(ItemStack display) {
|
|
||||||
this.display = display;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final boolean isActionActive() {
|
|
||||||
return this.actionActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setActionActive(boolean b) {
|
|
||||||
actionActive = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 玩家点击GUI后执行的代码
|
|
||||||
*
|
|
||||||
* @param type 点击的类型
|
|
||||||
*/
|
|
||||||
public void onClick(ClickType type) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addClickAction(GUIClickAction action) {
|
|
||||||
actions.add(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addActionIgnoreActive(GUIClickAction action) {
|
|
||||||
actionsIgnoreActive.add(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rawClickAction(InventoryClickEvent event) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 玩家点击GUI后执行的代码
|
|
||||||
*
|
|
||||||
* @param player 点击GUI的玩家
|
|
||||||
*/
|
|
||||||
public void customAction(Player player) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract static class GUIClickAction {
|
|
||||||
public abstract void run(ClickType type, Player player);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui;
|
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.HandlerList;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
||||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
|
||||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
|
|
||||||
public class GUIListener implements Listener {
|
|
||||||
|
|
||||||
GUI currentGUI;
|
|
||||||
|
|
||||||
public GUIListener(GUI gui) {
|
|
||||||
this.currentGUI = gui;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GUI getCurrentGUI() {
|
|
||||||
return currentGUI;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onInventoryClickEvent(InventoryClickEvent event) {
|
|
||||||
if (!(event.getWhoClicked() instanceof Player)) return;
|
|
||||||
Player player = (Player) event.getWhoClicked();
|
|
||||||
if (!GUI.hasOpenedGUI(player)) return;
|
|
||||||
if (GUI.getOpenedGUI(player) != getCurrentGUI()) return;
|
|
||||||
|
|
||||||
getCurrentGUI().rawClickListener(event);
|
|
||||||
|
|
||||||
if (event.getSlot() == -999 && getCurrentGUI().cancelOnOuter) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.getClickedInventory() == null) return;
|
|
||||||
|
|
||||||
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
|
|
||||||
|
|
||||||
if (getCurrentGUI().cancelOnTarget) event.setCancelled(true);
|
|
||||||
|
|
||||||
if (event.getSlot() != -999) {
|
|
||||||
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
|
|
||||||
if (clickedItem != null) {
|
|
||||||
if (clickedItem.isActionActive()) {
|
|
||||||
clickedItem.onClick(event.getClick());
|
|
||||||
clickedItem.rawClickAction(event);
|
|
||||||
clickedItem.actions.forEach(action -> action.run(event.getClick(), player));
|
|
||||||
}
|
|
||||||
clickedItem.actionsIgnoreActive.forEach(action -> action.run(event.getClick(), player));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (event.getClickedInventory().equals(player.getInventory()) && getCurrentGUI().cancelOnSelf) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onDrag(InventoryDragEvent e) {
|
|
||||||
if (!(e.getWhoClicked() instanceof Player)) return;
|
|
||||||
if (e.getInventory().equals(getCurrentGUI().inv)
|
|
||||||
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
|
|
||||||
getCurrentGUI().onDrag(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onInventoryCloseEvent(InventoryCloseEvent event) {
|
|
||||||
if (!(event.getPlayer() instanceof Player)) return;
|
|
||||||
if (!event.getInventory().equals(getCurrentGUI().inv)) return;
|
|
||||||
|
|
||||||
HandlerList.unregisterAll(this);
|
|
||||||
getCurrentGUI().listener = null;
|
|
||||||
GUI.removeOpenedGUI((Player) event.getPlayer());
|
|
||||||
getCurrentGUI().onClose();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerLeave(PlayerQuitEvent event) {
|
|
||||||
GUI.removeOpenedGUI(event.getPlayer());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public enum GUIType {
|
|
||||||
|
|
||||||
ONE_BY_NINE(1, 9),
|
|
||||||
TWO_BY_NINE(2, 18),
|
|
||||||
THREE_BY_NINE(3, 27),
|
|
||||||
FOUR_BY_NINE(4, 36),
|
|
||||||
FIVE_BY_NINE(5, 45),
|
|
||||||
SIX_BY_NINE(6, 54),
|
|
||||||
CANCEL(0, 0);
|
|
||||||
|
|
||||||
int lines;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
GUIType(int lines, int size) {
|
|
||||||
this.lines = lines;
|
|
||||||
this.size = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLines() {
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() {
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static GUIType getBySize(int size) {
|
|
||||||
return Arrays.stream(values()).filter(type -> type.getSize() == size).findFirst().orElse(CANCEL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static GUIType getByLines(int lines) {
|
|
||||||
return Arrays.stream(values()).filter(type -> type.getLines() == lines).findFirst().orElse(CANCEL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static GUIType getByName(String name) {
|
|
||||||
return Arrays.stream(values()).filter(type -> type.name().equalsIgnoreCase(name)).findFirst().orElse(CANCEL);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-53
@@ -1,53 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.gui.GUIItem;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public class GUIActionConfiguration {
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable ClickType clickType;
|
|
||||||
final @NotNull GUIActionType actionType;
|
|
||||||
final @Nullable String actionContent;
|
|
||||||
|
|
||||||
public GUIActionConfiguration(@Nullable ClickType clickType,
|
|
||||||
@NotNull GUIActionType actionType,
|
|
||||||
@Nullable String actionContent) {
|
|
||||||
this.clickType = clickType;
|
|
||||||
this.actionType = actionType;
|
|
||||||
this.actionContent = actionContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable ClickType getClickType() {
|
|
||||||
return clickType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull GUIActionType getActionType() {
|
|
||||||
return actionType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable String getActionContent() {
|
|
||||||
return actionContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void checkAction(Player player, ClickType type) {
|
|
||||||
if (getClickType() == null || getClickType() == type) executeAction(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void executeAction(Player targetPlayer) {
|
|
||||||
getActionType().getExecutor().accept(targetPlayer, getActionContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
public GUIItem.GUIClickAction toClickAction() {
|
|
||||||
return new GUIItem.GUIClickAction() {
|
|
||||||
@Override
|
|
||||||
public void run(ClickType type, Player player) {
|
|
||||||
checkAction(player, type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-88
@@ -1,88 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Sound;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
public enum GUIActionType {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 以玩家聊天的形式执行
|
|
||||||
* 若内容以 “/" 开头,则会以玩家身份执行命令。
|
|
||||||
*/
|
|
||||||
CHAT((player, string) -> {
|
|
||||||
if (string == null) return;
|
|
||||||
MessageUtils.setPlaceholders(player, Collections.singletonList(string)).forEach(player::chat);
|
|
||||||
}),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 以后台的形式执行指令
|
|
||||||
* 指令内容不需要以“/”开头。
|
|
||||||
*/
|
|
||||||
CONSOLE((player, string) -> {
|
|
||||||
if (string == null) return;
|
|
||||||
MessageUtils.setPlaceholders(player, Collections.singletonList(string))
|
|
||||||
.forEach(message -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), message));
|
|
||||||
}),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向玩家发送消息。
|
|
||||||
*/
|
|
||||||
MESSAGE(MessageUtils::send),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向玩家发送声音。
|
|
||||||
* 允许配置音量与音调
|
|
||||||
* <ul>
|
|
||||||
* <li>SOUND_NAME</li>
|
|
||||||
* <li>SOUND_NAME:VOLUME</li>
|
|
||||||
* <li>SOUND_NAME:VOLUME:PITCH</li>
|
|
||||||
* </ul>
|
|
||||||
*/
|
|
||||||
SOUND((player, string) -> {
|
|
||||||
if (string == null) return;
|
|
||||||
try {
|
|
||||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
|
||||||
Sound sound = Arrays.stream(Sound.values())
|
|
||||||
.filter(s -> s.name().equals(args[0]))
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
|
|
||||||
if (sound == null) return;
|
|
||||||
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
|
|
||||||
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
|
|
||||||
|
|
||||||
player.playSound(player.getLocation(), sound, volume, pitch);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为玩家关闭GUI。
|
|
||||||
*/
|
|
||||||
CLOSE((player, string) -> player.closeInventory());
|
|
||||||
|
|
||||||
BiConsumer<@NotNull Player, @Nullable String> executor;
|
|
||||||
|
|
||||||
|
|
||||||
GUIActionType(BiConsumer<@NotNull Player, @Nullable String> executor) {
|
|
||||||
this.executor = executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BiConsumer<@NotNull Player, @Nullable String> getExecutor() {
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GUIActionType readActionType(String string) {
|
|
||||||
return Arrays.stream(GUIActionType.values())
|
|
||||||
.filter(action -> action.name().equalsIgnoreCase(string))
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-72
@@ -1,72 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.gui.GUI;
|
|
||||||
import cc.carm.lib.easyplugin.gui.GUIType;
|
|
||||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class GUIConfiguration {
|
|
||||||
|
|
||||||
String title;
|
|
||||||
int lines;
|
|
||||||
|
|
||||||
List<GUIItemConfiguration> guiItems;
|
|
||||||
|
|
||||||
public GUIConfiguration(String title, int lines, List<GUIItemConfiguration> guiItems) {
|
|
||||||
this.title = title;
|
|
||||||
this.lines = lines;
|
|
||||||
this.guiItems = guiItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return ColorParser.parse(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLines() {
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GUIType getGUIType() {
|
|
||||||
return Optional.of(GUIType.getByLines(lines))
|
|
||||||
.map(type -> type == GUIType.CANCEL ? GUIType.SIX_BY_NINE : type)
|
|
||||||
.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<GUIItemConfiguration> getGuiItems() {
|
|
||||||
return guiItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setupItems(Player player, GUI gui) {
|
|
||||||
getGuiItems().forEach(itemConfiguration -> itemConfiguration.setupItems(player, gui));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static GUIConfiguration readConfiguration(@Nullable ConfigurationSection section) {
|
|
||||||
if (section == null) return new GUIConfiguration("name", 6, new ArrayList<>());
|
|
||||||
|
|
||||||
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(
|
|
||||||
title, lines, itemsSection.getKeys(false).stream()
|
|
||||||
.map(key -> GUIItemConfiguration.readFrom(itemsSection.getConfigurationSection(key)))
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.toList())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ClickType readClickType(String type) {
|
|
||||||
return Arrays.stream(ClickType.values())
|
|
||||||
.filter(click -> click.name().equalsIgnoreCase(type))
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-92
@@ -1,92 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.gui.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.gui.GUI;
|
|
||||||
import cc.carm.lib.easyplugin.gui.GUIItem;
|
|
||||||
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
|
|
||||||
import cc.carm.lib.easyplugin.utils.MessageUtils;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class GUIItemConfiguration {
|
|
||||||
|
|
||||||
Material material;
|
|
||||||
int data;
|
|
||||||
String name;
|
|
||||||
@NotNull List<String> lore;
|
|
||||||
|
|
||||||
@NotNull List<Integer> slots;
|
|
||||||
@NotNull List<GUIActionConfiguration> actions;
|
|
||||||
|
|
||||||
public GUIItemConfiguration(Material material, int data,
|
|
||||||
String name, @NotNull List<String> lore,
|
|
||||||
@NotNull List<GUIActionConfiguration> actions,
|
|
||||||
@NotNull List<Integer> slots) {
|
|
||||||
this.material = material;
|
|
||||||
this.data = data;
|
|
||||||
this.name = name;
|
|
||||||
this.lore = lore;
|
|
||||||
this.slots = slots;
|
|
||||||
this.actions = actions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setupItems(Player player, GUI gui) {
|
|
||||||
ItemStackFactory icon = new ItemStackFactory(this.material);
|
|
||||||
icon.setDurability(this.data);
|
|
||||||
if (this.name != null) icon.setDisplayName(this.name);
|
|
||||||
icon.setLore(MessageUtils.setPlaceholders(player, this.lore));
|
|
||||||
|
|
||||||
GUIItem item = new GUIItem(icon.toItemStack());
|
|
||||||
this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction);
|
|
||||||
this.slots.forEach(slot -> gui.setItem(slot, item));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
|
|
||||||
if (itemSection == null) return null;
|
|
||||||
Material material = Optional.ofNullable(Material.matchMaterial(itemSection.getString("material", "STONE"))).orElse(Material.STONE);
|
|
||||||
int data = itemSection.getInt("data", 0);
|
|
||||||
String name = itemSection.getString("name");
|
|
||||||
List<String> lore = itemSection.getStringList("lore");
|
|
||||||
|
|
||||||
List<Integer> slots = itemSection.getIntegerList("slots");
|
|
||||||
int slot = itemSection.getInt("slot", 0);
|
|
||||||
|
|
||||||
List<String> actionsString = itemSection.getStringList("actions");
|
|
||||||
List<GUIActionConfiguration> actions = new ArrayList<>();
|
|
||||||
for (String actionString : actionsString) {
|
|
||||||
int prefixStart = actionString.indexOf("[");
|
|
||||||
int prefixEnd = actionString.indexOf("]");
|
|
||||||
if (prefixStart < 0 || prefixEnd < 0) continue;
|
|
||||||
|
|
||||||
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(
|
|
||||||
material, data, name, lore, actions,
|
|
||||||
slots.size() > 0 ? slots : Collections.singletonList(slot)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
|
|
||||||
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
|
|
||||||
import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration;
|
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ActionReadTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() {
|
|
||||||
|
|
||||||
List<String> actions = Arrays.asList(
|
|
||||||
"[CHAT] 123123",
|
|
||||||
"[SHIFT_LEFT:CHAT] /test qwq",
|
|
||||||
"[CONSOLE] say hello",
|
|
||||||
"[CLOSE]"
|
|
||||||
);
|
|
||||||
|
|
||||||
for (String actionString : actions) {
|
|
||||||
int prefixStart = actionString.indexOf("[");
|
|
||||||
int prefixEnd = actionString.indexOf("]");
|
|
||||||
if (prefixStart < 0 || prefixEnd < 0) continue;
|
|
||||||
|
|
||||||
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) {
|
|
||||||
System.out.println("# " + actionString);
|
|
||||||
System.out.println("- actionType is Null");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("# " + actionType.name() + " " + (clickType == null ? "" : clickType.name()));
|
|
||||||
System.out.println("- " + actionString.substring(prefixEnd + 1).trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.3.0</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyplugin-lp</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>23-EasyPlugin-LuckPerms</name>
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
|
||||||
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandExecutor;
|
|
||||||
import org.bukkit.command.PluginCommand;
|
|
||||||
import org.bukkit.command.TabCompleter;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public abstract class EasyPlugin extends JavaPlugin {
|
|
||||||
|
|
||||||
protected EasyPluginMessageProvider messageProvider;
|
|
||||||
|
|
||||||
public EasyPlugin() {
|
|
||||||
this(new EasyPluginMessageProvider.en_US());
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyPlugin(EasyPluginMessageProvider messageProvider) {
|
|
||||||
this.messageProvider = messageProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
private SchedulerUtils scheduler;
|
|
||||||
private boolean initialized = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onLoad() {
|
|
||||||
scheduler = new SchedulerUtils(this);
|
|
||||||
if (!hasOverride("load")) return;
|
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
log(messageProvider.loading(this));
|
|
||||||
load();
|
|
||||||
log(messageProvider.loaded(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onEnable() {
|
|
||||||
outputInfo();
|
|
||||||
|
|
||||||
log(messageProvider.enabling(this));
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
if (!(this.initialized = initialize())) {
|
|
||||||
setEnabled(false);
|
|
||||||
log(messageProvider.enableFailure(this, startTime));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log(messageProvider.enableSuccess(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onDisable() {
|
|
||||||
if (!hasOverride("shutdown") || !isInitialized()) return;
|
|
||||||
outputInfo();
|
|
||||||
|
|
||||||
log(messageProvider.disabling(this));
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
shutdown();
|
|
||||||
log(messageProvider.disabled(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void load() {
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract boolean initialize();
|
|
||||||
|
|
||||||
protected void shutdown() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
|
||||||
*/
|
|
||||||
public void outputInfo() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isInitialized() {
|
|
||||||
return initialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDebugging() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SchedulerUtils getScheduler() {
|
|
||||||
return scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void regListener(@NotNull Listener... listeners) {
|
|
||||||
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommand(String commandName,
|
|
||||||
@NotNull CommandExecutor executor) {
|
|
||||||
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommand(String commandName,
|
|
||||||
@NotNull CommandExecutor executor,
|
|
||||||
@Nullable TabCompleter tabCompleter) {
|
|
||||||
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
|
||||||
if (command == null) return;
|
|
||||||
command.setExecutor(executor);
|
|
||||||
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void print(@Nullable String prefix, @Nullable String... messages) {
|
|
||||||
messageProvider.print(this, prefix, messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void log(@Nullable String... messages) {
|
|
||||||
print(null, messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error(String... messages) {
|
|
||||||
print("&c[ERROR] &r", messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void debug(@Nullable String... messages) {
|
|
||||||
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
|
||||||
private boolean hasOverride(String methodName) {
|
|
||||||
Map<Method, Method> methodMap = new HashMap<>();
|
|
||||||
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
|
||||||
.filter(method -> method.getName().equals(methodName))
|
|
||||||
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
|
||||||
.filter(extend -> extend.getName().equals(methodName))
|
|
||||||
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
|
||||||
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
|
||||||
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
|
||||||
);
|
|
||||||
return !methodMap.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-110
@@ -1,110 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.i18n;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public interface EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
String loading(Plugin plugin);
|
|
||||||
|
|
||||||
String loaded(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String enabling(Plugin plugin);
|
|
||||||
|
|
||||||
String enableSuccess(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String enableFailure(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String disabling(Plugin plugin);
|
|
||||||
|
|
||||||
String disabled(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
|
||||||
Arrays.stream(messages)
|
|
||||||
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
|
||||||
.map(ColorParser::parse)
|
|
||||||
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
class zh_CN implements EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loading(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loaded(Plugin plugin, long startMillis) {
|
|
||||||
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
|
||||||
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableFailure(Plugin plugin, long startMillis) {
|
|
||||||
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabled(Plugin plugin, long startMillis) {
|
|
||||||
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class en_US implements EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loading(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loaded(Plugin plugin, long startMillis) {
|
|
||||||
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
|
||||||
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableFailure(Plugin plugin, long startMillis) {
|
|
||||||
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabled(Plugin plugin, long startMillis) {
|
|
||||||
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
|
|
||||||
package cc.carm.lib.easyplugin.utils;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class ColorParser {
|
|
||||||
|
|
||||||
public static String parse(String text) {
|
|
||||||
text = parseHexColor(text);
|
|
||||||
return parseColor(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String[] parse(String... texts) {
|
|
||||||
return parse(Arrays.asList(texts)).toArray(new String[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> parse(List<String> texts) {
|
|
||||||
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parseColor(final String text) {
|
|
||||||
return text.replaceAll("&", "§").replace("§§", "&");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parseHexColor(String text) {
|
|
||||||
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
|
|
||||||
Matcher matcher = pattern.matcher(text);
|
|
||||||
while (matcher.find()) {
|
|
||||||
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
|
|
||||||
hexColor = hexColor.replace("&", "");
|
|
||||||
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
|
|
||||||
for (int i = 1; i < hexColor.length(); i++) {
|
|
||||||
bukkitColorCode.append('§').append(hexColor.charAt(i));
|
|
||||||
}
|
|
||||||
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
|
|
||||||
matcher.reset(text);
|
|
||||||
}
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.utils;
|
|
||||||
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.inventory.ItemFlag;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.inventory.meta.Damageable;
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class ItemStackFactory {
|
|
||||||
ItemStack item;
|
|
||||||
|
|
||||||
private ItemStackFactory() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory(ItemStack is) {
|
|
||||||
this.item = is.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory(Material type) {
|
|
||||||
this(type, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory(Material type, int amount) {
|
|
||||||
this(type, amount, (short) 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory(Material type, int amount, short data) {
|
|
||||||
this.item = new ItemStack(type, amount, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory(Material type, int amount, int data) {
|
|
||||||
this(type, amount, (short) data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStack toItemStack() {
|
|
||||||
return this.item;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setType(Material type) {
|
|
||||||
this.item.setType(type);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setDurability(int i) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im instanceof Damageable) {
|
|
||||||
((Damageable) im).setDamage(i);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setAmount(int a) {
|
|
||||||
this.item.setAmount(a);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setDisplayName(@NotNull String name) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.setDisplayName(ColorParser.parse(name));
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setLore(@NotNull List<String> loreList) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.setLore(
|
|
||||||
loreList.stream()
|
|
||||||
.map(ColorParser::parse)
|
|
||||||
.collect(Collectors.toList())
|
|
||||||
);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory addLore(@NotNull String s) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
List<String> lore = im.getLore() != null ? im.getLore() : new ArrayList<>();
|
|
||||||
lore.add(ColorParser.parse(s));
|
|
||||||
im.setLore(lore);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory addEnchant(@NotNull Enchantment enchant, int level, boolean ignoreLevelRestriction) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.addEnchant(enchant, level, ignoreLevelRestriction);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory removeEnchant(@NotNull Enchantment enchant) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.removeEnchant(enchant);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory addFlag(@NotNull ItemFlag flag) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.addItemFlags(flag);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory removeFlag(@NotNull ItemFlag flag) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.removeItemFlags(flag);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackFactory setUnbreakable(boolean unbreakable) {
|
|
||||||
ItemMeta im = this.item.getItemMeta();
|
|
||||||
if (im != null) {
|
|
||||||
im.setUnbreakable(unbreakable);
|
|
||||||
this.item.setItemMeta(im);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,355 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.utils;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
@SuppressWarnings("DuplicatedCode")
|
|
||||||
public class SchedulerUtils {
|
|
||||||
|
|
||||||
private final JavaPlugin plugin;
|
|
||||||
|
|
||||||
public SchedulerUtils(JavaPlugin plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
private JavaPlugin getPlugin() {
|
|
||||||
return plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在服务端主线程中执行一个任务
|
|
||||||
*
|
|
||||||
* @param runnable 需要执行的任务
|
|
||||||
*/
|
|
||||||
public void run(Runnable runnable) {
|
|
||||||
Bukkit.getScheduler().runTask(getPlugin(), runnable);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步执行一个任务。
|
|
||||||
*
|
|
||||||
* @param runnable 需要执行的任务
|
|
||||||
*/
|
|
||||||
public void runAsync(Runnable runnable) {
|
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), runnable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在主线程延时执行一个任务。
|
|
||||||
*
|
|
||||||
* @param delay 延迟的ticks
|
|
||||||
* @param runnable 需要执行的任务
|
|
||||||
*/
|
|
||||||
public void runLater(long delay, Runnable runnable) {
|
|
||||||
Bukkit.getScheduler().runTaskLater(getPlugin(), runnable, delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步延时执行一个任务。
|
|
||||||
*
|
|
||||||
* @param delay 延迟的ticks
|
|
||||||
* @param runnable 需要执行的任务
|
|
||||||
*/
|
|
||||||
public void runLaterAsync(long delay, Runnable runnable) {
|
|
||||||
Bukkit.getScheduler().runTaskLaterAsynchronously(getPlugin(), runnable, delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 间隔一段时间按顺序执行列表中的任务
|
|
||||||
*
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param tasks 任务列表
|
|
||||||
*/
|
|
||||||
public void runAtInterval(long interval, Runnable... tasks) {
|
|
||||||
runAtInterval(0L, interval, tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 间隔一段时间按顺序执行列表中的任务
|
|
||||||
*
|
|
||||||
* @param delay 延迟时间
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param tasks 任务列表
|
|
||||||
*/
|
|
||||||
public void runAtInterval(long delay, long interval, Runnable... tasks) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (this.index >= tasks.length) {
|
|
||||||
this.cancel();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks[index].run();
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}.runTaskTimer(getPlugin(), delay, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 间隔一段时间按顺序异步执行列表中的任务
|
|
||||||
*
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param tasks 任务列表
|
|
||||||
*/
|
|
||||||
public void runAtIntervalAsync(long interval, Runnable... tasks) {
|
|
||||||
runAtIntervalAsync(0L, interval, tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 间隔一段时间按顺序异步执行列表中的任务
|
|
||||||
*
|
|
||||||
* @param delay 延迟时间
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param tasks 任务列表
|
|
||||||
*/
|
|
||||||
public void runAtIntervalAsync(long delay, long interval, Runnable... tasks) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (this.index >= tasks.length) {
|
|
||||||
this.cancel();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks[index].run();
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}.runTaskTimerAsynchronously(getPlugin(), delay, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重复执行一个任务。
|
|
||||||
*
|
|
||||||
* @param repetitions 重复次数
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param task 任务
|
|
||||||
* @param onComplete 结束时执行的任务
|
|
||||||
*/
|
|
||||||
public void repeat(int repetitions, long interval, Runnable task, Runnable onComplete) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
index++;
|
|
||||||
if (this.index >= repetitions) {
|
|
||||||
this.cancel();
|
|
||||||
if (onComplete == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onComplete.run();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.run();
|
|
||||||
}
|
|
||||||
}.runTaskTimer(getPlugin(), 0L, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重复执行一个任务。
|
|
||||||
*
|
|
||||||
* @param repetitions 重复次数
|
|
||||||
* @param interval 间隔时间
|
|
||||||
* @param task 任务
|
|
||||||
* @param onComplete 结束时执行的任务
|
|
||||||
*/
|
|
||||||
public void repeatAsync(int repetitions, long interval, Runnable task, Runnable onComplete) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
private int index;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
index++;
|
|
||||||
if (this.index >= repetitions) {
|
|
||||||
this.cancel();
|
|
||||||
if (onComplete == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onComplete.run();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.run();
|
|
||||||
}
|
|
||||||
}.runTaskTimerAsynchronously(getPlugin(), 0L, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在满足某个条件时,重复执行一个任务。
|
|
||||||
*
|
|
||||||
* @param interval 重复间隔时间
|
|
||||||
* @param predicate 条件
|
|
||||||
* @param task 任务
|
|
||||||
* @param onComplete 结束时执行的任务
|
|
||||||
*/
|
|
||||||
public void repeatWhile(long interval, Callable<Boolean> predicate, Runnable task, Runnable onComplete) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
if (!predicate.call()) {
|
|
||||||
this.cancel();
|
|
||||||
if (onComplete == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onComplete.run();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.run();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskTimer(getPlugin(), 0L, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在满足某个条件时,重复执行一个任务。
|
|
||||||
*
|
|
||||||
* @param interval 重复间隔时间
|
|
||||||
* @param predicate 条件
|
|
||||||
* @param task 任务
|
|
||||||
* @param onComplete 结束时执行的任务
|
|
||||||
*/
|
|
||||||
public void repeatWhileAsync(long interval, Callable<Boolean> predicate, Runnable task, Runnable onComplete) {
|
|
||||||
new BukkitRunnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
if (!predicate.call()) {
|
|
||||||
this.cancel();
|
|
||||||
if (onComplete == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onComplete.run();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.run();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskTimerAsynchronously(getPlugin(), 0L, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Task {
|
|
||||||
void start(Runnable onComplete);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TaskBuilder {
|
|
||||||
private final Queue<Task> taskList;
|
|
||||||
|
|
||||||
public TaskBuilder() {
|
|
||||||
this.taskList = new LinkedList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder append(TaskBuilder builder) {
|
|
||||||
this.taskList.addAll(builder.taskList);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendDelay(long delay) {
|
|
||||||
this.taskList.add(onComplete -> SchedulerUtils.this.runLater(delay, onComplete));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendTask(Runnable task) {
|
|
||||||
this.taskList.add(onComplete ->
|
|
||||||
{
|
|
||||||
task.run();
|
|
||||||
onComplete.run();
|
|
||||||
});
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendTask(Task task) {
|
|
||||||
this.taskList.add(task);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendDelayedTask(long delay, Runnable task) {
|
|
||||||
this.taskList.add(onComplete -> SchedulerUtils.this.runLater(delay, () ->
|
|
||||||
{
|
|
||||||
task.run();
|
|
||||||
onComplete.run();
|
|
||||||
}));
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendTasks(long delay, long interval, Runnable... tasks) {
|
|
||||||
this.taskList.add(onComplete ->
|
|
||||||
{
|
|
||||||
Runnable[] runnables = Arrays.copyOf(tasks, tasks.length + 1);
|
|
||||||
runnables[runnables.length - 1] = onComplete;
|
|
||||||
SchedulerUtils.this.runAtInterval(delay, interval, runnables);
|
|
||||||
});
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendRepeatingTask(int repetitions, long interval, Runnable task) {
|
|
||||||
this.taskList.add(onComplete -> SchedulerUtils.this.repeat(repetitions, interval, task, onComplete));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder appendConditionalRepeatingTask(long interval, Callable<Boolean> predicate, Runnable task) {
|
|
||||||
this.taskList.add(onComplete -> SchedulerUtils.this.repeatWhile(interval, predicate, task, onComplete));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskBuilder waitFor(Callable<Boolean> predicate) {
|
|
||||||
this.taskList.add(onComplete -> new BukkitRunnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
if (!predicate.call()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cancel();
|
|
||||||
onComplete.run();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskTimer(getPlugin(), 0L, 1L));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void runTasks() {
|
|
||||||
this.startNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startNext() {
|
|
||||||
Task task = this.taskList.poll();
|
|
||||||
if (task == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.start(this::startNext);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.3.0</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyplugin-placeholderapi</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>21-EasyPlugin-PlaceholderAPI</name>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?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.5.2</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>
|
||||||
|
|
||||||
|
<!--suppress VulnerableLibrariesLocal -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
<artifactId>githubreleases4j</artifactId>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
+84
@@ -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).createRunner(currentVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Runnable runner(@NotNull Plugin plugin) {
|
||||||
|
return of(plugin).createRunner(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 createRunner(@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+16
@@ -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"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?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.5.2</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-placeholderapi</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>EasyPlugin-PlaceholderAPI</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>me.clip</groupId>
|
||||||
|
<artifactId>placeholderapi</artifactId>
|
||||||
|
<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,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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user