mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Refactor player location and server name fetching, prune unused locales
This commit is contained in:
1 parent
571f35429d
commit
7e3dab918f
23 files changed
+151
-200
No files matched your search
@@ -209,7 +209,8 @@ public interface HuskHomes {
|
||||
*
|
||||
* @return The server
|
||||
*/
|
||||
CompletableFuture<Server> getServer(@NotNull OnlineUser requester);
|
||||
@NotNull
|
||||
Server getServer(@NotNull OnlineUser requester);
|
||||
|
||||
/**
|
||||
* Returns a list of worlds on the server
|
||||
@@ -219,14 +220,6 @@ public interface HuskHomes {
|
||||
@NotNull
|
||||
List<World> getWorlds();
|
||||
|
||||
/**
|
||||
* Returns true if the position is a valid location on the server
|
||||
*
|
||||
* @param position The {@link Position} to check
|
||||
* @return {@code true} if the position is a valid one on the server; {@code false} otherwise
|
||||
*/
|
||||
boolean isValidPositionOnServer(Position position);
|
||||
|
||||
/**
|
||||
* Returns the plugin version
|
||||
*
|
||||
|
||||
@@ -164,8 +164,8 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
|
||||
default -> Optional.of(new MineDown(""));
|
||||
}).ifPresent(editor::sendMessage));
|
||||
}
|
||||
case "relocate" -> editor.getPosition().thenAccept(position ->
|
||||
plugin.getSavedPositionManager().updateHomePosition(home, position).thenRun(() -> {
|
||||
case "relocate" ->
|
||||
plugin.getSavedPositionManager().updateHomePosition(home, editor.getPosition()).thenRun(() -> {
|
||||
if (home.owner.uuid.equals(editor.uuid)) {
|
||||
editor.sendMessage(plugin.getLocales().getLocale("edit_home_update_location",
|
||||
home.meta.name).orElse(new MineDown("")));
|
||||
@@ -181,7 +181,7 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
|
||||
editor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node))
|
||||
.forEach(editor::sendMessage);
|
||||
}
|
||||
}));
|
||||
});
|
||||
case "privacy" -> {
|
||||
boolean newIsPublic = !home.isPublic;
|
||||
if (editArgs != null && !editArgs.isBlank()) {
|
||||
|
||||
@@ -114,8 +114,8 @@ public class EditWarpCommand extends CommandBase implements TabCompletable, Cons
|
||||
default -> Optional.of(new MineDown(""));
|
||||
}).ifPresent(editor::sendMessage));
|
||||
}
|
||||
case "relocate" -> editor.getPosition().thenAccept(position ->
|
||||
plugin.getSavedPositionManager().updateWarpPosition(warp, position).thenRun(() -> {
|
||||
case "relocate" ->
|
||||
plugin.getSavedPositionManager().updateWarpPosition(warp, editor.getPosition()).thenRun(() -> {
|
||||
editor.sendMessage(plugin.getLocales().getLocale("edit_warp_update_location",
|
||||
warp.meta.name).orElse(new MineDown("")));
|
||||
|
||||
@@ -124,7 +124,7 @@ public class EditWarpCommand extends CommandBase implements TabCompletable, Cons
|
||||
getWarpEditorWindow(warp, false, editor.hasPermission(Permission.COMMAND_WARP.node))
|
||||
.forEach(editor::sendMessage);
|
||||
}
|
||||
}));
|
||||
});
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/editwarp <name> [" + String.join("|", EDIT_WARP_COMPLETIONS) + "] [args]")
|
||||
.ifPresent(editor::sendMessage);
|
||||
|
||||
@@ -58,7 +58,12 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
|
||||
}
|
||||
plugin.getDatabase().getHomes(userData.user()).thenAccept(homes -> {
|
||||
if (homes.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
if (!onlineUser.uuid.equals(userData.user().uuid)) {
|
||||
plugin.getLocales().getLocale("error_no_homes_set_other",
|
||||
userData.user().username).ifPresent(onlineUser::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, userData.user(),
|
||||
|
||||
@@ -39,7 +39,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
final Position userPosition = target.getPosition().join();
|
||||
final Position userPosition = target.getPosition();
|
||||
if (plugin.getSettings().rtpRestrictedWorlds.stream()
|
||||
.anyMatch(worldName -> worldName.equals(userPosition.world.name))) {
|
||||
plugin.getLocales().getLocale("error_rtp_restricted_world")
|
||||
|
||||
@@ -85,7 +85,7 @@ public class SetHomeCommand extends CommandBase {
|
||||
|
||||
// Set the home in the saved position manager
|
||||
final SavedPositionManager.SaveResult setResult = plugin.getSavedPositionManager()
|
||||
.setHome(new PositionMeta(homeName, ""), onlineUser, onlineUser.getPosition().join()).join();
|
||||
.setHome(new PositionMeta(homeName, ""), onlineUser, onlineUser.getPosition()).join();
|
||||
|
||||
// Display feedback of the result of the set operation
|
||||
(switch (setResult.resultType()) {
|
||||
|
||||
@@ -2,12 +2,14 @@ package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.PositionMeta;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SetSpawnCommand extends CommandBase {
|
||||
|
||||
@@ -23,7 +25,8 @@ public class SetSpawnCommand extends CommandBase {
|
||||
return;
|
||||
}
|
||||
|
||||
onlineUser.getPosition().thenAccept(position -> {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
final Position position = onlineUser.getPosition();
|
||||
if (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn) {
|
||||
final Optional<Warp> warp = plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join();
|
||||
if (warp.isPresent()) {
|
||||
|
||||
@@ -26,8 +26,8 @@ public class SetWarpCommand extends CommandBase {
|
||||
}
|
||||
|
||||
private void setWarp(@NotNull OnlineUser onlineUser, @NotNull String warpName) {
|
||||
onlineUser.getPosition().thenAccept(position -> plugin.getSavedPositionManager().setWarp(
|
||||
new PositionMeta(warpName, ""), position).thenAccept(setResult ->
|
||||
plugin.getSavedPositionManager().setWarp(
|
||||
new PositionMeta(warpName, ""), onlineUser.getPosition()).thenAccept(setResult ->
|
||||
(switch (setResult.resultType()) {
|
||||
case SUCCESS -> {
|
||||
assert setResult.savedPosition().isPresent();
|
||||
@@ -38,7 +38,7 @@ public class SetWarpCommand extends CommandBase {
|
||||
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_warp_name_length");
|
||||
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_warp_name_characters");
|
||||
default -> Optional.of(new MineDown(""));
|
||||
}).ifPresent(onlineUser::sendMessage)));
|
||||
}).ifPresent(onlineUser::sendMessage));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public class SpawnCommand extends CommandBase {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
final Optional<? extends Position> position = (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn
|
||||
? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join()
|
||||
: plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser).join())));
|
||||
: plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser))));
|
||||
if (position.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_spawn_not_set").ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||
|
||||
@@ -54,11 +53,12 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
|
||||
}
|
||||
|
||||
// Parse the coordinates and set the target position
|
||||
final Position userPosition = onlineUser.getPosition().join();
|
||||
final Position userPosition = onlineUser.getPosition();
|
||||
final Optional<Position> parsedPosition = Position.parse(userPosition,
|
||||
Arrays.copyOfRange(args, coordinatesIndex, args.length));
|
||||
if (parsedPosition.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/tp <target> <x> <y> <z> [world] [server]")
|
||||
plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/tp <target> <x> <y> <z> [world] [server]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
@@ -71,12 +71,12 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
|
||||
if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) {
|
||||
// Teleport players by usernames
|
||||
assert targetPosition.targetPlayer != null;
|
||||
CompletableFuture.runAsync(() -> plugin.getTeleportManager()
|
||||
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))));
|
||||
.ifPresent(onlineUser::sendMessage)));
|
||||
return;
|
||||
} else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) {
|
||||
// Teleport players by specified position
|
||||
|
||||
@@ -22,7 +22,7 @@ public class TpHereCommand extends CommandBase implements TabCompletable {
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
plugin.getTeleportManager().teleportPlayerByName(args[0], onlineUser.getPosition().join(), onlineUser);
|
||||
plugin.getTeleportManager().teleportPlayerByName(args[0], onlineUser.getPosition(), onlineUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -109,14 +109,14 @@ public class Settings {
|
||||
@KeyPath("general.back_command_return_by_death")
|
||||
public boolean backCommandReturnByDeath = true;
|
||||
|
||||
@KeyPath("general.back_command_on_teleport_event")
|
||||
public boolean backCommandOnTeleportEvent = true;
|
||||
@KeyPath("general.back_command_save_teleport_event")
|
||||
public boolean backCommandSaveOnTeleportEvent = false;
|
||||
|
||||
@KeyPath("general.list_items_per_page")
|
||||
public int listItemsPerPage = 12;
|
||||
|
||||
@KeyPath("general.use_paper_lib")
|
||||
public boolean usePaperLib = true;
|
||||
@KeyPath("general.asynchronous_teleports")
|
||||
public boolean asynchronousTeleports = true;
|
||||
|
||||
@KeyPath("general.play_sound_effects")
|
||||
public boolean playSoundEffects = true;
|
||||
|
||||
@@ -3,13 +3,13 @@ package net.william278.huskhomes.listener;
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.teleport.TeleportType;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -28,16 +28,6 @@ public class EventListener {
|
||||
this.plugin = implementor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle when the first {@link OnlineUser} joins the server
|
||||
*
|
||||
* @param onlineUser {@link OnlineUser} joining the server
|
||||
*/
|
||||
private void handleFirstPlayerJoin(@NotNull OnlineUser onlineUser) {
|
||||
// Cache the server name
|
||||
plugin.getServer(onlineUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle when a {@link OnlineUser} joins the server
|
||||
*
|
||||
@@ -45,50 +35,52 @@ public class EventListener {
|
||||
*/
|
||||
protected final void handlePlayerJoin(@NotNull OnlineUser onlineUser) {
|
||||
// Handle the first player joining the server
|
||||
if (firstUser) {
|
||||
handleFirstPlayerJoin(onlineUser);
|
||||
firstUser = false;
|
||||
}
|
||||
|
||||
// Ensure the player is present on the database first
|
||||
plugin.getDatabase().ensureUser(onlineUser).thenRun(() -> {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
if (firstUser) {
|
||||
plugin.getServer(onlineUser);
|
||||
firstUser = false;
|
||||
}
|
||||
}).thenRun(() -> {
|
||||
// Ensure the player is present on the database first
|
||||
plugin.getDatabase().ensureUser(onlineUser).join();
|
||||
}).thenRun(() -> {
|
||||
// If the server is in proxy mode, check if the player is teleporting cross-server and handle
|
||||
if (plugin.getSettings().crossServer) {
|
||||
plugin.getDatabase().getCurrentTeleport(onlineUser).thenAccept(teleport -> teleport.ifPresent(activeTeleport -> {
|
||||
// Handle cross-server respawn
|
||||
if (activeTeleport.type == TeleportType.RESPAWN) {
|
||||
final Optional<Position> bedPosition = onlineUser.getBedSpawnPosition().join();
|
||||
if (bedPosition.isEmpty()) {
|
||||
plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser).join()))
|
||||
.ifPresent(onlineUser::teleport);
|
||||
onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid");
|
||||
// Handle cross-server respawn
|
||||
if (activeTeleport.type == TeleportType.RESPAWN) {
|
||||
final Optional<Position> bedPosition = onlineUser.getBedSpawnPosition();
|
||||
if (bedPosition.isEmpty()) {
|
||||
plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser)))
|
||||
.ifPresent(position -> onlineUser.teleport(position, plugin.getSettings().asynchronousTeleports));
|
||||
onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid");
|
||||
} else {
|
||||
onlineUser.teleport(bedPosition.get(), plugin.getSettings().asynchronousTeleports).join();
|
||||
}
|
||||
plugin.getDatabase().setCurrentTeleport(onlineUser, null).thenRun(() ->
|
||||
plugin.getDatabase().setRespawnPosition(onlineUser, bedPosition.orElse(null))
|
||||
.join()).join();
|
||||
return;
|
||||
}
|
||||
|
||||
// Teleport the player locally
|
||||
onlineUser.teleport(activeTeleport.target, plugin.getSettings().asynchronousTeleports).thenAccept(teleportResult -> {
|
||||
if (!teleportResult.successful) {
|
||||
plugin.getLocales().getLocale("error_invalid_on_arrival")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
} else {
|
||||
onlineUser.teleport(bedPosition.get()).join();
|
||||
plugin.getLocales().getLocale("teleporting_complete")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
plugin.getDatabase().setCurrentTeleport(onlineUser, null).thenRun(() ->
|
||||
plugin.getDatabase().setRespawnPosition(onlineUser, bedPosition.orElse(null))
|
||||
.join()).join();
|
||||
return;
|
||||
}
|
||||
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))
|
||||
.join();
|
||||
})).join();
|
||||
|
||||
// Teleport the player locally
|
||||
onlineUser.teleport(activeTeleport.target).thenAccept(teleportResult -> {
|
||||
if (!teleportResult.successful) {
|
||||
plugin.getLocales().getLocale("error_invalid_on_arrival")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("teleporting_complete")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))
|
||||
.join();
|
||||
}))
|
||||
.join();
|
||||
// Update the player list
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
plugin.getCache().updateOnlinePlayerList(plugin, onlineUser);
|
||||
}
|
||||
|
||||
}).thenRun(() -> {
|
||||
// Get this user's tp-ignore state and set locally
|
||||
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> {
|
||||
if (userData.isPresent()) {
|
||||
@@ -103,17 +95,12 @@ public class EventListener {
|
||||
}
|
||||
}
|
||||
}).join();
|
||||
|
||||
}).thenRun(() -> {
|
||||
// Cache this user's homes
|
||||
plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> plugin.getCache()
|
||||
.homes.put(onlineUser.uuid, homes
|
||||
.stream().map(home -> home.meta.name)
|
||||
plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> plugin.getCache().homes.put(onlineUser.uuid,
|
||||
homes.stream()
|
||||
.map(home -> home.meta.name)
|
||||
.collect(Collectors.toList()))).join();
|
||||
|
||||
// Ensure the server has been set
|
||||
if (plugin.getOnlinePlayers().size() == 1) {
|
||||
plugin.getServer(onlineUser);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -136,8 +123,7 @@ public class EventListener {
|
||||
}
|
||||
|
||||
// Set offline position
|
||||
onlineUser.getPosition().thenAcceptAsync(position -> plugin.getDatabase()
|
||||
.setOfflinePosition(onlineUser, position));
|
||||
plugin.getDatabase().setOfflinePosition(onlineUser, onlineUser.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +135,7 @@ public class EventListener {
|
||||
// Set the player's last position to where they died
|
||||
if (plugin.getSettings().backCommandReturnByDeath
|
||||
&& onlineUser.hasPermission(Permission.COMMAND_BACK_RETURN_BY_DEATH.node)) {
|
||||
onlineUser.getPosition().thenAccept(position -> plugin.getDatabase().setLastPosition(onlineUser, position).join());
|
||||
plugin.getDatabase().setLastPosition(onlineUser, onlineUser.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +154,7 @@ public class EventListener {
|
||||
// Respawn the player cross-server if needed
|
||||
if (plugin.getSettings().crossServer && plugin.getSettings().globalRespawning) {
|
||||
plugin.getDatabase().getRespawnPosition(onlineUser).thenAccept(position -> position.ifPresent(respawnPosition -> {
|
||||
if (!respawnPosition.server.equals(plugin.getServer(onlineUser).join())) {
|
||||
if (!respawnPosition.server.equals(plugin.getServer(onlineUser))) {
|
||||
plugin.getTeleportManager().teleport(onlineUser, respawnPosition, TeleportType.RESPAWN).thenAccept(
|
||||
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result));
|
||||
}
|
||||
@@ -183,10 +169,12 @@ public class EventListener {
|
||||
* @param sourcePosition the source {@link Position} they came from
|
||||
*/
|
||||
protected final void handlePlayerTeleport(@NotNull OnlineUser onlineUser, @NotNull Position sourcePosition) {
|
||||
if (!plugin.getSettings().backCommandOnTeleportEvent) return;
|
||||
if (!plugin.getSettings().backCommandSaveOnTeleportEvent) return;
|
||||
|
||||
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData ->
|
||||
userData.ifPresent(data -> plugin.getDatabase().setLastPosition(data.user(), sourcePosition).join()));
|
||||
plugin.getDatabase().getUserData(onlineUser.uuid)
|
||||
.thenAccept(userData -> userData.ifPresent(data -> plugin.getDatabase()
|
||||
.setLastPosition(data.user(), sourcePosition).join()))
|
||||
.join();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -155,7 +155,7 @@ public abstract class NetworkMessenger {
|
||||
message.payload = MessagePayload.empty();
|
||||
}
|
||||
}
|
||||
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition().join());
|
||||
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition());
|
||||
case TELEPORT_REQUEST -> {
|
||||
if (message.payload.teleportRequest != null) {
|
||||
plugin.getRequestManager().sendLocalTeleportRequest(message.payload.teleportRequest, receiver);
|
||||
|
||||
@@ -27,7 +27,7 @@ public abstract class OnlineUser extends User {
|
||||
*
|
||||
* @return the player's current {@link Position}
|
||||
*/
|
||||
public abstract CompletableFuture<Position> getPosition();
|
||||
public abstract Position getPosition();
|
||||
|
||||
/**
|
||||
* Returns the player's current bed or respawn anchor {@link Position}
|
||||
@@ -35,14 +35,7 @@ public abstract class OnlineUser extends User {
|
||||
* @return an optional with the player's current bed or respawn anchor {@link Position} if it has been set,
|
||||
* otherwise an {@link Optional#empty()}
|
||||
*/
|
||||
public abstract CompletableFuture<Optional<Position>> getBedSpawnPosition();
|
||||
|
||||
/**
|
||||
* Returns the current local {@link Location} of this player
|
||||
*
|
||||
* @return the player's current {@link Location} on the server
|
||||
*/
|
||||
public abstract Location getLocation();
|
||||
public abstract Optional<Position> getBedSpawnPosition();
|
||||
|
||||
/**
|
||||
* Returns the health of this player
|
||||
@@ -92,9 +85,10 @@ public abstract class OnlineUser extends User {
|
||||
/**
|
||||
* Teleport a player to the specified {@link Location}
|
||||
*
|
||||
* @param location the {@link Location} to teleport the player to
|
||||
* @param location the {@link Location} to teleport the player to
|
||||
* @param asynchronous if the teleport should be asynchronous
|
||||
*/
|
||||
public abstract CompletableFuture<TeleportResult> teleport(Location location);
|
||||
public abstract CompletableFuture<TeleportResult> teleport(@NotNull Location location, boolean asynchronous);
|
||||
|
||||
/**
|
||||
* Get the maximum number of homes this user may set
|
||||
|
||||
@@ -21,6 +21,11 @@ public class Position extends Location {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public Position(@NotNull Location location, @NotNull Server server) {
|
||||
super(location.x, location.y, location.z, location.yaw, location.pitch, location.world);
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Position() {
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class TeleportRequest {
|
||||
protected TeleportRequest(@NotNull OnlineUser requester, @NotNull RequestType requestType,
|
||||
final long expiryTime) {
|
||||
this.requesterName = requester.username;
|
||||
this.requesterPosition = requester.getPosition().join();
|
||||
this.requesterPosition = requester.getPosition();
|
||||
this.type = requestType;
|
||||
this.status = RequestStatus.PENDING;
|
||||
this.expiryTime = expiryTime;
|
||||
|
||||
@@ -122,8 +122,8 @@ public class TeleportManager {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
final Optional<OnlineUser> localPlayer = plugin.findPlayer(targetPlayer);
|
||||
if (localPlayer.isPresent()) {
|
||||
timedTeleport(onlineUser, localPlayer.get().getPosition().join())
|
||||
.thenAccept(result -> finishTeleport(onlineUser, result));
|
||||
timedTeleport(onlineUser, localPlayer.get().getPosition()).thenAccept(
|
||||
result -> finishTeleport(onlineUser, result));
|
||||
} else if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
getPlayerPositionByName(onlineUser, targetPlayer).thenAccept(optionalPosition -> {
|
||||
@@ -157,8 +157,7 @@ public class TeleportManager {
|
||||
if (localPlayer.isPresent()) {
|
||||
return teleport(localPlayer.get(), position).thenApply(Optional::of);
|
||||
}
|
||||
if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
if (plugin.getSettings().crossServer && plugin.getNetworkMessenger() != null) {
|
||||
return plugin.getNetworkMessenger().sendMessage(requester,
|
||||
new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST,
|
||||
requester.username,
|
||||
@@ -187,8 +186,7 @@ public class TeleportManager {
|
||||
public CompletableFuture<Optional<TeleportResult>> teleportPlayerToPlayerByName(@NotNull String playerName,
|
||||
@NotNull String targetPlayer,
|
||||
@NotNull OnlineUser requester) {
|
||||
final Optional<Position> localPositionTarget = plugin.findPlayer(targetPlayer)
|
||||
.map(onlineUser -> onlineUser.getPosition().join());
|
||||
final Optional<Position> localPositionTarget = plugin.findPlayer(targetPlayer).map(OnlineUser::getPosition);
|
||||
if (localPositionTarget.isPresent()) {
|
||||
return teleportPlayerByName(playerName, localPositionTarget.get(), requester);
|
||||
}
|
||||
@@ -211,7 +209,7 @@ public class TeleportManager {
|
||||
@NotNull String playerName) {
|
||||
final Optional<OnlineUser> localPlayer = plugin.findPlayer(playerName);
|
||||
if (localPlayer.isPresent()) {
|
||||
return localPlayer.get().getPosition().thenApply(Optional::of);
|
||||
return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition()));
|
||||
}
|
||||
if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
@@ -324,16 +322,18 @@ public class TeleportManager {
|
||||
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position,
|
||||
@NotNull TeleportType teleportType) {
|
||||
final Teleport teleport = new Teleport(onlineUser, position, teleportType);
|
||||
return onlineUser.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase()
|
||||
.setLastPosition(onlineUser, preTeleportPosition) // Update the player's last position
|
||||
.thenApply(ignored -> plugin.getServer(onlineUser).thenApply(server -> {
|
||||
// Teleport player locally, or across server depending on need
|
||||
if (position.server.equals(server)) {
|
||||
return onlineUser.teleport(teleport.target).join();
|
||||
} else {
|
||||
return teleportCrossServer(onlineUser, teleport).join();
|
||||
}
|
||||
}).join()).join());
|
||||
|
||||
// Update the player's last position
|
||||
if (!plugin.getSettings().backCommandSaveOnTeleportEvent) {
|
||||
plugin.getDatabase().setLastPosition(onlineUser, onlineUser.getPosition());
|
||||
}
|
||||
|
||||
// Teleport player locally, or across server depending on need
|
||||
if (position.server.equals(plugin.getServer(onlineUser))) {
|
||||
return onlineUser.teleport(teleport.target, plugin.getSettings().asynchronousTeleports);
|
||||
} else {
|
||||
return teleportCrossServer(onlineUser, teleport);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.teleport;
|
||||
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -12,7 +11,7 @@ public class TimedTeleport {
|
||||
|
||||
private final OnlineUser onlineUser;
|
||||
private final Position targetPosition;
|
||||
private final Location startLocation;
|
||||
private final Position startLocation;
|
||||
private final double startHealth;
|
||||
protected int timeLeft;
|
||||
public boolean cancelled = false;
|
||||
@@ -20,7 +19,7 @@ public class TimedTeleport {
|
||||
protected TimedTeleport(@NotNull OnlineUser onlineUser, @NotNull Position targetPosition, int duration) {
|
||||
this.onlineUser = onlineUser;
|
||||
this.targetPosition = targetPosition;
|
||||
this.startLocation = onlineUser.getLocation();
|
||||
this.startLocation = onlineUser.getPosition();
|
||||
this.startHealth = onlineUser.getHealth();
|
||||
this.timeLeft = duration;
|
||||
}
|
||||
@@ -49,9 +48,9 @@ public class TimedTeleport {
|
||||
*/
|
||||
public boolean hasMoved() {
|
||||
final double maxMovementDistance = 0.1d;
|
||||
double movementDistance = Math.abs(startLocation.x - onlineUser.getLocation().x) +
|
||||
Math.abs(startLocation.y - onlineUser.getLocation().y) +
|
||||
Math.abs(startLocation.z - onlineUser.getLocation().z);
|
||||
double movementDistance = Math.abs(startLocation.x - onlineUser.getPosition().x) +
|
||||
Math.abs(startLocation.y - onlineUser.getPosition().y) +
|
||||
Math.abs(startLocation.z - onlineUser.getPosition().z);
|
||||
return movementDistance > maxMovementDistance;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user