8000 Implement #495 · CommandAPI/CommandAPI@1bb92b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bb92b3

Browse files
committed
Implement #495
1 parent fbe216c commit 1bb92b3

File tree

24 files changed

+358
-86
lines changed

24 files changed

+358
-86
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/arguments/EntitySelectorArgument.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public CommandAPIArgumentType getArgumentType() {
6969

7070
@Override
7171
public <CommandSourceStack> Entity parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
72-
return (Entity) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_ONE_ENTITY);
72+
return (Entity) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_ONE_ENTITY, true);
7373
}
7474

7575
@Override
@@ -106,7 +106,7 @@ public CommandAPIArgumentType getArgumentType() {
106106

107107
@Override
108108
public <CommandSourceStack> Player parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
109-
return (Player) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_ONE_PLAYER);
109+
return (Player) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_ONE_PLAYER, true);
110110
}
111111

112112
@Override
@@ -124,12 +124,24 @@ public List<String> getEntityNames(Object argument) {
124124
@SuppressWarnings("rawtypes")
125125
public static class ManyEntities extends Argument<Collection> {
126126

127+
private final boolean allowEmpty;
128+
127129
/**
128130
* An argument that represents many entities
129131
* @param nodeName the name of the node for this argument
130132
*/
131133
public ManyEntities(String nodeName) {
134+
this(nodeName, true);
135+
}
136+
137+
/**
138+
* An argument that represents many entities
139+
* @param nodeName the name of the node for this argument
140+
* @param allowEmpty whether this entity selector should allow no entities found, or should throw an error instead
141+
*/
142+
public ManyEntities(String nodeName, boolean allowEmpty) {
132143
super(nodeName, CommandAPIBukkit.get()._ArgumentEntity(ArgumentSubType.ENTITYSELECTOR_MANY_ENTITIES));
144+
this.allowEmpty = allowEmpty;
133145
}
134146

135147
@Override
@@ -145,7 +157,7 @@ public CommandAPIArgumentType getArgumentType() {
145157
@SuppressWarnings("unchecked")
146158
@Override
147159
public <CommandSourceStack> Collection<Entity> parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
148-
return (Collection<Entity>) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_MANY_ENTITIES);
160+
return (Collection<Entity>) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_MANY_ENTITIES, this.allowEmpty);
149161
}
150162

151163
@SuppressWarnings("unchecked")
@@ -168,12 +180,24 @@ public List<String> getEntityNames(Object argument) {
168180
@SuppressWarnings("rawtypes")
169181
public static class ManyPlayers extends Argument<Collection> {
170182

183+
private final boolean allowEmpty;
184+
171185
/**
172186
* An argument that represents many players
173187
* @param nodeName the name of the node for this argument
174188
*/
175189
public ManyPlayers(String nodeName) {
190+
this(nodeName, true);
191+
}
192+
193+
/**
194+
* An argument that represents many players
195+
* @param nodeName the name of the node for this argument
196+
* * @param allowEmpty whether this entity selector should allow no entities found, or should throw an error instead
197+
*/
198+
public ManyPlayers(String nodeName, boolean allowEmpty) {
176199
super(nodeName, CommandAPIBukkit.get()._ArgumentEntity(ArgumentSubType.ENTITYSELECTOR_MANY_PLAYERS));
200+
this.allowEmpty = allowEmpty;
177201
}
178202

179203
@Override
@@ -189,7 +213,7 @@ public CommandAPIArgumentType getArgumentType() {
189213
@SuppressWarnings("unchecked")
190214
@Override
191215
public <CommandSourceStack> Collection<Player> parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
192-
return (Collection<Player>) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_MANY_PLAYERS);
216+
return (Collection<Player>) CommandAPIBukkit.<CommandSourceStack>get().getEntitySelector(cmdCtx, key, ArgumentSubType.ENTITYSELECTOR_MANY_PLAYERS, allowEmpty);
193217
}
194218

195219
@SuppressWarnings("unchecked")

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/nms/NMS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Predicate<Block> getBlockPredicate(CommandContext<CommandListenerWrapper> cmdCtx
310310

311311
Enchantment getEnchantment(CommandContext<CommandListenerWrapper> cmdCtx, String key) throws CommandSyntaxException; // Throws exception in 1.19.3
312312

313-
Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String key, ArgumentSubType subType)
313+
Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String key, ArgumentSubType subType, boolean allowEmpty)
314314
throws CommandSyntaxException;
315315

316316
EntityType getEntityType(CommandContext<CommandListenerWrapper> cmdCtx, String key) throws CommandSyntaxException;

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.15/src/main/java/dev/jorel/commandapi/nms/NMS_1_15.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public Enchantment getEnchantment(CommandContext<CommandListenerWrapper> cmdCtx,
559559
}
560560

561561
@Override
562-
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
562+
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
563563
EntitySelector argument = cmdCtx.getArgument(str, EntitySelector.class);
564564
try {
565565
entitySelectorCheckPermissions.set(argument, false);
@@ -575,9 +575,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
575575
for (Entity entity : argument.getEntities(cmdCtx.getSource())) {
576576
result.add(entity.getBukkitEntity());
577577
}
578-
yield result;
578+
if (result.isEmpty() && !allowEmpty) {
579+
throw ArgumentEntity.d.create();
580+
} else {
581+
yield result;
582+
}
579583
} catch (CommandSyntaxException e) {
580-
yield new ArrayList<org.bukkit.entity.Entity>();
584+
if (allowEmpty) {
585+
yield new ArrayList<org.bukkit.entity.Entity>();
586+
} else {
587+
throw e;
588+
}
581589
}
582590
case ENTITYSELECTOR_MANY_PLAYERS:
583591
// ArgumentEntity.d -> EntitySelector.d
@@ -586,9 +594,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
586594
for (EntityPlayer player : argument.d(cmdCtx.getSource())) {
587595
result.add(player.getBukkitEntity());
588596
}
589-
yield result;
597+
if (result.isEmpty() && !allowEmpty) {
598+
throw ArgumentEntity.e.create();
599+
} else {
600+
yield result;
601+
}
590602
} catch (CommandSyntaxException e) {
591-
yield new ArrayList<Player>();
603+
if (allowEmpty) {
604+
yield new ArrayList<Player>();
605+
} else {
606+
throw e;
607+
}
592608
}
593609
case ENTITYSELECTOR_ONE_ENTITY:
594610
// ArgumentEntity.a -> EntitySelector.a

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.1/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_R1.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public Enchantment getEnchantment(CommandContext<CommandListenerWrapper> cmdCtx,
583583
}
584584

585585
@Override
586-
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
586+
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
587587
EntitySelector argument = cmdCtx.getArgument(str, EntitySelector.class);
588588
try {
589589
entitySelectorCheckPermissions.set(argument, false);
@@ -599,9 +599,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
599599
for (Entity entity : argument.getEntities(cmdCtx.getSource())) {
600600
result.add(entity.getBukkitEntity());
601601
}
602-
yield result;
602+
if (result.isEmpty() && !allowEmpty) {
603+
throw ArgumentEntity.d.create();
604+
} else {
605+
yield result;
606+
}
603607
} catch (CommandSyntaxException e) {
604-
yield new ArrayList<org.bukkit.entity.Entity>();
608+
if (allowEmpty) {
609+
yield new ArrayList<org.bukkit.entity.Entity>();
610+
} else {
611+
throw e;
612+
}
605613
}
606614
case ENTITYSELECTOR_MANY_PLAYERS:
607615
// ArgumentEntity.d -> EntitySelector.d
@@ -610,9 +618,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
610618
for (EntityPlayer player : argument.d(cmdCtx.getSource())) {
611619
result.add(player.getBukkitEntity());
612620
}
613-
yield result;
621+
if (result.isEmpty() && !allowEmpty) {
622+
throw ArgumentEntity.e.create();
623+
} else {
624+
yield result;
625+
}
614626
} catch (CommandSyntaxException e) {
615-
yield new ArrayList<Player>();
627+
if (allowEmpty) {
628+
yield new ArrayList<Player>();
629+
} else {
630+
throw e;
631+
}
616632
}
617633
case ENTITYSELECTOR_ONE_ENTITY:
618634
// ArgumentEntity.a -> EntitySelector.a

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_R2.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public Enchantment getEnchantment(CommandContext<CommandListenerWrapper> cmdCtx,
580580
}
581581

582582
@Override
583-
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
583+
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
584584
EntitySelector argument = cmdCtx.getArgument(str, EntitySelector.class);
585585
try {
586586
entitySelectorCheckPermissions.set(argument, false);
@@ -596,9 +596,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
596596
for (Entity entity : argument.getEntities(cmdCtx.getSource())) {
597597
result.add(entity.getBukkitEntity());
598598
}
599-
yield result;
599+
if (result.isEmpty() && !allowEmpty) {
600+
throw ArgumentEntity.d.create();
601+
} else {
602+
yield result;
603+
}
600604
} catch (CommandSyntaxException e) {
601-
yield new ArrayList<org.bukkit.entity.Entity>();
605+
if (allowEmpty) {
606+
yield new ArrayList<org.bukkit.entity.Entity>();
607+
} else {
608+
throw e;
609+
}
602610
}
603611
case ENTITYSELECTOR_MANY_PLAYERS:
604612
// ArgumentEntity.d -> EntitySelector.d
@@ -607,9 +615,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
607615
for (EntityPlayer player : argument.d(cmdCtx.getSource())) {
608616
result.add(player.getBukkitEntity());
609617
}
610-
yield result;
618+
if (result.isEmpty() && !allowEmpty) {
619+
throw ArgumentEntity.e.create();
620+
} else {
621+
yield result;
622+
}
611623
} catch (CommandSyntaxException e) {
612-
yield new ArrayList<Player>();
624+
if (allowEmpty) {
625+
yield new ArrayList<Player>();
626+
} else {
627+
throw e;
628+
}
613629
}
614630
case ENTITYSELECTOR_ONE_ENTITY:
615631 C2EE
// ArgumentEntity.a -> EntitySelector.a

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_4_R3.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public Enchantment getEnchantment(CommandContext<CommandListenerWrapper> cmdCtx,
536536
}
537537

538538
@Override
539-
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
539+
public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
540540
EntitySelector argument = cmdCtx.getArgument(str, EntitySelector.class);
541541
try {
542542
entitySelectorCheckPermissions.set(argument, false);
@@ -552,9 +552,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
552552
for (Entity entity : argument.getEntities(cmdCtx.getSource())) {
553553
result.add(entity.getBukkitEntity());
554554
}
555-
yield result;
555+
if (result.isEmpty() && !allowEmpty) {
556+
throw ArgumentEntity.d.create();
557+
} else {
558+
yield result;
559+
}
556560
} catch (CommandSyntaxException e) {
557-
yield new ArrayList<org.bukkit.entity.Entity>();
561+
if (allowEmpty) {
562+
yield new ArrayList<org.bukkit.entity.Entity>();
563+
} else {
564+
throw e;
565+
}
558566
}
559567
case ENTITYSELECTOR_MANY_PLAYERS:
560568
// ArgumentEntity.d -> EntitySelector.d
@@ -563,9 +571,17 @@ public Object getEntitySelector(CommandContext<CommandListenerWrapper> cmdCtx, S
563571
for (EntityPlayer player : argument.d(cmdCtx.getSource())) {
564572
result.add(player.getBukkitEntity());
565573
}
566-
yield result;
574+
if (result.isEmpty() && !allowEmpty) {
575+
throw ArgumentEntity.e.create();
576+
} else {
577+
yield result;
578+
}
567579
} catch (CommandSyntaxException e) {
568-
yield new ArrayList<Player>();
580+
if (allowEmpty) {
581+
yield new ArrayList<Player>();
582+
} else {
583+
throw e;
584+
}
569585
}
570586
case ENTITYSELECTOR_ONE_ENTITY:
571587
// ArgumentEntity.a -> EntitySelector.a

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.17-common/src/main/java/dev/jorel/commandapi/nms/NMS_1_17_Common.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public final World getDimension(CommandContext<CommandSourceStack> cmdCtx, Strin
336336
}
337337

338338
@Override
339-
public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
339+
public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
340340

341341
// We override the rule whereby players need "minecraft.command.selector" and
342342
// have to have
@@ -357,19 +357,35 @@ public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, Strin
357357
for (Entity entity : argument.findEntities(cmdCtx.getSource())) {
358358
result.add(entity.getBukkitEntity());
359359
}
360-
yield result;
360+
if (result.isEmpty() && !allowEmpty) {
361+
throw EntityArgument.NO_ENTITIES_FOUND.create();
362+
} else {
363+
yield result;
364+
}
361365
} catch (CommandSyntaxException e) {
362-
yield new ArrayList<org.bukkit.entity.Entity>();
366+
if (allowEmpty) {
367+
yield new ArrayList<org.bukkit.entity.Entity>();
368+
} else {
369+
throw e;
370+
}
363371
}
364372
case ENTITYSELECTOR_MANY_PLAYERS:
365373
try {
366374
List<Player> result = new ArrayList<>();
367375
for (ServerPlayer player : argument.findPlayers(cmdCtx.getSource())) {
368376
result.add(player.getBukkitEntity());
369377
}
370-
yield result;
378+
if (result.isEmpty() && !allowEmpty) {
379+
throw EntityArgument.NO_PLAYERS_FOUND.create();
380+
} else {
381+
yield result;
382+
}
371383
} catch (CommandSyntaxException e) {
372-
yield new ArrayList<Player>();
384+
if (allowEmpty) {
385+
yield new ArrayList<Player>();
386+
} else {
387+
throw e;
388+
}
373389
}
374390
case ENTITYSELECTOR_ONE_ENTITY:
375391
yield argument.findSingleEntity(cmdCtx.getSource()).getBukkitEntity();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.18.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_18_R2.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public final World getDimension(CommandContext<CommandSourceStack> cmdCtx, Strin
396396
}
397397

398398
@Override
399-
public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, String str, ArgumentSubType subType) throws CommandSyntaxException {
399+
public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, String str, ArgumentSubType subType, boolean allowEmpty) throws CommandSyntaxException {
400400

401401
// We override the rule whereby players need "minecraft.command.selector" and
402402
// have to have
@@ -417,19 +417,35 @@ public Object getEntitySelector(CommandContext<CommandSourceStack> cmdCtx, Strin
417417
for (Entity entity : argument.findEntities(cmdCtx.getSource())) {
418418
result.add(entity.getBukkitEntity());
419419
}
420-
yield result;
420+
if (result.isEmpty() && !allowEmpty) {
421+
throw EntityArgument.NO_ENTITIES_FOUND.create();
422+
} else {
423+
yield result;
424+
}
421425
} catch (CommandSyntaxException e) {
422-
yield new ArrayList<org.bukkit.entity.Entity>();
426+
if (allowEmpty) {
427+
yield new ArrayList<org.bukkit.entity.Entity>();
428+
} else {
429+
throw e;
430+
}
423431
}
424432
case ENTITYSELECTOR_MANY_PLAYERS:
425433
try {
426434
List<Player> result = new ArrayList<>();
427435
for (ServerPlayer player : argument.findPlayers(cmdCtx.getSource())) {
428436
result.add(player.getBukkitEntity());
429437
}
430-
yield result;
438+
if (result.isEmpty() && !allowEmpty) {
439+
throw EntityArgument.NO_PLAYERS_FOUND.create();
440+
} else {
441+
yield result;
442+
}
431443
} catch (CommandSyntaxException e) {
432-
yield new ArrayList<Player>();
444+
if (allowEmpty) {
445+
yield new ArrayList<Player>();
446+
} else {
447+
throw e;
448+
}
433449
}
434450
case ENTITYSELECTOR_ONE_ENTITY:
435451
yield argument.findSingleEntity(cmdCtx.getSource()).getBukkitEntity();

0 commit comments

Comments
 (0)
0