1
mirror of https://github.com/CarmJos/UserPrefix.git synced 2024-09-19 12:05:47 +00:00

feat(actions): 支持切换前缀时执行自定义操作 (#32)

* feat(actions): 为单个前缀的选择添加可配置的操作
* feat(actions): 实现在前缀选择时执行操作
This commit is contained in:
Carm Jos 2022-09-09 21:28:15 +08:00 committed by GitHub
parent dc14b0cd2e
commit e3c911d375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 102 additions and 23 deletions

31
.documentation/ACTIONS.md Normal file
View File

@ -0,0 +1,31 @@
# UserPrefix Actions 操作
## 使用方式
`actions` 配置节点下,可以配置多个操作,格式为 `[操作类型] {操作参数}`,例如:
- `[CHAT] HELLO %player_name%!`
- `[SOUND] ENTITY_PLAYER_LEVELUP`
## 操作类型
目前支持以下操作类型:
- `CHAT` 以玩家聊天的形式发送,若需要发送指令则添加“/”前缀
- `CONSOLE` 以后台的形式发送指令
- `MESSAGE` 向玩家发送一条消息
- `SOUND` 向玩家发送声音
- `CLOSE` 关闭当前打开的GUI
所有需要键入文本的类型均支持 [PlaceholderAPI](https://www.spigotmc.org/resources/6245/) 变量 。
## 限定点击类型
若您需要限定玩家点击的类型,如左键、右键等,则可以添加在操作类型后,以“`:`”分割,如:
- `[CLOSE:LEFT]` 代表左键点击关闭
- `[MESSAGE:MIDDLE] HELLO WORLD` 代表鼠标中间点击发送消息
详细点击类型见 [`org.bukkit.event.inventory.ClickType`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/ClickType.html) 。

View File

@ -13,6 +13,7 @@
- 使用示例
- [前缀配置文件预设示例](../src/main/resources/prefixes/example-prefix.yml)
- [操作(Actions)配置](ACTIONS.md)
## [开发文档](JAVADOC-README.md)

View File

@ -13,7 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<easyplugin.version>1.4.13</easyplugin.version>
<easyplugin.version>1.4.14</easyplugin.version>
<mineconfig.version>2.0.2</mineconfig.version>
</properties>
@ -162,7 +162,7 @@
<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.3</version>
<version>5.4</version>
<scope>provided</scope>
</dependency>
@ -220,7 +220,7 @@
<configuration>
<classifier>javadoc</classifier>
<links>
<!-- <link>https://javadoc.io/doc/org.jetbrains/annotations/</link>-->
<link>https://javadoc.io/doc/org.jetbrains/annotations/</link>
<link>https://hub.spigotmc.org/javadocs/bukkit/</link>
<link>https://carmjos.github.io/EasyPlugin/</link>
<link>https://carmjos.github.io/MineConfiguration/</link>

View File

@ -13,19 +13,20 @@ public class AdminCommand extends CommandHandler {
public AdminCommand(@NotNull JavaPlugin plugin) {
super(plugin);
registerSubCommand(new ListCommand("list", "l"));
registerSubCommand(new ReloadCommand("reload"));
registerSubCommand(new ListCommand(this, "list", "l"));
registerSubCommand(new ReloadCommand(this, "reload"));
}
@Override
public void noArgs(CommandSender sender) {
help(sender);
public Void noArgs(CommandSender sender) {
return help(sender);
}
@Override
public void noPermission(CommandSender sender) {
public Void noPermission(CommandSender sender) {
PluginMessages.COMMAND_USAGE.NO_PERM.send(sender);
return null;
}
public static Void help(CommandSender sender) {

View File

@ -2,15 +2,17 @@ package cc.carm.plugin.userprefix.command.sub;
import cc.carm.lib.easyplugin.command.SubCommand;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.command.AdminCommand;
import cc.carm.plugin.userprefix.conf.PluginMessages;
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public class ListCommand extends SubCommand {
public class ListCommand extends SubCommand<AdminCommand> {
public ListCommand(String name, String... aliases) {
super(name, aliases);
public ListCommand(@NotNull AdminCommand parent, String name, String... aliases) {
super(parent, name, aliases);
}
@Override

View File

@ -2,17 +2,19 @@ package cc.carm.plugin.userprefix.command.sub;
import cc.carm.lib.easyplugin.command.SubCommand;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.command.AdminCommand;
import cc.carm.plugin.userprefix.conf.PluginMessages;
import cc.carm.plugin.userprefix.ui.PrefixSelectGUI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public class ReloadCommand extends SubCommand {
public class ReloadCommand extends SubCommand<AdminCommand> {
public ReloadCommand(String name, String... aliases) {
super(name, aliases);
public ReloadCommand(@NotNull AdminCommand parent, String name, String... aliases) {
super(parent, name, aliases);
}
@Override

View File

@ -4,6 +4,7 @@ import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
@ -154,6 +155,10 @@ public class PluginConfig extends ConfigurationRoot {
@HeaderComment({"默认前缀的内容,即用于显示的实际前缀"})
public static final ConfigValue<String> CONTENT = ConfiguredValue.of(String.class, "&r");
@HeaderComment({"选择默认前缀时执行的操作"})
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builder(String.class).fromString()
.defaults("[CONSOLE] " + "say %player_name% 选择了默认前缀")
.build();
@HeaderComment({"默认前缀的显示物品"})
public static final class ITEM {

View File

@ -1,5 +1,6 @@
package cc.carm.plugin.userprefix.conf.prefix;
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.plugin.userprefix.manager.ServiceManager;
@ -8,6 +9,8 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class PrefixConfig {
protected final @NotNull String identifier;
@ -19,12 +22,15 @@ public class PrefixConfig {
protected final @Nullable String permission;
protected final @NotNull List<GUIActionConfiguration> actions;
protected final @NotNull ItemConfig itemHasPermission;
protected final @Nullable ItemConfig itemNoPermission;
protected final @Nullable ItemConfig itemWhenUsing;
public PrefixConfig(@NotNull String identifier, @NotNull String name,
@NotNull String content, int weight, @Nullable String permission,
@NotNull List<GUIActionConfiguration> actions,
@NotNull ItemConfig itemHasPermission,
@Nullable ItemConfig itemWhenUsing,
@Nullable ItemConfig itemNoPermission) {
@ -33,6 +39,7 @@ public class PrefixConfig {
this.content = content;
this.weight = weight;
this.permission = permission;
this.actions = actions;
this.itemHasPermission = itemHasPermission;
this.itemNoPermission = itemNoPermission;
this.itemWhenUsing = itemWhenUsing;
@ -83,6 +90,10 @@ public class PrefixConfig {
return getPermission() == null;
}
public void executeActions(@NotNull Player player) {
this.actions.forEach(action -> action.executeAction(player));
}
public boolean isVisible(Player player) {
return this.itemWhenUsing != null || checkPermission(player);
}

View File

@ -1,5 +1,6 @@
package cc.carm.plugin.userprefix.manager;
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftSectionWrapper;
import cc.carm.plugin.userprefix.Main;
@ -57,7 +58,7 @@ public class PrefixManager {
if (files.size() > 0) {
for (File file : files) {
try {
PrefixConfig prefix = adPrefix(file);
PrefixConfig prefix = addPrefix(file);
Main.debugging("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
loaded.put(prefix.getIdentifier(), prefix);
} catch (Exception ex) {
@ -78,6 +79,7 @@ public class PrefixManager {
PluginConfig.DEFAULT_PREFIX.CONTENT.getNotNull(),
PluginConfig.DEFAULT_PREFIX.WEIGHT.getNotNull(),
null,
readActions(PluginConfig.DEFAULT_PREFIX.ACTIONS.get()),
PluginConfig.DEFAULT_PREFIX.ITEM.NOT_USING.getNotNull(),
PluginConfig.DEFAULT_PREFIX.ITEM.USING.get(),
null
@ -122,10 +124,11 @@ public class PrefixManager {
}
}
public static @NotNull PrefixConfig adPrefix(@NotNull File file) throws Exception {
public static @NotNull PrefixConfig addPrefix(@NotNull File file) throws Exception {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
String identifier = configuration.getString("identifier");
if (identifier == null) throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的唯一标识。");
if (identifier == null)
throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的唯一标识。");
String name = configuration.getString("name");
if (name == null) throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的显示名称。");
@ -135,6 +138,7 @@ public class PrefixManager {
configuration.getString("content", "&r"),
configuration.getInt("weight", 1),
configuration.getString("permission"),
readActions(configuration.getStringList("actions")),
readItem(
configuration.getConfigurationSection("item.has-perm"),
new ItemConfig(Material.STONE, name, Arrays.asList(" ", "§a➥ 点击切换到该前缀"))
@ -151,5 +155,9 @@ public class PrefixManager {
else return ItemConfig.deserialize(CraftSectionWrapper.of(section));
}
protected static List<GUIActionConfiguration> readActions(@NotNull List<String> strings) {
return strings.stream().map(GUIActionConfiguration::deserialize).filter(Objects::nonNull).collect(Collectors.toList());
}
}

View File

@ -141,9 +141,7 @@ public class UserManager {
}
UserPrefixChangeEvent.call(player, currentPrefix, newPrefix, (after) -> {
if (after != null) {
setPrefix(player, after, updateView);
}
if (after != null) setPrefix(player, after, updateView);
checkingPlayers.remove(player.getUniqueId());
});
@ -183,6 +181,7 @@ public class UserManager {
*/
public void setPrefix(Player player, PrefixConfig prefix, boolean updateView) {
setPrefixData(player, prefix.getIdentifier());
prefix.executeActions(player);
if (updateView) updatePrefixView(player, false);
}

View File

@ -7,6 +7,7 @@ import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.conf.PluginConfig;
import cc.carm.plugin.userprefix.conf.PluginMessages;
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
import cc.carm.plugin.userprefix.event.UserPrefixChangeEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
@ -51,15 +52,22 @@ public class PrefixSelectGUI extends AutoPagedGUI {
addItem(new GUIItem(prefix.getItemWhenUsing(player) != null ? prefix.getItemWhenUsing(player) : prefix.getItemHasPermission(player)));
} else if (prefix.checkPermission(player)) {
addItem(new GUIItem(prefix.getItemHasPermission(player)) {
@Override
public void onClick(ClickType type) {
public void onClick(Player clicker, ClickType type) {
player.closeInventory();
//再次检查防止打开GUI后选择前的时间段内权限消失
if (prefix.checkPermission(player)) {
UserPrefixAPI.getUserManager().setPrefix(player, prefix, true);
// 发送消息与提示
PluginConfig.SOUNDS.PREFIX_CHANGE.playTo(player);
PluginMessages.SELECTED.send(player, prefix.getName());
UserPrefixChangeEvent.call(player, usingPrefix, prefix, config -> {
if (config == null) return;
UserPrefixAPI.getUserManager().setPrefix(player, config, true);
});
} else {
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
}
@ -68,7 +76,7 @@ public class PrefixSelectGUI extends AutoPagedGUI {
} else {
addItem(new GUIItem(prefix.getItemNoPermission(player)) {
@Override
public void onClick(ClickType type) {
public void onClick(Player clicker, ClickType type) {
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
}
});

View File

@ -22,6 +22,11 @@ weight: 1
# (because it is impossible to display items without permission at all)
permission: "yc.vip"
# Actions when select [Unnecessary]
# Please check https://github.com/CarmJos/UserPrefix/tree/master/.documentation/ACTIONS.md
actions:
- "[CONSOLE] say %player_name% selected PRO "
item:
# itemHasPermission [Necessary]
# This Item will be displayed when player has permission

View File

@ -22,6 +22,12 @@ weight: 1
# 如果没有就是人人都能用也代表不用配置“itemNoPermission”了(因为压根不可能显示没权限时候的物品)
permission: "yc.pro"
# 选择这个前缀时执行的操作 [非必须]
# 用于在玩家选择/取消选择前缀时执行相应动作,以便于实现一些特殊的功能。
# 具体操作类型详见: https://github.com/CarmJos/UserPrefix/tree/master/.documentation/ACTIONS.md
actions:
- "[CONSOLE] say %player_name% 选择了 Pro会员前缀 "
# 该前缀的GUI物品配置
item: