-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceedPlusCommandHandler.java
284 lines (254 loc) · 11.2 KB
/
ExceedPlusCommandHandler.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package net.rhythmcore.exceedplus;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
/**
* Merged command handler with all original logic plus new disenchant/clean commands and full template sets.
*/
public class ExceedPlusCommandHandler implements CommandExecutor, TabCompleter {
private final ExceedPlus plugin;
public ExceedPlusCommandHandler(ExceedPlus plugin) {
this.plugin = plugin;
}
// Combine standard and special templates in TEMPLATE_IDS:
// Standard templates:
// grind_template (700000)
// frame_template (700001)
// exceed_template (700002)
// special templates (700010 onwards), as listed previously:
private static final Map<String, Integer> TEMPLATE_IDS = Map.ofEntries(
Map.entry("grind_template", 700000),
Map.entry("frame_template", 700001),
Map.entry("exceed_template", 700002),
Map.entry("template_sword_a", 700010),
Map.entry("template_sword_b", 700011),
Map.entry("template_axe_a", 700012),
Map.entry("template_axe_b", 700013),
Map.entry("template_pickaxe_a", 700014),
Map.entry("template_pickaxe_b", 700015),
Map.entry("template_shovel_a", 700016),
Map.entry("template_shovel_b", 700017),
Map.entry("template_hoe_a", 700018),
Map.entry("template_hoe_b", 700019),
Map.entry("template_bow_a", 700020),
Map.entry("template_bow_b", 700021),
Map.entry("template_crossbow_a", 700022),
Map.entry("template_crossbow_b", 700023),
Map.entry("template_trident_a", 700024),
Map.entry("template_trident_b", 700025),
Map.entry("template_mace_a", 700026),
Map.entry("template_mace_b", 700027)
);
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// If no args, show usage
if (args.length < 1) {
sender.sendMessage("Usage: /exceed <help|reload|give|upgrade|disenchant|clean>");
return true;
}
// help command
if (args[0].equalsIgnoreCase("help")) {
sender.sendMessage("Available commands:\n" +
"/exceed help - Show this help menu\n" +
"/exceed reload - Reloads the plugin configuration\n" +
"/exceed give <player> <template> [count] - Gives templates\n" +
"/exceed upgrade [count|max] - Upgrades held item\n" +
"/exceed disenchant - Remove all enchantments from held item\n" +
"/exceed clean - Reset held item to vanilla state"
);
return true;
}
// reload command
if (args[0].equalsIgnoreCase("reload")) {
plugin.reloadConfig();
sender.sendMessage("ExceedPlus configuration reloaded.");
return true;
}
// give command
if (args[0].equalsIgnoreCase("give") && args.length >= 3) {
String targetName = args[1];
String templateName = args[2].toLowerCase();
int count = 1;
if (args.length > 3) {
try {
count = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage("Invalid count. Must be a number.");
return true;
}
}
Player target = Bukkit.getPlayerExact(targetName);
if (target == null) {
sender.sendMessage("Player not found.");
return true;
}
if (templateName.equals("template_set")) {
giveTemplateSet(target, count);
sender.sendMessage("Gave " + count + " template set(s) to " + target.getName());
return true;
}
if (TEMPLATE_IDS.containsKey(templateName)) {
giveTemplate(target, templateName, count);
sender.sendMessage("Gave " + count + " " + templateName + " to " + target.getName());
return true;
} else {
sender.sendMessage("Invalid template name.");
return true;
}
}
// upgrade command
if (args[0].equalsIgnoreCase("upgrade")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
ItemStack handItem = player.getInventory().getItemInMainHand();
if (handItem == null || handItem.getType() == Material.AIR) {
player.sendMessage("You must hold an upgradable item in your main hand.");
return true;
}
int currentLevel = ExceedPlusUtil.getItemLevel(plugin, handItem);
if (currentLevel >= 10) {
player.sendMessage("Item is already at max level.");
return true;
}
int levelsToAdd = 1;
if (args.length > 1) {
if (args[1].equalsIgnoreCase("max")) {
levelsToAdd = 10 - currentLevel;
} else {
try {
levelsToAdd = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
player.sendMessage("Invalid number. Use a number 1-9 or 'max'.");
return true;
}
if (levelsToAdd < 1) {
player.sendMessage("Upgrade count must be >= 1.");
return true;
}
}
}
int newLevel = Math.min(currentLevel + levelsToAdd, 10);
ExceedPlusUtil.upgradeItem(plugin, handItem, newLevel);
player.getInventory().setItemInMainHand(handItem);
player.sendMessage("Item upgraded to level " + newLevel + ".");
return true;
}
// disenchant command
if (args[0].equalsIgnoreCase("disenchant")) {
if (!(sender instanceof Player p)) {
sender.sendMessage("Players only.");
return true;
}
if (!p.hasPermission("exceedplus.disenchant")) {
p.sendMessage("No permission.");
return true;
}
ItemStack item = p.getInventory().getItemInMainHand();
if (item == null || item.getType() == Material.AIR) {
p.sendMessage("Hold an item to disenchant.");
return true;
}
ItemStack cleaned = ExceedPlusItemUtil.disenchantItem(item);
p.getInventory().setItemInMainHand(cleaned);
p.sendMessage("Item disenchanted.");
return true;
}
// clean command
if (args[0].equalsIgnoreCase("clean")) {
if (!(sender instanceof Player p)) {
sender.sendMessage("Players only.");
return true;
}
if (!p.hasPermission("exceedplus.clean")) {
p.sendMessage("No permission.");
return true;
}
ItemStack item = p.getInventory().getItemInMainHand();
if (item == null || item.getType() == Material.AIR) {
p.sendMessage("Hold an item to clean.");
return true;
}
ItemStack vanilla = ExceedPlusItemUtil.cleanItem(item);
p.getInventory().setItemInMainHand(vanilla);
p.sendMessage("Item reset to vanilla state.");
return true;
}
sender.sendMessage("Invalid subcommand. Use /exceed help for a list of commands.");
return true;
}
private void giveTemplate(Player player, String templateName, int count) {
int customModelData = TEMPLATE_IDS.get(templateName);
String loreKey = templateName + "_lore";
ItemStack template = new ItemStack(Material.STRUCTURE_BLOCK, count);
ItemMeta meta = template.getItemMeta();
if (meta != null) {
// Check if template is special or normal:
// Normal templates stored under "templates.<loreKey>"
// Special templates stored under "specialtemplates.<templateName>"
String configPath;
if (templateName.startsWith("template_") && templateName.contains("_a") || templateName.contains("_b")) {
// It's a special template
configPath = "specialtemplates." + templateName;
} else {
// It's a normal template
configPath = "templates." + loreKey;
}
String configName = plugin.getConfig().getString(configPath + ".name", templateName.replace("_", " ").toUpperCase());
List<String> configLore = plugin.getConfig().getStringList(configPath + ".lore");
configName = ColorUtil.translate(configName);
List<String> translatedLore = new ArrayList<>();
for (String line : configLore) {
translatedLore.add(ColorUtil.translate(line));
}
meta.setDisplayName(configName);
meta.setCustomModelData(customModelData);
if (!translatedLore.isEmpty()) {
meta.setLore(translatedLore);
}
template.setItemMeta(meta);
}
player.getInventory().addItem(template);
}
private void giveTemplateSet(Player player, int count) {
// The template set presumably includes the three main templates:
giveTemplate(player, "grind_template", 6 * count);
giveTemplate(player, "frame_template", 3 * count);
giveTemplate(player, "exceed_template", count);
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
completions.add("help");
completions.add("reload");
completions.add("give");
completions.add("upgrade");
completions.add("disenchant");
completions.add("clean");
} else if (args[0].equalsIgnoreCase("give")) {
if (args.length == 2) {
for (Player player : Bukkit.getOnlinePlayers()) {
completions.add(player.getName());
}
} else if (args.length == 3) {
// templates
completions.add("template_set");
completions.addAll(TEMPLATE_IDS.keySet());
} else if (args.length == 4) {
completions.add("<count>");
}
} else if (args[0].equalsIgnoreCase("upgrade") && args.length == 2) {
completions.add("max");
for (int i=1; i<=9; i++) {
completions.add(String.valueOf(i));
}
}
return completions;
}
}