Skip to content

Commit

Permalink
Release 3.0.0-BETA-pre18. Add world and location context providers fo…
Browse files Browse the repository at this point in the history
…r bukkit platform.
  • Loading branch information
Rollczi committed Oct 4, 2023
1 parent 6df0a55 commit 0fff2e3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -10,19 +11,29 @@ public class LiteBukkitMessages extends LiteMessages {
input -> "&cWorld " + input + " doesn't exist! (WORLD_NOT_EXIST)"
);

public static final MessageKey<CommandSender> WORLD_PLAYER_ONLY = MessageKey.of(
"world-player-only",
sender -> "&cOnly player can execute this command! (WORLD_PLAYER_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)"
);

public static final MessageKey<CommandSender> LOCATION_PLAYER_ONLY = MessageKey.of(
"location-player-only",
sender -> "&cOnly player can execute this command! (LOCATION_PLAYER_ONLY)"
);

public static final MessageKey<String> PLAYER_NOT_FOUND = MessageKey.of(
"player-not-found",
input -> "&cPlayer " + input + " not found! (PLAYER_NOT_FOUND)"
);

public static final MessageKey<Void> PLAYER_ONLY = MessageKey.of(
public static final MessageKey<CommandSender> PLAYER_ONLY = MessageKey.of(
"only-player",
"&cOnly player can execute this command! (PLAYER_ONLY)"
sender -> "&cOnly player can execute this command! (PLAYER_ONLY)"
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSender, Location> {

private final MessageRegistry messageRegistry;

public LocationContext(MessageRegistry messageRegistry) {
this.messageRegistry = messageRegistry;
}

@Override
public ContextResult<Location> provide(Invocation<CommandSender> 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));
}

}
Original file line number Diff line number Diff line change
@@ -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<CommandSender, World> {

private final MessageRegistry messageRegistry;

public WorldContext(MessageRegistry messageRegistry) {
this.messageRegistry = messageRegistry;
}

@Override
public ContextResult<World> provide(Invocation<CommandSender> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ContextResult<Player> provide(Invocation<CommandSender> 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()));
}

}

0 comments on commit 0fff2e3

Please sign in to comment.