Skip to content

Commit 4dfbc0e

Browse files
committed
fix: more gracefully handle platform mismatches
1 parent 07d0376 commit 4dfbc0e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

common/src/main/java/net/william278/husksync/data/DataSnapshot.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ public static DataSnapshot.Packed deserialize(@NotNull HuskSync plugin, byte[] d
111111
@Nullable OffsetDateTime timestamp) throws IllegalStateException {
112112
final DataSnapshot.Packed snapshot = plugin.getDataAdapter().fromBytes(data, DataSnapshot.Packed.class);
113113
if (snapshot.getMinecraftVersion().compareTo(plugin.getMinecraftVersion()) > 0) {
114-
throw new IllegalStateException(String.format("Cannot set data for user because the Minecraft version of " +
115-
"their user data (%s) is newer than the server's Minecraft version (%s)." +
114+
throw new IllegalStateException(String.format("Cannot deserialize data because the Minecraft version of " +
115+
"the data snapshot (%s) is newer than the server's Minecraft version (%s)." +
116116
"Please ensure each server is running the same version of Minecraft.",
117117
snapshot.getMinecraftVersion(), plugin.getMinecraftVersion()));
118118
}
119119
if (snapshot.getFormatVersion() > CURRENT_FORMAT_VERSION) {
120-
throw new IllegalStateException(String.format("Cannot set data for user because the format version of " +
121-
"their user data (%s) is newer than the current format version (%s). " +
120+
throw new IllegalStateException(String.format("Cannot deserialize data because the format version of " +
121+
"the data snapshot (%s) is newer than the current format version (%s). " +
122122
"Please ensure each server is running the latest version of HuskSync.",
123123
snapshot.getFormatVersion(), CURRENT_FORMAT_VERSION));
124124
}
@@ -135,8 +135,8 @@ public static DataSnapshot.Packed deserialize(@NotNull HuskSync plugin, byte[] d
135135
));
136136
}
137137
if (!snapshot.getPlatformType().equalsIgnoreCase(plugin.getPlatformType())) {
138-
throw new IllegalStateException(String.format("Cannot set data for user because the platform type of " +
139-
"their user data (%s) is different to the server platform type (%s). " +
138+
throw new IllegalStateException(String.format("Cannot deserialize data because the platform type of " +
139+
"the data snapshot (%s) is different to the server platform type (%s). " +
140140
"Please ensure each server is running the same platform type.",
141141
snapshot.getPlatformType(), plugin.getPlatformType()));
142142
}

common/src/main/java/net/william278/husksync/redis/RedisManager.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,15 @@ public void onMessage(@NotNull String channel, @NotNull String message) {
158158
final RedisMessage redisMessage = RedisMessage.fromJson(plugin, message);
159159
switch (messageType) {
160160
case UPDATE_USER_DATA -> plugin.getOnlineUser(redisMessage.getTargetUuid()).ifPresent(
161-
user -> user.applySnapshot(
162-
DataSnapshot.deserialize(plugin, redisMessage.getPayload()),
163-
DataSnapshot.UpdateCause.UPDATED
164-
)
161+
user -> {
162+
try {
163+
final DataSnapshot.Packed data = DataSnapshot.deserialize(plugin, redisMessage.getPayload());
164+
user.applySnapshot(data, DataSnapshot.UpdateCause.UPDATED);
165+
} catch (Throwable e) {
166+
plugin.log(Level.SEVERE, "An exception occurred updating user data from Redis", e);
167+
user.completeSync(false, DataSnapshot.UpdateCause.UPDATED, plugin);
168+
}
169+
}
165170
);
166171
case REQUEST_USER_DATA -> plugin.getOnlineUser(redisMessage.getTargetUuid()).ifPresent(
167172
user -> RedisMessage.create(
@@ -174,7 +179,13 @@ public void onMessage(@NotNull String channel, @NotNull String message) {
174179
redisMessage.getTargetUuid()
175180
);
176181
if (future != null) {
177-
future.complete(Optional.of(DataSnapshot.deserialize(plugin, redisMessage.getPayload())));
182+
try {
183+
final DataSnapshot.Packed data = DataSnapshot.deserialize(plugin, redisMessage.getPayload());
184+
future.complete(Optional.of(data));
185+
} catch (Throwable e) {
186+
plugin.log(Level.SEVERE, "An exception occurred returning user data from Redis", e);
187+
future.complete(Optional.empty());
188+
}
178189
pendingRequests.remove(redisMessage.getTargetUuid());
179190
}
180191
}

common/src/main/java/net/william278/husksync/sync/DataSyncer.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.function.BiConsumer;
3939
import java.util.function.Function;
4040
import java.util.function.Supplier;
41+
import java.util.logging.Level;
4142

4243
/**
4344
* Handles the synchronization of data when a player changes servers or logs in
@@ -156,10 +157,15 @@ private long getMaxListenAttempts() {
156157
// Set a user's data from the database, or set them as a new user
157158
@ApiStatus.Internal
158159
protected void setUserFromDatabase(@NotNull OnlineUser user) {
159-
getDatabase().getLatestSnapshot(user).ifPresentOrElse(
160-
snapshot -> user.applySnapshot(snapshot, DataSnapshot.UpdateCause.SYNCHRONIZED),
161-
() -> user.completeSync(true, DataSnapshot.UpdateCause.NEW_USER, plugin)
162-
);
160+
try {
161+
getDatabase().getLatestSnapshot(user).ifPresentOrElse(
162+
snapshot -> user.applySnapshot(snapshot, DataSnapshot.UpdateCause.SYNCHRONIZED),
163+
() -> user.completeSync(true, DataSnapshot.UpdateCause.NEW_USER, plugin)
164+
);
165+
} catch (Throwable e) {
166+
plugin.log(Level.WARNING, "Failed to set %s's data from the database".formatted(user.getUsername()), e);
167+
user.completeSync(false, DataSnapshot.UpdateCause.SYNCHRONIZED, plugin);
168+
}
163169
}
164170

165171
// Continuously listen for data from Redis

0 commit comments

Comments
 (0)