fix: make World Gson-safe

This commit is contained in:
William committed 2024-11-05 23:22:48 +00:00
1 parent 5ea12b97c4
commit 6f96c0a618
10 files changed
+115 -94

No files matched your search

@@ -21,6 +21,7 @@ package net.william278.huskhomes.network;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
@@ -35,7 +36,7 @@ import java.util.Optional;
/**
* Represents a payload sent in a cross-server {@link Message}.
*/
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Payload {
@Nullable
@@ -40,17 +40,20 @@ public class PluginMessageBroker extends Broker {
/**
* The name of BungeeCord's provided plugin channel.
*
* <p>Internally, this is <a href="https://wiki.vg/Plugin_channels#bungeecord:main">{@code bungeecord:main}</a>,
* but Spigot remaps {@code BungeeCord} automatically to the new one (hence BungeeCord is kept for back-compat).
* @implNote Technically, the effective identifier of this channel is {@code bungeecord:main}, but Spigot remaps
* {@code BungeeCord} automatically to the new one (<a href="https://wiki.vg/Plugin_channels#bungeecord:main">source</a>).
* Spigot's <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/">official documentation</a>
* still instructs usage of {@code BungeeCord} as the name to use, however. It's all a bit inconsistent, so just in case
* it's best to leave it how it is for to maintain backwards compatibility.
*/
public static final String BUNGEE_CHANNEL_ID = "BungeeCord";
public PluginMessageBroker(@NotNull HuskHomes plugin) {
protected PluginMessageBroker(@NotNull HuskHomes plugin) {
super(plugin);
}
@Override
public void initialize() throws IllegalStateException {
public void initialize() throws RuntimeException {
plugin.setupPluginMessagingChannels();
}
@@ -70,16 +73,14 @@ public class PluginMessageBroker extends Broker {
inputStream.readFully(messageBody);
try (final DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(messageBody))) {
super.handle(user, plugin.getGson().fromJson(messageReader.readUTF(), Message.class));
super.handle(user, plugin.getMessageFromJson(messageReader.readUTF()));
} catch (IOException e) {
plugin.log(Level.SEVERE, "Failed to fully read plugin message", e);
}
}
@Override
protected void send(@NotNull Message message, @Nullable OnlineUser sender) {
Preconditions.checkNotNull(sender, "Sender cannot be null with a Plugin Message broker");
protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
messageWriter.writeUTF(message.getTargetType().getPluginMessageChannel());
messageWriter.writeUTF(message.getTarget());
@@ -196,7 +196,7 @@ public class RedisBroker extends PluginMessageBroker {
public void onMessage(@NotNull String channel, @NotNull String encoded) {
final Message message;
try {
message = broker.plugin.getGson().fromJson(encoded, Message.class);
message = broker.plugin.getMessageFromJson(encoded);
} catch (Exception e) {
broker.plugin.log(Level.WARNING, "Failed to decode message from Redis: " + e.getMessage());
return;