mirror of
https://github.com/CarmJos/UserPrefix.git
synced 2024-09-19 20:15:47 +00:00
chore(expansion): 适配新版EasyPlugin提供的解析器
This commit is contained in:
parent
6fb9daa3d3
commit
8ce82ec7fd
22
pom.xml
22
pom.xml
@ -13,8 +13,8 @@
|
|||||||
<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>
|
||||||
|
|
||||||
<easyplugin.version>1.4.18</easyplugin.version>
|
<deps.easyplugin.version>1.5.0</deps.easyplugin.version>
|
||||||
<mineconfig.version>2.3.0</mineconfig.version>
|
<deps.mineconfig.version>2.3.0</deps.mineconfig.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
@ -108,7 +108,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-main</artifactId>
|
<artifactId>easyplugin-main</artifactId>
|
||||||
<version>${easyplugin.version}</version>
|
<version>${deps.easyplugin.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -116,7 +116,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-command</artifactId>
|
<artifactId>easyplugin-command</artifactId>
|
||||||
<version>${easyplugin.version}</version>
|
<version>${deps.easyplugin.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -124,7 +124,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-gui</artifactId>
|
<artifactId>easyplugin-gui</artifactId>
|
||||||
<version>${easyplugin.version}</version>
|
<version>${deps.easyplugin.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -132,7 +132,15 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-githubchecker</artifactId>
|
<artifactId>easyplugin-githubchecker</artifactId>
|
||||||
<version>${easyplugin.version}</version>
|
<version>${deps.easyplugin.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
<artifactId>easyplugin-placeholderapi</artifactId>
|
||||||
|
<version>${deps.easyplugin.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -140,7 +148,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||||
<version>${mineconfig.version}</version>
|
<version>${deps.mineconfig.version}</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -66,7 +66,7 @@ public class Main extends EasyPlugin {
|
|||||||
|
|
||||||
if (MessageUtils.hasPlaceholderAPI()) {
|
if (MessageUtils.hasPlaceholderAPI()) {
|
||||||
log("注册变量...");
|
log("注册变量...");
|
||||||
new UserPrefixExpansion(getInstance()).register();
|
new UserPrefixExpansion(this, getName()).register();
|
||||||
} else {
|
} else {
|
||||||
log("未安装 PlaceholderAPI ,跳过变量注册...");
|
log("未安装 PlaceholderAPI ,跳过变量注册...");
|
||||||
log("若您想使用变量进行前缀的显示,请安装PlaceholderAPI!");
|
log("若您想使用变量进行前缀的显示,请安装PlaceholderAPI!");
|
||||||
|
@ -1,93 +1,69 @@
|
|||||||
package cc.carm.plugin.userprefix.hooker;
|
package cc.carm.plugin.userprefix.hooker;
|
||||||
|
|
||||||
import cc.carm.plugin.userprefix.Main;
|
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
|
||||||
|
import cc.carm.lib.easyplugin.papi.expansion.SubExpansion;
|
||||||
|
import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
|
||||||
import cc.carm.plugin.userprefix.UserPrefixAPI;
|
import cc.carm.plugin.userprefix.UserPrefixAPI;
|
||||||
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
|
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class UserPrefixExpansion extends PlaceholderExpansion {
|
public class UserPrefixExpansion extends EasyPlaceholder {
|
||||||
|
|
||||||
protected static final @NotNull List<String> SUPPORTED_PLACEHOLDERS = Arrays.asList(
|
public UserPrefixExpansion(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
|
||||||
"%UserPrefix_prefix%", "%UserPrefix_amount%", "%UserPrefix_weight%",
|
super(plugin, rootIdentifier);
|
||||||
"%UserPrefix_identifier%", "%UserPrefix_name%",
|
|
||||||
"%UserPrefix_has_<Identifier>%"
|
|
||||||
);
|
|
||||||
|
|
||||||
protected final @NotNull Main plugin;
|
handle("version", (player, args) -> getVersion());
|
||||||
|
|
||||||
public UserPrefixExpansion(@NotNull Main plugin) {
|
handle("identifier", handlePrefix(PrefixConfig::getIdentifier), "id");
|
||||||
this.plugin = plugin;
|
handle("prefix", handlePrefix(PrefixConfig::getContent));
|
||||||
|
handle("name", handlePrefix(PrefixConfig::getName));
|
||||||
|
handle("weight", handlePrefix(PrefixConfig::getWeight));
|
||||||
|
handle("amount", handlePlayer(
|
||||||
|
(player) -> UserPrefixAPI.getUserManager().getUsablePrefixes(player).size() + 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
handle("has", handlePlayer((player, args) -> {
|
||||||
|
if (args.length < 1) return "参数不足";
|
||||||
|
PrefixConfig prefix = UserPrefixAPI.getPrefixManager().getPrefix(args[0]);
|
||||||
|
if (prefix == null) return "该前缀不存在";
|
||||||
|
return prefix.checkPermission(player);
|
||||||
|
}), Collections.singletonList("<前缀ID>"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlaceholderHandler handlePrefix(Function<PrefixConfig, Object> handler) {
|
||||||
|
return handlePlayer((player, args) -> handler.apply(UserPrefixAPI.getUserManager().getPrefix(player)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlaceholderHandler handlePlayer(BiFunction<Player, String[], Object> handler) {
|
||||||
|
return (player, args) -> {
|
||||||
|
if (player == null || !player.isOnline()) return "Loading...";
|
||||||
|
return handler.apply((Player) player, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlaceholderHandler handlePlayer(Function<Player, Object> handler) {
|
||||||
|
return handlePlayer((player, args) -> handler.apply(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<String> getPlaceholders() {
|
public String onErrorParams(@Nullable OfflinePlayer player) {
|
||||||
return SUPPORTED_PLACEHOLDERS;
|
return "参数不足";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean persist() {
|
public String onException(@Nullable OfflinePlayer player, @NotNull SubExpansion<?> expansion,
|
||||||
return true;
|
@NotNull Exception exception) {
|
||||||
}
|
exception.printStackTrace();
|
||||||
|
return "参数错误";
|
||||||
@Override
|
|
||||||
public @NotNull String getAuthor() {
|
|
||||||
return plugin.getDescription().getAuthors().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String getIdentifier() {
|
|
||||||
return "UserPrefix";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String getVersion() {
|
|
||||||
return plugin.getDescription().getVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String onPlaceholderRequest(Player player, @NotNull String identifier) {
|
|
||||||
if (player == null) return "加载中...";
|
|
||||||
String[] args = identifier.split("_");
|
|
||||||
|
|
||||||
|
|
||||||
if (args.length < 1) {
|
|
||||||
return "参数不足";
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (args[0].toLowerCase()) {
|
|
||||||
case "identifier": {
|
|
||||||
return UserPrefixAPI.getUserManager().getPrefix(player).getIdentifier();
|
|
||||||
}
|
|
||||||
case "prefix": {
|
|
||||||
return UserPrefixAPI.getUserManager().getPrefix(player).getContent();
|
|
||||||
}
|
|
||||||
case "amount": {
|
|
||||||
return String.valueOf(UserPrefixAPI.getUserManager().getUsablePrefixes(player).size() + 1);
|
|
||||||
}
|
|
||||||
case "name": {
|
|
||||||
return UserPrefixAPI.getUserManager().getPrefix(player).getName();
|
|
||||||
}
|
|
||||||
case "weight": {
|
|
||||||
return Integer.toString(UserPrefixAPI.getUserManager().getPrefix(player).getWeight());
|
|
||||||
}
|
|
||||||
case "has": {
|
|
||||||
if (args.length < 2) return "参数不足";
|
|
||||||
PrefixConfig prefix = UserPrefixAPI.getPrefixManager().getPrefix(args[1]);
|
|
||||||
if (prefix == null) return "该前缀不存在";
|
|
||||||
return Boolean.toString(prefix.checkPermission(player));
|
|
||||||
}
|
|
||||||
case "version": {
|
|
||||||
return getVersion();
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
return "参数错误";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user