Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer overflow fix for duel wagers. #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public interface Match {
*
* @return bet Bet amount for this {@link Match} or 0 if no bet was specified.
*/
int getBet();
double getBet();


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface DQueue {
*
* @return Bet amount for this {@link DQueue} or 0 if no bet was specified.
*/
int getBet();
double getBet();


/**
Expand All @@ -50,7 +50,7 @@ public interface DQueue {
* Whether or not this {@link DQueue} has been removed.
*
* @return True if this {@link DQueue} has been removed. False otherwise.
* @see DQueueManager#remove(Kit, int)
* @see DQueueManager#remove(Kit, double)
*/
boolean isRemoved();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface DQueueManager {
* @return DQueue with the given kit and bet if found, otherwise null.
*/
@Nullable
DQueue get(@Nullable final Kit kit, final int bet);
DQueue get(@Nullable final Kit kit, final double bet);


/**
Expand All @@ -48,16 +48,16 @@ public interface DQueueManager {
* @return The newly created DQueue or null if a queue with given kit and bet already exists
*/
@Nullable
DQueue create(@Nullable final CommandSender source, @Nullable final Kit kit, final int bet);
DQueue create(@Nullable final CommandSender source, @Nullable final Kit kit, final double bet);


/**
* Calls {@link #create(CommandSender, Kit, int)} with source being null.
* Calls {@link #create(CommandSender, Kit, double)} with source being null.
*
* @see #create(CommandSender, Kit, int)
* @see #create(CommandSender, Kit, double)
*/
@Nullable
DQueue create(@Nullable final Kit kit, final int bet);
DQueue create(@Nullable final Kit kit, final double bet);


/**
Expand All @@ -70,16 +70,16 @@ public interface DQueueManager {
* @see DQueue#isRemoved()
*/
@Nullable
DQueue remove(@Nullable final CommandSender source, @Nullable final Kit kit, final int bet);
DQueue remove(@Nullable final CommandSender source, @Nullable final Kit kit, final double bet);


/**
* Calls {@link #remove(CommandSender, Kit, int)} with source being null.
* Calls {@link #remove(CommandSender, Kit, double)} with source being null.
*
* @see #remove(CommandSender, Kit, int)
* @see #remove(CommandSender, Kit, double)
*/
@Nullable
DQueue remove(@Nullable final Kit kit, final int bet);
DQueue remove(@Nullable final Kit kit, final double bet);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ public interface Request {
*
* @return Bet amount for this {@link Request} or 0 if not specified.
*/
int getBet();
double getBet();
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean isAvailable() {
return !isDisabled() && !isUsed() && getPosition(1) != null && getPosition(2) != null;
}

public MatchImpl startMatch(final KitImpl kit, final Map<UUID, List<ItemStack>> items, final int bet, final Queue source) {
public MatchImpl startMatch(final KitImpl kit, final Map<UUID, List<ItemStack>> items, final double bet, final Queue source) {
this.match = new MatchImpl(this, kit, items, bet, source);
refreshGui(false);
return match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MatchImpl implements Match {
private final KitImpl kit;
private final Map<UUID, List<ItemStack>> items;
@Getter
private final int bet;
private final double bet;
@Getter
private final Queue source;

Expand All @@ -38,7 +38,7 @@ public class MatchImpl implements Match {
// Default value for players is false, which is set to true if player is killed in the match.
private final Map<Player, Boolean> players = new HashMap<>();

MatchImpl(final ArenaImpl arena, final KitImpl kit, final Map<UUID, List<ItemStack>> items, final int bet, final Queue source) {
MatchImpl(final ArenaImpl arena, final KitImpl kit, final Map<UUID, List<ItemStack>> items, final double bet, final Queue source) {
this.arena = arena;
this.start = System.currentTimeMillis();
this.kit = kit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.realized.duels.arena.MatchImpl;
import me.realized.duels.command.BaseCommand;
import me.realized.duels.spectate.SpectatorImpl;
import me.realized.duels.util.NumberUtil;
import me.realized.duels.util.inventory.InventoryUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected void execute(final CommandSender sender, final String label, final Str
"opponent", arena.getOpponent(target).getName(),
"kit", kit,
"arena", arena.getName(),
"bet_amount", match.getBet()
"bet_amount", NumberUtil.formatDouble(match.getBet())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ protected boolean executeFirst(final CommandSender sender, final String label, f
boolean sendRequest = false;

if (args.length > 1) {
final int amount = NumberUtil.parseInt(args[1]).orElse(0);
final double amount = NumberUtil.parseDouble(args[1]).orElse(0.0);

if (amount > 0 && config.isMoneyBettingEnabled()) {
if (amount >= config.getMoneyBettingMinimumBetAmount() && config.isMoneyBettingEnabled()) {
if (config.isMoneyBettingUsePermission() && !player.hasPermission(Permissions.MONEY_BETTING) && !player.hasPermission(Permissions.SETTING_ALL)) {
lang.sendMessage(player, "ERROR.no-permission", "permission", Permissions.MONEY_BETTING);
return true;
Expand All @@ -173,6 +173,11 @@ protected boolean executeFirst(final CommandSender sender, final String label, f
return true;
}

if (amount > config.getMoneyBettingMaximumBetAmount()) {
lang.sendMessage(sender, "ERROR.command.exceeding-maximum-betting-amount");
return true;
}

if (!vault.getEconomy().has(player, amount)) {
lang.sendMessage(sender, "ERROR.command.not-enough-money");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.realized.duels.hook.hooks.worldguard.WorldGuardHook;
import me.realized.duels.request.RequestImpl;
import me.realized.duels.setting.Settings;
import me.realized.duels.util.NumberUtil;
import me.realized.duels.util.inventory.InventoryUtil;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
Expand Down Expand Up @@ -107,9 +108,9 @@ protected void execute(final CommandSender sender, final String label, final Str
final double bet = settings.getBet();
final String itemBetting = settings.isItemBetting() ? lang.getMessage("GENERAL.enabled") : lang.getMessage("GENERAL.disabled");
lang.sendMessage(player, "COMMAND.duel.request.accept.receiver",
"name", target.getName(), "kit", kit, "arena", arena, "bet_amount", bet, "item_betting", itemBetting);
"name", target.getName(), "kit", kit, "arena", arena, "bet_amount", NumberUtil.formatDouble(bet), "item_betting", itemBetting);
lang.sendMessage(target, "COMMAND.duel.request.accept.sender",
"name", player.getName(), "kit", kit, "arena", arena, "bet_amount", bet, "item_betting", itemBetting);
"name", player.getName(), "kit", kit, "arena", arena, "bet_amount", NumberUtil.formatDouble(bet), "item_betting", itemBetting);

if (settings.isItemBetting()) {
settings.setBaseLoc(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void execute(final CommandSender sender, final String label, final Str
return;
}

final int bet = NumberUtil.parseInt(args[1]).orElse(0);
final double bet = NumberUtil.parseDouble(args[1]).orElse(0.0);
KitImpl kit = null;

if (!args[2].equals("-")) {
Expand All @@ -48,7 +48,7 @@ protected void execute(final CommandSender sender, final String label, final Str
final Queue queue = queueManager.get(kit, bet);

if (queue == null) {
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", bet, "kit", kitName);
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", NumberUtil.formatDouble(bet), "kit", kitName);
return;
}

Expand All @@ -58,7 +58,7 @@ protected void execute(final CommandSender sender, final String label, final Str
}

final Location location = sign.getLocation();
lang.sendMessage(sender, "COMMAND.duels.add-sign", "location", StringUtil.parse(location), "kit", kitName, "bet_amount", bet);
lang.sendMessage(sender, "COMMAND.duels.add-sign", "location", StringUtil.parse(location), "kit", kitName, "bet_amount", NumberUtil.formatDouble(bet));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CreatequeueCommand(final DuelsPlugin plugin) {

@Override
protected void execute(final CommandSender sender, final String label, final String[] args) {
final int bet = NumberUtil.parseInt(args[1]).orElse(0);
final double bet = NumberUtil.parseDouble(args[1]).orElse(0.0);
KitImpl kit = null;

if (!args[2].equals("-")) {
Expand All @@ -34,11 +34,11 @@ protected void execute(final CommandSender sender, final String label, final Str
final String kitName = kit != null ? kit.getName() : lang.getMessage("GENERAL.none");

if (queueManager.create(sender, kit, bet) == null) {
lang.sendMessage(sender, "ERROR.queue.already-exists", "kit", kitName, "bet_amount", bet);
lang.sendMessage(sender, "ERROR.queue.already-exists", "kit", kitName, "bet_amount", NumberUtil.formatDouble(bet));
return;
}

lang.sendMessage(sender, "COMMAND.duels.create-queue", "kit", kitName, "bet_amount", bet);
lang.sendMessage(sender, "COMMAND.duels.create-queue", "kit", kitName, "bet_amount", NumberUtil.formatDouble(bet));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public DeletequeueCommand(final DuelsPlugin plugin) {

@Override
protected void execute(final CommandSender sender, final String label, final String[] args) {
final int bet = NumberUtil.parseInt(args[1]).orElse(0);
final double bet = NumberUtil.parseDouble(args[1]).orElse(0.0);
KitImpl kit = null;

if (!args[2].equals("-")) {
Expand All @@ -34,11 +34,11 @@ protected void execute(final CommandSender sender, final String label, final Str
final String kitName = kit != null ? kit.getName() : lang.getMessage("GENERAL.none");

if (queueManager.remove(sender, kit, bet) == null) {
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", bet, "kit", kitName);
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", NumberUtil.formatDouble(bet), "kit", kitName);
return;
}

lang.sendMessage(sender, "COMMAND.duels.delete-queue", "kit", kitName, "bet_amount", bet);
lang.sendMessage(sender, "COMMAND.duels.delete-queue", "kit", kitName, "bet_amount", NumberUtil.formatDouble(bet));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.realized.duels.queue.Queue;
import me.realized.duels.queue.sign.QueueSignImpl;
import me.realized.duels.util.BlockUtil;
import me.realized.duels.util.NumberUtil;
import me.realized.duels.util.StringUtil;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -43,6 +44,6 @@ protected void execute(final CommandSender sender, final String label, final Str
final Queue queue = queueSign.getQueue();
final Kit kit = queue.getKit();
final String kitName = kit != null ? kit.getName() : lang.getMessage("GENERAL.none");
lang.sendMessage(sender, "COMMAND.duels.del-sign", "location", StringUtil.parse(location), "kit", kitName, "bet_amount", queue.getBet());
lang.sendMessage(sender, "COMMAND.duels.del-sign", "location", StringUtil.parse(location), "kit", kitName, "bet_amount", NumberUtil.formatDouble(queue.getBet()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ protected void execute(final CommandSender sender, final String label, final Str
}

final String kitName = kit != null ? kit.getName() : lang.getMessage("GENERAL.none");
final int bet = args.length > 2 ? NumberUtil.parseInt(args[args.length - 1]).orElse(0) : 0;
final double bet = args.length > 2 ? NumberUtil.parseDouble(args[args.length - 1]).orElse(0) : 0;
final Queue queue = args[1].equals("-r") ? queueManager.randomQueue() : queueManager.get(kit, bet);

if (queue == null) {
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", bet, "kit", kitName);
lang.sendMessage(sender, "ERROR.queue.not-found", "bet_amount", NumberUtil.formatDouble(bet), "kit", kitName);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public class Config extends AbstractConfiguration<DuelsPlugin> {
@Getter
private boolean moneyBettingUsePermission;
@Getter
private double moneyBettingMinimumBetAmount;
@Getter
private double moneyBettingMaximumBetAmount;
@Getter
private int expiration;

@Getter
Expand Down Expand Up @@ -297,6 +301,8 @@ protected void loadValues(FileConfiguration configuration) throws Exception {
itemBettingUsePermission = configuration.getBoolean("request.item-betting.use-permission", false);
moneyBettingEnabled = configuration.getBoolean("request.money-betting.enabled", true);
moneyBettingUsePermission = configuration.getBoolean("request.money-betting.use-permission", false);
moneyBettingMinimumBetAmount = configuration.getDouble("request.money-betting.minimum-bet-amount", 0.001);
moneyBettingMaximumBetAmount = configuration.getDouble("request.money-betting.maximum-bet-amount", 10000000000.0);
expiration = Math.max(configuration.getInt("request.expiration", 30), 0);

maxDuration = configuration.getInt("duel.match.max-duration", -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class QueueData {

private String kit;
private int bet;
private double bet;

private QueueData() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.realized.duels.kit.KitImpl;
import me.realized.duels.queue.Queue;
import me.realized.duels.queue.sign.QueueSignImpl;
import me.realized.duels.util.NumberUtil;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
Expand All @@ -12,7 +13,7 @@ public class QueueSignData {

private LocationData location;
private String kit;
private int bet;
private double bet;

private QueueSignData() {}

Expand Down Expand Up @@ -45,6 +46,9 @@ public QueueSignImpl toQueueSign(final DuelsPlugin plugin) {
queue = plugin.getQueueManager().get(kit, bet);
}

return new QueueSignImpl(location, plugin.getLang().getMessage("SIGN.format", "kit", this.kit != null ? this.kit : plugin.getLang().getMessage("GENERAL.none"), "bet_amount", bet), queue);
return new QueueSignImpl(location, plugin.getLang().getMessage("SIGN.format",
"kit", this.kit != null ? this.kit : plugin.getLang().getMessage("GENERAL.none"),
"bet_amount", NumberUtil.formatDouble(bet)
), queue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ private void handleWin(final Player player, final Player opponent, final ArenaIm
final String opponentName = opponent != null ? opponent.getName() : lang.getMessage("GENERAL.none");

if (vault != null && match.getBet() > 0) {
final int amount = match.getBet() * 2;
final double amount = match.getBet() * 2;
vault.add(amount, player);
lang.sendMessage(player, "DUEL.reward.money.message", "name", opponentName, "money", amount);
lang.sendMessage(player, "DUEL.reward.money.message", "name", opponentName, "money", NumberUtil.formatDouble(amount));

final String title = lang.getMessage("DUEL.reward.money.title", "name", opponentName, "money", amount);
final String title = lang.getMessage("DUEL.reward.money.title", "name", opponentName, "money", NumberUtil.formatDouble(amount));

if (title != null) {
Titles.send(player, title, null, 0, 20, 50);
Expand Down Expand Up @@ -338,11 +338,12 @@ public void startMatch(final Player first, final Player second, final Settings s
return;
}

final int bet = settings.getBet();
final double bet = settings.getBet();

if (bet > 0 && vault != null && vault.getEconomy() != null) {
if (!vault.has(bet, first, second)) {
lang.sendMessage(Arrays.asList(first, second), "DUEL.start-failure.not-enough-money", "bet_amount", bet);
lang.sendMessage(Arrays.asList(first, second), "DUEL.start-failure.not-enough-money",
"bet_amount", NumberUtil.formatDouble(bet));
refundItems(items, first, second);
return;
}
Expand Down Expand Up @@ -591,7 +592,7 @@ public void on(final PlayerDeathEvent event) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command
.replace("%winner%", winner.getName()).replace("%loser%", player.getName())
.replace("%kit%", kitName).replace("%arena%", arena.getName())
.replace("%bet_amount%", String.valueOf(match.getBet()))
.replace("%bet_amount%", NumberUtil.formatDouble(match.getBet()))
);
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.realized.duels.DuelsPlugin;
import me.realized.duels.gui.BaseButton;
import me.realized.duels.setting.Settings;
import me.realized.duels.util.NumberUtil;
import me.realized.duels.util.compat.Items;
import me.realized.duels.util.inventory.ItemBuilder;
import org.bukkit.entity.Player;
Expand All @@ -25,7 +26,7 @@ public void update(final Player player) {
final String lore = lang.getMessage("GUI.item-betting.buttons.details.lore",
"kit", settings.getKit() != null ? settings.getKit().getName() : lang.getMessage("GENERAL.not-selected"),
"arena", settings.getArena() != null ? settings.getArena().getName() : lang.getMessage("GENERAL.random"),
"bet_amount", settings.getBet()
"bet_amount", NumberUtil.formatDouble(settings.getBet())
);
setLore(lore.split("\n"));
}
Expand Down
Loading