diff --git a/Essentials/src/com/earth2me/essentials/api/Economy.java b/Essentials/src/com/earth2me/essentials/api/Economy.java index 12aed9ce3..82943da86 100644 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/com/earth2me/essentials/api/Economy.java @@ -8,6 +8,7 @@ import com.earth2me.essentials.utils.StringUtil; import com.google.common.base.Charsets; import net.ess3.api.IEssentials; import net.ess3.api.MaxMoneyException; +import org.bukkit.entity.Player; import java.io.File; import java.math.BigDecimal; @@ -18,7 +19,7 @@ import java.util.logging.Logger; /** - * Instead of using this api directly, we recommend to use the register plugin: http://bit.ly/RegisterMethod + * You should use Vault instead of directly using this class. */ public class Economy { public Economy() { @@ -64,7 +65,24 @@ public class Economy { if (name == null) { throw new RuntimeException("Economy username cannot be null"); } - return ess.getUser(name); + + User user = ess.getUser(name); + if (user == null) { + /* + Attempt lookup using UUID - this prevents balance resets when accessing economy + via Vault during player join. + See: https://github.com/EssentialsX/Essentials/issues/2400 + */ + Player player = ess.getServer().getPlayerExact(name); + if (player != null) { + user = ess.getUser(player.getUniqueId()); + if (user != null) { + logger.info(String.format("[Economy] Found player %s by UUID %s but not by their actual name - they may have changed their username", name, player.getUniqueId().toString())); + } + } + } + + return user; } /** diff --git a/Essentials/test/com/earth2me/essentials/FakeServer.java b/Essentials/test/com/earth2me/essentials/FakeServer.java index 9e0a10b3a..63a08b545 100644 --- a/Essentials/test/com/earth2me/essentials/FakeServer.java +++ b/Essentials/test/com/earth2me/essentials/FakeServer.java @@ -500,7 +500,12 @@ public class FakeServer implements Server { @Override public Player getPlayerExact(String string) { - throw new UnsupportedOperationException("Not supported yet."); + for (Player player : players) { + if (player.getName().equals(string)) { + return player; + } + } + return null; } @Override