|
20 | 20 | import java.util.Collection;
|
21 | 21 |
|
22 | 22 | public class NameCommand implements CommandRegistrationCallback {
|
| 23 | + public static final SimpleCommandExceptionType ITEM_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.item.name.failed")); |
| 24 | + public static final SimpleCommandExceptionType ITEM_REMOVE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.item.remove.failed")); |
| 25 | + public static final SimpleCommandExceptionType ENTITY_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.entity.name.failed")); |
| 26 | + public static final SimpleCommandExceptionType ENTITY_REMOVE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.entity.remove.failed")); |
| 27 | + |
23 | 28 | @Override
|
24 |
| - public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) { |
| 29 | + public void register( |
| 30 | + CommandDispatcher<ServerCommandSource> dispatcher, |
| 31 | + CommandRegistryAccess registryAccess, |
| 32 | + CommandManager.RegistrationEnvironment environment |
| 33 | + ) { |
25 | 34 | dispatcher.register(CommandManager.literal("name")
|
26 | 35 | .requires(source -> source.hasPermissionLevel(2))
|
27 | 36 | .then(CommandManager.literal("item")
|
28 | 37 | .then(CommandManager.argument("targets", EntityArgumentType.entities())
|
| 38 | + .executes(context -> removeNameItem( |
| 39 | + context.getSource(), |
| 40 | + EntityArgumentType.getEntities(context, "targets") |
| 41 | + )) |
29 | 42 | .then(CommandManager.argument("slot", ItemSlotArgumentType.itemSlot())
|
| 43 | + .executes(context -> removeNameItem( |
| 44 | + context.getSource(), |
| 45 | + EntityArgumentType.getEntities(context, "targets"), |
| 46 | + ItemSlotArgumentType.getItemSlot(context, "slot") |
| 47 | + )) |
30 | 48 | .then(CommandManager.argument("name", MessageArgumentType.message())
|
31 | 49 | .executes(context -> nameItem(
|
32 | 50 | context.getSource(),
|
33 | 51 | EntityArgumentType.getEntities(context, "targets"),
|
34 | 52 | ItemSlotArgumentType.getItemSlot(context, "slot"),
|
35 |
| - MessageArgumentType.getMessage(context, "name").getString() |
| 53 | + MessageArgumentType.getMessage(context, "name") |
36 | 54 | ))
|
37 | 55 | )
|
38 | 56 | )
|
39 | 57 | .then(CommandManager.argument("name", MessageArgumentType.message())
|
40 | 58 | .executes(context -> nameItem(
|
41 | 59 | context.getSource(),
|
42 | 60 | EntityArgumentType.getEntities(context, "targets"),
|
43 |
| - 0, |
44 |
| - MessageArgumentType.getMessage(context, "name").getString() |
| 61 | + MessageArgumentType.getMessage(context, "name") |
45 | 62 | ))
|
46 | 63 | )
|
47 | 64 | )
|
48 | 65 | )
|
49 | 66 | .then(CommandManager.literal("entity")
|
50 | 67 | .then(CommandManager.argument("targets", EntityArgumentType.entities())
|
| 68 | + .executes(context -> removeNameEntity( |
| 69 | + context.getSource(), |
| 70 | + EntityArgumentType.getEntities(context, "targets") |
| 71 | + )) |
51 | 72 | .then(CommandManager.argument("name", MessageArgumentType.message())
|
52 | 73 | .executes(context -> nameEntity(
|
53 | 74 | context.getSource(),
|
54 | 75 | EntityArgumentType.getEntities(context, "targets"),
|
55 |
| - MessageArgumentType.getMessage(context, "name").getString() |
| 76 | + MessageArgumentType.getMessage(context, "name") |
56 | 77 | ))
|
57 | 78 | )
|
58 | 79 | )
|
59 | 80 | )
|
60 | 81 | );
|
61 | 82 | }
|
62 | 83 |
|
63 |
| - private static int nameItem(ServerCommandSource source, Collection<? extends Entity> targets, int slot, String name) throws CommandSyntaxException { |
| 84 | + private static int nameItem( |
| 85 | + ServerCommandSource source, |
| 86 | + Collection<? extends Entity> targets, |
| 87 | + Text name |
| 88 | + ) throws CommandSyntaxException { |
| 89 | + int i = 0; |
| 90 | + |
| 91 | + for (Entity entity : targets) { |
| 92 | + if (nameItemTarget(entity, name)) { |
| 93 | + i++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (i > 0) { |
| 98 | + source.sendFeedback( |
| 99 | + () -> Text.translatable("commands.name.item.name.success", name), |
| 100 | + false |
| 101 | + ); |
| 102 | + return i; |
| 103 | + } else { |
| 104 | + throw ITEM_FAILED.create(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static int nameItem( |
| 109 | + ServerCommandSource source, |
| 110 | + Collection<? extends Entity> targets, |
| 111 | + int slot, |
| 112 | + Text name |
| 113 | + ) throws CommandSyntaxException { |
| 114 | + int i = 0; |
| 115 | + |
| 116 | + for (Entity entity : targets) { |
| 117 | + if (nameItemTarget(entity, slot, name)) { |
| 118 | + i++; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (i > 0) { |
| 123 | + source.sendFeedback( |
| 124 | + () -> Text.translatable("commands.name.item.name.success", name), |
| 125 | + false |
| 126 | + ); |
| 127 | + return i; |
| 128 | + } else { |
| 129 | + throw ITEM_FAILED.create(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static boolean nameItemTarget(Entity entity, int slot, Text name) { |
| 134 | + StackReference stack = entity.getStackReference(slot); |
| 135 | + |
| 136 | + if (stack == StackReference.EMPTY) return false; |
| 137 | + |
| 138 | + return nameItemTarget(stack.get(), name); |
| 139 | + } |
| 140 | + |
| 141 | + private static boolean nameItemTarget(Entity entity, Text name) { |
| 142 | + if (entity instanceof LivingEntity livingEntity) { |
| 143 | + ItemStack stack = livingEntity.getMainHandStack(); |
| 144 | + return nameItemTarget(stack, name); |
| 145 | + } else { |
| 146 | + return false; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static boolean nameItemTarget(ItemStack stack, Text name) { |
| 151 | + if (stack.isEmpty()) return false; |
| 152 | + |
| 153 | + stack.set(DataComponentTypes.CUSTOM_NAME, name); |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + private static int removeNameItem( |
| 158 | + ServerCommandSource source, |
| 159 | + Collection<? extends Entity> targets |
| 160 | + ) throws CommandSyntaxException { |
64 | 161 | int i = 0;
|
65 | 162 |
|
66 | 163 | for (Entity entity : targets) {
|
67 |
| - StackReference stackReference = entity.getStackReference(slot); |
| 164 | + if (removeNameItemTarget(entity)) { |
| 165 | + i++; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (i > 0) { |
| 170 | + source.sendFeedback(() -> Text.translatable("commands.name.item.remove.success"), false); |
| 171 | + return i; |
| 172 | + } else { |
| 173 | + throw ITEM_REMOVE_FAILED.create(); |
| 174 | + } |
| 175 | + } |
68 | 176 |
|
69 |
| - if (stackReference != StackReference.EMPTY) { |
70 |
| - ItemStack itemStack = stackReference.get(); |
| 177 | + private static int removeNameItem( |
| 178 | + ServerCommandSource source, |
| 179 | + Collection<? extends Entity> targets, |
| 180 | + int slot |
| 181 | + ) throws CommandSyntaxException { |
| 182 | + int i = 0; |
71 | 183 |
|
72 |
| - if (!itemStack.isEmpty()) { |
73 |
| - itemStack.set(DataComponentTypes.ITEM_NAME, Text.of(name)); |
74 |
| - i++; |
75 |
| - } |
| 184 | + for (Entity entity : targets) { |
| 185 | + if (removeNameItemTarget(entity, slot)) { |
| 186 | + i++; |
76 | 187 | }
|
77 | 188 | }
|
78 | 189 |
|
79 | 190 | if (i > 0) {
|
80 |
| - source.sendFeedback(() -> Text.translatable("commands.name.item.success", name), false); |
| 191 | + source.sendFeedback(() -> Text.translatable("commands.name.item.remove.success"), false); |
| 192 | + return i; |
81 | 193 | } else {
|
82 |
| - throw new SimpleCommandExceptionType(Text.translatable("commands.name.item.failed")).create(); |
| 194 | + throw ITEM_REMOVE_FAILED.create(); |
83 | 195 | }
|
| 196 | + } |
| 197 | + |
| 198 | + private static boolean removeNameItemTarget(Entity entity, int slot) { |
| 199 | + StackReference stack = entity.getStackReference(slot); |
| 200 | + |
| 201 | + if (stack == StackReference.EMPTY) return false; |
84 | 202 |
|
85 |
| - return i; |
| 203 | + return removeNameItemTarget(stack.get()); |
86 | 204 | }
|
87 | 205 |
|
88 |
| - public static int nameEntity(ServerCommandSource source, Collection<? extends Entity> targets, String name) throws CommandSyntaxException { |
| 206 | + private static boolean removeNameItemTarget(Entity entity) { |
| 207 | + if (entity instanceof LivingEntity livingEntity) { |
| 208 | + ItemStack stack = livingEntity.getMainHandStack(); |
| 209 | + return removeNameItemTarget(stack); |
| 210 | + } else { |
| 211 | + return false; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private static boolean removeNameItemTarget(ItemStack stack) { |
| 216 | + if (stack.isEmpty()) return false; |
| 217 | + |
| 218 | + return stack.remove(DataComponentTypes.CUSTOM_NAME) != null; |
| 219 | + } |
| 220 | + |
| 221 | + public static int nameEntity( |
| 222 | + ServerCommandSource source, |
| 223 | + Collection<? extends Entity> targets, |
| 224 | + Text name |
| 225 | + ) throws CommandSyntaxException { |
89 | 226 | int i = 0;
|
90 | 227 |
|
91 | 228 | for (Entity entity : targets) {
|
92 | 229 | if (entity instanceof LivingEntity livingEntity) {
|
93 |
| - livingEntity.setCustomName(Text.of(name)); |
| 230 | + livingEntity.setCustomName(name); |
94 | 231 | livingEntity.setCustomNameVisible(true);
|
95 | 232 | i++;
|
96 | 233 | }
|
97 | 234 | }
|
98 | 235 |
|
99 | 236 | if (i > 0) {
|
100 |
| - source.sendFeedback(() -> Text.translatable("commands.name.entity.success", name), false); |
| 237 | + source.sendFeedback( |
| 238 | + () -> Text.translatable("commands.name.entity.name.success", name), |
| 239 | + false |
| 240 | + ); |
| 241 | + return i; |
101 | 242 | } else {
|
102 |
| - throw new SimpleCommandExceptionType(Text.translatable("commands.name.entity.failed")).create(); |
| 243 | + throw ENTITY_FAILED.create(); |
103 | 244 | }
|
| 245 | + } |
104 | 246 |
|
105 |
| - return i; |
| 247 | + public static int removeNameEntity( |
| 248 | + ServerCommandSource source, |
| 249 | + Collection<? extends Entity> targets |
| 250 | + ) throws CommandSyntaxException { |
| 251 | + int i = 0; |
| 252 | + |
| 253 | + for (Entity entity : targets) { |
| 254 | + if (entity.getCustomName() == null) continue; |
| 255 | + |
| 256 | + entity.setCustomNameVisible(false); |
| 257 | + entity.setCustomName(null); |
| 258 | + i++; |
| 259 | + } |
| 260 | + |
| 261 | + if (i > 0) { |
| 262 | + source.sendFeedback( |
| 263 | + () -> Text.translatable("commands.name.entity.remove.success"), |
| 264 | + false |
| 265 | + ); |
| 266 | + return i; |
| 267 | + } else { |
| 268 | + throw ENTITY_REMOVE_FAILED.create(); |
| 269 | + } |
106 | 270 | }
|
107 | 271 | }
|
0 commit comments