Skip to content

Commit e3c911d

Browse files
authored
feat(actions): 支持切换前缀时执行自定义操作 (CarmJos#32)
* feat(actions): 为单个前缀的选择添加可配置的操作 * feat(actions): 实现在前缀选择时执行操作
1 parent dc14b0c commit e3c911d

File tree

13 files changed

+102
-23
lines changed

13 files changed

+102
-23
lines changed

Diff for: .documentation/ACTIONS.md

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

Diff for: .documentation/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
- 使用示例
1515
- [前缀配置文件预设示例](../src/main/resources/prefixes/example-prefix.yml)
16+
- [操作(Actions)配置](ACTIONS.md)
1617

1718
## [开发文档](JAVADOC-README.md)
1819

Diff for: pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
1515

16-
<easyplugin.version>1.4.13</easyplugin.version>
16+
<easyplugin.version>1.4.14</easyplugin.version>
1717
<mineconfig.version>2.0.2</mineconfig.version>
1818
</properties>
1919

@@ -162,7 +162,7 @@
162162
<dependency>
163163
<groupId>net.luckperms</groupId>
164164
<artifactId>api</artifactId>
165-
<version>5.3</version>
165+
<version>5.4</version>
166166
<scope>provided</scope>
167167
</dependency>
168168

@@ -220,7 +220,7 @@
220220
<configuration>
221221
<classifier>javadoc</classifier>
222222
<links>
223-
<!-- <link>https://javadoc.io/doc/org.jetbrains/annotations/</link>-->
223+
<link>https://javadoc.io/doc/org.jetbrains/annotations/</link>
224224
<link>https://hub.spigotmc.org/javadocs/bukkit/</link>
225225
<link>https://carmjos.github.io/EasyPlugin/</link>
226226
<link>https://carmjos.github.io/MineConfiguration/</link>

Diff for: src/main/java/cc/carm/plugin/userprefix/command/AdminCommand.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ public class AdminCommand extends CommandHandler {
1313

1414
public AdminCommand(@NotNull JavaPlugin plugin) {
1515
super(plugin);
16-
registerSubCommand(new ListCommand("list", "l"));
17-
registerSubCommand(new ReloadCommand("reload"));
16+
registerSubCommand(new ListCommand(this, "list", "l"));
17+
registerSubCommand(new ReloadCommand(this, "reload"));
1818

1919
}
2020

2121
@Override
22-
public void noArgs(CommandSender sender) {
23-
help(sender);
22+
public Void noArgs(CommandSender sender) {
23+
return help(sender);
2424
}
2525

2626
@Override
27-
public void noPermission(CommandSender sender) {
27+
public Void noPermission(CommandSender sender) {
2828
PluginMessages.COMMAND_USAGE.NO_PERM.send(sender);
29+
return null;
2930
}
3031

3132
public static Void help(CommandSender sender) {

Diff for: src/main/java/cc/carm/plugin/userprefix/command/sub/ListCommand.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import cc.carm.lib.easyplugin.command.SubCommand;
44
import cc.carm.plugin.userprefix.UserPrefixAPI;
5+
import cc.carm.plugin.userprefix.command.AdminCommand;
56
import cc.carm.plugin.userprefix.conf.PluginMessages;
67
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
78
import org.bukkit.command.CommandSender;
89
import org.bukkit.plugin.java.JavaPlugin;
10+
import org.jetbrains.annotations.NotNull;
911

10-
public class ListCommand extends SubCommand {
12+
public class ListCommand extends SubCommand<AdminCommand> {
1113

12-
public ListCommand(String name, String... aliases) {
13-
super(name, aliases);
14+
public ListCommand(@NotNull AdminCommand parent, String name, String... aliases) {
15+
super(parent, name, aliases);
1416
}
1517

1618
@Override

Diff for: src/main/java/cc/carm/plugin/userprefix/command/sub/ReloadCommand.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
import cc.carm.lib.easyplugin.command.SubCommand;
44
import cc.carm.plugin.userprefix.UserPrefixAPI;
5+
import cc.carm.plugin.userprefix.command.AdminCommand;
56
import cc.carm.plugin.userprefix.conf.PluginMessages;
67
import cc.carm.plugin.userprefix.ui.PrefixSelectGUI;
78
import org.bukkit.Bukkit;
89
import org.bukkit.command.CommandSender;
910
import org.bukkit.entity.Player;
1011
import org.bukkit.plugin.java.JavaPlugin;
12+
import org.jetbrains.annotations.NotNull;
1113

12-
public class ReloadCommand extends SubCommand {
14+
public class ReloadCommand extends SubCommand<AdminCommand> {
1315

14-
public ReloadCommand(String name, String... aliases) {
15-
super(name, aliases);
16+
public ReloadCommand(@NotNull AdminCommand parent, String name, String... aliases) {
17+
super(parent, name, aliases);
1618
}
1719

1820
@Override

Diff for: src/main/java/cc/carm/plugin/userprefix/conf/PluginConfig.java

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cc.carm.lib.configuration.core.annotation.ConfigPath;
55
import cc.carm.lib.configuration.core.annotation.HeaderComment;
66
import cc.carm.lib.configuration.core.value.ConfigValue;
7+
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
78
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
89
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
910
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
@@ -154,6 +155,10 @@ public static final class DEFAULT_PREFIX {
154155
@HeaderComment({"默认前缀的内容,即用于显示的实际前缀"})
155156
public static final ConfigValue<String> CONTENT = ConfiguredValue.of(String.class, "&r");
156157

158+
@HeaderComment({"选择默认前缀时执行的操作"})
159+
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builder(String.class).fromString()
160+
.defaults("[CONSOLE] " + "say %player_name% 选择了默认前缀")
161+
.build();
157162
@HeaderComment({"默认前缀的显示物品"})
158163
public static final class ITEM {
159164

Diff for: src/main/java/cc/carm/plugin/userprefix/conf/prefix/PrefixConfig.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.carm.plugin.userprefix.conf.prefix;
22

3+
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
34
import cc.carm.lib.easyplugin.utils.ColorParser;
45
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
56
import cc.carm.plugin.userprefix.manager.ServiceManager;
@@ -8,6 +9,8 @@
89
import org.jetbrains.annotations.NotNull;
910
import org.jetbrains.annotations.Nullable;
1011

12+
import java.util.List;
13+
1114
public class PrefixConfig {
1215

1316
protected final @NotNull String identifier;
@@ -19,12 +22,15 @@ public class PrefixConfig {
1922

2023
protected final @Nullable String permission;
2124

25+
protected final @NotNull List<GUIActionConfiguration> actions;
26+
2227
protected final @NotNull ItemConfig itemHasPermission;
2328
protected final @Nullable ItemConfig itemNoPermission;
2429
protected final @Nullable ItemConfig itemWhenUsing;
2530

2631
public PrefixConfig(@NotNull String identifier, @NotNull String name,
2732
@NotNull String content, int weight, @Nullable String permission,
33+
@NotNull List<GUIActionConfiguration> actions,
2834
@NotNull ItemConfig itemHasPermission,
2935
@Nullable ItemConfig itemWhenUsing,
3036
@Nullable ItemConfig itemNoPermission) {
@@ -33,6 +39,7 @@ public PrefixConfig(@NotNull String identifier, @NotNull String name,
3339
this.content = content;
3440
this.weight = weight;
3541
this.permission = permission;
42+
this.actions = actions;
3643
this.itemHasPermission = itemHasPermission;
3744
this.itemNoPermission = itemNoPermission;
3845
this.itemWhenUsing = itemWhenUsing;
@@ -83,6 +90,10 @@ public boolean isPublic() {
8390
return getPermission() == null;
8491
}
8592

93+
public void executeActions(@NotNull Player player) {
94+
this.actions.forEach(action -> action.executeAction(player));
95+
}
96+
8697
public boolean isVisible(Player player) {
8798
return this.itemWhenUsing != null || checkPermission(player);
8899
}

Diff for: src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.carm.plugin.userprefix.manager;
22

3+
import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
34
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
45
import cc.carm.lib.mineconfiguration.bukkit.source.CraftSectionWrapper;
56
import cc.carm.plugin.userprefix.Main;
@@ -57,7 +58,7 @@ public void loadConfiguredPrefixes() {
5758
if (files.size() > 0) {
5859
for (File file : files) {
5960
try {
60-
PrefixConfig prefix = adPrefix(file);
61+
PrefixConfig prefix = addPrefix(file);
6162
Main.debugging("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
6263
loaded.put(prefix.getIdentifier(), prefix);
6364
} catch (Exception ex) {
@@ -78,6 +79,7 @@ public void loadDefaultPrefix() {
7879
PluginConfig.DEFAULT_PREFIX.CONTENT.getNotNull(),
7980
PluginConfig.DEFAULT_PREFIX.WEIGHT.getNotNull(),
8081
null,
82+
readActions(PluginConfig.DEFAULT_PREFIX.ACTIONS.get()),
8183
PluginConfig.DEFAULT_PREFIX.ITEM.NOT_USING.getNotNull(),
8284
PluginConfig.DEFAULT_PREFIX.ITEM.USING.get(),
8385
null
@@ -122,10 +124,11 @@ protected File getStorageFolder() {
122124
}
123125
}
124126

125-
public static @NotNull PrefixConfig adPrefix(@NotNull File file) throws Exception {
127+
public static @NotNull PrefixConfig addPrefix(@NotNull File file) throws Exception {
126128
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
127129
String identifier = configuration.getString("identifier");
128-
if (identifier == null) throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的唯一标识。");
130+
if (identifier == null)
131+
throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的唯一标识。");
129132

130133
String name = configuration.getString("name");
131134
if (name == null) throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的显示名称。");
@@ -135,6 +138,7 @@ protected File getStorageFolder() {
135138
configuration.getString("content", "&r"),
136139
configuration.getInt("weight", 1),
137140
configuration.getString("permission"),
141+
readActions(configuration.getStringList("actions")),
138142
readItem(
139143
configuration.getConfigurationSection("item.has-perm"),
140144
new ItemConfig(Material.STONE, name, Arrays.asList(" ", "§a➥ 点击切换到该前缀"))
@@ -151,5 +155,9 @@ protected static ItemConfig readItem(@Nullable ConfigurationSection section, @Nu
151155
else return ItemConfig.deserialize(CraftSectionWrapper.of(section));
152156
}
153157

158+
protected static List<GUIActionConfiguration> readActions(@NotNull List<String> strings) {
159+
return strings.stream().map(GUIActionConfiguration::deserialize).filter(Objects::nonNull).collect(Collectors.toList());
160+
}
161+
154162

155163
}

Diff for: src/main/java/cc/carm/plugin/userprefix/manager/UserManager.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ public void checkPrefix(Player player, boolean updateView) {
141141
}
142142

143143
UserPrefixChangeEvent.call(player, currentPrefix, newPrefix, (after) -> {
144-
if (after != null) {
145-
setPrefix(player, after, updateView);
146-
}
144+
if (after != null) setPrefix(player, after, updateView);
147145
checkingPlayers.remove(player.getUniqueId());
148146
});
149147

@@ -183,6 +181,7 @@ public PrefixConfig getPrefix(Player player) {
183181
*/
184182
public void setPrefix(Player player, PrefixConfig prefix, boolean updateView) {
185183
setPrefixData(player, prefix.getIdentifier());
184+
prefix.executeActions(player);
186185
if (updateView) updatePrefixView(player, false);
187186
}
188187

Diff for: src/main/java/cc/carm/plugin/userprefix/ui/PrefixSelectGUI.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import cc.carm.plugin.userprefix.conf.PluginConfig;
88
import cc.carm.plugin.userprefix.conf.PluginMessages;
99
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
10+
import cc.carm.plugin.userprefix.event.UserPrefixChangeEvent;
1011
import org.bukkit.entity.Player;
1112
import org.bukkit.event.inventory.ClickType;
1213

@@ -51,15 +52,22 @@ public void loadItems() {
5152
addItem(new GUIItem(prefix.getItemWhenUsing(player) != null ? prefix.getItemWhenUsing(player) : prefix.getItemHasPermission(player)));
5253
} else if (prefix.checkPermission(player)) {
5354
addItem(new GUIItem(prefix.getItemHasPermission(player)) {
55+
5456
@Override
55-
public void onClick(ClickType type) {
57+
public void onClick(Player clicker, ClickType type) {
5658
player.closeInventory();
5759
//再次检查,防止打开GUI后、选择前的时间段内权限消失
5860
if (prefix.checkPermission(player)) {
59-
UserPrefixAPI.getUserManager().setPrefix(player, prefix, true);
6061

62+
// 发送消息与提示
6163
PluginConfig.SOUNDS.PREFIX_CHANGE.playTo(player);
6264
PluginMessages.SELECTED.send(player, prefix.getName());
65+
66+
UserPrefixChangeEvent.call(player, usingPrefix, prefix, config -> {
67+
if (config == null) return;
68+
UserPrefixAPI.getUserManager().setPrefix(player, config, true);
69+
});
70+
6371
} else {
6472
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
6573
}
@@ -68,7 +76,7 @@ public void onClick(ClickType type) {
6876
} else {
6977
addItem(new GUIItem(prefix.getItemNoPermission(player)) {
7078
@Override
71-
public void onClick(ClickType type) {
79+
public void onClick(Player clicker, ClickType type) {
7280
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
7381
}
7482
});

Diff for: src/main/resources/en_US/example-prefix.yml

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ weight: 1
2222
# (because it is impossible to display items without permission at all)
2323
permission: "yc.vip"
2424

25+
# Actions when select [Unnecessary]
26+
# Please check https://github.com/CarmJos/UserPrefix/tree/master/.documentation/ACTIONS.md
27+
actions:
28+
- "[CONSOLE] say %player_name% selected PRO !"
29+
2530
item:
2631
# itemHasPermission [Necessary]
2732
# This Item will be displayed when player has permission

Diff for: src/main/resources/prefixes/example-prefix.yml

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ weight: 1
2222
# 如果没有就是人人都能用,也代表不用配置“itemNoPermission”了(因为压根不可能显示没权限时候的物品)
2323
permission: "yc.pro"
2424

25+
# 选择这个前缀时执行的操作 [非必须]
26+
# 用于在玩家选择/取消选择前缀时执行相应动作,以便于实现一些特殊的功能。
27+
# 具体操作类型详见: https://github.com/CarmJos/UserPrefix/tree/master/.documentation/ACTIONS.md
28+
actions:
29+
- "[CONSOLE] say %player_name% 选择了 Pro会员前缀 !"
30+
2531
# 该前缀的GUI物品配置
2632
item:
2733

0 commit comments

Comments
 (0)