mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
SQLite, network messaging, caching, redis, more bukkit implementing
This commit is contained in:
1 parent
28a54b0b4a
commit
052eb57438
24 files changed
+1855
-38
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A message sent cross-proxy to a player
|
||||
*/
|
||||
public class Message {
|
||||
|
||||
/**
|
||||
* A unique ID representing this message
|
||||
*/
|
||||
@NotNull
|
||||
public UUID uuid;
|
||||
|
||||
/**
|
||||
* The type of the message
|
||||
*/
|
||||
@NotNull
|
||||
public MessageType type;
|
||||
|
||||
/**
|
||||
* The kind of message this is - {@link MessageKind#MESSAGE} or a {@link MessageKind#REPLY} to a message.
|
||||
*/
|
||||
@NotNull
|
||||
public MessageKind kind;
|
||||
|
||||
/**
|
||||
* Username of the target player the message is bound for
|
||||
*/
|
||||
@SerializedName("target_player")
|
||||
@NotNull
|
||||
public String targetPlayer;
|
||||
|
||||
/**
|
||||
* Username of the sender of the message
|
||||
*/
|
||||
@NotNull
|
||||
public String sender;
|
||||
|
||||
/**
|
||||
* ID of the cluster this message is for
|
||||
*/
|
||||
@SerializedName("cluster_id")
|
||||
public int clusterId;
|
||||
|
||||
/**
|
||||
* Data to send in the message
|
||||
*/
|
||||
@NotNull
|
||||
public String payload;
|
||||
|
||||
public Message(@NotNull MessageType type, @NotNull String sender, @NotNull String targetPlayer,
|
||||
@NotNull MessagePayload payload, @NotNull MessageKind messageKind, int clusterId) {
|
||||
this.type = type;
|
||||
this.sender = sender;
|
||||
this.targetPlayer = targetPlayer;
|
||||
this.payload = payload.toJson();
|
||||
this.kind = messageKind;
|
||||
this.clusterId = clusterId;
|
||||
this.uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
|
||||
}
|
||||
|
||||
public static Message fromJson(String json) {
|
||||
return new GsonBuilder().setPrettyPrinting().create().fromJson(json, Message.class);
|
||||
}
|
||||
|
||||
public enum MessageType {
|
||||
TP_REQUEST
|
||||
}
|
||||
|
||||
public enum MessageKind {
|
||||
MESSAGE,
|
||||
REPLY
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other instanceof Message message) {
|
||||
return message.uuid.equals(this.uuid);
|
||||
}
|
||||
return super.equals(other);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
|
||||
/**
|
||||
* Represents data that can be sent as a payload in a {@link Message}
|
||||
*/
|
||||
public interface MessagePayload {
|
||||
|
||||
String toJson();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.config.Settings;
|
||||
import net.william278.huskhomes.player.Player;
|
||||
import net.william278.huskhomes.position.Server;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public abstract class NetworkMessenger {
|
||||
|
||||
/**
|
||||
* Name of the network message channel for HuskHomes messages
|
||||
*/
|
||||
public static final String NETWORK_MESSAGE_CHANNEL = "huskhomes:main";
|
||||
|
||||
/**
|
||||
* Map of message UUIDs to messages being processed
|
||||
*/
|
||||
protected HashMap<UUID, CompletableFuture<Message>> processingMessages;
|
||||
|
||||
/**
|
||||
* Future for processing {@link #getServerName(Player)} requests
|
||||
*/
|
||||
protected CompletableFuture<String> serverNameRequest;
|
||||
|
||||
/**
|
||||
* Future for processing {@link #getOnlinePlayerNames(Player)} requests
|
||||
*/
|
||||
protected CompletableFuture<String[]> onlinePlayerNamesRequest;
|
||||
|
||||
/**
|
||||
* Future for processing {@link #getOnlineServers(Player)} requests
|
||||
*/
|
||||
protected CompletableFuture<String[]> onlineServersRequest;
|
||||
|
||||
/**
|
||||
* ID of the HuskHomes network cluster this server is on
|
||||
*/
|
||||
protected int clusterId;
|
||||
|
||||
/**
|
||||
* Initialize the network messenger
|
||||
*
|
||||
* @param implementor Instance of the implementing plugin
|
||||
*/
|
||||
public void initialize(@NotNull HuskHomes implementor) {
|
||||
this.processingMessages = new HashMap<>();
|
||||
this.clusterId = implementor.getSettings().getIntegerValue(Settings.ConfigOption.CLUSTER_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a list of online player usernames from the proxy
|
||||
*
|
||||
* @param requester {@link Player} to send the request
|
||||
* @return Future returning a {@link List} of usernames of players on the proxy network
|
||||
*/
|
||||
public abstract CompletableFuture<String[]> getOnlinePlayerNames(@NotNull Player requester);
|
||||
|
||||
/**
|
||||
* Fetch the name of this server on the proxy network
|
||||
*
|
||||
* @param requester {@link Player} to send the request
|
||||
* @return Future returning the name of this server on the network
|
||||
*/
|
||||
public abstract CompletableFuture<String> getServerName(@NotNull Player requester);
|
||||
|
||||
/**
|
||||
* Fetch a list of online server names proxy
|
||||
*
|
||||
* @param requester {@link Player} to send the request
|
||||
* @return Future returning a {@link List} of online servers connected to the proxy network
|
||||
*/
|
||||
public abstract CompletableFuture<String[]> getOnlineServers(@NotNull Player requester);
|
||||
|
||||
/**
|
||||
* Send a {@link Player} to a target {@link Server} on the proxy network
|
||||
*
|
||||
* @param server The {@link Server} to send the {@link Player} to
|
||||
* @return A future, returning {@code true} if the player was sent.<p>
|
||||
* If the player could not be sent (i.e. the server was offline), this will return {@code false}
|
||||
*/
|
||||
public abstract CompletableFuture<Boolean> sendPlayer(@NotNull Player player, @NotNull Server server);
|
||||
|
||||
/**
|
||||
* Send a network {@link Message} to a target player on the proxy
|
||||
*
|
||||
* @param message The {@link Message} to send
|
||||
* @return A future containing a reply to the {@link Message} that was sent
|
||||
*/
|
||||
public abstract CompletableFuture<Message> sendMessage(@NotNull Player sender, @NotNull Message message);
|
||||
|
||||
/**
|
||||
* Send a reply to a received {@link Message}
|
||||
*
|
||||
* @param reply The reply {@link Message} to send
|
||||
* @return A future completing when the message has been sent
|
||||
*/
|
||||
protected abstract CompletableFuture<Void> sendReply(@NotNull Player replier, @NotNull Message reply);
|
||||
|
||||
/**
|
||||
* Handle and action received network {@link Message}s
|
||||
*
|
||||
* @param receiver The online {@link Player} receiving the message
|
||||
* @param message The received {@link Message}
|
||||
*/
|
||||
protected final void handleMessage(@NotNull Player receiver, @NotNull Message message) {
|
||||
switch (message.kind) {
|
||||
// Handle a message and send reply
|
||||
case MESSAGE -> {
|
||||
// Handle different message types and apply changes for reply message
|
||||
switch (message.type) {
|
||||
case TP_REQUEST -> {
|
||||
message = message;
|
||||
}
|
||||
}
|
||||
|
||||
// Reply with the modified message
|
||||
message.targetPlayer = message.sender;
|
||||
message.sender = receiver.getName();
|
||||
sendReply(receiver, message);
|
||||
}
|
||||
// Handle a reply message
|
||||
case REPLY -> {
|
||||
if (processingMessages.containsKey(message.uuid)) {
|
||||
final Message finalMessage = message;
|
||||
processingMessages.get(message.uuid).completeAsync(() -> finalMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the network messenger
|
||||
*/
|
||||
public abstract void terminate();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user