forked from PyvesB/advanced-achievements
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractParsableCommand.java
More file actions
81 lines (72 loc) · 2.85 KB
/
AbstractParsableCommand.java
File metadata and controls
81 lines (72 loc) · 2.85 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.hm.achievement.command.executable;
import com.hm.achievement.command.external.CommandUtils;
import com.hm.achievement.config.PluginHeader;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Abstract class in charge of factoring out common functionality for commands with more than one argument (/aach give,
* delete and check).
*
* @author Pyves
*/
public abstract class AbstractParsableCommand extends AbstractCommand {
private String langPlayerOffline;
private String langEntityNotPlayer;
AbstractParsableCommand(YamlConfiguration mainConfig, YamlConfiguration langConfig, PluginHeader pluginHeader) {
super(mainConfig, langConfig, pluginHeader);
}
@Override
public void extractConfigurationParameters() {
super.extractConfigurationParameters();
langPlayerOffline = langConfig.getString("player-offline");
langEntityNotPlayer = langConfig.getString("not-a-player");
}
/**
* Executes actions specific to the class extending this abstract class.
*
* @param sender
* @param args
* @param player
*/
abstract void onExecuteForPlayer(CommandSender sender, String[] args, Player player);
@Override
void onExecute(CommandSender sender, String[] args) {
String searchedName = args[args.length - 1];
Entity[] entities = CommandUtils.getTargets(sender, searchedName);
if (entities == null) {
sender.sendMessage(pluginHeader + langEntityNotPlayer);
return;
}
for (Entity entity : entities) {
if (entity == null) {
sender.sendMessage(pluginHeader + StringUtils.replaceEach(langPlayerOffline, new String[]{"PLAYER"}, new String[]{searchedName}));
break;
}
if (entity instanceof Player) {
onExecuteForPlayer(sender, args, (Player) entity);
} else {
sender.sendMessage(pluginHeader + StringUtils.replaceEach(langEntityNotPlayer, new String[]{"ENTITY"}, new String[]{entity.getName()}));
}
}
}
/**
* Extracts the name of the achievement from the command line arguments.
*
* @param args
* @return the achievement name
*/
String parseAchievementName(String[] args) {
StringBuilder achievementName = new StringBuilder();
// Rebuild name of achievement by concatenating elements in the string array. The name of the player is last.
for (int i = 1; i < args.length - 1; i++) {
achievementName.append(args[i]);
if (i != args.length - 2) {
achievementName.append(' ');
}
}
return achievementName.toString();
}
}