From fbb3a9a0a43b5abf82766a6d51b815968cf8b93e Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:47:39 -0400 Subject: [PATCH] Fix various NPC account lookup issues (#4188) --- .../java/com/earth2me/essentials/EssentialsUserConf.java | 6 +++++- .../src/main/java/com/earth2me/essentials/UserData.java | 9 +++++++++ .../src/main/java/com/earth2me/essentials/UserMap.java | 4 ++++ .../main/java/com/earth2me/essentials/api/Economy.java | 4 ++++ .../test/java/com/earth2me/essentials/FakeServer.java | 2 +- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/EssentialsUserConf.java b/Essentials/src/main/java/com/earth2me/essentials/EssentialsUserConf.java index 86ca5fd23..3ddf08520 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/EssentialsUserConf.java +++ b/Essentials/src/main/java/com/earth2me/essentials/EssentialsUserConf.java @@ -11,7 +11,7 @@ import java.util.UUID; import java.util.logging.Level; public class EssentialsUserConf extends EssentialsConf { - public final String username; + public String username; public final UUID uuid; public EssentialsUserConf(final String username, final UUID uuid, final File configFile) { @@ -20,6 +20,10 @@ public class EssentialsUserConf extends EssentialsConf { this.uuid = uuid; } + public void setUsername(String username) { + this.username = username; + } + @Override public boolean legacyFileExists() { final File file = new File(configFile.getParentFile(), username + ".yml"); diff --git a/Essentials/src/main/java/com/earth2me/essentials/UserData.java b/Essentials/src/main/java/com/earth2me/essentials/UserData.java index 26b4d5c71..d7d035b6d 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/UserData.java +++ b/Essentials/src/main/java/com/earth2me/essentials/UserData.java @@ -2,6 +2,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.StringUtil; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableMap; import net.ess3.api.IEssentials; import net.ess3.api.InvalidWorldException; @@ -90,6 +91,10 @@ public abstract class UserData extends PlayerExtension implements IConf { config = new EssentialsUserConf(base.getName(), base.getUniqueId(), new File(folder, filename + ".yml")); reloadConfig(); + + if (config.username == null) { + config.setUsername(getLastAccountName()); + } } public final void reset() { @@ -97,6 +102,10 @@ public abstract class UserData extends PlayerExtension implements IConf { config.getFile().delete(); if (config.username != null) { ess.getUserMap().removeUser(config.username); + if (isNPC()) { + final String uuid = UUID.nameUUIDFromBytes(("NPC:" + config.username).getBytes(Charsets.UTF_8)).toString(); + ess.getUserMap().removeUserUUID(uuid); + } } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/UserMap.java b/Essentials/src/main/java/com/earth2me/essentials/UserMap.java index 2bee9fe56..dff04c46e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/main/java/com/earth2me/essentials/UserMap.java @@ -219,6 +219,10 @@ public class UserMap extends CacheLoader implements IConf { names.remove(StringUtil.safeString(name)); } + public void removeUserUUID(final String uuid) { + users.invalidate(uuid); + } + public Set getAllUniqueUsers() { return Collections.unmodifiableSet(keys); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/api/Economy.java b/Essentials/src/main/java/com/earth2me/essentials/api/Economy.java index 4ae5d6265..69dc7a361 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/api/Economy.java @@ -95,6 +95,10 @@ public class Economy { } } + if (user == null) { + user = getUserByUUID(UUID.nameUUIDFromBytes(("NPC:" + name).getBytes(Charsets.UTF_8))); + } + return user; } diff --git a/Essentials/src/test/java/com/earth2me/essentials/FakeServer.java b/Essentials/src/test/java/com/earth2me/essentials/FakeServer.java index b4a3ace42..9b2a1d39a 100644 --- a/Essentials/src/test/java/com/earth2me/essentials/FakeServer.java +++ b/Essentials/src/test/java/com/earth2me/essentials/FakeServer.java @@ -660,7 +660,7 @@ public class FakeServer implements Server { case "testPlayer2": return UUID.fromString("2c9ebe1a-9098-43fd-bc0c-a369b76817ba"); case "npc1": - return null; + return UUID.fromString("f4a37409-5c40-3b2c-9cd6-57d3c5abdc76"); } throw new UnsupportedOperationException("Not supported yet."); }