mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
fix: make World Gson-safe
This commit is contained in:
10 files changed
+115
-94
No files matched your search
@@ -364,7 +364,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static World adapt(@NotNull org.bukkit.World world) {
|
public static World adapt(@NotNull org.bukkit.World world) {
|
||||||
return World.from(world.getName(), world.getUID(), World.Environment.match(world.getEnvironment().name()));
|
return World.from(
|
||||||
|
world.getName(),
|
||||||
|
world.getUID(),
|
||||||
|
World.Environment.match(world.getEnvironment().name())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ import java.util.stream.Collectors;
|
|||||||
public class BukkitUser extends OnlineUser {
|
public class BukkitUser extends OnlineUser {
|
||||||
|
|
||||||
private final NamespacedKey INVULNERABLE_KEY = new NamespacedKey((BukkitHuskHomes) plugin, "invulnerable");
|
private final NamespacedKey INVULNERABLE_KEY = new NamespacedKey((BukkitHuskHomes) plugin, "invulnerable");
|
||||||
private final Player player;
|
private final Player bukkitPlayer;
|
||||||
|
|
||||||
private BukkitUser(@NotNull Player player, @NotNull BukkitHuskHomes plugin) {
|
private BukkitUser(@NotNull Player bukkitPlayer, @NotNull BukkitHuskHomes plugin) {
|
||||||
super(player.getUniqueId(), player.getName(), plugin);
|
super(bukkitPlayer.getUniqueId(), bukkitPlayer.getName(), plugin);
|
||||||
this.player = player;
|
this.bukkitPlayer = bukkitPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -60,35 +60,35 @@ public class BukkitUser extends OnlineUser {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Player getPlayer() {
|
public Player getPlayer() {
|
||||||
return player;
|
return bukkitPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Position getPosition() {
|
public Position getPosition() {
|
||||||
return BukkitHuskHomes.Adapter.adapt(player.getLocation(), plugin.getServerName());
|
return BukkitHuskHomes.Adapter.adapt(bukkitPlayer.getLocation(), plugin.getServerName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Position> getBedSpawnPosition() {
|
public Optional<Position> getBedSpawnPosition() {
|
||||||
return Optional.ofNullable(player.getBedSpawnLocation())
|
return Optional.ofNullable(bukkitPlayer.getBedSpawnLocation())
|
||||||
.map(loc -> BukkitHuskHomes.Adapter.adapt(loc, plugin.getServerName()));
|
.map(loc -> BukkitHuskHomes.Adapter.adapt(loc, plugin.getServerName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getHealth() {
|
public double getHealth() {
|
||||||
return player.getHealth();
|
return bukkitPlayer.getHealth();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasPermission(@NotNull String node) {
|
public boolean hasPermission(@NotNull String node) {
|
||||||
return player.hasPermission(node);
|
return bukkitPlayer.hasPermission(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public Map<String, Boolean> getPermissions() {
|
public Map<String, Boolean> getPermissions() {
|
||||||
return player.getEffectivePermissions().stream()
|
return bukkitPlayer.getEffectivePermissions().stream()
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
PermissionAttachmentInfo::getPermission,
|
PermissionAttachmentInfo::getPermission,
|
||||||
PermissionAttachmentInfo::getValue, (a, b) -> b
|
PermissionAttachmentInfo::getValue, (a, b) -> b
|
||||||
@@ -99,8 +99,8 @@ public class BukkitUser extends OnlineUser {
|
|||||||
public CompletableFuture<Void> dismount() {
|
public CompletableFuture<Void> dismount() {
|
||||||
final CompletableFuture<Void> future = new CompletableFuture<>();
|
final CompletableFuture<Void> future = new CompletableFuture<>();
|
||||||
plugin.runSync(() -> {
|
plugin.runSync(() -> {
|
||||||
player.leaveVehicle();
|
bukkitPlayer.leaveVehicle();
|
||||||
player.eject();
|
bukkitPlayer.eject();
|
||||||
future.complete(null);
|
future.complete(null);
|
||||||
}, this);
|
}, this);
|
||||||
return future;
|
return future;
|
||||||
@@ -119,29 +119,29 @@ public class BukkitUser extends OnlineUser {
|
|||||||
|
|
||||||
// Run on the appropriate thread scheduler for this platform
|
// Run on the appropriate thread scheduler for this platform
|
||||||
plugin.runSync(() -> {
|
plugin.runSync(() -> {
|
||||||
player.leaveVehicle();
|
bukkitPlayer.leaveVehicle();
|
||||||
player.eject();
|
bukkitPlayer.eject();
|
||||||
if (async || ((BukkitHuskHomes) plugin).getScheduler().isUsingFolia()) {
|
if (async || ((BukkitHuskHomes) plugin).getScheduler().isUsingFolia()) {
|
||||||
PaperLib.teleportAsync(player, location, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
PaperLib.teleportAsync(bukkitPlayer, location, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
player.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
bukkitPlayer.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isMoving() {
|
public boolean isMoving() {
|
||||||
return player.getVelocity().length() >= 0.1;
|
return bukkitPlayer.getVelocity().length() >= 0.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendPluginMessage(byte[] message) {
|
public void sendPluginMessage(byte[] message) {
|
||||||
player.sendPluginMessage((BukkitHuskHomes) plugin, PluginMessageBroker.BUNGEE_CHANNEL_ID, message);
|
bukkitPlayer.sendPluginMessage((BukkitHuskHomes) plugin, PluginMessageBroker.BUNGEE_CHANNEL_ID, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isVanished() {
|
public boolean isVanished() {
|
||||||
return player.getMetadata("vanished")
|
return bukkitPlayer.getMetadata("vanished")
|
||||||
.stream()
|
.stream()
|
||||||
.map(MetadataValue::asBoolean)
|
.map(MetadataValue::asBoolean)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
@@ -150,7 +150,7 @@ public class BukkitUser extends OnlineUser {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasInvulnerability() {
|
public boolean hasInvulnerability() {
|
||||||
return player.getPersistentDataContainer().has(INVULNERABLE_KEY, PersistentDataType.INTEGER);
|
return bukkitPlayer.getPersistentDataContainer().has(INVULNERABLE_KEY, PersistentDataType.INTEGER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -159,17 +159,17 @@ public class BukkitUser extends OnlineUser {
|
|||||||
if (invulnerableTicks <= 0) {
|
if (invulnerableTicks <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
player.getPersistentDataContainer().set(INVULNERABLE_KEY, PersistentDataType.INTEGER, 1);
|
bukkitPlayer.getPersistentDataContainer().set(INVULNERABLE_KEY, PersistentDataType.INTEGER, 1);
|
||||||
player.setInvulnerable(true);
|
bukkitPlayer.setInvulnerable(true);
|
||||||
plugin.runSyncDelayed(this::removeInvulnerabilityIfPermitted, this, invulnerableTicks);
|
plugin.runSyncDelayed(this::removeInvulnerabilityIfPermitted, this, invulnerableTicks);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeInvulnerabilityIfPermitted() {
|
public void removeInvulnerabilityIfPermitted() {
|
||||||
if (this.hasInvulnerability()) {
|
if (this.hasInvulnerability()) {
|
||||||
player.setInvulnerable(false);
|
bukkitPlayer.setInvulnerable(false);
|
||||||
}
|
}
|
||||||
player.getPersistentDataContainer().remove(INVULNERABLE_KEY);
|
bukkitPlayer.getPersistentDataContainer().remove(INVULNERABLE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ dependencies {
|
|||||||
api 'commons-io:commons-io:2.17.0'
|
api 'commons-io:commons-io:2.17.0'
|
||||||
api 'org.apache.commons:commons-text:1.12.0'
|
api 'org.apache.commons:commons-text:1.12.0'
|
||||||
api 'com.google.code.gson:gson:2.11.0'
|
api 'com.google.code.gson:gson:2.11.0'
|
||||||
|
api 'com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2'
|
||||||
api 'com.github.Exlll.ConfigLib:configlib-yaml:v4.5.0'
|
api 'com.github.Exlll.ConfigLib:configlib-yaml:v4.5.0'
|
||||||
api('com.zaxxer:HikariCP:6.0.0') {
|
api('com.zaxxer:HikariCP:6.0.0') {
|
||||||
exclude module: 'slf4j-api'
|
exclude module: 'slf4j-api'
|
||||||
|
|||||||
@@ -19,8 +19,6 @@
|
|||||||
|
|
||||||
package net.william278.huskhomes;
|
package net.william278.huskhomes;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.william278.huskhomes.api.BaseHuskHomesAPI;
|
import net.william278.huskhomes.api.BaseHuskHomesAPI;
|
||||||
import net.william278.huskhomes.command.Command;
|
import net.william278.huskhomes.command.Command;
|
||||||
@@ -57,7 +55,8 @@ import java.util.logging.Level;
|
|||||||
*/
|
*/
|
||||||
public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionProvider, TransactionResolver,
|
public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionProvider, TransactionResolver,
|
||||||
ConfigProvider, DatabaseProvider, BrokerProvider, MetaProvider, HookProvider, RandomTeleportProvider,
|
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;
|
int BSTATS_BUKKIT_PLUGIN_ID = 8430;
|
||||||
|
|
||||||
@@ -252,9 +251,4 @@ public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionP
|
|||||||
return Key.key("huskhomes", joined);
|
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.Expose;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import lombok.AccessLevel;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.position.World;
|
import net.william278.huskhomes.position.World;
|
||||||
@@ -35,7 +36,7 @@ import java.util.Optional;
|
|||||||
/**
|
/**
|
||||||
* Represents a payload sent in a cross-server {@link Message}.
|
* Represents a payload sent in a cross-server {@link Message}.
|
||||||
*/
|
*/
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
public class Payload {
|
public class Payload {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -40,17 +40,20 @@ public class PluginMessageBroker extends Broker {
|
|||||||
/**
|
/**
|
||||||
* The name of BungeeCord's provided plugin channel.
|
* 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>,
|
* @implNote Technically, the effective identifier of this channel is {@code bungeecord:main}, but Spigot remaps
|
||||||
* but Spigot remaps {@code BungeeCord} automatically to the new one (hence BungeeCord is kept for back-compat).
|
* {@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 static final String BUNGEE_CHANNEL_ID = "BungeeCord";
|
||||||
|
|
||||||
public PluginMessageBroker(@NotNull HuskHomes plugin) {
|
protected PluginMessageBroker(@NotNull HuskHomes plugin) {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize() throws IllegalStateException {
|
public void initialize() throws RuntimeException {
|
||||||
plugin.setupPluginMessagingChannels();
|
plugin.setupPluginMessagingChannels();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,16 +73,14 @@ public class PluginMessageBroker extends Broker {
|
|||||||
inputStream.readFully(messageBody);
|
inputStream.readFully(messageBody);
|
||||||
|
|
||||||
try (final DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(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) {
|
} catch (IOException e) {
|
||||||
plugin.log(Level.SEVERE, "Failed to fully read plugin message", e);
|
plugin.log(Level.SEVERE, "Failed to fully read plugin message", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void send(@NotNull Message message, @Nullable OnlineUser sender) {
|
protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
|
||||||
Preconditions.checkNotNull(sender, "Sender cannot be null with a Plugin Message broker");
|
|
||||||
|
|
||||||
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
|
||||||
messageWriter.writeUTF(message.getTargetType().getPluginMessageChannel());
|
messageWriter.writeUTF(message.getTargetType().getPluginMessageChannel());
|
||||||
messageWriter.writeUTF(message.getTarget());
|
messageWriter.writeUTF(message.getTarget());
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ public class RedisBroker extends PluginMessageBroker {
|
|||||||
public void onMessage(@NotNull String channel, @NotNull String encoded) {
|
public void onMessage(@NotNull String channel, @NotNull String encoded) {
|
||||||
final Message message;
|
final Message message;
|
||||||
try {
|
try {
|
||||||
message = broker.plugin.getGson().fromJson(encoded, Message.class);
|
message = broker.plugin.getMessageFromJson(encoded);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
broker.plugin.log(Level.WARNING, "Failed to decode message from Redis: " + e.getMessage());
|
broker.plugin.log(Level.WARNING, "Failed to decode message from Redis: " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -19,78 +19,41 @@
|
|||||||
|
|
||||||
package net.william278.huskhomes.position;
|
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.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a world on a server.
|
* Represents a world on a server.
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor(staticName = "from")
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class World {
|
public class World {
|
||||||
|
|
||||||
|
@Expose
|
||||||
private String name;
|
private String name;
|
||||||
|
@Expose
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
@Expose
|
||||||
@Nullable
|
@Nullable
|
||||||
private Environment environment;
|
@Getter(AccessLevel.NONE)
|
||||||
|
private Environment environment = null;
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static World from(@NotNull String name, @NotNull UUID uuid) {
|
public static World from(@NotNull String name, @NotNull UUID uuid) {
|
||||||
return new World(name, uuid, null);
|
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
|
@NotNull
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public Environment getEnvironment() {
|
public Environment getEnvironment() {
|
||||||
return Optional.ofNullable(environment).orElse(Environment.OVERWORLD);
|
return environment == null ? Environment.OVERWORLD : environment;
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnvironment(@Nullable Environment environment) {
|
|
||||||
this.environment = environment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,6 +65,7 @@ public class World {
|
|||||||
THE_END,
|
THE_END,
|
||||||
CUSTOM;
|
CUSTOM;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static Environment match(@NotNull String name) {
|
public static Environment match(@NotNull String name) {
|
||||||
return switch (name.toLowerCase(Locale.ENGLISH)) {
|
return switch (name.toLowerCase(Locale.ENGLISH)) {
|
||||||
case "overworld" -> OVERWORLD;
|
case "overworld" -> OVERWORLD;
|
||||||
@@ -115,8 +79,9 @@ public class World {
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(@NotNull Object obj) {
|
public boolean equals(@NotNull Object obj) {
|
||||||
if (obj instanceof World world) {
|
if (obj instanceof World world) {
|
||||||
return world.getUuid().equals(getUuid());
|
return this.uuid.equals(world.uuid);
|
||||||
}
|
}
|
||||||
return super.equals(obj);
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
+4
-1
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package net.william278.huskhomes.network;
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
|
import com.fatboyindustrial.gsonjavatime.Converters;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
@@ -89,7 +90,9 @@ public class MessageSerializationTests {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Gson createGson() {
|
private static Gson createGson() {
|
||||||
return new GsonBuilder().create();
|
return Converters.registerOffsetDateTime(new GsonBuilder()
|
||||||
|
.excludeFieldsWithoutExposeAnnotation()
|
||||||
|
).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> provideMessages() {
|
private static Stream<Arguments> provideMessages() {
|
||||||
|
|||||||
Reference in new issue
Block a user