Skip to content

Commit 6764161

Browse files
made CommandCore.createSource actually a factory method
1 parent cf21538 commit 6764161

File tree

7 files changed

+182
-43
lines changed

7 files changed

+182
-43
lines changed

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

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
import com.datasiqn.commandcore.argument.type.ArgumentType;
55
import com.datasiqn.commandcore.command.Command;
66
import com.datasiqn.commandcore.command.CommandContext;
7-
import com.datasiqn.commandcore.command.CommandSource;
87
import com.datasiqn.commandcore.command.builder.ArgumentBuilder;
98
import com.datasiqn.commandcore.command.builder.CommandBuilder;
10-
import com.datasiqn.commandcore.locatable.LocatableBlockSender;
11-
import com.datasiqn.commandcore.locatable.LocatableCommandSender;
12-
import com.datasiqn.commandcore.locatable.LocatableEntitySender;
9+
import com.datasiqn.commandcore.command.source.*;
1310
import com.datasiqn.commandcore.managers.CommandManager;
14-
import com.datasiqn.resultapi.Result;
1511
import org.bukkit.Bukkit;
1612
import org.bukkit.ChatColor;
1713
import org.bukkit.command.BlockCommandSender;
@@ -235,37 +231,14 @@ public static CommandCore getInstance() {
235231
*/
236232
@Contract(value = "_ -> new", pure = true)
237233
public static @NotNull CommandSource createSource(CommandSender sender) {
238-
return new CommandSource() {
239-
@Override
240-
public @NotNull Result<Player, String> getPlayerChecked() {
241-
return Result.resolve(() -> (Player) sender, error -> "Sender is not a player");
242-
}
243-
244-
@Override
245-
public @NotNull Result<Entity, String> getEntityChecked() {
246-
return Result.resolve(() -> (Entity) sender, error -> "Sender is not an entity");
247-
}
248-
249-
@Override
250-
public @NotNull Result<BlockCommandSender, String> getBlockChecked() {
251-
return Result.resolve(() -> (BlockCommandSender) sender, error -> "Sender is not a block");
252-
}
253-
254-
@Override
255-
public @NotNull Result<LocatableCommandSender, String> getLocatableChecked() {
256-
Result<LocatableCommandSender, String> result = Result.error("Sender is not locatable");
257-
if (sender instanceof Entity) {
258-
result = result.or(Result.ok(new LocatableEntitySender((Entity) sender)));
259-
} else if (sender instanceof BlockCommandSender) {
260-
result = result.or(Result.ok(new LocatableBlockSender((BlockCommandSender) sender)));
261-
}
262-
return result;
263-
}
264-
265-
@Override
266-
public @NotNull CommandSender getSender() {
267-
return sender;
268-
}
269-
};
234+
if (sender instanceof Player) {
235+
return new PlayerCommandSource(((Player) sender));
236+
} else if (sender instanceof Entity) {
237+
return new EntityCommandSource(((Entity) sender));
238+
} else if (sender instanceof BlockCommandSender) {
239+
return new BlockCommandSource(((BlockCommandSender) sender));
240+
} else {
241+
return new GenericCommandSource(sender);
242+
}
270243
}
271244
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.datasiqn.commandcore.command;
22

33
import com.datasiqn.commandcore.argument.Arguments;
4+
import com.datasiqn.commandcore.command.source.CommandSource;
45
import org.jetbrains.annotations.NotNull;
56

67
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.datasiqn.commandcore.command.source;
2+
3+
import com.datasiqn.commandcore.CommandCore;
4+
import com.datasiqn.commandcore.locatable.LocatableBlockSender;
5+
import com.datasiqn.commandcore.locatable.LocatableCommandSender;
6+
import com.datasiqn.resultapi.Result;
7+
import org.bukkit.command.BlockCommandSender;
8+
import org.bukkit.command.CommandSender;
9+
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* Represents a {@code CommandSource} where the sender is a {@link BlockCommandSender}
14+
* <br><br>
15+
* <strong>NOTE: Do not directly instantiate this class! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
16+
*/
17+
public class BlockCommandSource implements CommandSource {
18+
private final BlockCommandSender sender;
19+
20+
/**
21+
* Creates a new {@code BlockCommandSource} with the internal sender of {@code sender}.
22+
* <br><br>
23+
* <strong>NOTE: Do not directly use this constructor! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
24+
* @param sender The {@code BlockCommandSender} to internally use
25+
*/
26+
public BlockCommandSource(BlockCommandSender sender) {
27+
this.sender = sender;
28+
}
29+
30+
@Override
31+
public @NotNull Result<BlockCommandSender, String> getBlockChecked() {
32+
return Result.ok(sender);
33+
}
34+
35+
@Override
36+
public @NotNull Result<LocatableCommandSender, String> getLocatableChecked() {
37+
return Result.ok(new LocatableBlockSender(sender));
38+
}
39+
40+
@Override
41+
public @NotNull CommandSender getSender() {
42+
return sender;
43+
}
44+
}

src/main/java/com/datasiqn/commandcore/command/CommandSource.java renamed to src/main/java/com/datasiqn/commandcore/command/source/CommandSource.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.datasiqn.commandcore.command;
1+
package com.datasiqn.commandcore.command.source;
22

3+
import com.datasiqn.commandcore.CommandCore;
34
import com.datasiqn.commandcore.command.builder.CommandLink;
45
import com.datasiqn.commandcore.locatable.LocatableCommandSender;
56
import com.datasiqn.resultapi.Result;
@@ -12,7 +13,7 @@
1213
import org.jetbrains.annotations.Nullable;
1314

1415
/**
15-
* Represents the source of a command
16+
* Represents the source of a command. Get one using the factory method {@link CommandCore#createSource(CommandSender) createSource}.
1617
*/
1718
public interface CommandSource {
1819
/**
@@ -26,9 +27,13 @@ public interface CommandSource {
2627

2728
/**
2829
* Same as {@link #getPlayer()}, except checks if the sender is a player and returns a {@code Result}
30+
*
2931
* @return A result describing the player. If the command link {@link CommandLink#requiresPlayer() requiresPlayer}, use {@link #getPlayer()} instead
3032
*/
31-
@NotNull Result<Player, String> getPlayerChecked();
33+
@NotNull
34+
default Result<Player, String> getPlayerChecked() {
35+
return Result.error("Sender is not a player");
36+
}
3237

3338
/**
3439
* Gets the entity executing command
@@ -41,9 +46,13 @@ public interface CommandSource {
4146

4247
/**
4348
* Same as {@link #getEntity()}, except checks if the sender is an entity and returns a {@code Result}
49+
*
4450
* @return A result describing the entity. If the command link {@link CommandLink#requiresEntity() requiresEntity}, use {@link #getEntity()} instead
4551
*/
46-
@NotNull Result<Entity, String> getEntityChecked();
52+
@NotNull
53+
default Result<Entity, String> getEntityChecked() {
54+
return Result.error("Sender is not an entity");
55+
}
4756

4857
/**
4958
* Gets the block executing command
@@ -56,9 +65,13 @@ public interface CommandSource {
5665

5766
/**
5867
* Same as {@link #getBlock()}, except checks if the sender is a command block and returns a {@code Result}
68+
*
5969
* @return A result describing the command block. If the command link {@link CommandLink#requiresBlock()} requiresCommandBlock}, use {@link #getBlock()} instead
6070
*/
61-
@NotNull Result<BlockCommandSender, String> getBlockChecked();
71+
@NotNull
72+
default Result<BlockCommandSender, String> getBlockChecked() {
73+
return Result.error("Sender is not a block");
74+
}
6275

6376
/**
6477
* Gets the locatable sender executing command
@@ -71,9 +84,13 @@ public interface CommandSource {
7184

7285
/**
7386
* Same as {@link #getLocatable()}, except checks if the sender is locatable and returns a {@code Result}
87+
*
7488
* @return A result describing the locatable sender. If the command link {@link CommandLink#requiresLocatable()} requiresCommandBlock}, use {@link #getLocatable()} instead
7589
*/
76-
@NotNull Result<LocatableCommandSender, String> getLocatableChecked();
90+
@NotNull
91+
default Result<LocatableCommandSender, String> getLocatableChecked() {
92+
return Result.error("Sender is not locatable");
93+
}
7794

7895
/**
7996
* Gets the sender of the command
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.datasiqn.commandcore.command.source;
2+
3+
import com.datasiqn.commandcore.CommandCore;
4+
import com.datasiqn.commandcore.locatable.LocatableCommandSender;
5+
import com.datasiqn.commandcore.locatable.LocatableEntitySender;
6+
import com.datasiqn.resultapi.Result;
7+
import org.bukkit.command.CommandSender;
8+
import org.bukkit.entity.Entity;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
/**
12+
* Represents a {@code CommandSource} where the sender is an {@link Entity}.
13+
* <br><br>
14+
* <strong>NOTE: Do not directly instantiate this class! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
15+
*/
16+
public class EntityCommandSource implements CommandSource {
17+
private final Entity sender;
18+
19+
/**
20+
* Creates a new {@code EntityCommandSource} with the internal sender of {@code sender}.
21+
* <br><br>
22+
* <strong>NOTE: Do not directly use this constructor! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
23+
* @param sender The sender
24+
*/
25+
public EntityCommandSource(Entity sender) {
26+
this.sender = sender;
27+
}
28+
29+
@Override
30+
public @NotNull Result<Entity, String> getEntityChecked() {
31+
return Result.ok(sender);
32+
}
33+
34+
@Override
35+
public @NotNull Result<LocatableCommandSender, String> getLocatableChecked() {
36+
return Result.ok(new LocatableEntitySender(sender));
37+
}
38+
39+
@Override
40+
public @NotNull CommandSender getSender() {
41+
return sender;
42+
}
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.datasiqn.commandcore.command.source;
2+
3+
import com.datasiqn.commandcore.CommandCore;
4+
import org.bukkit.command.CommandSender;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* Represents a generic command source that is none of the other command sources.
9+
* <br><br>
10+
* <strong>Do not directly instantiate this class! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
11+
*/
12+
public class GenericCommandSource implements CommandSource {
13+
private final CommandSender sender;
14+
15+
/**
16+
* Creates a new {@code GenericCommandSource} with the internal sender of {@code sender}.
17+
* <br><br>
18+
* <strong>Do not directly use constructor! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
19+
* @param sender The command sender
20+
*/
21+
public GenericCommandSource(CommandSender sender) {
22+
this.sender = sender;
23+
}
24+
25+
@Override
26+
public @NotNull CommandSender getSender() {
27+
return sender;
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.datasiqn.commandcore.command.source;
2+
3+
import com.datasiqn.commandcore.CommandCore;
4+
import com.datasiqn.resultapi.Result;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
/**
10+
* Represents a {@code CommandSource} where the sender is a {@link Player}.\
11+
* <br><br>
12+
* <strong>NOTE: Do not directly instantiate this class! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
13+
*/
14+
public class PlayerCommandSource extends EntityCommandSource {
15+
private final Player player;
16+
17+
/**
18+
* Creates a new {@code CommandSource} with the internal sender of {@code sender}.
19+
* <br><br>
20+
* <strong>NOTE: Do not directly use this constructor! Instead, use the factory method {@link CommandCore#createSource(CommandSender) createSource}</strong>
21+
* @param sender The sender
22+
*/
23+
public PlayerCommandSource(Player sender) {
24+
super(sender);
25+
this.player = sender;
26+
}
27+
28+
@Override
29+
public @NotNull Result<Player, String> getPlayerChecked() {
30+
return Result.ok(player);
31+
}
32+
}

0 commit comments

Comments
 (0)