mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
fix: make World Gson-safe
This commit is contained in:
10 files changed
+115
-94
No files matched your search
@@ -19,8 +19,6 @@
|
||||
|
||||
package net.william278.huskhomes;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.william278.huskhomes.api.BaseHuskHomesAPI;
|
||||
import net.william278.huskhomes.command.Command;
|
||||
@@ -57,7 +55,8 @@ import java.util.logging.Level;
|
||||
*/
|
||||
public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionProvider, TransactionResolver,
|
||||
ConfigProvider, DatabaseProvider, BrokerProvider, MetaProvider, HookProvider, RandomTeleportProvider,
|
||||
AudiencesProvider, UserProvider, TextValidator, ManagerProvider, ListenerProvider, CommandProvider {
|
||||
AudiencesProvider, UserProvider, TextValidator, ManagerProvider, ListenerProvider, CommandProvider,
|
||||
GsonProvider {
|
||||
|
||||
int BSTATS_BUKKIT_PLUGIN_ID = 8430;
|
||||
|
||||
@@ -252,9 +251,4 @@ public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionP
|
||||
return Key.key("huskhomes", joined);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default Gson getGson() {
|
||||
return new GsonBuilder().create();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -19,78 +19,41 @@
|
||||
|
||||
package net.william278.huskhomes.position;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import lombok.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Represents a world on a server.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor(staticName = "from")
|
||||
@NoArgsConstructor
|
||||
public class World {
|
||||
|
||||
@Expose
|
||||
private String name;
|
||||
@Expose
|
||||
private UUID uuid;
|
||||
@Expose
|
||||
@Nullable
|
||||
private Environment environment;
|
||||
|
||||
private World(@NotNull String name, @NotNull UUID uuid, @Nullable Environment environment) {
|
||||
this.setName(name);
|
||||
this.setUuid(uuid);
|
||||
this.setEnvironment(environment);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static World from(@NotNull String name, @NotNull UUID uuid, @NotNull Environment environment) {
|
||||
return new World(name, uuid, environment);
|
||||
}
|
||||
@Getter(AccessLevel.NONE)
|
||||
private Environment environment = null;
|
||||
|
||||
@NotNull
|
||||
public static World from(@NotNull String name, @NotNull UUID uuid) {
|
||||
return new World(name, uuid, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of this world, as defined by the world directory name.
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID of this world, as defined by the {@code uid.dat} file in the world directory.
|
||||
*/
|
||||
@NotNull
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(@NotNull UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Environment of the world ({@link Environment#OVERWORLD}, {@link Environment#NETHER}, {@link Environment#THE_END},
|
||||
* or {@link Environment#CUSTOM}).
|
||||
*
|
||||
* <p>Will return {@link Environment#OVERWORLD} if the environment is null
|
||||
*/
|
||||
@NotNull
|
||||
@SuppressWarnings("unused")
|
||||
public Environment getEnvironment() {
|
||||
return Optional.ofNullable(environment).orElse(Environment.OVERWORLD);
|
||||
}
|
||||
|
||||
public void setEnvironment(@Nullable Environment environment) {
|
||||
this.environment = environment;
|
||||
return environment == null ? Environment.OVERWORLD : environment;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,6 +65,7 @@ public class World {
|
||||
THE_END,
|
||||
CUSTOM;
|
||||
|
||||
@NotNull
|
||||
public static Environment match(@NotNull String name) {
|
||||
return switch (name.toLowerCase(Locale.ENGLISH)) {
|
||||
case "overworld" -> OVERWORLD;
|
||||
@@ -115,8 +79,9 @@ public class World {
|
||||
@Override
|
||||
public boolean equals(@NotNull Object obj) {
|
||||
if (obj instanceof World world) {
|
||||
return world.getUuid().equals(getUuid());
|
||||
return this.uuid.equals(world.uuid);
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* This file is part of HuskHomes, licensed under the Apache License 2.0.
|
||||
*
|
||||
* Copyright (c) William278 <[email protected]>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import com.fatboyindustrial.gsonjavatime.Converters;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.network.Message;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface GsonProvider {
|
||||
|
||||
@NotNull
|
||||
default GsonBuilder getGsonBuilder() {
|
||||
return Converters.registerOffsetDateTime(new GsonBuilder()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default Gson getGson() {
|
||||
return getGsonBuilder().create();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default Message getMessageFromJson(@NotNull String json) throws JsonSyntaxException {
|
||||
return getGson().fromJson(json, Message.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
HuskHomes getPlugin();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user