forked from PyvesB/advanced-achievements
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractCommand.java
More file actions
66 lines (55 loc) · 2.19 KB
/
AbstractCommand.java
File metadata and controls
66 lines (55 loc) · 2.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.hm.achievement.command.executable;
import com.hm.achievement.config.PluginHeader;
import com.hm.achievement.lifecycle.Reloadable;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jspecify.annotations.NonNull;
/**
* Abstract class in charge of factoring out common functionality for commands.
*
* @author Pyves
*/
public abstract class AbstractCommand implements Reloadable {
final YamlConfiguration mainConfig;
final YamlConfiguration langConfig;
final PluginHeader pluginHeader;
private String langNoPermissions;
AbstractCommand(YamlConfiguration mainConfig, YamlConfiguration langConfig, PluginHeader pluginHeader) {
this.mainConfig = mainConfig;
this.langConfig = langConfig;
this.pluginHeader = pluginHeader;
}
@Override
public void extractConfigurationParameters() {
langNoPermissions = pluginHeader.toString() + langConfig.getString("no-permissions");
}
/**
* Executes the command issued by the sender if he has the relevant permissions. If permission null, skip check.
*
* @param sender
* @param args
*/
public void execute(CommandSender sender, String[] args) {
String permission = getClass().getAnnotation(CommandSpec.class).permission();
if (!permission.isEmpty() && !sender.hasPermission("achievement." + permission)) {
sender.sendMessage(langNoPermissions);
return;
}
onExecute(sender, args);
}
/**
* Executes behaviour specific to the implementing command.
*
* @param sender
* @param args
*/
abstract void onExecute(CommandSender sender, String[] args);
protected Component replace(@NonNull Component component, String placeholder, String value) {
return component.replaceText(b -> b.matchLiteral(placeholder).replacement(value));
}
protected Component replace(@NonNull Component component, String @NonNull [] placeholders, String[] values) {
for (int i = 0; i < placeholders.length; i++) component = replace(component, placeholders[i], values[i]);
return component;
}
}