Fixed /withdraw not parsing arguments right

This commit is contained in:
Jules 2025-04-10 19:44:41 +02:00
parent eaabe32c6c
commit cbe53f088a
4 changed files with 63 additions and 51 deletions

View File

@ -6,6 +6,7 @@ import net.Indyuce.mmocore.api.player.PlayerData;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.Validate;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -89,26 +90,33 @@ public class ConfigMessage {
send(player);
}
public void send(Player player) {
public void send(CommandSender player) {
for (String line : lines) send(player, line);
}
public void send(Collection<? extends Player> players) {
for (Player player : players) for (String line : lines) send(player, line);
public void send(Collection<CommandSender> players) {
for (CommandSender player : players) for (String line : lines) send(player, line);
}
/**
* Sends a line of text to a target player
*
* @param player Player to send message to. His player
* @param recipient Player to send message to. His player
* data is not necessarily fully loaded
* @param messageFormat Raw/normal message to send
*/
private void send(@NotNull Player player, String messageFormat) {
Validate.notNull(player, "Player cannot be null");
private void send(@NotNull CommandSender recipient, String messageFormat) {
Validate.notNull(recipient, "Recipient cannot be null");
// Command blocks and console
if (!(recipient instanceof Player)) {
recipient.sendMessage(format(null, messageFormat));
return;
}
final Player player = (Player) recipient;
final String rawMessage = format(player, messageFormat);
final PlayerData playerData = PlayerData.has(player) ? PlayerData.get(player) : null;
final PlayerData playerData = PlayerData.has((Player) recipient) ? PlayerData.get((Player) recipient) : null;
// Handle special case with player data + action bar
if (playerData != null && playerData.isOnline() && actionbar) {

View File

@ -261,7 +261,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
*/
@NotNull
public Map<String, Integer> mapSkillTreePoints() {
return new HashMap(skillTreePoints);
return new HashMap<>(skillTreePoints);
}
@Override

View File

@ -154,8 +154,8 @@ public class MMOCoreUtils {
dataOutput.writeObject(item);
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
return null;
} catch (Throwable throwable) {
throw new RuntimeException("Could not serialize inventory", throwable);
}
}
@ -168,8 +168,8 @@ public class MMOCoreUtils {
items[i] = (ItemStack) dataInput.readObject();
dataInput.close();
return items;
} catch (Exception e) {
return null;
} catch (Throwable throwable) {
throw new RuntimeException("Could not deserialize inventory", throwable);
}
}

View File

@ -14,50 +14,54 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
public class WithdrawCommand extends RegisteredCommand {
public WithdrawCommand(ConfigurationSection config) {
super(config, ToggleableCommand.WITHDRAW);
}
public WithdrawCommand(ConfigurationSection config) {
super(config, ToggleableCommand.WITHDRAW);
}
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!sender.hasPermission("mmocore.currency"))
return false;
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!sender.hasPermission("mmocore.currency"))
return false;
String playerArgument = args.length < 2 ? null : args[args.length - 2];
String amountArgument = args.length == 0 ? "0" : args[args.length - 1];
final Player player;
if (args.length >= 2 && sender.hasPermission("mmocore.admin")) player = Bukkit.getPlayer(args[0]);
else if (args.length == 1 && sender.hasPermission("mmocore.admin")) {
Player tryFirstArg = Bukkit.getPlayer(args[0]);
player = tryFirstArg != null ? tryFirstArg : sender instanceof Player ? (Player) sender : null;
} else if (sender instanceof Player) player = (Player) sender;
else player = null;
if (player == null) {
sender.sendMessage(ChatColor.RED + "Please specify a valid player.");
return true;
}
Player player = playerArgument != null && sender.hasPermission("mmocore.admin") ? Bukkit.getPlayer(playerArgument) : sender instanceof Player ? (Player) sender : null;
if (player == null) {
sender.sendMessage(ChatColor.RED + "Please specify a valid player.");
return true;
}
int amount;
try {
if (args.length == 0) amount = 0;
else amount = Integer.parseInt(args[args.length - 1]);
Validate.isTrue(amount >= 0);
} catch (IllegalArgumentException exception) {
ConfigMessage.fromKey("wrong-number", "arg", args[0]).send(sender);
return true;
}
int amount;
try {
amount = Integer.parseInt(amountArgument);
Validate.isTrue(amount >= 0);
} catch (IllegalArgumentException exception) {
sender.sendMessage(ConfigMessage.fromKey("wrong-number", "arg", "" + args[0]).asLine());
return true;
}
Withdraw request = new Withdraw(player);
Withdraw request = new Withdraw(player);
if (amount == 0) {
request.open();
return true;
}
if (amount == 0) {
request.open();
return true;
}
int left = (int) MMOCore.plugin.economy.getEconomy().getBalance(player) - amount;
if (left < 0) {
ConfigMessage.fromKey("not-enough-money", "left", "" + -left).send(player);
return true;
}
int left = (int) MMOCore.plugin.economy.getEconomy().getBalance(player) - amount;
if (left < 0) {
ConfigMessage.fromKey("not-enough-money", "left", "" + -left).send(player);
return true;
}
MMOCore.plugin.economy.getEconomy().withdrawPlayer(player, amount);
request.withdrawAlgorithm(amount);
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
ConfigMessage.fromKey("withdrew", "worth", amount).send(player);
return true;
}
MMOCore.plugin.economy.getEconomy().withdrawPlayer(player, amount);
request.withdrawAlgorithm(amount);
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
ConfigMessage.fromKey("withdrew", "worth", amount).send(player);
return true;
}
}