feat: Add the option to random teleport cross-server (#647)

* feat: Cross-Server RTP

* feat: Cross-Server RTP

* feat: Cross-Server RTP

* docs

* fix req changes

* amend for api whoops
This commit is contained in:
Preva1l authored and GitHub committed 2024-06-24 13:25:59 +01:00
1 parent dac1b9f981
commit af3a691976
13 files changed
+296 -15

No files matched your search

@@ -23,8 +23,11 @@ import net.kyori.adventure.key.InvalidKeyException;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportBuilder;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.TransactionResolver;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
@@ -113,10 +116,55 @@ public abstract class Broker {
plugin.getManager().homes().updatePublicHomeCache();
plugin.getManager().warps().updateWarpCache();
}
case RTP_LOCATION -> message.getPayload()
.getRTPResponse()
.ifPresentOrElse(response -> {
final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(receiver)
.actions(TransactionResolver.Action.RANDOM_TELEPORT)
.target(response.getPosition());
builder.buildAndComplete(true);
}, () -> plugin.getLocales().getLocale("error_rtp_randomization_timeout")
.ifPresent(receiver::sendMessage));
default -> throw new IllegalStateException("Unexpected value: " + message.getType());
}
}
/**
* Separate handler for RTP Request because it doesn't need a receiver to handle it.
*
* @param message the message to handle
*/
protected void handleRTPRequest(@NotNull Message message) {
if (message.getSourceServer().equals(getServer())) {
return;
}
message.getPayload()
.getRTPRequest()
.ifPresent((request) -> {
Optional<World> world = plugin.getWorlds().stream()
.filter(w -> w.getName().equals(request.getWorldName())).findFirst();
if (world.isEmpty()) {
throw new RuntimeException("%s requested a position in a world we don't have! World: %s"
.formatted(message.getSourceServer(), request.getWorldName()));
}
plugin.getRandomTeleportEngine().getRandomPosition(world.get(), null)
.thenAccept((position) -> {
final Message.Builder builder = Message.builder()
.type(Message.Type.RTP_LOCATION)
.target(request.getUsername());
if (position.isEmpty()) {
builder.payload(Payload.empty());
} else {
builder.payload(Payload.withRTPResponse(
Payload.RTPResponse.of(request.getUsername(), position.get())));
}
builder.build().send(plugin.getMessenger(), request.getUsername());
});
});
}
/**
* Initialize the message broker.
*
@@ -132,6 +180,13 @@ public abstract class Broker {
*/
protected abstract void send(@NotNull Message message, @NotNull OnlineUser sender);
/**
* Send a message to the broker. (For Redis Only)
*
* @param message the message to send
*/
protected abstract void send(@NotNull Message message);
/**
* Move an {@link OnlineUser} to a new server on the proxy network.
*
@@ -75,6 +75,13 @@ public class Message {
broker.send(this, sender);
}
public void send(@NotNull Broker broker, @NotNull String sender) {
this.sender = sender;
this.sourceServer = broker.getServer();
broker.send(this);
}
@NotNull
public Type getType() {
return type;
@@ -173,6 +180,8 @@ public class Message {
UPDATE_HOME,
UPDATE_WARP,
UPDATE_CACHES,
REQUEST_RTP_LOCATION,
RTP_LOCATION,
}
public enum Scope {
@@ -21,7 +21,9 @@ package net.william278.huskhomes.network;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.Value;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.teleport.TeleportRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -38,11 +40,25 @@ public class Payload {
@Expose
private Position position;
@Nullable
@Expose
private World world;
@Nullable
@Expose
@SerializedName("teleport_request")
private TeleportRequest teleportRequest;
@Nullable
@Expose
@SerializedName("rtp_response")
private RTPResponse rtpResponse;
@Nullable
@Expose
@SerializedName("rtp_request")
private RTPRequest rtpRequest;
@Nullable
@Expose
private String string;
@@ -75,6 +91,19 @@ public class Payload {
return payload;
}
/**
* Returns a payload containing a {@link World}.
*
* @param world the world to send
* @return a payload containing the world
*/
@NotNull
public static Payload withWorld(@NotNull World world) {
final Payload payload = new Payload();
payload.world = world;
return payload;
}
/**
* Returns a payload containing a {@link TeleportRequest}.
*
@@ -108,6 +137,26 @@ public class Payload {
return payload;
}
/**
* An RTP Response field.
*/
@NotNull
public static Payload withRTPResponse(@NotNull RTPResponse rtpResponse) {
final Payload payload = new Payload();
payload.rtpResponse = rtpResponse;
return payload;
}
/**
* An RTP Request field.
*/
@NotNull
public static Payload withRTPRequest(@NotNull RTPRequest rtpRequest) {
final Payload payload = new Payload();
payload.rtpRequest = rtpRequest;
return payload;
}
private Payload() {
}
@@ -118,6 +167,12 @@ public class Payload {
return Optional.ofNullable(position);
}
/**
* A world field.
*/
public Optional<World> getWorld() {
return Optional.ofNullable(world);
}
/**
* A teleport request field.
@@ -140,4 +195,29 @@ public class Payload {
return Optional.ofNullable(stringList);
}
/**
* An RTP response.
*/
public Optional<RTPResponse> getRTPResponse() {
return Optional.ofNullable(rtpResponse);
}
/**
* An RTP request.
*/
public Optional<RTPRequest> getRTPRequest() {
return Optional.ofNullable(rtpRequest);
}
@Value(staticConstructor = "of")
public static class RTPResponse {
@Expose String username;
@Expose Position position;
}
@Value(staticConstructor = "of")
public static class RTPRequest {
@Expose String username;
@Expose String worldName;
}
}
@@ -96,6 +96,16 @@ public class PluginMessageBroker extends Broker {
sender.sendPluginMessage(messageWriter.toByteArray());
}
/**
* Send a message to the broker. (For Redis Only)
*
* @param message the message to send
*/
@Override
protected void send(@NotNull Message message) {
throw new IllegalStateException("Tried to send a plugin message without a sender!");
}
@Override
public void changeServer(@NotNull OnlineUser user, @NotNull String server) {
user.dismount().thenRun(() -> {
@@ -100,6 +100,11 @@ public class RedisBroker extends PluginMessageBroker {
plugin.runAsync(() -> subscriber.send(message));
}
@Override
protected void send(@NotNull Message message) {
plugin.runAsync(() -> subscriber.send(message));
}
@Override
@Blocking
public void close() {
@@ -199,6 +204,11 @@ public class RedisBroker extends PluginMessageBroker {
return;
}
if (message.getType() == Message.Type.REQUEST_RTP_LOCATION) {
broker.handleRTPRequest(message);
return;
}
if (message.getScope() == Message.Scope.PLAYER) {
broker.plugin.getOnlineUsers().stream()
.filter(online -> message.getTarget().equals(Message.TARGET_ALL)