From fdd8fffbb33befb681c680201fbd45c41be7c4b7 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 7 Dec 2011 10:31:18 +0100 Subject: Support for experience as trading goods on signs. This will not work until experience is fixed in Bukkit. --- Essentials/src/com/earth2me/essentials/IUser.java | 4 ++ Essentials/src/com/earth2me/essentials/Trade.java | 50 ++++++++++++-- .../earth2me/essentials/signs/EssentialsSign.java | 5 ++ .../com/earth2me/essentials/signs/SignTrade.java | 80 ++++++++++++++++------ 4 files changed, 114 insertions(+), 25 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 63081b4f9..09d3c294d 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -64,4 +64,8 @@ public interface IUser Teleport getTeleport(); void setJail(String jail); + + public int getTotalExperience(); + + public void setTotalExperience(int l); } diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index cf4c3080d..b9d38958f 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -20,28 +20,35 @@ public class Trade private final transient String command; private final transient Double money; private final transient ItemStack itemStack; + private final transient Integer exp; private final transient IEssentials ess; public Trade(final String command, final IEssentials ess) { - this(command, null, null, ess); + this(command, null, null, null, ess); } public Trade(final double money, final IEssentials ess) { - this(null, money, null, ess); + this(null, money, null, null, ess); } public Trade(final ItemStack items, final IEssentials ess) { - this(null, null, items, ess); + this(null, null, items, null, ess); + } + + public Trade(final int exp, final IEssentials ess) + { + this(null, null, null, exp, ess); } - private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess) + private Trade(final String command, final Double money, final ItemStack item, final Integer exp, final IEssentials ess) { this.command = command; this.money = money; this.itemStack = item; + this.exp = exp; this.ess = ess; } @@ -71,6 +78,11 @@ public class Trade { throw new ChargeException(_("notEnoughMoney")); } + + if (exp != null && exp > 0 + && user.getTotalExperience() < exp) { + throw new ChargeException(_("notEnoughExperience")); + } } public void pay(final IUser user) @@ -101,6 +113,10 @@ public class Trade } user.updateInventory(); } + if (getExperience() != null) + { + user.setTotalExperience(user.getTotalExperience() + getExperience()); + } return success; } @@ -136,6 +152,15 @@ public class Trade } user.takeMoney(cost); } + if (getExperience() != null) + { + final int experience = user.getTotalExperience(); + if (experience < getExperience() && getExperience() > 0) + { + throw new ChargeException(_("notEnoughExperience")); + } + user.setTotalExperience(experience - getExperience()); + } } public Double getMoney() @@ -147,6 +172,11 @@ public class Trade { return itemStack; } + + public Integer getExperience() + { + return exp; + } private static FileWriter fw = null; public static void log(String type, String subtype, String event, String sender, Trade charge, String receiver, Trade pay, Location loc, IEssentials ess) @@ -193,6 +223,12 @@ public class Trade sb.append("money").append(","); sb.append(ess.getSettings().getCurrencySymbol()); } + if (charge.getExperience() != null) + { + sb.append(charge.getExperience()).append(","); + sb.append("exp").append(","); + sb.append("\"\""); + } } sb.append(",\""); if (receiver != null) @@ -218,6 +254,12 @@ public class Trade sb.append("money").append(","); sb.append(ess.getSettings().getCurrencySymbol()); } + if (pay.getExperience() != null) + { + sb.append(pay.getExperience()).append(","); + sb.append("exp").append(","); + sb.append("\"\""); + } } if (loc == null) { diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index aaf040c02..1e62a6097 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -399,6 +399,11 @@ public class EssentialsSign sign.setLine(index, (quantity - decrement) + " times"); return new Trade(signName.toLowerCase(Locale.ENGLISH) + "sign", ess); } + else if (item.equalsIgnoreCase("exp") || item.equalsIgnoreCase("xp")) + { + sign.setLine(index, quantity + " exp"); + return new Trade(quantity, ess); + } else { final ItemStack stack = getItemStack(item, quantity, ess); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index 4b6e4395c..9296eb6f4 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -160,8 +160,13 @@ public class SignTrade extends EssentialsSign if (split.length == 2 && !amountNeeded) { final int amount = getIntegerPositive(split[0]); - final ItemStack item = getItemStack(split[1], amount, ess); - if (amount < 1 || item.getTypeId() == 0) + + if (amount < 1) + { + throw new SignException(_("moreThanZero")); + } + if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) + && getItemStack(split[1], amount, ess).getTypeId() == 0) { throw new SignException(_("moreThanZero")); } @@ -177,10 +182,14 @@ public class SignTrade extends EssentialsSign if (split.length == 3 && amountNeeded) { final int stackamount = getIntegerPositive(split[0]); - final ItemStack item = getItemStack(split[1], stackamount, ess); int amount = getIntegerPositive(split[2]); amount -= amount % stackamount; - if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) + if (amount < 1 || stackamount < 1) + { + throw new SignException(_("moreThanZero")); + } + if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) + && getItemStack(split[1], stackamount, ess).getTypeId() == 0) { throw new SignException(_("moreThanZero")); } @@ -218,16 +227,30 @@ public class SignTrade extends EssentialsSign if (split.length == 3) { - final int stackamount = getIntegerPositive(split[0]); - final ItemStack item = getItemStack(split[1], stackamount, ess); - int amount = getInteger(split[2]); - amount -= amount % stackamount; - if (notEmpty && (amount < 1 || stackamount < 1 || item.getTypeId() == 0)) + if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) { - throw new SignException(_("tradeSignEmpty")); + final int stackamount = getIntegerPositive(split[0]); + int amount = getInteger(split[2]); + amount -= amount % stackamount; + if (notEmpty && (amount < 1 || stackamount < 1)) + { + throw new SignException(_("tradeSignEmpty")); + } + return new Trade(fullAmount ? amount : stackamount, ess); + } + else + { + final int stackamount = getIntegerPositive(split[0]); + final ItemStack item = getItemStack(split[1], stackamount, ess); + int amount = getInteger(split[2]); + amount -= amount % stackamount; + if (notEmpty && (amount < 1 || stackamount < 1 || item.getTypeId() == 0)) + { + throw new SignException(_("tradeSignEmpty")); + } + item.setAmount(fullAmount ? amount : stackamount); + return new Trade(item, ess); } - item.setAmount(fullAmount ? amount : stackamount); - return new Trade(item, ess); } throw new SignException(_("invalidSignLine", index + 1)); } @@ -287,17 +310,32 @@ public class SignTrade extends EssentialsSign if (split.length == 3) { - final int stackamount = getIntegerPositive(split[0]); - //TODO: Unused local variable - final ItemStack item = getItemStack(split[1], stackamount, ess); - final int amount = getInteger(split[2]); - final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); - if (newline.length() > 15) + if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) { - throw new SignException("Line too long!"); + final int stackamount = getIntegerPositive(split[0]); + final int amount = getInteger(split[2]); + final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); + if (newline.length() > 15) + { + throw new SignException("Line too long!"); + } + sign.setLine(index, newline); + return; + } + else + { + final int stackamount = getIntegerPositive(split[0]); + //TODO: Unused local variable + final ItemStack item = getItemStack(split[1], stackamount, ess); + final int amount = getInteger(split[2]); + final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); + if (newline.length() > 15) + { + throw new SignException("Line too long!"); + } + sign.setLine(index, newline); + return; } - sign.setLine(index, newline); - return; } throw new SignException(_("invalidSignLine", index + 1)); } -- cgit v1.2.3