From 0fff2e3d237a40fe3912e9371273528efd058912 Mon Sep 17 00:00:00 2001 From: Rollczi Date: Wed, 4 Oct 2023 20:16:31 +0200 Subject: [PATCH] Release 3.0.0-BETA-pre18. Add world and location context providers for bukkit platform. --- .../bukkit/LiteBukkitMessages.java | 15 ++++++-- .../bukkit/LiteCommandsBukkit.java | 8 +++-- .../bukkit/argument/LocationContext.java | 34 +++++++++++++++++++ .../bukkit/argument/WorldContext.java | 33 ++++++++++++++++++ .../context/PlayerOnlyContextProvider.java | 2 +- 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/LocationContext.java create mode 100644 litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/WorldContext.java diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitMessages.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitMessages.java index 24e5e028a..605cca6f6 100644 --- a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitMessages.java +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitMessages.java @@ -2,6 +2,7 @@ import dev.rollczi.litecommands.message.LiteMessages; import dev.rollczi.litecommands.message.MessageKey; +import org.bukkit.command.CommandSender; public class LiteBukkitMessages extends LiteMessages { @@ -10,19 +11,29 @@ public class LiteBukkitMessages extends LiteMessages { input -> "&cWorld " + input + " doesn't exist! (WORLD_NOT_EXIST)" ); + public static final MessageKey WORLD_PLAYER_ONLY = MessageKey.of( + "world-player-only", + sender -> "&cOnly player can execute this command! (WORLD_PLAYER_ONLY)" + ); + public static final MessageKey LOCATION_INVALID_FORMAT = MessageKey.of( "location-invalid-format", input -> "&cInvalid location format '" + input + "'! Use: (LOCATION_INVALID_FORMAT)" ); + public static final MessageKey LOCATION_PLAYER_ONLY = MessageKey.of( + "location-player-only", + sender -> "&cOnly player can execute this command! (LOCATION_PLAYER_ONLY)" + ); + public static final MessageKey PLAYER_NOT_FOUND = MessageKey.of( "player-not-found", input -> "&cPlayer " + input + " not found! (PLAYER_NOT_FOUND)" ); - public static final MessageKey PLAYER_ONLY = MessageKey.of( + public static final MessageKey PLAYER_ONLY = MessageKey.of( "only-player", - "&cOnly player can execute this command! (PLAYER_ONLY)" + sender -> "&cOnly player can execute this command! (PLAYER_ONLY)" ); } diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java index 887a4089f..7dccc649b 100644 --- a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java @@ -3,7 +3,9 @@ import dev.rollczi.litecommands.LiteCommandsFactory; import dev.rollczi.litecommands.builder.LiteCommandsBuilder; import dev.rollczi.litecommands.bukkit.argument.LocationArgument; +import dev.rollczi.litecommands.bukkit.argument.LocationContext; import dev.rollczi.litecommands.bukkit.argument.WorldArgument; +import dev.rollczi.litecommands.bukkit.argument.WorldContext; import dev.rollczi.litecommands.bukkit.context.PlayerOnlyContextProvider; import dev.rollczi.litecommands.bukkit.argument.PlayerArgument; import dev.rollczi.litecommands.message.MessageRegistry; @@ -54,11 +56,13 @@ private LiteCommandsBukkit() { .settings(bukkitSettings -> bukkitSettings.tabCompleter(TabComplete.create(pattern.getScheduler(), plugin))) - .argument(Location.class, new LocationArgument(messageRegistry)) - .argument(World.class, new WorldArgument(server, messageRegistry)) .argument(Player.class, new PlayerArgument(server, messageRegistry)) + .argument(World.class, new WorldArgument(server, messageRegistry)) + .argument(Location.class, new LocationArgument(messageRegistry)) .context(Player.class, new PlayerOnlyContextProvider(messageRegistry)) + .context(World.class, new WorldContext(messageRegistry)) + .context(Location.class, new LocationContext(messageRegistry)) .result(String.class, new StringHandler()); }); diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/LocationContext.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/LocationContext.java new file mode 100644 index 000000000..26f9231c1 --- /dev/null +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/LocationContext.java @@ -0,0 +1,34 @@ +package dev.rollczi.litecommands.bukkit.argument; + +import dev.rollczi.litecommands.bukkit.LiteBukkitMessages; +import dev.rollczi.litecommands.context.ContextProvider; +import dev.rollczi.litecommands.context.ContextResult; +import dev.rollczi.litecommands.invocation.Invocation; +import dev.rollczi.litecommands.message.MessageRegistry; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class LocationContext implements ContextProvider { + + private final MessageRegistry messageRegistry; + + public LocationContext(MessageRegistry messageRegistry) { + this.messageRegistry = messageRegistry; + } + + @Override + public ContextResult provide(Invocation invocation) { + CommandSender sender = invocation.sender(); + + if (sender instanceof Player) { + Player player = (Player) sender; + + return ContextResult.ok(() -> player.getLocation()); + } + + return ContextResult.error(messageRegistry.get(LiteBukkitMessages.LOCATION_PLAYER_ONLY, sender)); + } + +} diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/WorldContext.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/WorldContext.java new file mode 100644 index 000000000..11e4249d6 --- /dev/null +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/WorldContext.java @@ -0,0 +1,33 @@ +package dev.rollczi.litecommands.bukkit.argument; + +import dev.rollczi.litecommands.bukkit.LiteBukkitMessages; +import dev.rollczi.litecommands.context.ContextProvider; +import dev.rollczi.litecommands.context.ContextResult; +import dev.rollczi.litecommands.invocation.Invocation; +import dev.rollczi.litecommands.message.MessageRegistry; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class WorldContext implements ContextProvider { + + private final MessageRegistry messageRegistry; + + public WorldContext(MessageRegistry messageRegistry) { + this.messageRegistry = messageRegistry; + } + + @Override + public ContextResult provide(Invocation invocation) { + CommandSender sender = invocation.sender(); + + if (sender instanceof Player) { + Player player = (Player) sender; + + return ContextResult.ok(() -> player.getWorld()); + } + + return ContextResult.error(messageRegistry.get(LiteBukkitMessages.WORLD_PLAYER_ONLY, sender)); + } + +} diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/context/PlayerOnlyContextProvider.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/context/PlayerOnlyContextProvider.java index 77ff70895..9579cadb1 100644 --- a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/context/PlayerOnlyContextProvider.java +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/context/PlayerOnlyContextProvider.java @@ -22,7 +22,7 @@ public ContextResult provide(Invocation invocation) { return ContextResult.ok(() -> (Player) invocation.sender()); } - return ContextResult.error(messageRegistry.get(LiteBukkitMessages.PLAYER_ONLY)); + return ContextResult.error(messageRegistry.get(LiteBukkitMessages.PLAYER_ONLY, invocation.sender())); } }