More command refactoring

This commit is contained in:
William278 committed 2023-03-20 00:01:09 +00:00
1 parent 3bde5305d7
commit 34eb2c1f54
17 files changed
+382 -540

No files matched your search

@@ -1,55 +1,74 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.teleport.TeleportationException;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class TpOfflineCommand extends Command implements TabProvider {
protected TpOfflineCommand(@NotNull HuskHomes implementor) {
super("tpoffline", Permission.COMMAND_TPOFFLINE, implementor);
protected TpOfflineCommand(@NotNull HuskHomes plugin) {
super("tpoffline", List.of(), "<player>", plugin);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length != 1) {
plugin.getLocales().getLocale("error_invalid_syntax", "/tpoffline <player>")
.ifPresent(onlineUser::sendMessage);
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
if (!(executor instanceof OnlineUser user)) {
plugin.getLocales().getLocale("error_in_game_only")
.ifPresent(executor::sendMessage);
return;
}
final String targetUser = args[0];
plugin.getDatabase().getUserDataByName(targetUser).thenAccept(userData -> {
if (userData.isEmpty()) {
plugin.getLocales().getLocale("error_player_not_found", targetUser)
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getDatabase().getOfflinePosition(userData.get().user()).thenAccept(offlinePosition -> {
if (offlinePosition.isEmpty()) {
plugin.getLocales().getLocale("error_no_offline_position", targetUser)
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getLocales().getLocale("teleporting_offline_player", targetUser)
.ifPresent(onlineUser::sendMessage);
Teleport.builder(plugin, onlineUser)
.setTarget(offlinePosition.get())
.toTeleport()
.thenAccept(teleport -> teleport.execute().thenAccept(result -> {
if (result.successful()) {
plugin.getLocales().getLocale("teleporting_offline_complete", targetUser)
.ifPresent(onlineUser::sendMessage);
}
}));
});
});
final Optional<String> optionalUser = parseStringArg(args, 0);
if (optionalUser.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
return;
}
final Optional<User> targetUserData = plugin.getDatabase()
.getUserDataByName(optionalUser.get())
.map(SavedUser::getUser);
if (targetUserData.isEmpty()) {
plugin.getLocales().getLocale("error_player_not_found", optionalUser.get())
.ifPresent(executor::sendMessage);
return;
}
this.teleportToOfflinePosition(user, targetUserData.get(), args);
}
private void teleportToOfflinePosition(@NotNull OnlineUser user, @NotNull User target, @NotNull String[] args) {
final Optional<Position> position = plugin.getDatabase().getOfflinePosition(target);
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_no_offline_position", target.getUsername())
.ifPresent(user::sendMessage);
return;
}
plugin.getLocales().getLocale("teleporting_offline_player", target.getUsername())
.ifPresent(user::sendMessage);
try {
Teleport.builder(plugin)
.teleporter(user)
.target(position.get())
.toTeleport().execute();
plugin.getLocales().getLocale("teleporting_offline_complete", target.getUsername())
.ifPresent(user::sendMessage);
} catch (TeleportationException e) {
e.displayMessage(user, plugin, args);
}
}
@Override
@@ -59,4 +78,5 @@ public class TpOfflineCommand extends Command implements TabProvider {
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
}
}