Make /pay work on offline players.

This commit is contained in:
AppleDash 2017-01-22 03:50:30 -05:00
parent 38e64d2d9a
commit 742997cd3d
3 changed files with 18 additions and 8 deletions

View File

@ -11,7 +11,8 @@ import org.appledash.saneeconomy.economy.transaction.TransactionReason;
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
import org.appledash.saneeconomy.utils.MessageUtils;
import org.appledash.saneeconomy.utils.NumberUtils;
import org.bukkit.Bukkit;
import org.appledash.saneeconomy.utils.PlayerUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -53,10 +54,10 @@ public class PayCommand extends SaneEconomyCommand {
Player fromPlayer = (Player) sender;
String sToPlayer = args[0];
Player toPlayer = Bukkit.getServer().getPlayer(sToPlayer);
OfflinePlayer toPlayer = PlayerUtils.getOfflinePlayer(sToPlayer);
if (toPlayer == null) {
MessageUtils.sendMessage(sender, "That player is not online.");
MessageUtils.sendMessage(sender, "That player does not exist or has never played before.");
return;
}
@ -93,9 +94,11 @@ public class PayCommand extends SaneEconomyCommand {
sToPlayer
);
MessageUtils.sendMessage(toPlayer, "You have received {1} from {2}.",
ecoMan.getCurrency().formatAmount(amount),
fromPlayer.getDisplayName()
);
if (toPlayer.isOnline()) {
MessageUtils.sendMessage(((CommandSender) toPlayer), "You have received {1} from {2}.",
ecoMan.getCurrency().formatAmount(amount),
fromPlayer.getDisplayName()
);
}
}
}

View File

@ -3,6 +3,7 @@ package org.appledash.saneeconomy.utils;
import com.google.common.base.Strings;
import org.appledash.saneeconomy.SaneEconomy;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import java.util.regex.Matcher;
@ -38,6 +39,12 @@ public class MessageUtils {
target.sendMessage(prefix + formatted);
}
public static synchronized void sendMessage(OfflinePlayer target, String fmt, Object... args) {
if (target.isOnline() && (target instanceof CommandSender)) {
sendMessage(((CommandSender) target), fmt, (Object[])args);
}
}
public static String indexedFormat(String fmt, Object... arguments) {
Matcher m = Pattern.compile("\\{([0-9]+)(:[^}]+)?\\}").matcher(fmt);
StringBuffer formatted = new StringBuffer();

View File

@ -19,7 +19,7 @@ public class PlayerUtils {
public static OfflinePlayer getOfflinePlayer(String playerNameOrUUID) {
OfflinePlayer player = tryGetFromUUID(playerNameOrUUID);
if (player != null && (player.hasPlayedBefore() || player.isOnline())) {
if ((player != null) && (player.hasPlayedBefore() || player.isOnline())) {
return player;
}