Skip to content

Commit c41c3d3

Browse files
CommandSource now has getChecked methods to avoid constant unwrapping
1 parent 59f2fc7 commit c41c3d3

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/main/java/com/datasiqn/commandcore/CommandCore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ public static CommandCore getInstance() {
229229
public static @NotNull CommandSource createSource(CommandSender sender) {
230230
return new CommandSource() {
231231
@Override
232-
public @NotNull Result<Player, String> getPlayer() {
232+
public @NotNull Result<Player, String> getPlayerChecked() {
233233
return Result.resolve(() -> (Player) sender, error -> "Sender is not a player");
234234
}
235235

236236
@Override
237-
public @NotNull Result<Entity, String> getEntity() {
237+
public @NotNull Result<Entity, String> getEntityChecked() {
238238
return Result.resolve(() -> (Entity) sender, error -> "Sender is not an entity");
239239
}
240240

src/main/java/com/datasiqn/commandcore/argument/type/VectorArgumentType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class VectorArgumentType implements ArgumentType<Vector> {
3333

3434
@Override
3535
public @NotNull List<String> getTabComplete(@NotNull CommandContext context) {
36-
Result<Player, String> player = context.getSource().getPlayer();
36+
Result<Player, String> player = context.getSource().getPlayerChecked();
3737
if (player.isError()) return Collections.emptyList();
3838
Block targetBlock = player.unwrap().getTargetBlockExact(5);
3939
if (targetBlock == null) return Collections.emptyList();

src/main/java/com/datasiqn/commandcore/command/CommandSource.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,40 @@
1414
*/
1515
public interface CommandSource {
1616
/**
17-
* Gets the player executing command
18-
* @return A result describing the player. This can be safely unwrapped if the command link {@link CommandLink#requiresPlayer() requiresPlayer}.
17+
* Gets the player executing command. This can be safely called if the command link {@link CommandLink#requiresPlayer() requiresPlayer}.
18+
* @return The player
19+
* @throws IllegalStateException If the sender is not a player
1920
*/
20-
@NotNull
21-
Result<Player, String> getPlayer();
21+
default @NotNull Player getPlayer() {
22+
return getPlayerChecked().<IllegalStateException>unwrapOrThrow(IllegalStateException::new);
23+
}
24+
25+
/**
26+
* Same as {@link #getPlayer()}, except checks if the sender is a player and returns a {@code Result}
27+
* @return A result describing the player. If the command link {@link CommandLink#requiresPlayer() requiresPlayer}, use {@link #getPlayer()} instead
28+
*/
29+
@NotNull Result<Player, String> getPlayerChecked();
2230

2331
/**
2432
* Gets the entity executing command
25-
* @return A result describing the entity. This can be safely unwrapped if the command link {@link CommandLink#requiresEntity() requiresEntity}.
33+
* @return The entity. This can be safely called if the command link {@link CommandLink#requiresEntity() requiresEntity}.
34+
* @throws IllegalStateException If the sender is not an entity
35+
*/
36+
default @NotNull Entity getEntity() {
37+
return getEntityChecked().<IllegalStateException>unwrapOrThrow(IllegalStateException::new);
38+
}
39+
40+
/**
41+
* Same as {@link #getEntity()}, except checks if the sender is an entity and returns a {@code Result}
42+
* @return A result describing the entity. If the command link {@link CommandLink#requiresEntity() requiresEntity}, use {@link #getEntity()} instead
2643
*/
27-
@NotNull
28-
Result<Entity, String> getEntity();
44+
@NotNull Result<Entity, String> getEntityChecked();
2945

3046
/**
3147
* Gets the sender of the command
3248
* @return The sender
3349
*/
34-
@NotNull
35-
CommandSender getSender();
50+
@NotNull CommandSender getSender();
3651

3752
/**
3853
* Sends the command source a message

src/main/java/com/datasiqn/commandcore/command/builder/CommandLink.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public T requires(@NotNull Requirement requirement) {
3737
* @return Itself, for chaining
3838
*/
3939
public T requiresPlayer() {
40-
return requires(context -> context.getSource().getPlayer().and(Result.ok()).or(Result.error("A player is required to run this")));
40+
return requires(context -> context.getSource().getPlayerChecked().and(Result.ok()).or(Result.error("A player is required to run this")));
4141
}
4242

4343
/**
@@ -46,7 +46,7 @@ public T requiresPlayer() {
4646
* @return Itself, for chaining
4747
*/
4848
public T requiresEntity() {
49-
return requires(context -> context.getSource().getEntity().and(Result.ok()).or(Result.error("An entity is required to run this")));
49+
return requires(context -> context.getSource().getEntityChecked().and(Result.ok()).or(Result.error("An entity is required to run this")));
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)