Skip to content

fix: should be able to retrieve context objects from all entities and command blocks #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.rollczi.litecommands.message.LiteMessages;
import dev.rollczi.litecommands.message.MessageKey;
import org.bukkit.command.CommandSender;

public class LiteBukkitMessages extends LiteMessages {

Expand All @@ -16,6 +15,11 @@ public class LiteBukkitMessages extends LiteMessages {
unused -> "&cOnly player can execute this command! (WORLD_PLAYER_ONLY)"
);

public static final MessageKey<Void> WORLD_NON_CONSOLE_ONLY = MessageKey.of(
"location-non-console-only",
unused -> "&cConsole cannot execute this command! (WORLD_NON_CONSOLE_ONLY)"
);

public static final MessageKey<String> LOCATION_INVALID_FORMAT = MessageKey.of(
"location-invalid-format",
input -> "&cInvalid location format '" + input + "'! Use: <x> <y> <z> (LOCATION_INVALID_FORMAT)"
Expand All @@ -26,6 +30,11 @@ public class LiteBukkitMessages extends LiteMessages {
unused -> "&cOnly player can execute this command! (LOCATION_PLAYER_ONLY)"
);

public static final MessageKey<Void> LOCATION_NON_CONSOLE_ONLY = MessageKey.of(
"location-player-only",
unused -> "&cConsole cannot execute this command! (LOCATION_NON_CONSOLE_ONLY)"
);

public static final MessageKey<String> PLAYER_NOT_FOUND = MessageKey.of(
"player-not-found",
input -> "&cPlayer " + input + " not found! (PLAYER_NOT_FOUND)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageRegistry;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;

public class LocationContext implements ContextProvider<CommandSender, Location> {

Expand All @@ -21,13 +22,17 @@ public LocationContext(MessageRegistry<CommandSender> messageRegistry) {
public ContextResult<Location> provide(Invocation<CommandSender> invocation) {
CommandSender sender = invocation.sender();

if (sender instanceof Player) {
Player player = (Player) sender;
if (sender instanceof Entity) {
Entity entity = (Entity) sender;

return ContextResult.ok(() -> player.getLocation());
return ContextResult.ok(() -> entity.getLocation());
} else if (sender instanceof BlockCommandSender) {
BlockCommandSender blockCommandSender = (BlockCommandSender) sender;

return ContextResult.ok(() -> blockCommandSender.getBlock().getLocation());
}

return ContextResult.error(messageRegistry.getInvoked(LiteBukkitMessages.LOCATION_PLAYER_ONLY, invocation));
return ContextResult.error(messageRegistry.getInvoked(LiteBukkitMessages.LOCATION_NON_CONSOLE_ONLY, invocation));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageRegistry;
import org.bukkit.World;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;

public class WorldContext implements ContextProvider<CommandSender, World> {

Expand All @@ -21,13 +22,17 @@ public WorldContext(MessageRegistry<CommandSender> messageRegistry) {
public ContextResult<World> provide(Invocation<CommandSender> invocation) {
CommandSender sender = invocation.sender();

if (sender instanceof Player) {
Player player = (Player) sender;
if (sender instanceof Entity) {
Entity entity = (Entity) sender;

return ContextResult.ok(() -> player.getWorld());
return ContextResult.ok(() -> entity.getWorld());
} else if (sender instanceof BlockCommandSender) {
BlockCommandSender blockCommandSender = (BlockCommandSender) sender;

return ContextResult.ok(() -> blockCommandSender.getBlock().getWorld());
}

return ContextResult.error(messageRegistry.getInvoked(LiteBukkitMessages.WORLD_PLAYER_ONLY, invocation));
return ContextResult.error(messageRegistry.getInvoked(LiteBukkitMessages.WORLD_NON_CONSOLE_ONLY, invocation));
}

}