Skip to content

Commit f849822

Browse files
created locatable and command block sources
1 parent ffa373e commit f849822

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
import com.datasiqn.commandcore.command.CommandSource;
88
import com.datasiqn.commandcore.command.builder.ArgumentBuilder;
99
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;
1013
import com.datasiqn.commandcore.managers.CommandManager;
1114
import com.datasiqn.resultapi.Result;
1215
import org.bukkit.Bukkit;
1316
import org.bukkit.ChatColor;
17+
import org.bukkit.command.BlockCommandSender;
1418
import org.bukkit.command.CommandMap;
1519
import org.bukkit.command.CommandSender;
1620
import org.bukkit.command.PluginCommand;
@@ -238,6 +242,22 @@ public static CommandCore getInstance() {
238242
return Result.resolve(() -> (Entity) sender, error -> "Sender is not an entity");
239243
}
240244

245+
@Override
246+
public @NotNull Result<BlockCommandSender, String> getBlockChecked() {
247+
return Result.resolve(() -> (BlockCommandSender) sender, error -> "Sender is not a block");
248+
}
249+
250+
@Override
251+
public @NotNull Result<LocatableCommandSender, String> getLocatableChecked() {
252+
Result<LocatableCommandSender, String> result = Result.error("Sender is not locatable");
253+
if (sender instanceof Entity) {
254+
result = result.or(Result.ok(new LocatableEntitySender((Entity) sender)));
255+
} else if (sender instanceof BlockCommandSender) {
256+
result = result.or(Result.ok(new LocatableBlockSender((BlockCommandSender) sender)));
257+
}
258+
return result;
259+
}
260+
241261
@Override
242262
public @NotNull CommandSender getSender() {
243263
return sender;

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

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

33
import com.datasiqn.commandcore.command.builder.CommandLink;
4+
import com.datasiqn.commandcore.locatable.LocatableCommandSender;
45
import com.datasiqn.resultapi.Result;
6+
import org.bukkit.command.BlockCommandSender;
57
import org.bukkit.command.CommandSender;
68
import org.bukkit.entity.Entity;
79
import org.bukkit.entity.Player;
@@ -43,6 +45,36 @@ public interface CommandSource {
4345
*/
4446
@NotNull Result<Entity, String> getEntityChecked();
4547

48+
/**
49+
* Gets the block executing command
50+
* @return The block. This can be safely called if the command link {@link CommandLink#requiresBlock()} requiresBlock}.
51+
* @throws IllegalStateException If the sender is not a block
52+
*/
53+
default @NotNull BlockCommandSender getBlock() {
54+
return getBlockChecked().<IllegalStateException>unwrapOrThrow(IllegalStateException::new);
55+
}
56+
57+
/**
58+
* Same as {@link #getBlock()}, except checks if the sender is a command block and returns a {@code Result}
59+
* @return A result describing the command block. If the command link {@link CommandLink#requiresBlock()} requiresCommandBlock}, use {@link #getBlock()} instead
60+
*/
61+
@NotNull Result<BlockCommandSender, String> getBlockChecked();
62+
63+
/**
64+
* Gets the locatable sender executing command
65+
* @return The locatable sender. This can be safely called if the command link {@link CommandLink#requiresLocatable() requiresLocatable}.
66+
* @throws IllegalStateException If the sender is not a locatable sender
67+
*/
68+
default @NotNull LocatableCommandSender getLocatable() {
69+
return getLocatableChecked().<IllegalStateException>unwrapOrThrow(IllegalStateException::new);
70+
}
71+
72+
/**
73+
* Same as {@link #getLocatable()}, except checks if the sender is locatable and returns a {@code Result}
74+
* @return A result describing the locatable sender. If the command link {@link CommandLink#requiresLocatable()} requiresCommandBlock}, use {@link #getLocatable()} instead
75+
*/
76+
@NotNull Result<LocatableCommandSender, String> getLocatableChecked();
77+
4678
/**
4779
* Gets the sender of the command
4880
* @return The sender

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public T requiresEntity() {
4949
return requires(context -> context.getSource().getEntityChecked().and(Result.ok()).or(Result.error("An entity is required to run this")));
5050
}
5151

52+
/**
53+
* Requires the sender to be a {@code BlockCommandSender}
54+
* @see #requires(Requirement)
55+
* @return Itself, for chaining
56+
*/
57+
public T requiresBlock() {
58+
return requires(context -> context.getSource().getBlockChecked().and(Result.ok()).or(Result.error("A block is required to run this")));
59+
}
60+
61+
/**
62+
* Requires the sender to be locatable
63+
* @see #requires(Requirement)
64+
* @return Itself, for chaining
65+
*/
66+
public T requiresLocatable() {
67+
return requires(context -> context.getSource().getLocatableChecked().and(Result.ok()).or(Result.error("A sender with a location is required to run this")));
68+
}
69+
5270
/**
5371
* Adds a new node onto this command builder
5472
* @param node The node
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.datasiqn.commandcore.locatable;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.World;
5+
import org.bukkit.command.BlockCommandSender;
6+
import org.bukkit.command.CommandSender;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
/**
10+
* Represents a block that can be located
11+
*/
12+
public class LocatableBlockSender implements LocatableCommandSender {
13+
private final BlockCommandSender blockSender;
14+
15+
/**
16+
* Constructs a new {@code LocatableBlock} with an internal command sender of {@code blockSender}
17+
* @param blockSender The internal blockSender to use
18+
*/
19+
public LocatableBlockSender(BlockCommandSender blockSender) {
20+
this.blockSender = blockSender;
21+
}
22+
23+
@Override
24+
public @NotNull Location getLocation() {
25+
return blockSender.getBlock().getLocation();
26+
}
27+
28+
@Override
29+
public @NotNull World getWorld() {
30+
return blockSender.getBlock().getWorld();
31+
}
32+
33+
@Override
34+
public @NotNull CommandSender getSender() {
35+
return blockSender;
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.datasiqn.commandcore.locatable;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.World;
5+
import org.bukkit.command.CommandSender;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
/**
9+
* Represents a command sender that can be located
10+
*/
11+
public interface LocatableCommandSender {
12+
/**
13+
* Gets the location of this object
14+
* @return A clone of location
15+
*/
16+
@NotNull
17+
Location getLocation();
18+
19+
/**
20+
* Gets which world this object is located in
21+
* @return The world
22+
*/
23+
@NotNull
24+
World getWorld();
25+
26+
/**
27+
* Gets the sender of the command that got executed
28+
* @return The sender
29+
*/
30+
@NotNull
31+
CommandSender getSender();
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.datasiqn.commandcore.locatable;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.World;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Entity;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
/**
10+
* Represents an entity that can be located
11+
*/
12+
public class LocatableEntitySender implements LocatableCommandSender {
13+
private final Entity entity;
14+
15+
/**
16+
* Constructs a new {@code LocatableEntity} with an internal command sender of {@code entity}
17+
* @param entity The internal entity to use
18+
*/
19+
public LocatableEntitySender(Entity entity) {
20+
this.entity = entity;
21+
}
22+
23+
@Override
24+
public @NotNull Location getLocation() {
25+
return entity.getLocation();
26+
}
27+
28+
@Override
29+
public @NotNull World getWorld() {
30+
return entity.getWorld();
31+
}
32+
33+
@Override
34+
public @NotNull CommandSender getSender() {
35+
return entity;
36+
}
37+
}

0 commit comments

Comments
 (0)