Skip to content

Commit

Permalink
fix: make User serializable by Gson
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Nov 5, 2024
1 parent c61b0a2 commit 5ea12b9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,11 +57,12 @@ public Optional<OnlineUser> 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;
}

Expand Down
14 changes: 8 additions & 6 deletions common/src/main/java/net/william278/huskhomes/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<User> {

@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) {
Expand Down

0 comments on commit 5ea12b9

Please sign in to comment.