Skip to content

Commit 9bd0e1f

Browse files
committed
Updated: Convert /attribute to parent command
This also adds autocomplete to /attribute remove, but it's pretty unhelpful as we're still generating randomised attribute keys.
1 parent 6bf8c07 commit 9bd0e1f

File tree

5 files changed

+309
-158
lines changed

5 files changed

+309
-158
lines changed

modules/RoyalCommands/src/main/java/org/royaldev/royalcommands/rcommands/CmdAttributes.java

Lines changed: 12 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -5,185 +5,39 @@
55
*/
66
package org.royaldev.royalcommands.rcommands;
77

8-
import org.bukkit.attribute.Attribute;
9-
10-
import java.util.ArrayList;
11-
import java.util.Arrays;
12-
import java.util.List;
13-
import java.util.UUID;
14-
15-
import org.bukkit.Material;
16-
import org.bukkit.NamespacedKey;
17-
import org.bukkit.Registry;
18-
import org.bukkit.attribute.AttributeModifier;
198
import org.bukkit.command.Command;
209
import org.bukkit.command.CommandSender;
2110
import org.bukkit.entity.Player;
22-
import org.bukkit.inventory.EquipmentSlotGroup;
2311
import org.bukkit.inventory.ItemStack;
24-
import org.bukkit.inventory.meta.ItemMeta;
2512
import org.royaldev.royalcommands.MessageColor;
2613
import org.royaldev.royalcommands.RUtils;
2714
import org.royaldev.royalcommands.RoyalCommands;
28-
29-
import com.google.common.collect.Multimap;
30-
31-
import net.md_5.bungee.api.chat.BaseComponent;
32-
import net.md_5.bungee.api.chat.ClickEvent;
33-
import net.md_5.bungee.api.chat.ComponentBuilder;
34-
import net.md_5.bungee.api.chat.HoverEvent;
35-
import net.md_5.bungee.api.chat.TranslatableComponent;
36-
import net.md_5.bungee.api.chat.hover.content.Text;
15+
import org.royaldev.royalcommands.rcommands.attributes.SCmdAdd;
16+
import org.royaldev.royalcommands.rcommands.attributes.SCmdClear;
17+
import org.royaldev.royalcommands.rcommands.attributes.SCmdList;
18+
import org.royaldev.royalcommands.rcommands.attributes.SCmdRemove;
3719

3820
@ReflectCommand
39-
public class CmdAttributes extends TabCommand {
21+
public class CmdAttributes extends ParentCommand {
4022

4123
public CmdAttributes(final RoyalCommands instance, final String name) {
42-
super(instance, name, true, new Short[]{CompletionType.LIST.getShort(), CompletionType.ATTRIBUTE.getShort(), CompletionType.CUSTOM.getShort()});
24+
super(instance, name, true);
25+
super.addSubCommand(new SCmdAdd(this.plugin, this));
26+
super.addSubCommand(new SCmdClear(this.plugin, this));
27+
super.addSubCommand(new SCmdList(this.plugin, this));
28+
super.addSubCommand(new SCmdRemove(this.plugin, this));
4329
}
4430

45-
@Override
46-
protected List<String> getCustomCompletions(final CommandSender cs, final Command cmd, final String label, final String[] args, final String arg) {
47-
ArrayList<String> endpoints = new ArrayList<>();
48-
for (AttributeModifier.Operation param : AttributeModifier.Operation.values()) {
49-
if (!param.name().toLowerCase().startsWith(arg.toLowerCase())) continue;
50-
endpoints.add(param.name().toLowerCase());
51-
}
52-
return endpoints;
53-
}
54-
55-
@Override
56-
protected List<String> customList(final CommandSender cs, final Command cmd, final String label, final String[] args, final String arg) {
57-
return new ArrayList<>(Arrays.asList("add", "remove", "clear", "list"));
58-
}
59-
60-
@Override
61-
protected boolean runCommand(CommandSender cs, Command cmd, String label, String[] eargs, CommandArguments ca) {
31+
public boolean validHand(CommandSender cs) {
6232
if (!(cs instanceof final Player p)) {
6333
cs.sendMessage(MessageColor.NEGATIVE + "This command is only available to players!");
64-
return true;
65-
}
66-
if (eargs.length < 1) {
67-
cs.sendMessage(cmd.getDescription());
6834
return false;
6935
}
7036
ItemStack hand = p.getInventory().getItemInMainHand();
7137
if (RUtils.isBlockAir(hand.getType())) {
7238
cs.sendMessage(MessageColor.NEGATIVE + "You cannot apply attributes to air!");
73-
return true;
39+
return false;
7440
}
75-
ItemMeta meta = hand.getItemMeta();
76-
Attribute ats = null;
77-
AttributeModifier.Operation o;
78-
NamespacedKey nsKey = null;
79-
double amount;
80-
81-
String subcommand = eargs[0];
82-
if (subcommand.equalsIgnoreCase("list")) {
83-
if (meta.hasAttributeModifiers()) {
84-
final BaseComponent[] bc = new ComponentBuilder("Item ")
85-
.color(MessageColor.POSITIVE.bc())
86-
.append(RUtils.getItemName(hand))
87-
.color(MessageColor.NEUTRAL.bc())
88-
.append(" has these Attribute Modifiers:")
89-
.color(MessageColor.POSITIVE.bc())
90-
.create();
91-
cs.spigot().sendMessage(bc);
92-
Multimap<Attribute, AttributeModifier> modifiers = meta.getAttributeModifiers();
93-
for (Attribute a : modifiers.keySet()) {
94-
String name = new TranslatableComponent(a.getTranslationKey()).toPlainText();
95-
for (AttributeModifier am : modifiers.get(a)) {
96-
String operation = RUtils.getFriendlyEnumName(am.getOperation());
97-
Text tt = new Text(
98-
MessageColor.POSITIVE + "Attribute: " + MessageColor.NEUTRAL + name + "\n" +
99-
MessageColor.POSITIVE + "Operation: " + MessageColor.NEUTRAL + operation + "\n" +
100-
MessageColor.POSITIVE + "Amount: " + MessageColor.NEUTRAL + am.getAmount() + "\n" +
101-
MessageColor.POSITIVE + "Key: " + MessageColor.NEUTRAL + am.getKey() + "\n" +
102-
MessageColor.NEUTRAL + "Click to remove");
103-
BaseComponent[] bca = new ComponentBuilder(" " + name + " " + operation + " " + am.getAmount())
104-
.color(MessageColor.NEUTRAL.bc())
105-
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, tt))
106-
.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/attr remove " + am.getKey().toString()))
107-
.create();
108-
cs.spigot().sendMessage(bca);
109-
}
110-
}
111-
} else {
112-
cs.sendMessage(MessageColor.NEGATIVE + "Item has no Attribute Modifiers applied!");
113-
}
114-
} else if (subcommand.equalsIgnoreCase("clear")) {
115-
if (meta.hasAttributeModifiers()) {
116-
cs.sendMessage(MessageColor.POSITIVE + "Clearing Attributes of " + MessageColor.NEUTRAL + hand.getType().name());
117-
for (Attribute a : meta.getAttributeModifiers().keys()) {
118-
meta.removeAttributeModifier(a);
119-
}
120-
hand.setItemMeta(meta);
121-
p.getInventory().setItemInMainHand(hand);
122-
} else {
123-
cs.sendMessage(MessageColor.NEGATIVE + "Item has no Attribute Modifiers applied!");
124-
}
125-
126-
} else if (subcommand.equalsIgnoreCase("remove")) {
127-
if (meta.hasAttributeModifiers()) {
128-
if (eargs.length == 2) {
129-
Multimap<Attribute, AttributeModifier> modifiers = meta.getAttributeModifiers();
130-
for (Attribute a : modifiers.keySet()) {
131-
nsKey = NamespacedKey.fromString(eargs[1]);
132-
for (AttributeModifier am : modifiers.get(a)) {
133-
if (am.getKey().equals(nsKey)) {
134-
meta.removeAttributeModifier(a, am);
135-
cs.sendMessage(MessageColor.POSITIVE + "Removed " + MessageColor.NEUTRAL + am.getKey().toString() + MessageColor.POSITIVE + " from " + MessageColor.NEUTRAL + RUtils.getItemName(hand));
136-
}
137-
}
138-
}
139-
hand.setItemMeta(meta);
140-
p.getInventory().setItemInMainHand(hand);
141-
} else {
142-
cs.sendMessage(MessageColor.NEGATIVE + "A UUID for a Attribute Modifier must be specified");
143-
}
144-
} else {
145-
cs.sendMessage(MessageColor.NEGATIVE + "Item has no Attribute Modifiers applied!");
146-
}
147-
148-
// } else if (subcommand.equalsIgnoreCase("modify")) { TODO think about this
149-
150-
} else if (subcommand.equalsIgnoreCase("add")) {
151-
try {
152-
ats = Registry.ATTRIBUTE.getOrThrow(NamespacedKey.fromString(eargs[1]));
153-
} catch (IllegalArgumentException e) {
154-
cs.sendMessage(MessageColor.NEGATIVE + "Enter a valid attribute type!");
155-
return true;
156-
}
157-
try {
158-
o = AttributeModifier.Operation.valueOf(eargs[2].toUpperCase());
159-
} catch (IllegalArgumentException e) {
160-
cs.sendMessage(MessageColor.NEGATIVE + "Enter a correct operation for the attribute modifier!");
161-
return true;
162-
}
163-
try {
164-
amount = Double.parseDouble(eargs[3]);
165-
} catch (NumberFormatException e) {
166-
cs.sendMessage(MessageColor.NEGATIVE + "Enter a valid modifier amount for the attribute!");
167-
return true;
168-
}
169-
if (eargs.length < 5) {
170-
try {
171-
// String rand = RandomStringUtils.randomAlphanumeric(12);
172-
String rand = UUID.randomUUID().toString().replaceAll("-", "");
173-
nsKey = NamespacedKey.fromString(rand.substring(0, (int) Math.floor(rand.length() / 2)));
174-
} catch (IllegalArgumentException e) {
175-
this.plugin.getLogger().warning("Failed to generate attribute key for " + p.getDisplayName());
176-
}
177-
} else {
178-
nsKey = NamespacedKey.fromString(eargs[4]);
179-
}
180-
if (ats != null && nsKey != null) {
181-
meta.addAttributeModifier(ats, new AttributeModifier(nsKey, amount, o, EquipmentSlotGroup.MAINHAND));
182-
hand.setItemMeta(meta);
183-
p.getInventory().setItemInMainHand(hand);
184-
cs.sendMessage(MessageColor.POSITIVE + "The attribute has been applied, with a key of "+ MessageColor.NEUTRAL + nsKey.toString());
185-
}
186-
} else cs.sendMessage(MessageColor.NEGATIVE + "No such subcommand!");
18741
return true;
18842
}
18943
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*/
6+
package org.royaldev.royalcommands.rcommands.attributes;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
import org.bukkit.NamespacedKey;
13+
import org.bukkit.Registry;
14+
import org.bukkit.attribute.Attribute;
15+
import org.bukkit.attribute.AttributeModifier;
16+
import org.bukkit.command.Command;
17+
import org.bukkit.command.CommandSender;
18+
import org.bukkit.entity.Player;
19+
import org.bukkit.inventory.EquipmentSlotGroup;
20+
import org.bukkit.inventory.ItemStack;
21+
import org.bukkit.inventory.meta.ItemMeta;
22+
import org.royaldev.royalcommands.MessageColor;
23+
import org.royaldev.royalcommands.RoyalCommands;
24+
import org.royaldev.royalcommands.rcommands.CmdAttributes;
25+
import org.royaldev.royalcommands.rcommands.SubCommand;
26+
import org.royaldev.royalcommands.rcommands.TabCommand;
27+
28+
public class SCmdAdd extends SubCommand<CmdAttributes> {
29+
30+
public SCmdAdd(final RoyalCommands instance, final CmdAttributes parent) {
31+
super(instance, parent, "add", true, "Add an attribute to the held item", "<command> (attribute) (operation) (amount)", new String[0], new Short[]{TabCommand.CompletionType.ATTRIBUTE.getShort(), CompletionType.LIST.getShort(), CompletionType.NONE.getShort()});
32+
}
33+
34+
@Override
35+
protected List<String> customList(final CommandSender cs, final Command cmd, final String label, final String[] args, final String arg) {
36+
ArrayList<String> endpoints = new ArrayList<>();
37+
for (AttributeModifier.Operation param : AttributeModifier.Operation.values()) {
38+
if (!param.name().toLowerCase().startsWith(arg.toLowerCase())) continue;
39+
endpoints.add(param.name().toLowerCase());
40+
}
41+
return endpoints;
42+
}
43+
44+
@Override
45+
public boolean runCommand(final CommandSender cs, final Command cmd, final String label, final String[] eargs, final CommandArguments ca) {
46+
if (!this.getParent().validHand(cs))
47+
return true;
48+
49+
final Player p = (Player) cs;
50+
ItemStack hand = p.getInventory().getItemInMainHand();
51+
ItemMeta meta = hand.getItemMeta();
52+
Attribute ats = null;
53+
AttributeModifier.Operation o;
54+
NamespacedKey nsKey = null;
55+
double amount;
56+
try {
57+
ats = Registry.ATTRIBUTE.getOrThrow(NamespacedKey.fromString(eargs[0]));
58+
} catch (IllegalArgumentException e) {
59+
cs.sendMessage(MessageColor.NEGATIVE + "Enter a valid attribute type!");
60+
return true;
61+
}
62+
try {
63+
o = AttributeModifier.Operation.valueOf(eargs[1].toUpperCase());
64+
} catch (IllegalArgumentException e) {
65+
cs.sendMessage(MessageColor.NEGATIVE + "Enter a correct operation for the attribute modifier!");
66+
return true;
67+
}
68+
try {
69+
amount = Double.parseDouble(eargs[2]);
70+
} catch (NumberFormatException e) {
71+
cs.sendMessage(MessageColor.NEGATIVE + "Enter a valid modifier amount for the attribute!");
72+
return true;
73+
}
74+
if (eargs.length < 4) {
75+
try {
76+
// String rand = RandomStringUtils.randomAlphanumeric(12);
77+
String rand = UUID.randomUUID().toString().replaceAll("-", "");
78+
nsKey = NamespacedKey.fromString(rand.substring(0, (int) Math.floor(rand.length() / 2)));
79+
} catch (IllegalArgumentException e) {
80+
this.plugin.getLogger().warning("Failed to generate attribute key for " + p.getDisplayName());
81+
}
82+
} else {
83+
nsKey = NamespacedKey.fromString(eargs[3]);
84+
}
85+
if (ats != null && nsKey != null) {
86+
meta.addAttributeModifier(ats, new AttributeModifier(nsKey, amount, o, EquipmentSlotGroup.MAINHAND));
87+
hand.setItemMeta(meta);
88+
p.getInventory().setItemInMainHand(hand);
89+
cs.sendMessage(MessageColor.POSITIVE + "The attribute has been applied, with a key of "+ MessageColor.NEUTRAL + nsKey.toString());
90+
}
91+
return true;
92+
}
93+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*/
6+
package org.royaldev.royalcommands.rcommands.attributes;
7+
8+
import org.bukkit.attribute.Attribute;
9+
import org.bukkit.command.Command;
10+
import org.bukkit.command.CommandSender;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.inventory.ItemStack;
13+
import org.bukkit.inventory.meta.ItemMeta;
14+
import org.royaldev.royalcommands.MessageColor;
15+
import org.royaldev.royalcommands.RUtils;
16+
import org.royaldev.royalcommands.RoyalCommands;
17+
import org.royaldev.royalcommands.rcommands.CmdAttributes;
18+
import org.royaldev.royalcommands.rcommands.SubCommand;
19+
20+
public class SCmdClear extends SubCommand<CmdAttributes> {
21+
22+
public SCmdClear(final RoyalCommands instance, final CmdAttributes parent) {
23+
super(instance, parent, "clear", true, "Clear all attributes attached to the held item", "<command>", new String[0], new Short[0]);
24+
}
25+
26+
@Override
27+
public boolean runCommand(final CommandSender cs, final Command cmd, final String label, final String[] eargs, final CommandArguments ca) {
28+
if (!this.getParent().validHand(cs))
29+
return true;
30+
31+
final Player p = (Player) cs;
32+
ItemStack hand = p.getInventory().getItemInMainHand();
33+
ItemMeta meta = hand.getItemMeta();
34+
if (meta.hasAttributeModifiers()) {
35+
cs.sendMessage(MessageColor.POSITIVE + "Clearing Attributes of " + MessageColor.NEUTRAL + RUtils.getItemName(hand));
36+
for (Attribute a : meta.getAttributeModifiers().keys()) {
37+
meta.removeAttributeModifier(a);
38+
}
39+
hand.setItemMeta(meta);
40+
p.getInventory().setItemInMainHand(hand);
41+
} else {
42+
cs.sendMessage(MessageColor.NEGATIVE + "Item has no Attribute Modifiers applied!");
43+
}
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)