Networking rework, further refactoring

This commit is contained in:
William278 committed 2023-03-02 22:51:50 +00:00
1 parent 1cebdac9b8
commit 4c90e5498c
88 files changed
+1116 -1364

No files matched your search

@@ -1,8 +1,8 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -2,18 +2,18 @@ package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.network.Request;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/**
* Represents a teleport in the process of being executed
@@ -55,10 +55,10 @@ public class Teleport {
public final TeleportType type;
/**
* {@link Settings.EconomyAction}s to be checked against the {@link #executor executor}'s balance
* {@link EconomyHook.EconomyAction}s to be checked against the {@link #executor executor}'s balance
*/
@NotNull
public final Set<Settings.EconomyAction> economyActions;
public final Set<EconomyHook.EconomyAction> economyActions;
/**
* Whether to update the {@link #teleporter teleporter}'s last position (i.e. their {@code /back} position)
@@ -69,7 +69,7 @@ public class Teleport {
* <b>Internal</b> - use TeleportBuilder to instantiate a teleport
*/
protected Teleport(@Nullable User teleporter, @NotNull OnlineUser executor, @Nullable Position target,
@NotNull TeleportType type, @NotNull Set<Settings.EconomyAction> economyActions,
@NotNull TeleportType type, @NotNull Set<EconomyHook.EconomyAction> economyActions,
final boolean updateLastPosition, @NotNull HuskHomes plugin) {
this.teleporter = teleporter;
this.executor = executor;
@@ -112,7 +112,7 @@ public class Teleport {
}
// Check economy actions
for (Settings.EconomyAction economyAction : economyActions) {
for (EconomyHook.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
@@ -183,20 +183,9 @@ public class Teleport {
}
// If the target position is on another server, execute a cross-server teleport
final CompletableFuture<TeleportResult> teleportFuture = new CompletableFuture<>();
plugin.getDatabase().setCurrentTeleport(teleporter, this);
plugin.getMessenger()
.sendPlayer(teleporter, target.getServer())
.thenApply(completed -> completed
? TeleportResult.COMPLETED_CROSS_SERVER
: TeleportResult.FAILED_INVALID_SERVER)
.orTimeout(10, TimeUnit.SECONDS)
.exceptionally(throwable -> {
plugin.getLoggingAdapter().log(Level.WARNING, "Cross-server teleport timed out for " + teleporter.username);
plugin.getDatabase().setCurrentTeleport(teleporter, null);
return TeleportResult.FAILED_INVALID_SERVER;
});
return teleportFuture;
plugin.getMessenger().changeServer(teleporter, target.getServer());
return CompletableFuture.completedFuture(TeleportResult.COMPLETED_CROSS_SERVER);
}
/**
@@ -212,20 +201,13 @@ public class Teleport {
assert teleporter != null;
// Send a network message to a user on another server to teleport them to the target position
return Request.builder()
.withType(Request.MessageType.TELEPORT_TO_POSITION_REQUEST)
.withPayload(Payload.withPosition(target))
.withTargetPlayer(teleporter.username)
.build().send(executor, plugin)
.thenApply(result -> {
if (result.isPresent()) {
final Request reply = result.get();
if (reply.getPayload().resultState != null) {
return reply.getPayload().resultState;
}
}
return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
});
return Message.builder()
.type(Message.Type.TELEPORT_TO_POSITION_REQUEST)
.payload(Payload.withPosition(target))
.target(teleporter.getUsername())
.build().send(plugin.getMessenger(), executor)
.thenApply(result -> Optional.ofNullable(result.getPayload().getResultState())
.orElse(TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED));
}
}
@@ -1,10 +1,10 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Request;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
@@ -43,12 +43,12 @@ public class TeleportBuilder {
private CompletableFuture<Position> target;
/**
* List of {@link Settings.EconomyAction}s to check against.
* List of {@link EconomyHook.EconomyAction}s to check against.
* <p>
* Note that these are checked against the <i>{@link #executor executor}</i> of the teleport;
* not necessarily the one doing the teleporting
*/
private final Set<Settings.EconomyAction> economyActions = new HashSet<>();
private final Set<EconomyHook.EconomyAction> economyActions = new HashSet<>();
/**
* The type of teleport. Defaults to {@link TeleportType#TELEPORT}
@@ -85,18 +85,7 @@ public class TeleportBuilder {
* @return The {@link TeleportBuilder} instance
*/
public TeleportBuilder setTeleporter(@NotNull String teleporterUsername) {
this.teleporter = CompletableFuture.supplyAsync(() -> plugin
.findOnlinePlayer(teleporterUsername)
.map(onlineUser -> (User) onlineUser)
.or(() -> {
if (plugin.getSettings().isCrossServer()) {
return plugin.getMessenger()
.findPlayer(executor, teleporterUsername).join()
.map(username -> new User(UUID.randomUUID(), username));
}
return Optional.empty();
})
.orElse(null));
this.teleporter = teleporterUsername;
return this;
}
@@ -148,12 +137,12 @@ public class TeleportBuilder {
/**
* Set the economy actions to check against during the teleport
*
* @param economyActions The {@link Settings.EconomyAction}s to check against
* @param economyActions The {@link EconomyHook.EconomyAction}s to check against
* @return The {@link TeleportBuilder} instance
* @implNote These are checked against the <i>{@link #executor executor}</i> of the teleport;
* not necessarily the one doing the teleporting
*/
public TeleportBuilder setEconomyActions(@NotNull Set<Settings.EconomyAction> economyActions) {
public TeleportBuilder setEconomyActions(@NotNull Set<EconomyHook.EconomyAction> economyActions) {
this.economyActions.addAll(economyActions);
return this;
}
@@ -161,12 +150,12 @@ public class TeleportBuilder {
/**
* Set the economy actions to check against during the teleport
*
* @param economyActions The {@link Settings.EconomyAction}s to check against
* @param economyActions The {@link EconomyHook.EconomyAction}s to check against
* @return The {@link TeleportBuilder} instance
* @implNote These are checked against the <i>{@link #executor executor}</i> of the teleport;
* not necessarily the one doing the teleporting
*/
public TeleportBuilder setEconomyActions(@NotNull Settings.EconomyAction... economyActions) {
public TeleportBuilder setEconomyActions(@NotNull EconomyHook.EconomyAction... economyActions) {
this.economyActions.addAll(Arrays.asList(economyActions));
return this;
}
@@ -243,7 +232,7 @@ public class TeleportBuilder {
.withType(Request.MessageType.POSITION_REQUEST)
.withTargetPlayer(playerName)
.build().send(executor, plugin)
.thenApply(reply -> reply.map(message -> message.getPayload().position)).join();
.thenApply(reply -> reply.map(message -> message.getPayload().getPosition())).join();
});
}
return CompletableFuture.supplyAsync(Optional::empty);
@@ -1,7 +1,7 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import org.jetbrains.annotations.NotNull;
@@ -3,7 +3,8 @@ package net.william278.huskhomes.teleport;
import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -24,7 +25,7 @@ public class TimedTeleport extends Teleport {
private boolean cancelled = false;
protected TimedTeleport(@NotNull OnlineUser teleporter, @NotNull OnlineUser executor, @NotNull Position target,
@NotNull TeleportType type, int warmupTime, @NotNull Set<Settings.EconomyAction> economyActions,
@NotNull TeleportType type, int warmupTime, @NotNull Set<EconomyHook.EconomyAction> economyActions,
final boolean updateLastPosition, @NotNull HuskHomes plugin) {
super(teleporter, executor, target, type, economyActions, updateLastPosition, plugin);
this.plugin = plugin;
@@ -53,13 +54,13 @@ public class TimedTeleport extends Teleport {
}
// Check if the teleporter is already warming up to teleport
if (plugin.getCache().getCurrentlyOnWarmup().contains(teleporter.uuid)) {
if (plugin.getCache().getCurrentlyOnWarmup().contains(teleporter.getUuid())) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_ALREADY_TELEPORTING)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
}
// Check economy actions
for (Settings.EconomyAction economyAction : economyActions) {
for (EconomyHook.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
@@ -98,7 +99,7 @@ public class TimedTeleport extends Teleport {
}
// Mark the player as warming up and display the message
plugin.getCache().getCurrentlyOnWarmup().add(teleporter.uuid);
plugin.getCache().getCurrentlyOnWarmup().add(teleporter.getUuid());
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(timeLeft))
.ifPresent(teleporter::sendMessage);
@@ -118,7 +119,7 @@ public class TimedTeleport extends Teleport {
// Tick (decrement) the timed teleport timer and end it if done
if (tickWarmup()) {
plugin.getCache().getCurrentlyOnWarmup().remove(teleporter.uuid);
plugin.getCache().getCurrentlyOnWarmup().remove(teleporter.getUuid());
timedTeleportFuture.complete(null);
executor.shutdown();
}