Refactor entry fee logic.

This commit swaps out the naive call to takeFee with a more robust approach that actually checks the result. If it fails, the player doesn't join.

It also refactors takeFee so that it will refund any already taken parts of the entry fee if one of the parts fails.
This commit is contained in:
Andreas Troelsen 2018-05-04 03:52:07 +02:00
parent 866c928b0b
commit dd95e16881

View File

@ -49,9 +49,11 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.potion.PotionEffect;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@ -668,7 +670,13 @@ public class ArenaImpl implements Arena
specPlayers.remove(p);
takeFee(p);
if (!entryFee.isEmpty()) {
if (!takeFee(p)) {
messenger.tell(p, Msg.JOIN_FEE_REQUIRED, MAUtils.listToString(entryFee, plugin));
return false;
}
messenger.tell(p, Msg.JOIN_FEE_PAID.format(MAUtils.listToString(entryFee, plugin)));
}
// Announce globally (must happen before moving player)
if (settings.getBoolean("global-join-announce", false)) {
@ -1498,13 +1506,17 @@ public class ArenaImpl implements Arena
@Override
public boolean takeFee(Player p) {
if (entryFee.isEmpty()) return true;
Deque<Thing> paid = new ArrayDeque<>();
for (Thing fee : entryFee) {
fee.takeFrom(p);
if (fee.takeFrom(p)) {
paid.push(fee);
} else {
while (!paid.isEmpty()) {
paid.pop().giveTo(p);
}
return false;
}
}
messenger.tell(p, Msg.JOIN_FEE_PAID.format(MAUtils.listToString(entryFee, plugin)));
return true;
}