fix: make User serializable by Gson

This commit is contained in:
William committed 2024-11-05 22:43:18 +00:00
1 parent c61b0a21b8
commit 5ea12b97c4
5 files changed
+24 -14

No files matched your search

@@ -194,7 +194,7 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
.teleporter(teleporter) .teleporter(teleporter)
.actions(actions) .actions(actions)
.target(position) .target(position)
.buildAndComplete(executor.equals(teleporter), teleporter.getUsername()); .buildAndComplete(executor.equals(teleporter), teleporter.getName());
} }
protected boolean isInvalidOperation(String operation, CommandUser executor) { protected boolean isInvalidOperation(String operation, CommandUser executor) {
@@ -98,7 +98,7 @@ public class TpCommand extends Command implements TabCompletable {
: target instanceof OnlineUser online ? online.getName() : null; : target instanceof OnlineUser online ? online.getName() : null;
if (executor instanceof OnlineUser online) { if (executor instanceof OnlineUser online) {
if (online.equals(teleporter)) { if (online.equals(teleporter)) {
if (teleporter.getUsername().equalsIgnoreCase(targetName)) { if (teleporter.getName().equalsIgnoreCase(targetName)) {
plugin.getLocales().getLocale("error_cannot_teleport_self") plugin.getLocales().getLocale("error_cannot_teleport_self")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
@@ -118,14 +118,14 @@ public class TpCommand extends Command implements TabCompletable {
// Display the teleport completion message // Display the teleport completion message
if (target instanceof Position position) { 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.getX()), Integer.toString((int) position.getY()),
Integer.toString((int) position.getZ())) Integer.toString((int) position.getZ()))
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
plugin.getLocales().getLocale("teleporting_other_complete", plugin.getLocales().getLocale("teleporting_other_complete",
teleporter.getUsername(), Objects.requireNonNull(targetName)) teleporter.getName(), Objects.requireNonNull(targetName))
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
} }
@@ -47,6 +47,12 @@ public interface Teleportable {
* @return the username string * @return the username string
*/ */
@NotNull @NotNull
String getUsername(); String getName();
@NotNull
@Deprecated(since = "4.8")
default String getUsername() {
return getName();
}
} }
@@ -19,6 +19,7 @@
package net.william278.huskhomes.teleport; package net.william278.huskhomes.teleport;
import lombok.Getter;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -56,11 +57,12 @@ public record Username(@NotNull String name) implements Teleportable, Target {
/** /**
* Get the username {@link String} being represented by this object. * Get the username {@link String} being represented by this object.
* *
* @return the username * @return the name
* @since 4.8
*/ */
@NotNull
@Override @Override
public String getUsername() { @NotNull
public String getName() {
return name; return name;
} }
@@ -19,9 +19,8 @@
package net.william278.huskhomes.user; package net.william278.huskhomes.user;
import lombok.AccessLevel; import com.google.gson.annotations.Expose;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
@@ -30,14 +29,17 @@ import java.util.UUID;
* Represents a user who has data saved in the database. * Represents a user who has data saved in the database.
*/ */
@Getter @Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PROTECTED)
public class User implements Comparable<User> { public class User implements Comparable<User> {
@Expose
@NotNull @NotNull
private final UUID uuid; private UUID uuid;
@Expose
@Setter
@NotNull @NotNull
private final String name; private String name;
@NotNull @NotNull
public static User of(@NotNull UUID uuid, @NotNull String username) { public static User of(@NotNull UUID uuid, @NotNull String username) {