1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-17 20:11:21 +02:00

Trade sign

This commit is contained in:
snowleo
2011-06-13 15:05:31 +02:00
parent fd2d2456b9
commit 49bdf5719e
11 changed files with 382 additions and 204 deletions

View File

@@ -33,6 +33,8 @@ public interface IUser
void takeMoney(double value); void takeMoney(double value);
void giveMoney(double value);
PlayerInventory getInventory(); PlayerInventory getInventory();
void updateInventory(); void updateInventory();

View File

@@ -48,11 +48,11 @@ public class Teleport implements Runnable
private long initY; private long initY;
private long initZ; private long initZ;
private Target teleportTarget; private Target teleportTarget;
private Charge chargeFor; private Trade chargeFor;
private final IEssentials ess; private final IEssentials ess;
private static final Logger logger = Logger.getLogger("Minecraft"); private static final Logger logger = Logger.getLogger("Minecraft");
private void initTimer(long delay, Target target, Charge chargeFor) private void initTimer(long delay, Target target, Trade chargeFor)
{ {
this.started = System.currentTimeMillis(); this.started = System.currentTimeMillis();
this.delay = delay; this.delay = delay;
@@ -118,12 +118,12 @@ public class Teleport implements Runnable
this.ess = ess; this.ess = ess;
} }
public void respawn(Spawn spawn, Charge chargeFor) throws Exception public void respawn(Spawn spawn, Trade chargeFor) throws Exception
{ {
teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor); teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor);
} }
public void warp(String warp, Charge chargeFor) throws Exception public void warp(String warp, Trade chargeFor) throws Exception
{ {
Location loc = ess.getWarps().getWarp(warp); Location loc = ess.getWarps().getWarp(warp);
teleport(new Target(loc), chargeFor); teleport(new Target(loc), chargeFor);
@@ -177,17 +177,17 @@ public class Teleport implements Runnable
cancel(false); cancel(false);
} }
public void teleport(Location loc, Charge chargeFor) throws Exception public void teleport(Location loc, Trade chargeFor) throws Exception
{ {
teleport(new Target(loc), chargeFor); teleport(new Target(loc), chargeFor);
} }
public void teleport(Entity entity, Charge chargeFor) throws Exception public void teleport(Entity entity, Trade chargeFor) throws Exception
{ {
teleport(new Target(entity), chargeFor); teleport(new Target(entity), chargeFor);
} }
private void teleport(Target target, Charge chargeFor) throws Exception private void teleport(Target target, Trade chargeFor) throws Exception
{ {
double delay = ess.getSettings().getTeleportDelay(); double delay = ess.getSettings().getTeleportDelay();
@@ -230,7 +230,7 @@ public class Teleport implements Runnable
now(new Target(loc)); now(new Target(loc));
} }
public void now(Location loc, Charge chargeFor) throws Exception public void now(Location loc, Trade chargeFor) throws Exception
{ {
cooldown(false); cooldown(false);
chargeFor.charge(user); chargeFor.charge(user);
@@ -243,7 +243,7 @@ public class Teleport implements Runnable
now(new Target(entity)); now(new Target(entity));
} }
public void back(Charge chargeFor) throws Exception public void back(Trade chargeFor) throws Exception
{ {
teleport(new Target(user.getLastLocation()), chargeFor); teleport(new Target(user.getLastLocation()), chargeFor);
} }
@@ -253,12 +253,12 @@ public class Teleport implements Runnable
back(null); back(null);
} }
public void home(Charge chargeFor) throws Exception public void home(Trade chargeFor) throws Exception
{ {
home(user, chargeFor); home(user, chargeFor);
} }
public void home(IUser user, Charge chargeFor) throws Exception public void home(IUser user, Trade chargeFor) throws Exception
{ {
Location loc = user.getHome(this.user.getLocation()); Location loc = user.getHome(this.user.getLocation());
if (loc == null) if (loc == null)

View File

@@ -1,52 +1,53 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import java.util.Map;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class Charge public class Trade
{ {
private final transient String command; private final transient String command;
private final transient Double costs; private final transient Double money;
private final transient ItemStack items; private final transient ItemStack itemStack;
private final transient IEssentials ess; private final transient IEssentials ess;
public Charge(final String command, final IEssentials ess) public Trade(final String command, final IEssentials ess)
{ {
this(command, null, null, ess); this(command, null, null, ess);
} }
public Charge(final double money, final IEssentials ess) public Trade(final double money, final IEssentials ess)
{ {
this(null, money, null, ess); this(null, money, null, ess);
} }
public Charge(final ItemStack items, final IEssentials ess) public Trade(final ItemStack items, final IEssentials ess)
{ {
this(null, null, items, ess); this(null, null, items, ess);
} }
private Charge(final String command, final Double money, final ItemStack item, final IEssentials ess) private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess)
{ {
this.command = command; this.command = command;
this.costs = money; this.money = money;
this.items = item; this.itemStack = item;
this.ess = ess; this.ess = ess;
} }
public void isAffordableFor(final IUser user) throws ChargeException public void isAffordableFor(final IUser user) throws ChargeException
{ {
final double mon = user.getMoney(); final double mon = user.getMoney();
if (costs != null if (getMoney() != null
&& mon < costs && mon < getMoney()
&& !user.isAuthorized("essentials.eco.loan")) && !user.isAuthorized("essentials.eco.loan"))
{ {
throw new ChargeException(Util.i18n("notEnoughMoney")); throw new ChargeException(Util.i18n("notEnoughMoney"));
} }
if (items != null if (getItemStack() != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, items)) && !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
{ {
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " "))); throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
} }
if (command != null && !command.isEmpty() if (command != null && !command.isEmpty()
@@ -59,24 +60,41 @@ public class Charge
} }
} }
public void pay(final IUser user)
{
if (getMoney() != null)
{
user.giveMoney(getMoney());
}
if (getItemStack() != null)
{
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
}
user.updateInventory();
}
}
public void charge(final IUser user) throws ChargeException public void charge(final IUser user) throws ChargeException
{ {
if (costs != null) if (getMoney() != null)
{ {
final double mon = user.getMoney(); final double mon = user.getMoney();
if (mon < costs && !user.isAuthorized("essentials.eco.loan")) if (mon < getMoney() && !user.isAuthorized("essentials.eco.loan"))
{ {
throw new ChargeException(Util.i18n("notEnoughMoney")); throw new ChargeException(Util.i18n("notEnoughMoney"));
} }
user.takeMoney(costs); user.takeMoney(getMoney());
} }
if (items != null) if (getItemStack() != null)
{ {
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items)) if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
{ {
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " "))); throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
} }
InventoryWorkaround.removeItem(user.getInventory(), true, items); InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack());
user.updateInventory(); user.updateInventory();
} }
if (command != null && !command.isEmpty() if (command != null && !command.isEmpty()
@@ -92,4 +110,14 @@ public class Charge
user.takeMoney(cost); user.takeMoney(cost);
} }
} }
public Double getMoney()
{
return money;
}
public ItemStack getItemStack()
{
return itemStack;
}
} }

View File

@@ -1,6 +1,6 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.ItemDb; import com.earth2me.essentials.ItemDb;
@@ -36,21 +36,24 @@ public class EssentialsSign
{ {
return false; return false;
} }
boolean ret;
try try
{ {
ret = onSignCreate(sign, user, getUsername(user), ess); final boolean ret = onSignCreate(sign, user, getUsername(user), ess);
if (ret)
{
sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName));
}
return ret;
}
catch (ChargeException ex)
{
ess.showError(user, ex, signName);
} }
catch (SignException ex) catch (SignException ex)
{ {
ess.showError(user, ex, signName); ess.showError(user, ex, signName);
ret = false;
} }
if (ret) return false;
{
sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName));
}
return ret;
} }
private String getUsername(final User user) private String getUsername(final User user)
@@ -97,7 +100,7 @@ public class EssentialsSign
} }
} }
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
return true; return true;
} }
@@ -112,50 +115,189 @@ public class EssentialsSign
return true; return true;
} }
protected final void validateCharge(final ISign sign, final int index) throws SignException protected final void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
{ {
final String line = sign.getLine(index).trim(); final String line = sign.getLine(index).trim();
if (line.isEmpty()) if (line.isEmpty())
{ {
return; return;
} }
final Trade trade = getTrade(sign, index, 0, ess);
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+"); final Double money = trade.getMoney();
if (isMoney) if (money != null)
{ {
final double quantity = Double.parseDouble(line.substring(1)); sign.setLine(index, Util.formatCurrency(money));
if (quantity <= 0) }
}
protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 1 && !amountNeeded)
{
final Double money = getMoney(split[0]);
if (money != null)
{
sign.setLine(index, Util.formatCurrency(money) + ":0");
return;
}
}
if (split.length == 2 && amountNeeded)
{
final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]);
if (money != null && amount != null)
{
sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount).substring(1));
return;
}
}
if (split.length == 2 && !amountNeeded)
{
final int amount = getInteger(split[0]);
final ItemStack item = getItemStack(split[1], amount);
if (amount < 1 || item.getTypeId() == 0)
{ {
throw new SignException(Util.i18n("moreThanZero")); throw new SignException(Util.i18n("moreThanZero"));
} }
sign.setLine(index, Util.formatCurrency(quantity)); sign.setLine(index, amount + " " + split[1] + ":0");
return;
} }
else
if (split.length == 3 && amountNeeded)
{ {
final String[] split = line.split("[ :-]+", 2); final int stackamount = getInteger(split[0]);
if (split.length != 2) final ItemStack item = getItemStack(split[1], stackamount);
int amount = getInteger(split[2]);
amount -= amount % stackamount;
if (amount < 1 || stackamount < 1 || item.getTypeId() == 0)
{ {
throw new SignException(Util.i18n("invalidCharge")); throw new SignException(Util.i18n("moreThanZero"));
} }
try sign.setLine(index, stackamount + " " + split[1] + ":" + amount);
return;
}
throw new SignException(Util.format("invalidSignLine", index));
}
protected final Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 2)
{
final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]);
if (money != null && amount != null)
{ {
final int quantity = Integer.parseInt(split[0]); return new Trade(fullAmount ? amount : money, ess);
if (quantity <= 1)
{
throw new SignException(Util.i18n("moreThanZero"));
}
final String item = split[1].toLowerCase();
if (!item.equalsIgnoreCase("times"))
{
getItemStack(item);
}
sign.setLine(index, quantity + " " + item);
}
catch (NumberFormatException ex)
{
throw new SignException(Util.i18n("invalidCharge"), ex);
} }
} }
if (split.length == 3)
{
final int stackamount = getInteger(split[0]);
final ItemStack item = getItemStack(split[1], stackamount);
int amount = getInteger(split[2]);
amount -= amount % stackamount;
if (amount < 1 || stackamount < 1 || item.getTypeId() == 0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
item.setAmount(fullAmount ? amount : stackamount);
return new Trade(item, ess);
}
throw new SignException(Util.format("invalidSignLine", index));
}
protected final void substractAmount(final ISign sign, final int index, final Trade trade) throws SignException
{
final Double money = trade.getMoney();
if (money != null) {
changeAmount(sign, index, -money);
}
final ItemStack item = trade.getItemStack();
if (item != null) {
changeAmount(sign, index, -item.getAmount());
}
}
protected final void addAmount(final ISign sign, final int index, final Trade trade) throws SignException
{
final Double money = trade.getMoney();
if (money != null) {
changeAmount(sign, index, money);
}
final ItemStack item = trade.getItemStack();
if (item != null) {
changeAmount(sign, index, item.getAmount());
}
}
private void changeAmount(final ISign sign, final int index, final double value) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line");
}
final String[] split = line.split("[ :]+");
if (split.length == 2)
{
final Double money = getMoney(split[0]);
final Double amount = getDouble(split[1]);
if (money != null && amount != null)
{
sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount+value).substring(1));
return;
}
}
if (split.length == 3)
{
final int stackamount = getInteger(split[0]);
final ItemStack item = getItemStack(split[1], stackamount);
int amount = getInteger(split[2]);
sign.setLine(index, stackamount + " " + split[1] + ":" + (amount+Math.round(value)));
return;
}
throw new SignException(Util.format("invalidSignLine", index));
}
protected final void validateTrade(final ISign sign, final int amountIndex, final int itemIndex,
final User player, final IEssentials ess) throws SignException
{
final Trade trade = getTrade(sign, amountIndex, itemIndex, player, ess);
final ItemStack item = trade.getItemStack();
sign.setLine(amountIndex, Integer.toString(item.getAmount()));
sign.setLine(itemIndex, sign.getLine(itemIndex).trim());
}
protected final Trade getTrade(final ISign sign, final int amountIndex, final int itemIndex,
final User player, final IEssentials ess) throws SignException
{
final ItemStack item = getItemStack(sign.getLine(itemIndex), 1);
final int amount = Math.min(getInteger(sign.getLine(amountIndex)), item.getType().getMaxStackSize() * player.getInventory().getSize());
if (item.getTypeId() == 0 || amount < 1)
{
throw new SignException(Util.i18n("moreThanZero"));
}
item.setAmount(amount);
return new Trade(item, ess);
} }
protected final void validateInteger(final ISign sign, final int index) throws SignException protected final void validateInteger(final ISign sign, final int index) throws SignException
@@ -186,26 +328,13 @@ public class EssentialsSign
} }
} }
protected final void validateItem(final ISign sign, final int index, final boolean noair) throws SignException protected final ItemStack getItemStack(final String itemName, final int quantity) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line " + index);
}
ItemStack item = getItemStack(line);
if (noair && item.getTypeId() == 0)
{
throw new SignException("Don't sell air.");
}
sign.setLine(index, line);
}
protected final ItemStack getItemStack(final String itemName) throws SignException
{ {
try try
{ {
return ItemDb.get(itemName); final ItemStack item = ItemDb.get(itemName);
item.setAmount(quantity);
return item;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -213,99 +342,69 @@ public class EssentialsSign
} }
} }
protected final void validateMoney(final ISign sign, final int index) throws SignException private final Double getMoney(final String line) throws SignException
{
final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+");
return isMoney ? getDouble(line.substring(1)) : null;
}
private final Double getDouble(final String line) throws SignException
{
try
{
final double quantity = Double.parseDouble(line);
if (quantity <= 0.0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return quantity;
}
catch (NumberFormatException ex)
{
throw new SignException(ex.getMessage(), ex);
}
}
protected final Trade getTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
{
return getTrade(sign, index, 1, ess);
}
protected final Trade getTrade(final ISign sign, final int index, final int decrement, final IEssentials ess) throws SignException
{ {
final String line = sign.getLine(index).trim(); final String line = sign.getLine(index).trim();
if (line.isEmpty()) if (line.isEmpty())
{ {
throw new SignException("Empty line " + index); return new Trade(signName.toLowerCase() + "sign", ess);
}
final double quantity = getMoney(line);
sign.setLine(index, Util.formatCurrency(quantity));
}
protected final double getMoney(final String line) throws SignException
{
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
if (isMoney)
{
try
{
final double quantity = Double.parseDouble(line.substring(1));
if (quantity <= 0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return quantity;
}
catch (NumberFormatException ex)
{
throw new SignException(ex.getMessage(), ex);
}
}
else
{
throw new SignException("Invalid money");
}
}
protected final Charge getCharge(final ISign sign, final int index, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
return new Charge(signName.toLowerCase() + "sign", ess);
} }
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+"); final Double money = getMoney(line);
if (isMoney) if (money == null)
{ {
try final String[] split = line.split("[ :]+", 2);
{
final double quantity = Double.parseDouble(line.substring(1));
if (quantity <= 0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return new Charge(quantity, ess);
}
catch (NumberFormatException ex)
{
throw new SignException(ex.getMessage(), ex);
}
}
else
{
final String[] split = line.split("[ :-]+", 2);
if (split.length != 2) if (split.length != 2)
{ {
throw new SignException(Util.i18n("invalidCharge")); throw new SignException(Util.i18n("invalidCharge"));
} }
try final int quantity = getInteger(split[0]);
final String item = split[1].toLowerCase();
if (item.equalsIgnoreCase("times"))
{ {
final int quantity = Integer.parseInt(split[0]); sign.setLine(index, (quantity - decrement) + " times");
if (quantity <= 1) return new Trade(signName.toLowerCase() + "sign", ess);
{
throw new SignException(Util.i18n("moreThanZero"));
}
final String item = split[1].toLowerCase();
if (item.equalsIgnoreCase("times"))
{
sign.setLine(index, (quantity - 1) + " times");
return new Charge(signName.toLowerCase() + "sign", ess);
}
else
{
final ItemStack stack = getItemStack(item);
stack.setAmount(quantity);
return new Charge(quantity, ess);
}
} }
catch (NumberFormatException ex) else
{ {
throw new SignException(Util.i18n("invalidCharge"), ex); final ItemStack stack = getItemStack(item, quantity);
sign.setLine(index, quantity + " " + item);
return new Trade(quantity, ess);
} }
} }
else
{
return new Trade(money, ess);
}
} }

View File

@@ -1,12 +1,9 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.InventoryWorkaround;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import java.util.Map;
import org.bukkit.inventory.ItemStack;
public class SignBuy extends EssentialsSign public class SignBuy extends EssentialsSign
@@ -19,26 +16,18 @@ public class SignBuy extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
validateInteger(sign, 1); validateTrade(sign, 1, 2, player, ess);
validateItem(sign, 2, true); validateTrade(sign, 3, ess);
validateCharge(sign, 3);
return true; return true;
} }
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
final ItemStack item = getItemStack(sign.getLine(2)); final Trade items = getTrade(sign, 1, 2, player, ess);
final int amount = Math.min(getInteger(sign.getLine(1)), item.getType().getMaxStackSize()*player.getInventory().getSize()); final Trade charge = getTrade(sign, 3, ess);
item.setAmount(amount);
final Charge charge = getCharge(sign, 3, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(player.getInventory(), true, item); items.pay(player);
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(player.getLocation(), itemStack);
}
player.updateInventory();
charge.charge(player); charge.charge(player);
return true; return true;
} }

View File

@@ -1,6 +1,6 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.ItemDb; import com.earth2me.essentials.ItemDb;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@@ -19,17 +19,16 @@ public class SignFree extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
getItemStack(sign.getLine(1)); getItemStack(sign.getLine(1), 9 * 4 * 64);
return true; return true;
} }
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
final ItemStack item = getItemStack(sign.getLine(1)); final ItemStack item = getItemStack(sign.getLine(1), 9 * 4 * 64);
final CraftInventoryPlayer inv = new CraftInventoryPlayer(new InventoryPlayer(player.getHandle())); final CraftInventoryPlayer inv = new CraftInventoryPlayer(new InventoryPlayer(player.getHandle()));
inv.clear(); inv.clear();
item.setAmount(9 * 4 * 64);
inv.addItem(item); inv.addItem(item);
player.showInventory(inv); player.showInventory(inv);
return true; return true;

View File

@@ -1,6 +1,6 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@@ -17,14 +17,14 @@ public class SignHeal extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
validateCharge(sign, 1); validateTrade(sign, 1, ess);
return true; return true;
} }
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
final Charge charge = getCharge(sign, 1, ess); final Trade charge = getTrade(sign, 1, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
player.setHealth(20); player.setHealth(20);
player.sendMessage(Util.i18n("youAreHealed")); player.sendMessage(Util.i18n("youAreHealed"));

View File

@@ -1,10 +1,9 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.inventory.ItemStack;
public class SignSell extends EssentialsSign public class SignSell extends EssentialsSign
@@ -17,22 +16,18 @@ public class SignSell extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
validateInteger(sign, 1); validateTrade(sign, 1, 2, player, ess);
validateItem(sign, 2, true); validateTrade(sign, 3, ess);
validateMoney(sign, 3);
return true; return true;
} }
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
final int amount = getInteger(sign.getLine(1)); final Trade charge = getTrade(sign, 1, 2, player, ess);
final ItemStack item = getItemStack(sign.getLine(2)); final Trade money = getTrade(sign, 3, ess);
item.setAmount(amount);
final double money = getMoney(sign.getLine(3));
final Charge charge = new Charge(item, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
player.giveMoney(money); money.pay(player);
charge.charge(player); charge.charge(player);
return true; return true;
} }

View File

@@ -1,6 +1,6 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@@ -17,7 +17,7 @@ public class SignTime extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
validateCharge(sign, 2); validateTrade(sign, 2, ess);
final String timeString = sign.getLine(1); final String timeString = sign.getLine(1);
if ("Day".equalsIgnoreCase(timeString)) if ("Day".equalsIgnoreCase(timeString))
{ {
@@ -35,7 +35,7 @@ public class SignTime extends EssentialsSign
@Override @Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
final Charge charge = getCharge(sign, 2, ess); final Trade charge = getTrade(sign, 2, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
final String timeString = sign.getLine(1); final String timeString = sign.getLine(1);
long time = player.getWorld().getTime(); long time = player.getWorld().getTime();

View File

@@ -0,0 +1,66 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
public class SignTrade extends EssentialsSign
{
public SignTrade()
{
super("Trade");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
validateTrade(sign, 1, false, ess);
validateTrade(sign, 2, true, ess);
final Trade charge = getTrade(sign, 2, true, ess);
charge.isAffordableFor(player);
sign.setLine(3, "§8" + username);
charge.charge(player);
return true;
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
if (sign.getLine(3).substring(2).equalsIgnoreCase(username))
{
final Trade stored = getTrade(sign, 1, true, ess);
substractAmount(sign, 1, stored);
stored.pay(player);
}
else
{
final Trade charge = getTrade(sign, 1, false, ess);
final Trade trade = getTrade(sign, 2, false, ess);
charge.isAffordableFor(player);
substractAmount(sign, 2, trade);
trade.pay(player);
addAmount(sign, 1, charge);
charge.charge(player);
}
return true;
}
@Override
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
if (sign.getLine(3).substring(2).equalsIgnoreCase(username))
{
final Trade stored1 = getTrade(sign, 1, true, ess);
final Trade stored2 = getTrade(sign, 2, true, ess);
stored1.pay(player);
stored2.pay(player);
return true;
}
else
{
return false;
}
}
}

View File

@@ -1,6 +1,6 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@@ -16,7 +16,7 @@ public class SignWarp extends EssentialsSign
@Override @Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{ {
validateCharge(sign, 3); validateTrade(sign, 3, ess);
final String warpName = sign.getLine(1); final String warpName = sign.getLine(1);
if (warpName.isEmpty()) if (warpName.isEmpty())
@@ -53,7 +53,7 @@ public class SignWarp extends EssentialsSign
|| player.inGroup(group))) || player.inGroup(group)))
|| (!ess.getSettings().getPerWarpPermission() || player.isAuthorized("essentials.warp." + warpName))) || (!ess.getSettings().getPerWarpPermission() || player.isAuthorized("essentials.warp." + warpName)))
{ {
final Charge charge = getCharge(sign, 3, ess); final Trade charge = getTrade(sign, 3, ess);
try try
{ {
player.getTeleport().warp(warpName, charge); player.getTeleport().warp(warpName, charge);