Use the Things API for entry fees and class prices.

While it doesn't really make sense to have class prices be a Thing (or maybe it does?), this does get rid of the dirty "ItemStacks wit ID -29" hack for economy money, which will eventually break, when the upstream int-based ID API breaks.
This commit is contained in:
Andreas Troelsen 2017-07-30 23:51:56 +02:00
parent 2c96122e7d
commit d1ad24b487
10 changed files with 63 additions and 107 deletions

View File

@ -4,6 +4,7 @@ import static org.bukkit.Material.*;
import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster;
import com.garbagemule.MobArena.things.Thing;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -28,14 +29,14 @@ public class ArenaClass
private Map<String,Boolean> perms;
private Map<String,Boolean> lobbyperms;
private boolean unbreakableWeapons, unbreakableArmor;
private double price;
private Thing price;
private Location classchest;
/**
* Create a new, empty arena class with the given name.
* @param name the class name as it appears in the config-file
*/
public ArenaClass(String name, double price, boolean unbreakableWeapons, boolean unbreakableArmor) {
public ArenaClass(String name, Thing price, boolean unbreakableWeapons, boolean unbreakableArmor) {
this.configName = name;
this.lowercaseName = name.toLowerCase().replace(" ", "");
@ -283,7 +284,7 @@ public class ArenaClass
return unbreakableArmor;
}
public double getPrice() {
public Thing getPrice() {
return price;
}
@ -377,7 +378,7 @@ public class ArenaClass
public static class MyItems extends ArenaClass {
private ArenaMaster am;
public MyItems(double price, boolean unbreakableWeapons, boolean unbreakableArmor, ArenaMaster am) {
public MyItems(Thing price, boolean unbreakableWeapons, boolean unbreakableArmor, ArenaMaster am) {
super("My Items", price, unbreakableWeapons, unbreakableArmor);
this.am = am;
}

View File

@ -54,6 +54,7 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.potion.PotionEffect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@ -119,7 +120,7 @@ public class ArenaImpl implements Arena
// Misc
private ArenaListener eventListener;
private List<ItemStack> entryFee;
private List<Thing> entryFee;
private TimeStrategy timeStrategy;
private AutoStartTimer autoStartTimer;
private StartDelayTimer startDelayTimer;
@ -190,10 +191,26 @@ public class ArenaImpl implements Arena
// Misc
this.eventListener = new ArenaListener(this, plugin);
this.entryFee = ItemParser.parseItems(settings.getString("entry-fee", ""));
this.allowMonsters = world.getAllowMonsters();
this.allowAnimals = world.getAllowAnimals();
this.entryFee = new ArrayList<>();
String feeString = settings.getString("entry-fee", "");
if (feeString != null && !feeString.isEmpty()) {
for (String fee : feeString.split(",")) {
try {
Thing thing = plugin.getThingManager().parse(fee.trim());
if (thing == null) {
plugin.getLogger().warning("Failed to parse entry fee: " + fee.trim());
} else {
this.entryFee.add(thing);
}
} catch (Exception e) {
plugin.getLogger().severe("Exception parsing entry fee '" + fee.trim() + "': " + e.getLocalizedMessage());
}
}
}
this.autoStartTimer = new AutoStartTimer(this);
this.startDelayTimer = new StartDelayTimer(this, autoStartTimer);
@ -289,7 +306,7 @@ public class ArenaImpl implements Arena
}
@Override
public List<ItemStack> getEntryFee() {
public List<Thing> getEntryFee() {
return entryFee;
}
@ -512,9 +529,9 @@ public class ArenaImpl implements Arena
assignClassPermissions(p);
arenaPlayerMap.get(p).resetStats();
double price = arenaPlayerMap.get(p).getArenaClass().getPrice();
if (price > 0D) {
plugin.takeMoney(p, price);
Thing price = arenaPlayerMap.get(p).getArenaClass().getPrice();
if (price != null) {
price.takeFrom(p);
}
scoreboard.addPlayer(p);
@ -1523,21 +1540,9 @@ public class ArenaImpl implements Arena
@Override
public boolean canAfford(Player p) {
if (entryFee.isEmpty()) return true;
PlayerInventory inv = p.getInventory();
for (ItemStack stack : entryFee) {
// Economy money
if (stack.getTypeId() == MobArena.ECONOMY_MONEY_ID) {
if (!plugin.hasEnough(p, stack)) {
return false;
}
}
// Normal stack
else {
if (!inv.contains(stack.getType(), stack.getAmount())) {
return false;
}
for (Thing fee : entryFee) {
if (!fee.heldBy(p)) {
return false;
}
}
return true;
@ -1546,34 +1551,11 @@ public class ArenaImpl implements Arena
@Override
public boolean takeFee(Player p) {
if (entryFee.isEmpty()) return true;
PlayerInventory inv = p.getInventory();
// Take some economy money
for (ItemStack stack : InventoryUtils.extractAll(MobArena.ECONOMY_MONEY_ID, entryFee)) {
plugin.takeMoney(p, stack);
for (Thing fee : entryFee) {
fee.takeFrom(p);
}
// Take any other items
for (ItemStack fee : entryFee) {
if (fee.getTypeId() < 0) continue;
int remaining = fee.getAmount();
while (remaining > 0) {
int slot = inv.first(fee.getType());
if (slot < 0) break;
ItemStack item = inv.getItem(slot);
remaining -= item.getAmount();
if (remaining >= 0) {
inv.setItem(slot, null);
} else {
item.setAmount(-remaining);
inv.setItem(slot, item);
}
}
}
messenger.tell(p, Msg.JOIN_FEE_PAID.format(MAUtils.listToString(entryFee, plugin)));
return true;
}
@ -1582,17 +1564,9 @@ public class ArenaImpl implements Arena
public boolean refund(Player p) {
if (entryFee.isEmpty()) return true;
if (!inLobby(p)) return false;
// Refund economy money
for (ItemStack stack : InventoryUtils.extractAll(MobArena.ECONOMY_MONEY_ID, entryFee)) {
plugin.giveMoney(p, stack);
}
// Refund other items.
for (ItemStack stack : entryFee) {
if (stack.getTypeId() > 0) {
p.getInventory().addItem(stack);
}
for (Thing fee : entryFee) {
fee.giveTo(p);
}
return true;
}

View File

@ -1067,10 +1067,10 @@ public class ArenaListener
}
// Check price, balance, and inform
double price = newAC.getPrice();
if (price > 0D) {
if (!plugin.hasEnough(p, price)) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_TOO_EXPENSIVE, plugin.economyFormat(price));
Thing price = newAC.getPrice();
if (price != null) {
if (!price.heldBy(p)) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_TOO_EXPENSIVE, price.toString());
return;
}
}
@ -1095,7 +1095,7 @@ public class ArenaListener
return true;
}*/
private void delayAssignClass(final Player p, final String className, final double price, final Sign sign) {
private void delayAssignClass(final Player p, final String className, final Thing price, final Sign sign) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() {
public void run() {
if (!className.equalsIgnoreCase("random")) {
@ -1111,8 +1111,8 @@ public class ArenaListener
}
arena.assignClass(p, className);
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PICKED, arena.getClasses().get(className).getConfigName());
if (price > 0D) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PRICE, plugin.economyFormat(price));
if (price != null) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PRICE, price.toString());
}
}
else {

View File

@ -6,6 +6,7 @@ import static com.garbagemule.MobArena.util.config.ConfigUtils.parseLocation;
import com.garbagemule.MobArena.ArenaClass.ArmorType;
import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster;
import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.util.ItemParser;
import com.garbagemule.MobArena.util.TextUtils;
import com.garbagemule.MobArena.util.config.ConfigUtils;
@ -294,7 +295,7 @@ public class ArenaMasterImpl implements ArenaMaster
if (section == null) {
// We may not have a class entry for My Items, but that's fine
if (classname.equals("My Items")) {
ArenaClass myItems = new ArenaClass.MyItems(0, false, false, this);
ArenaClass myItems = new ArenaClass.MyItems(null, false, false, this);
classes.put(lowercase, myItems);
return myItems;
}
@ -307,15 +308,14 @@ public class ArenaMasterImpl implements ArenaMaster
boolean arms = section.getBoolean("unbreakable-armor", true);
// Grab the class price, if any
double price = -1D;
Thing price = null;
String priceString = section.getString("price", null);
if (priceString != null) {
ItemStack priceItem = ItemParser.parseItem(priceString);
if (priceItem != null && priceItem.getTypeId() == MobArena.ECONOMY_MONEY_ID) {
price = (priceItem.getAmount() + (priceItem.getDurability() / 100D));
} else {
plugin.getLogger().warning("The price for class '" + classname + "' could not be parsed!");
plugin.getLogger().warning("- expected e.g. '$10', found '" + priceString + "'");
try {
price = plugin.getThingManager().parse(priceString);
} catch (Exception e) {
plugin.getLogger().warning("Exception parsing class price: " + e.getLocalizedMessage());
price = null;
}
}

View File

@ -223,19 +223,6 @@ public class MAUtils
ItemStack stack;
for (E e : list) {
stack = (ItemStack) e;
if (stack.getTypeId() == MobArena.ECONOMY_MONEY_ID) {
String formatted = plugin.economyFormat(stack);
if (formatted != null) {
buffy.append(formatted);
buffy.append(", ");
}
else {
plugin.getLogger().warning("Tried to do some money stuff, but no economy plugin was detected!");
return buffy.toString();
}
continue;
}
buffy.append(stack.getType().toString().toLowerCase());
buffy.append(":");
buffy.append(stack.getAmount());

View File

@ -55,7 +55,6 @@ public class MobArena extends JavaPlugin
private FileConfiguration config;
public static final double MIN_PLAYER_DISTANCE_SQUARED = 225D;
public static final int ECONOMY_MONEY_ID = -29;
public static Random random = new Random();
private Messenger messenger;

View File

@ -8,6 +8,7 @@ import com.garbagemule.MobArena.commands.CommandInfo;
import com.garbagemule.MobArena.commands.Commands;
import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster;
import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.util.ClassChests;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -70,10 +71,10 @@ public class PickClassCommand implements Command
}
// Check price, balance, and inform
double price = ac.getPrice();
if (price > 0D) {
if (!am.getPlugin().hasEnough(p, price)) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_TOO_EXPENSIVE, am.getPlugin().economyFormat(price));
Thing price = ac.getPrice();
if (price != null) {
if (!price.heldBy(p)) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_TOO_EXPENSIVE, price.toString());
return true;
}
}
@ -91,8 +92,8 @@ public class PickClassCommand implements Command
}
arena.assignClass(p, lowercase);
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PICKED, arena.getClasses().get(lowercase).getConfigName());
if (price > 0D) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PRICE, am.getPlugin().economyFormat(price));
if (price != null) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PRICE, price.toString());
}
} else {
arena.addRandomPlayer(p);

View File

@ -63,7 +63,7 @@ public interface Arena
int getMaxPlayers();
List<ItemStack> getEntryFee();
List<Thing> getEntryFee();
Set<Map.Entry<Integer,List<Thing>>> getEveryWaveEntrySet();

View File

@ -3,6 +3,7 @@ package com.garbagemule.MobArena.util;
import com.garbagemule.MobArena.ArenaClass;
import com.garbagemule.MobArena.Msg;
import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.things.Thing;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
@ -98,9 +99,9 @@ public class ClassChests {
arena.assignClassGiveInv(player, classname, contents);
arena.getMessenger().tell(player, Msg.LOBBY_CLASS_PICKED, arena.getClasses().get(classname).getConfigName());
double price = ac.getPrice();
if (price > 0D) {
arena.getMessenger().tell(player, Msg.LOBBY_CLASS_PRICE, arena.getPlugin().economyFormat(price));
Thing price = ac.getPrice();
if (price != null) {
arena.getMessenger().tell(player, Msg.LOBBY_CLASS_PRICE, price.toString());
}
}

View File

@ -182,13 +182,6 @@ public class ItemParser
}
private static ItemStack singleItem(String item) {
if (item.matches("\\$(([1-9]\\d*)|(\\d*.\\d\\d?))")) {
double amount = Double.parseDouble(item.substring(1));
int major = (int) amount;
int minor = ((int) (amount * 100D)) % 100;
return new ItemStack(MobArena.ECONOMY_MONEY_ID, major, (short) minor);
}
int id = getTypeId(item);
return new ItemStack(id);
}