Skip to content

Commit

Permalink
GH-601 Add PlayerArgument, fix /tp command usage. (#608)
Browse files Browse the repository at this point in the history
* Add PlayerArgument for replace standard argument from litecommands, and fix /tp command usage.

* Remove unnecessary <world> in DescriptionDocs annotation.
  • Loading branch information
vLuckyyy authored Dec 27, 2023
1 parent de3c78a commit ce13cb1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.eternalcode.core.bridge.litecommand.argument;

import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerProvider;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;

@LiteArgument(type = Player.class)
public class PlayerArgument extends AbstractViewerArgument<Player> {

private final Server server;

@Inject
public PlayerArgument(ViewerProvider viewerProvider, TranslationManager translationManager, Server server) {
super(viewerProvider, translationManager);
this.server = server;
}

@Override
public ParseResult<Player> parse(Invocation<CommandSender> invocation, String argument, Translation translation) {
Player target = this.server.getPlayer(argument);

if (target == null) {
return ParseResult.failure(translation.argument().offlinePlayer());
}

return ParseResult.success(target);
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<Player> argument, SuggestionContext context) {
return this.server.getOnlinePlayers().stream()
.map(HumanEntity::getName)
.collect(SuggestionResult.collector());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ private record TeleportContext(Player player, Location location) {}
this.teleportService = teleportService;
}

@Execute
@DescriptionDocs(description = "Teleport to specified player", arguments = "<player>")
void execute(@Context Player sender, @Context Viewer senderViewer, @Arg Player player) {
this.teleportService.teleport(sender, player.getLocation());

Formatter formatter = this.formatter(player, player.getLocation());

this.noticeService.viewer(senderViewer, translation -> translation.teleport().teleportedToPlayer(), formatter);
}

@Execute
@DescriptionDocs(description = "Teleport player to player", arguments = "<player> <target-player>")
void other(@Context Viewer sender, @Arg Player player, @Arg Player target) {
Expand All @@ -55,13 +65,15 @@ void other(@Context Viewer sender, @Arg Player player, @Arg Player target) {
}

@Execute
@DescriptionDocs(description = "Teleport to specified player", arguments = "<player>")
void execute(@Context Player sender, @Context Viewer senderViewer, @Arg Player player) {
this.teleportService.teleport(sender, player.getLocation());
@DescriptionDocs(description = "Teleport to specified location", arguments = "<x> <y> <z>")
void to(@Context Player sender, @Arg Location location) {
location.setWorld(sender.getWorld());

Formatter formatter = this.formatter(player, player.getLocation());
this.teleportService.teleport(sender, location);

this.noticeService.viewer(senderViewer, translation -> translation.teleport().teleportedToPlayer(), formatter);
Formatter formatter = this.formatter(sender, location);

this.noticeService.player(sender.getUniqueId(), translation -> translation.teleport().teleportedToCoordinates(), formatter);
}

@Execute
Expand All @@ -77,13 +89,24 @@ void to(@Context Player sender, @Arg Location location, @Arg World world) {
}

@Execute
@DescriptionDocs(description = "Teleport player to specified player, location and world", arguments = "<x> <y> <z> <player> <world>")
void to(@Context Viewer sender, @Arg Location location, @Arg Player player, @Arg World world) {
@DescriptionDocs(description = "Teleport player to specified location and world", arguments = "<player> <x> <y> <z>")
void to(@Context Viewer sender, @Arg Player target, @Arg Location location) {
location.setWorld(target.getWorld());

Formatter formatter = this.formatter(target, location);

this.teleportService.teleport(target, location);
this.noticeService.viewer(sender, translation -> translation.teleport().teleportedSpecifiedPlayerToCoordinates(), formatter);
}

@Execute
@DescriptionDocs(description = "Teleport player to specified player, location and world", arguments = "<player> <x> <y> <z> <world>")
void to(@Context Viewer sender, @Arg Player target, @Arg Location location, @Arg World world) {
location.setWorld(world);

Formatter formatter = this.formatter(player, location);
Formatter formatter = this.formatter(target, location);

this.teleportService.teleport(player, location);
this.teleportService.teleport(target, location);
this.noticeService.viewer(sender, translation -> translation.teleport().teleportedSpecifiedPlayerToCoordinates(), formatter);
}

Expand Down

0 comments on commit ce13cb1

Please sign in to comment.