mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-26 18:11:43 +01:00
Fixes IllegalArgumentException in VaultHook (#574)
* Added ability to get a User instance from an OfflinePlayer * Made VaultHook use OfflinePlayer instead of Player Fixes #572 * Fixed incomplete previous commit
This commit is contained in:
parent
2f432e9f35
commit
54aa2e61d3
@ -89,7 +89,6 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
|
||||
else {
|
||||
getPlayers().getPlayer(targetUUID).addToPendingKick(getWorld());
|
||||
getPlayers().save(targetUUID);
|
||||
|
||||
}
|
||||
}
|
||||
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
|
||||
|
@ -20,6 +20,8 @@ import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
|
||||
@ -50,6 +52,7 @@ public class User {
|
||||
* @param sender - command sender, e.g. console
|
||||
* @return user - user
|
||||
*/
|
||||
@Nullable
|
||||
public static User getInstance(CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
return getInstance((Player)sender);
|
||||
@ -63,6 +66,7 @@ public class User {
|
||||
* @param player - the player
|
||||
* @return user - user
|
||||
*/
|
||||
@Nullable
|
||||
public static User getInstance(Player player) {
|
||||
if (player == null) {
|
||||
return null;
|
||||
@ -78,6 +82,7 @@ public class User {
|
||||
* @param uuid - UUID
|
||||
* @return user - user
|
||||
*/
|
||||
@Nullable
|
||||
public static User getInstance(UUID uuid) {
|
||||
if (uuid == null) {
|
||||
return null;
|
||||
@ -89,6 +94,23 @@ public class User {
|
||||
return new User(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of User from an OfflinePlayer
|
||||
* @param offlinePlayer offline Player
|
||||
* @return user
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Nullable
|
||||
public static User getInstance(OfflinePlayer offlinePlayer) {
|
||||
if (offlinePlayer == null) {
|
||||
return null;
|
||||
}
|
||||
if (users.containsKey(offlinePlayer.getUniqueId())) {
|
||||
return users.get(offlinePlayer.getUniqueId());
|
||||
}
|
||||
return new User(offlinePlayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes this player from the User cache
|
||||
* @param player the player
|
||||
@ -103,7 +125,9 @@ public class User {
|
||||
|
||||
private static BentoBox plugin = BentoBox.getInstance();
|
||||
|
||||
@Nullable
|
||||
private Player player;
|
||||
private OfflinePlayer offlinePlayer;
|
||||
private final UUID playerUUID;
|
||||
private final CommandSender sender;
|
||||
|
||||
@ -115,17 +139,26 @@ public class User {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
private User(Player player) {
|
||||
private User(@NonNull Player player) {
|
||||
this.player = player;
|
||||
offlinePlayer = player;
|
||||
sender = player;
|
||||
playerUUID = player.getUniqueId();
|
||||
users.put(player.getUniqueId(), this);
|
||||
users.put(playerUUID, this);
|
||||
}
|
||||
|
||||
private User(@NonNull OfflinePlayer offlinePlayer) {
|
||||
this.player = offlinePlayer.isOnline() ? offlinePlayer.getPlayer() : null;
|
||||
this.playerUUID = offlinePlayer.getUniqueId();
|
||||
this.sender = offlinePlayer.isOnline() ? offlinePlayer.getPlayer() : null;
|
||||
this.offlinePlayer = offlinePlayer;
|
||||
}
|
||||
|
||||
private User(UUID playerUUID) {
|
||||
player = Bukkit.getPlayer(playerUUID);
|
||||
this.playerUUID = playerUUID;
|
||||
sender = player;
|
||||
offlinePlayer = Bukkit.getOfflinePlayer(playerUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -140,10 +173,12 @@ public class User {
|
||||
return sender.getEffectivePermissions();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PlayerInventory getInventory() {
|
||||
return player != null ? player.getInventory() : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Location getLocation() {
|
||||
return player != null ? player.getLocation() : null;
|
||||
}
|
||||
@ -166,6 +201,22 @@ public class User {
|
||||
return player != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the offline player
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public OfflinePlayer getOfflinePlayer() {
|
||||
return offlinePlayer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this user is an OfflinePlayer, false if not, e.g., console
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public boolean isOfflinePlayer() {
|
||||
return offlinePlayer != null;
|
||||
}
|
||||
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class VaultHook extends Hook {
|
||||
* @return the balance of this User.
|
||||
*/
|
||||
public double getBalance(User user) {
|
||||
return (user.isPlayer()) ? economy.getBalance(user.getPlayer()) : 0.0D;
|
||||
return (user.isOfflinePlayer()) ? economy.getBalance(user.getOfflinePlayer()) : 0.0D;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,13 +59,13 @@ public class VaultHook extends Hook {
|
||||
* @return the EconomyResponse of this withdrawal.
|
||||
*/
|
||||
public EconomyResponse withdraw(User user, double amount) {
|
||||
if (!user.isPlayer()) {
|
||||
if (!user.isOfflinePlayer()) {
|
||||
throw new IllegalArgumentException("User must be a Player or an OfflinePlayer");
|
||||
}
|
||||
if (amount < 0.0D) {
|
||||
throw new IllegalArgumentException(AMOUNT_MUST_BE_POSITIVE);
|
||||
}
|
||||
return economy.withdrawPlayer(user.getPlayer(), amount);
|
||||
return economy.withdrawPlayer(user.getOfflinePlayer(), amount);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,13 +75,13 @@ public class VaultHook extends Hook {
|
||||
* @return the EconomyResponse of this deposit.
|
||||
*/
|
||||
public EconomyResponse deposit(User user, double amount) {
|
||||
if (!user.isPlayer()) {
|
||||
if (!user.isOfflinePlayer()) {
|
||||
throw new IllegalArgumentException("User must be a Player or an OfflinePlayer");
|
||||
}
|
||||
if (amount < 0.0D) {
|
||||
throw new IllegalArgumentException(AMOUNT_MUST_BE_POSITIVE);
|
||||
}
|
||||
return economy.depositPlayer(user.getPlayer(), amount);
|
||||
return economy.depositPlayer(user.getOfflinePlayer(), amount);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,6 +96,6 @@ public class VaultHook extends Hook {
|
||||
if (amount < 0.0D) {
|
||||
throw new IllegalArgumentException(AMOUNT_MUST_BE_POSITIVE);
|
||||
}
|
||||
return user.isPlayer() && economy.has(user.getPlayer(), amount);
|
||||
return user.isOfflinePlayer() && economy.has(user.getOfflinePlayer(), amount);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user