From 5ea12b97c4e1e5a1b31db5f045f646a541a195f2 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 5 Nov 2024 22:43:18 +0000 Subject: [PATCH] fix: make `User` serializable by Gson --- .../huskhomes/command/SavedPositionCommand.java | 2 +- .../william278/huskhomes/command/TpCommand.java | 6 +++--- .../huskhomes/teleport/Teleportable.java | 8 +++++++- .../william278/huskhomes/teleport/Username.java | 8 +++++--- .../java/net/william278/huskhomes/user/User.java | 14 ++++++++------ 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java index 789623087..bdf9efb39 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java @@ -194,7 +194,7 @@ protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable tel .teleporter(teleporter) .actions(actions) .target(position) - .buildAndComplete(executor.equals(teleporter), teleporter.getUsername()); + .buildAndComplete(executor.equals(teleporter), teleporter.getName()); } protected boolean isInvalidOperation(String operation, CommandUser executor) { diff --git a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java index 05df9cdba..992202237 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -98,7 +98,7 @@ private void execute(@NotNull CommandUser executor, @NotNull Teleportable telepo : target instanceof OnlineUser online ? online.getName() : null; if (executor instanceof OnlineUser online) { if (online.equals(teleporter)) { - if (teleporter.getUsername().equalsIgnoreCase(targetName)) { + if (teleporter.getName().equalsIgnoreCase(targetName)) { plugin.getLocales().getLocale("error_cannot_teleport_self") .ifPresent(executor::sendMessage); return; @@ -118,14 +118,14 @@ private void execute(@NotNull CommandUser executor, @NotNull Teleportable telepo // Display the teleport completion message if (target instanceof Position position) { - plugin.getLocales().getLocale("teleporting_other_complete_position", teleporter.getUsername(), + plugin.getLocales().getLocale("teleporting_other_complete_position", teleporter.getName(), Integer.toString((int) position.getX()), Integer.toString((int) position.getY()), Integer.toString((int) position.getZ())) .ifPresent(executor::sendMessage); return; } plugin.getLocales().getLocale("teleporting_other_complete", - teleporter.getUsername(), Objects.requireNonNull(targetName)) + teleporter.getName(), Objects.requireNonNull(targetName)) .ifPresent(executor::sendMessage); } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java index e299221c2..af8d33fd0 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java @@ -47,6 +47,12 @@ static Teleportable username(@NotNull String teleporter) { * @return the username string */ @NotNull - String getUsername(); + String getName(); + + @NotNull + @Deprecated(since = "4.8") + default String getUsername() { + return getName(); + } } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Username.java b/common/src/main/java/net/william278/huskhomes/teleport/Username.java index a0e31a77a..abd7992d5 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Username.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Username.java @@ -19,6 +19,7 @@ package net.william278.huskhomes.teleport; +import lombok.Getter; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; @@ -56,11 +57,12 @@ public Optional findLocally(@NotNull HuskHomes plugin) { /** * Get the username {@link String} being represented by this object. * - * @return the username + * @return the name + * @since 4.8 */ - @NotNull @Override - public String getUsername() { + @NotNull + public String getName() { return name; } diff --git a/common/src/main/java/net/william278/huskhomes/user/User.java b/common/src/main/java/net/william278/huskhomes/user/User.java index b29370f7f..730514207 100644 --- a/common/src/main/java/net/william278/huskhomes/user/User.java +++ b/common/src/main/java/net/william278/huskhomes/user/User.java @@ -19,9 +19,8 @@ package net.william278.huskhomes.user; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; +import com.google.gson.annotations.Expose; +import lombok.*; import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -30,14 +29,17 @@ * Represents a user who has data saved in the database. */ @Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PROTECTED) public class User implements Comparable { + @Expose @NotNull - private final UUID uuid; - + private UUID uuid; + @Expose + @Setter @NotNull - private final String name; + private String name; @NotNull public static User of(@NotNull UUID uuid, @NotNull String username) {