Support for experience as trading goods on signs. This will not work until experience is fixed in Bukkit.

This commit is contained in:
snowleo 2011-12-07 10:31:18 +01:00
parent 67a3a55f5a
commit fdd8fffbb3
4 changed files with 114 additions and 25 deletions

View File

@ -64,4 +64,8 @@ public interface IUser
Teleport getTeleport();
void setJail(String jail);
public int getTotalExperience();
public void setTotalExperience(int l);
}

View File

@ -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)
{

View File

@ -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);

View File

@ -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));
}