Add /tphere, make /tp instantaneous, add images, slight refactoring

This commit is contained in:
William committed 2022-08-18 20:00:07 +01:00
1 parent e37a55b214
commit 6149ebe983
17 files changed
+198 -50

No files matched your search

@@ -2,7 +2,6 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -15,7 +14,7 @@ public class BackCommand extends CommandBase {
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
plugin.getDatabase().getLastPosition(onlineUser).thenAccept(lastPosition ->
lastPosition.ifPresentOrElse(position -> plugin.getTeleportManager().teleport(onlineUser, position)
lastPosition.ifPresentOrElse(position -> plugin.getTeleportManager().timedTeleport(onlineUser, position)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)),
() -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage)));
}
@@ -23,7 +23,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
// Send the home list if they have homes set. If they have just one home set, teleport the player
switch (homes.size()) {
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result ->
case 1 -> plugin.getTeleportManager().timedTeleport(onlineUser, homes.get(0)).thenAccept(result ->
plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
default -> onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, onlineUser,
plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage,1));
@@ -35,10 +35,10 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.homeName()),
userData -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, userData.user(), homeIdentifier.homeName()),
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
.ifPresent(onlineUser::sendMessage))),
() -> plugin.getTeleportManager().teleportToHome(onlineUser, onlineUser, homeName));
() -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, onlineUser, homeName));
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]")
.ifPresent(onlineUser::sendMessage);
@@ -38,7 +38,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.homeName()),
userData -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, userData.user(), homeIdentifier.homeName()),
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
.ifPresent(onlineUser::sendMessage))),
() -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> {
@@ -22,7 +22,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
OnlineUser userToTeleport = onlineUser;
String teleportingUserName = onlineUser.username;
TeleportCommandTarget targetPosition;
// Validate argument length
@@ -40,13 +40,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3])))
|| (args.length == 2)) ? 1 : 0;
if (coordinatesIndex == 1) {
userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0]))
.findFirst().orElse(plugin.getOnlinePlayers().stream().filter(user -> user.username.startsWith(args[0]))
.findFirst().orElse(null));
if (userToTeleport == null) {
plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage);
return;
}
teleportingUserName = args[0];
}
// Determine the target position (player or coordinates)
@@ -73,15 +67,25 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
}
// Execute the teleport
final OnlineUser teleportingUser = userToTeleport;
final String playerToTeleportName = teleportingUserName;
if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) {
// Teleport players by usernames
assert targetPosition.targetPlayer != null;
CompletableFuture.runAsync(() -> plugin.getTeleportManager().teleportToPlayer(teleportingUser, targetPosition.targetPlayer));
CompletableFuture.runAsync(() -> plugin.getTeleportManager()
.teleportPlayerToPlayerByName(playerToTeleportName, targetPosition.targetPlayer, onlineUser)
.thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse(
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result),
() -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName)
.ifPresent(onlineUser::sendMessage))));
return;
} else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) {
// Teleport players by specified position
assert targetPosition.targetPosition != null;
plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept(
result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result));
plugin.getTeleportManager().teleportPlayerByName(playerToTeleportName, targetPosition.targetPosition, onlineUser)
.thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse(
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result),
() -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName)
.ifPresent(onlineUser::sendMessage)));
return;
}
throw new HuskHomesException("Attempted to execute invalid teleport command operation");
@@ -0,0 +1,34 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpHereCommand extends CommandBase implements TabCompletable {
protected TpHereCommand(@NotNull HuskHomes implementor) {
super("tphere", Permission.COMMAND_TPHERE, implementor, "tpohere");
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length != 1) {
plugin.getLocales().getLocale("error_invalid_syntax", "/tphere <player>")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().teleportPlayerByName(args[0], onlineUser.getPosition().join(), onlineUser);
}
@Override
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
return args.length <= 1 ? plugin.getCache().players.stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
}
}
@@ -27,7 +27,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
});
case 1 -> {
final String warpName = args[0];
plugin.getTeleportManager().teleportToWarp(onlineUser, warpName);
plugin.getTeleportManager().teleportToWarpByName(onlineUser, warpName);
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warp [name]")
.ifPresent(onlineUser::sendMessage);