|
5 | 5 | */ |
6 | 6 | package org.royaldev.royalcommands.rcommands; |
7 | 7 |
|
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; |
19 | 8 | import org.bukkit.command.Command; |
20 | 9 | import org.bukkit.command.CommandSender; |
21 | 10 | import org.bukkit.entity.Player; |
22 | | -import org.bukkit.inventory.EquipmentSlotGroup; |
23 | 11 | import org.bukkit.inventory.ItemStack; |
24 | | -import org.bukkit.inventory.meta.ItemMeta; |
25 | 12 | import org.royaldev.royalcommands.MessageColor; |
26 | 13 | import org.royaldev.royalcommands.RUtils; |
27 | 14 | 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; |
37 | 19 |
|
38 | 20 | @ReflectCommand |
39 | | -public class CmdAttributes extends TabCommand { |
| 21 | +public class CmdAttributes extends ParentCommand { |
40 | 22 |
|
41 | 23 | 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)); |
43 | 29 | } |
44 | 30 |
|
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) { |
62 | 32 | if (!(cs instanceof final Player p)) { |
63 | 33 | 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()); |
68 | 34 | return false; |
69 | 35 | } |
70 | 36 | ItemStack hand = p.getInventory().getItemInMainHand(); |
71 | 37 | if (RUtils.isBlockAir(hand.getType())) { |
72 | 38 | cs.sendMessage(MessageColor.NEGATIVE + "You cannot apply attributes to air!"); |
73 | | - return true; |
| 39 | + return false; |
74 | 40 | } |
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!"); |
187 | 41 | return true; |
188 | 42 | } |
189 | 43 | } |
0 commit comments