Skip to content

Commit 6f96c0a

Browse files
committed
fix: make World Gson-safe
1 parent 5ea12b9 commit 6f96c0a

File tree

10 files changed

+115
-94
lines changed

10 files changed

+115
-94
lines changed

bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,11 @@ public static org.bukkit.World adapt(@NotNull World world) {
364364

365365
@NotNull
366366
public static World adapt(@NotNull org.bukkit.World world) {
367-
return World.from(world.getName(), world.getUID(), World.Environment.match(world.getEnvironment().name()));
367+
return World.from(
368+
world.getName(),
369+
world.getUID(),
370+
World.Environment.match(world.getEnvironment().name())
371+
);
368372
}
369373

370374
}

bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
public class BukkitUser extends OnlineUser {
4646

4747
private final NamespacedKey INVULNERABLE_KEY = new NamespacedKey((BukkitHuskHomes) plugin, "invulnerable");
48-
private final Player player;
48+
private final Player bukkitPlayer;
4949

50-
private BukkitUser(@NotNull Player player, @NotNull BukkitHuskHomes plugin) {
51-
super(player.getUniqueId(), player.getName(), plugin);
52-
this.player = player;
50+
private BukkitUser(@NotNull Player bukkitPlayer, @NotNull BukkitHuskHomes plugin) {
51+
super(bukkitPlayer.getUniqueId(), bukkitPlayer.getName(), plugin);
52+
this.bukkitPlayer = bukkitPlayer;
5353
}
5454

5555
@NotNull
@@ -60,35 +60,35 @@ public static BukkitUser adapt(@NotNull Player player, @NotNull BukkitHuskHomes
6060

6161
@NotNull
6262
public Player getPlayer() {
63-
return player;
63+
return bukkitPlayer;
6464
}
6565

6666
@Override
6767
public Position getPosition() {
68-
return BukkitHuskHomes.Adapter.adapt(player.getLocation(), plugin.getServerName());
68+
return BukkitHuskHomes.Adapter.adapt(bukkitPlayer.getLocation(), plugin.getServerName());
6969
}
7070

7171
@Override
7272
public Optional<Position> getBedSpawnPosition() {
73-
return Optional.ofNullable(player.getBedSpawnLocation())
73+
return Optional.ofNullable(bukkitPlayer.getBedSpawnLocation())
7474
.map(loc -> BukkitHuskHomes.Adapter.adapt(loc, plugin.getServerName()));
7575
}
7676

7777
@Override
7878
public double getHealth() {
79-
return player.getHealth();
79+
return bukkitPlayer.getHealth();
8080
}
8181

8282
@Override
8383
public boolean hasPermission(@NotNull String node) {
84-
return player.hasPermission(node);
84+
return bukkitPlayer.hasPermission(node);
8585
}
8686

8787

8888
@Override
8989
@NotNull
9090
public Map<String, Boolean> getPermissions() {
91-
return player.getEffectivePermissions().stream()
91+
return bukkitPlayer.getEffectivePermissions().stream()
9292
.collect(Collectors.toMap(
9393
PermissionAttachmentInfo::getPermission,
9494
PermissionAttachmentInfo::getValue, (a, b) -> b
@@ -99,8 +99,8 @@ public Map<String, Boolean> getPermissions() {
9999
public CompletableFuture<Void> dismount() {
100100
final CompletableFuture<Void> future = new CompletableFuture<>();
101101
plugin.runSync(() -> {
102-
player.leaveVehicle();
103-
player.eject();
102+
bukkitPlayer.leaveVehicle();
103+
bukkitPlayer.eject();
104104
future.complete(null);
105105
}, this);
106106
return future;
@@ -119,29 +119,29 @@ public void teleportLocally(@NotNull Location target, boolean async) throws Tele
119119

120120
// Run on the appropriate thread scheduler for this platform
121121
plugin.runSync(() -> {
122-
player.leaveVehicle();
123-
player.eject();
122+
bukkitPlayer.leaveVehicle();
123+
bukkitPlayer.eject();
124124
if (async || ((BukkitHuskHomes) plugin).getScheduler().isUsingFolia()) {
125-
PaperLib.teleportAsync(player, location, PlayerTeleportEvent.TeleportCause.PLUGIN);
125+
PaperLib.teleportAsync(bukkitPlayer, location, PlayerTeleportEvent.TeleportCause.PLUGIN);
126126
return;
127127
}
128-
player.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
128+
bukkitPlayer.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
129129
}, this);
130130
}
131131

132132
@Override
133133
public boolean isMoving() {
134-
return player.getVelocity().length() >= 0.1;
134+
return bukkitPlayer.getVelocity().length() >= 0.1;
135135
}
136136

137137
@Override
138138
public void sendPluginMessage(byte[] message) {
139-
player.sendPluginMessage((BukkitHuskHomes) plugin, PluginMessageBroker.BUNGEE_CHANNEL_ID, message);
139+
bukkitPlayer.sendPluginMessage((BukkitHuskHomes) plugin, PluginMessageBroker.BUNGEE_CHANNEL_ID, message);
140140
}
141141

142142
@Override
143143
public boolean isVanished() {
144-
return player.getMetadata("vanished")
144+
return bukkitPlayer.getMetadata("vanished")
145145
.stream()
146146
.map(MetadataValue::asBoolean)
147147
.findFirst()
@@ -150,7 +150,7 @@ public boolean isVanished() {
150150

151151
@Override
152152
public boolean hasInvulnerability() {
153-
return player.getPersistentDataContainer().has(INVULNERABLE_KEY, PersistentDataType.INTEGER);
153+
return bukkitPlayer.getPersistentDataContainer().has(INVULNERABLE_KEY, PersistentDataType.INTEGER);
154154
}
155155

156156
@Override
@@ -159,17 +159,17 @@ public void handleInvulnerability() {
159159
if (invulnerableTicks <= 0) {
160160
return;
161161
}
162-
player.getPersistentDataContainer().set(INVULNERABLE_KEY, PersistentDataType.INTEGER, 1);
163-
player.setInvulnerable(true);
162+
bukkitPlayer.getPersistentDataContainer().set(INVULNERABLE_KEY, PersistentDataType.INTEGER, 1);
163+
bukkitPlayer.setInvulnerable(true);
164164
plugin.runSyncDelayed(this::removeInvulnerabilityIfPermitted, this, invulnerableTicks);
165165
}
166166

167167
@Override
168168
public void removeInvulnerabilityIfPermitted() {
169169
if (this.hasInvulnerability()) {
170-
player.setInvulnerable(false);
170+
bukkitPlayer.setInvulnerable(false);
171171
}
172-
player.getPersistentDataContainer().remove(INVULNERABLE_KEY);
172+
bukkitPlayer.getPersistentDataContainer().remove(INVULNERABLE_KEY);
173173
}
174174

175175
}

common/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
api 'commons-io:commons-io:2.17.0'
1010
api 'org.apache.commons:commons-text:1.12.0'
1111
api 'com.google.code.gson:gson:2.11.0'
12+
api 'com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2'
1213
api 'com.github.Exlll.ConfigLib:configlib-yaml:v4.5.0'
1314
api('com.zaxxer:HikariCP:6.0.0') {
1415
exclude module: 'slf4j-api'

common/src/main/java/net/william278/huskhomes/HuskHomes.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package net.william278.huskhomes;
2121

22-
import com.google.gson.Gson;
23-
import com.google.gson.GsonBuilder;
2422
import net.kyori.adventure.key.Key;
2523
import net.william278.huskhomes.api.BaseHuskHomesAPI;
2624
import net.william278.huskhomes.command.Command;
@@ -57,7 +55,8 @@
5755
*/
5856
public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionProvider, TransactionResolver,
5957
ConfigProvider, DatabaseProvider, BrokerProvider, MetaProvider, HookProvider, RandomTeleportProvider,
60-
AudiencesProvider, UserProvider, TextValidator, ManagerProvider, ListenerProvider, CommandProvider {
58+
AudiencesProvider, UserProvider, TextValidator, ManagerProvider, ListenerProvider, CommandProvider,
59+
GsonProvider {
6160

6261
int BSTATS_BUKKIT_PLUGIN_ID = 8430;
6362

@@ -252,9 +251,4 @@ default Key getKey(@NotNull String... data) {
252251
return Key.key("huskhomes", joined);
253252
}
254253

255-
@NotNull
256-
default Gson getGson() {
257-
return new GsonBuilder().create();
258-
}
259-
260254
}

common/src/main/java/net/william278/huskhomes/network/Payload.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.gson.annotations.Expose;
2323
import com.google.gson.annotations.SerializedName;
24+
import lombok.AccessLevel;
2425
import lombok.NoArgsConstructor;
2526
import net.william278.huskhomes.position.Position;
2627
import net.william278.huskhomes.position.World;
@@ -35,7 +36,7 @@
3536
/**
3637
* Represents a payload sent in a cross-server {@link Message}.
3738
*/
38-
@NoArgsConstructor
39+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3940
public class Payload {
4041

4142
@Nullable

common/src/main/java/net/william278/huskhomes/network/PluginMessageBroker.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ public class PluginMessageBroker extends Broker {
4040
/**
4141
* The name of BungeeCord's provided plugin channel.
4242
*
43-
* <p>Internally, this is <a href="https://wiki.vg/Plugin_channels#bungeecord:main">{@code bungeecord:main}</a>,
44-
* but Spigot remaps {@code BungeeCord} automatically to the new one (hence BungeeCord is kept for back-compat).
43+
* @implNote Technically, the effective identifier of this channel is {@code bungeecord:main}, but Spigot remaps
44+
* {@code BungeeCord} automatically to the new one (<a href="https://wiki.vg/Plugin_channels#bungeecord:main">source</a>).
45+
* Spigot's <a href="https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/">official documentation</a>
46+
* still instructs usage of {@code BungeeCord} as the name to use, however. It's all a bit inconsistent, so just in case
47+
* it's best to leave it how it is for to maintain backwards compatibility.
4548
*/
4649
public static final String BUNGEE_CHANNEL_ID = "BungeeCord";
4750

48-
public PluginMessageBroker(@NotNull HuskHomes plugin) {
51+
protected PluginMessageBroker(@NotNull HuskHomes plugin) {
4952
super(plugin);
5053
}
5154

5255
@Override
53-
public void initialize() throws IllegalStateException {
56+
public void initialize() throws RuntimeException {
5457
plugin.setupPluginMessagingChannels();
5558
}
5659

@@ -70,16 +73,14 @@ public final void onReceive(@NotNull String channel, @NotNull OnlineUser user, b
7073
inputStream.readFully(messageBody);
7174

7275
try (final DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(messageBody))) {
73-
super.handle(user, plugin.getGson().fromJson(messageReader.readUTF(), Message.class));
76+
super.handle(user, plugin.getMessageFromJson(messageReader.readUTF()));
7477
} catch (IOException e) {
7578
plugin.log(Level.SEVERE, "Failed to fully read plugin message", e);
7679
}
7780
}
7881

7982
@Override
80-
protected void send(@NotNull Message message, @Nullable OnlineUser sender) {
81-
Preconditions.checkNotNull(sender, "Sender cannot be null with a Plugin Message broker");
82-
83+
protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
8384
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
8485
messageWriter.writeUTF(message.getTargetType().getPluginMessageChannel());
8586
messageWriter.writeUTF(message.getTarget());

common/src/main/java/net/william278/huskhomes/network/RedisBroker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void onThreadUnlock(@NotNull Throwable t) {
196196
public void onMessage(@NotNull String channel, @NotNull String encoded) {
197197
final Message message;
198198
try {
199-
message = broker.plugin.getGson().fromJson(encoded, Message.class);
199+
message = broker.plugin.getMessageFromJson(encoded);
200200
} catch (Exception e) {
201201
broker.plugin.log(Level.WARNING, "Failed to decode message from Redis: " + e.getMessage());
202202
return;

common/src/main/java/net/william278/huskhomes/position/World.java

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,41 @@
1919

2020
package net.william278.huskhomes.position;
2121

22-
import lombok.NoArgsConstructor;
22+
import com.google.gson.annotations.Expose;
23+
import lombok.*;
2324
import org.jetbrains.annotations.NotNull;
2425
import org.jetbrains.annotations.Nullable;
2526

2627
import java.util.Locale;
27-
import java.util.Optional;
2828
import java.util.UUID;
2929

3030
/**
3131
* Represents a world on a server.
3232
*/
33+
@Getter
34+
@Setter
35+
@AllArgsConstructor(staticName = "from")
3336
@NoArgsConstructor
3437
public class World {
3538

39+
@Expose
3640
private String name;
41+
@Expose
3742
private UUID uuid;
43+
@Expose
3844
@Nullable
39-
private Environment environment;
40-
41-
private World(@NotNull String name, @NotNull UUID uuid, @Nullable Environment environment) {
42-
this.setName(name);
43-
this.setUuid(uuid);
44-
this.setEnvironment(environment);
45-
}
46-
47-
@NotNull
48-
public static World from(@NotNull String name, @NotNull UUID uuid, @NotNull Environment environment) {
49-
return new World(name, uuid, environment);
50-
}
45+
@Getter(AccessLevel.NONE)
46+
private Environment environment = null;
5147

5248
@NotNull
5349
public static World from(@NotNull String name, @NotNull UUID uuid) {
5450
return new World(name, uuid, null);
5551
}
5652

57-
/**
58-
* The name of this world, as defined by the world directory name.
59-
*/
60-
@NotNull
61-
public String getName() {
62-
return name;
63-
}
64-
65-
public void setName(@NotNull String name) {
66-
this.name = name;
67-
}
68-
69-
/**
70-
* UUID of this world, as defined by the {@code uid.dat} file in the world directory.
71-
*/
72-
@NotNull
73-
public UUID getUuid() {
74-
return uuid;
75-
}
76-
77-
public void setUuid(@NotNull UUID uuid) {
78-
this.uuid = uuid;
79-
}
80-
81-
/**
82-
* Environment of the world ({@link Environment#OVERWORLD}, {@link Environment#NETHER}, {@link Environment#THE_END},
83-
* or {@link Environment#CUSTOM}).
84-
*
85-
* <p>Will return {@link Environment#OVERWORLD} if the environment is null
86-
*/
8753
@NotNull
54+
@SuppressWarnings("unused")
8855
public Environment getEnvironment() {
89-
return Optional.ofNullable(environment).orElse(Environment.OVERWORLD);
90-
}
91-
92-
public void setEnvironment(@Nullable Environment environment) {
93-
this.environment = environment;
56+
return environment == null ? Environment.OVERWORLD : environment;
9457
}
9558

9659
/**
@@ -102,6 +65,7 @@ public enum Environment {
10265
THE_END,
10366
CUSTOM;
10467

68+
@NotNull
10569
public static Environment match(@NotNull String name) {
10670
return switch (name.toLowerCase(Locale.ENGLISH)) {
10771
case "overworld" -> OVERWORLD;
@@ -115,8 +79,9 @@ public static Environment match(@NotNull String name) {
11579
@Override
11680
public boolean equals(@NotNull Object obj) {
11781
if (obj instanceof World world) {
118-
return world.getUuid().equals(getUuid());
82+
return this.uuid.equals(world.uuid);
11983
}
12084
return super.equals(obj);
12185
}
86+
12287
}

0 commit comments

Comments
 (0)