TF-EssentialsX/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java

460 lines
12 KiB
Java
Raw Normal View History

package com.earth2me.essentials.signs;
2011-06-13 13:05:31 +00:00
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.ItemDb;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.block.CraftSign;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
public class EssentialsSign
{
protected transient final String signName;
private static final String FORMAT_SUCCESS = "§1[%s]";
private static final String FORMAT_FAIL = "§4[%s]";
public EssentialsSign(final String signName)
{
this.signName = signName;
}
public final boolean onSignCreate(final SignChangeEvent event, final IEssentials ess)
{
final ISign sign = new EventSign(event);
sign.setLine(0, String.format(FORMAT_FAIL, this.signName));
final User user = ess.getUser(event.getPlayer());
if (!(user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".create")
|| user.isAuthorized("essentials.signs.create." + signName.toLowerCase())))
{
return false;
}
try
{
2011-06-13 13:05:31 +00:00
final boolean ret = onSignCreate(sign, user, getUsername(user), ess);
if (ret)
{
sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName));
}
return ret;
}
2011-06-13 13:05:31 +00:00
catch (ChargeException ex)
{
ess.showError(user, ex, signName);
}
2011-06-13 13:05:31 +00:00
catch (SignException ex)
{
2011-06-13 13:05:31 +00:00
ess.showError(user, ex, signName);
}
2011-06-13 13:05:31 +00:00
return false;
}
private String getUsername(final User user)
{
return user.getName().substring(0, user.getName().length() > 14 ? 14 : user.getName().length());
}
public final boolean onSignInteract(final PlayerInteractEvent event, final IEssentials ess)
{
final ISign sign = new BlockSign(event.getClickedBlock());
final User user = ess.getUser(event.getPlayer());
try
{
return (user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".use")
|| user.isAuthorized("essentials.signs.use." + signName.toLowerCase()))
&& onSignInteract(sign, user, getUsername(user), ess);
}
catch (ChargeException ex)
{
ess.showError(user, ex, signName);
return false;
}
catch (SignException ex)
{
ess.showError(user, ex, signName);
return false;
}
}
public final boolean onSignBreak(final BlockBreakEvent event, final IEssentials ess)
{
final ISign sign = new BlockSign(event.getBlock());
final User user = ess.getUser(event.getPlayer());
try
{
return (user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".break")
|| user.isAuthorized("essentials.signs.break." + signName.toLowerCase()))
&& onSignBreak(sign, user, getUsername(user), ess);
}
catch (SignException ex)
{
ess.showError(user, ex, signName);
return false;
}
}
2011-06-13 13:05:31 +00:00
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
return true;
}
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
return true;
}
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
return true;
}
2011-06-13 13:05:31 +00:00
protected final void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException
{
2011-06-12 20:33:47 +00:00
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
return;
}
2011-06-13 13:05:31 +00:00
final Trade trade = getTrade(sign, index, 0, ess);
final Double money = trade.getMoney();
if (money != null)
{
sign.setLine(index, Util.formatCurrency(money));
}
}
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("[ :]+");
2011-06-13 13:05:31 +00:00
if (split.length == 1 && !amountNeeded)
{
2011-06-13 13:05:31 +00:00
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"));
}
2011-06-13 13:05:31 +00:00
sign.setLine(index, amount + " " + split[1] + ":0");
return;
}
2011-06-13 13:05:31 +00:00
if (split.length == 3 && amountNeeded)
{
2011-06-13 13:05:31 +00:00
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)
{
2011-06-13 13:05:31 +00:00
throw new SignException(Util.i18n("moreThanZero"));
}
2011-06-13 13:05:31 +00:00
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)
{
2011-06-13 13:05:31 +00:00
return new Trade(fullAmount ? amount : money, ess);
}
2011-06-13 13:05:31 +00:00
}
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)
{
2011-06-13 13:05:31 +00:00
throw new SignException(Util.i18n("moreThanZero"));
}
2011-06-13 13:05:31 +00:00
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());
}
}
2011-06-13 13:05:31 +00:00
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);
}
2011-06-12 20:33:47 +00:00
protected final void validateInteger(final ISign sign, final int index) throws SignException
{
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
throw new SignException("Empty line " + index);
}
final int quantity = getInteger(line);
sign.setLine(index, Integer.toString(quantity));
}
protected final int getInteger(final String line) throws SignException
{
try
{
final int quantity = Integer.parseInt(line);
if (quantity <= 1)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return quantity;
}
catch (NumberFormatException ex)
{
throw new SignException("Invalid sign", ex);
}
}
2011-06-13 13:05:31 +00:00
protected final ItemStack getItemStack(final String itemName, final int quantity) throws SignException
{
try
{
2011-06-13 13:05:31 +00:00
final ItemStack item = ItemDb.get(itemName);
item.setAmount(quantity);
return item;
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
}
2011-06-13 13:05:31 +00:00
private final Double getMoney(final String line) throws SignException
2011-06-12 20:33:47 +00:00
{
2011-06-13 13:05:31 +00:00
final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+");
return isMoney ? getDouble(line.substring(1)) : null;
2011-06-12 20:33:47 +00:00
}
2011-06-13 13:05:31 +00:00
private final Double getDouble(final String line) throws SignException
2011-06-12 20:33:47 +00:00
{
2011-06-13 13:05:31 +00:00
try
2011-06-12 20:33:47 +00:00
{
2011-06-13 13:05:31 +00:00
final double quantity = Double.parseDouble(line);
if (quantity <= 0.0)
2011-06-12 20:33:47 +00:00
{
2011-06-13 13:05:31 +00:00
throw new SignException(Util.i18n("moreThanZero"));
2011-06-12 20:33:47 +00:00
}
2011-06-13 13:05:31 +00:00
return quantity;
2011-06-12 20:33:47 +00:00
}
2011-06-13 13:05:31 +00:00
catch (NumberFormatException ex)
2011-06-12 20:33:47 +00:00
{
2011-06-13 13:05:31 +00:00
throw new SignException(ex.getMessage(), ex);
2011-06-12 20:33:47 +00:00
}
}
2011-06-13 13:05:31 +00:00
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
{
2011-06-12 20:33:47 +00:00
final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
2011-06-13 13:05:31 +00:00
return new Trade(signName.toLowerCase() + "sign", ess);
}
2011-06-13 13:05:31 +00:00
final Double money = getMoney(line);
if (money == null)
{
2011-06-13 13:05:31 +00:00
final String[] split = line.split("[ :]+", 2);
if (split.length != 2)
{
throw new SignException(Util.i18n("invalidCharge"));
}
2011-06-13 13:05:31 +00:00
final int quantity = getInteger(split[0]);
final String item = split[1].toLowerCase();
if (item.equalsIgnoreCase("times"))
{
2011-06-13 13:05:31 +00:00
sign.setLine(index, (quantity - decrement) + " times");
return new Trade(signName.toLowerCase() + "sign", ess);
}
2011-06-13 13:05:31 +00:00
else
{
2011-06-13 13:05:31 +00:00
final ItemStack stack = getItemStack(item, quantity);
sign.setLine(index, quantity + " " + item);
return new Trade(quantity, ess);
}
}
2011-06-13 13:05:31 +00:00
else
{
return new Trade(money, ess);
}
}
static class EventSign implements ISign
{
private final transient SignChangeEvent event;
public EventSign(final SignChangeEvent event)
{
this.event = event;
}
public final String getLine(final int index)
{
return event.getLine(index);
}
public final void setLine(final int index, final String text)
{
event.setLine(index, text);
}
}
static class BlockSign implements ISign
{
private final transient Sign sign;
public BlockSign(final Block block)
{
this.sign = new CraftSign(block);
}
public final String getLine(final int index)
{
return sign.getLine(index);
}
public final void setLine(final int index, final String text)
{
sign.setLine(index, text);
}
}
public interface ISign
{
String getLine(final int index);
void setLine(final int index, final String text);
}
}