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(); if (line.isEmpty()) { return new Trade(signName.toLowerCase() + "sign", ess); } final Double money = getMoney(line); if (money == null) { final String[] split = line.split("[ :]+", 2); if (split.length != 2) { throw new SignException(Util.i18n("invalidCharge")); } final int quantity = getIntegerPositive(split[0]); final String item = split[1].toLowerCase(); if (item.equalsIgnoreCase("times")) { sign.setLine(index, (quantity - decrement) + " times"); return new Trade(signName.toLowerCase() + "sign", ess); } else { final ItemStack stack = getItemStack(item, quantity, ess); sign.setLine(index, quantity + " " + item); return new Trade(stack, ess); } } else { return new Trade(money, ess); } }
@Override protected boolean onSignBreak(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException { if ((sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username)) || SignsPermissions.TRADE_OVERRIDE.isAuthorized(player)) { try { final Trade stored1 = getTrade(sign, 1, true, false, ess); final Trade stored2 = getTrade(sign, 2, true, false, ess); stored1.pay(player); stored2.pay(player); Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess); } catch (SignException e) { if (SignsPermissions.TRADE_OVERRIDE.isAuthorized(player)) { return true; } throw e; } return true; } else { return false; } }
public final boolean onSignCreate(final SignChangeEvent event, final IEssentials ess) { final ISign sign = new EventSign(event); final IUser user = ess.getUserMap().getUser(event.getPlayer()); if (!SignsPermissions.CREATE.isAuthorized(user, signName)) { // Return true, so other plugins can use the same sign title, just hope // they won't change it to §1[Signname] return true; } sign.setLine(0, _("[{0}]", this.signName)); try { final boolean ret = onSignCreate(sign, user, getUsername(user), ess); if (ret) { sign.setLine(0, getSuccessName()); } return ret; } catch (ChargeException ex) { ess.getCommandHandler().showCommandError(user, signName, ex); } catch (SignException ex) { ess.getCommandHandler().showCommandError(user, signName, ex); } // Return true, so the player sees the wrong sign. return true; }
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 = getIntegerPositive(line); sign.setLine(index, Integer.toString(quantity)); }
//TODO: Translate these exceptions. private void changeAmount(final ISign sign, final int index, final double value, 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 String newline = FormatUtil.shortCurrency(money, ess) + ":" + FormatUtil.shortCurrency(amount + value, ess).substring(1); if (newline.length() > 15) { throw new SignException("This sign is full: Line too long!"); } sign.setLine(index, newline); return; } } if (split.length == 3) { if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) { 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("This sign is full: 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("This sign is full: Line too long!"); } sign.setLine(index, newline); return; } } throw new SignException(_("invalidSignLine", index + 1)); }
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 void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException { final String line = sign.getLine(index).trim(); if (line.isEmpty()) { return; } final Trade trade = getTrade(sign, index, 0, ess); final Double money = trade.getMoney(); if (money != null) { sign.setLine(index, Util.formatCurrency(money, ess)); } }
@Override protected boolean onSignCreate(final ISign sign, final IUser 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, true, ess); charge.isAffordableFor(player); sign.setLine(3, "§8" + username); charge.charge(player); Trade.log("Sign", "Trade", "Create", username, charge, username, null, sign.getBlock().getLocation(), ess); return true; }
protected final Trade getTrade(final ISign sign, final int amountIndex, final int itemIndex, final IUser player, final IEssentials ess) throws SignException { if (sign.getLine(itemIndex).equalsIgnoreCase("exp") || sign.getLine(itemIndex).equalsIgnoreCase("xp")) { final int amount = getIntegerPositive(sign.getLine(amountIndex)); return new Trade(amount, ess); } final ItemStack item = getItemStack(sign.getLine(itemIndex), 1, ess); final int amount = Math.min( getIntegerPositive(sign.getLine(amountIndex)), item.getType().getMaxStackSize() * player.getPlayer().getInventory().getSize()); if (item.getTypeId() == 0 || amount < 1) { throw new SignException(_("Quantities must be greater than 0.")); } item.setAmount(amount); return new Trade(item, ess); }
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, ess); final int amount = Math.min( getIntegerPositive(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 Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final boolean notEmpty, 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) { try { final Double money = getMoney(split[0]); final Double amount = notEmpty ? getDoublePositive(split[1]) : getDouble(split[1]); if (money != null && amount != null) { return new Trade(fullAmount ? amount : money, ess); } } catch (SignException e) { throw new SignException(_("tradeSignEmpty"), e); } } if (split.length == 3) { if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) { 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); } } throw new SignException(_("invalidSignLine", index + 1)); }
@Override protected boolean onSignInteract(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException { if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) { final Trade store = rechargeSign(sign, ess, player); Trade stored = null; try { stored = getTrade(sign, 1, true, true, ess); subtractAmount(sign, 1, stored, ess); stored.pay(player); } catch (SignException e) { if (store == null) { throw new SignException(_("tradeSignEmptyOwner"), e); } } Trade.log("Sign", "Trade", "OwnerInteract", username, store, username, stored, sign.getBlock().getLocation(), ess); } else { final Trade charge = getTrade(sign, 1, false, false, ess); final Trade trade = getTrade(sign, 2, false, true, ess); charge.isAffordableFor(player); addAmount(sign, 1, charge, ess); subtractAmount(sign, 2, trade, ess); if (!trade.pay(player, false)) { subtractAmount(sign, 1, charge, ess); addAmount(sign, 2, trade, ess); throw new ChargeException("Full inventory"); } charge.charge(player); Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, sign.getBlock().getLocation(), ess); } sign.updateSign(); return true; }
protected final void validateTrade(final ISign sign, final int amountIndex, final int itemIndex, final IUser player, final IEssentials ess) throws SignException { if (sign.getLine(itemIndex).equalsIgnoreCase("exp") || sign.getLine(itemIndex).equalsIgnoreCase("xp")) { int amount = getIntegerPositive(sign.getLine(amountIndex)); sign.setLine(amountIndex, Integer.toString(amount)); sign.setLine(itemIndex, "exp"); return; } 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()); }
public final boolean onSignCreate(final SignChangeEvent event, final IEssentials ess) { final ISign sign = new EventSign(event); final User user = ess.getUser(event.getPlayer()); if (!(user.isAuthorized("essentials.signs." + signName.toLowerCase() + ".create") || user.isAuthorized("essentials.signs.create." + signName.toLowerCase()))) { // Return true, so other plugins can use the same sign title, just hope // they won't change it to §1[Signname] return true; } sign.setLine(0, Util.format("signFormatFail", this.signName)); try { final boolean ret = onSignCreate(sign, user, getUsername(user), ess); if (ret) { sign.setLine(0, getSuccessName()); } return ret; } catch (ChargeException ex) { ess.showError(user, ex, signName); } catch (SignException ex) { ess.showError(user, ex, signName); } // Return true, so the player sees the wrong sign. return true; }
public static boolean isValidSign(final ISign sign) { return sign.getLine(0).matches("§1\\[.*\\]"); }
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) { if (FormatUtil.shortCurrency(money, ess).length() * 2 > 15) { throw new SignException("Line can be too long!"); } sign.setLine(index, FormatUtil.shortCurrency(money, ess) + ":0"); return; } } if (split.length == 2 && amountNeeded) { final Double money = getMoney(split[0]); Double amount = getDoublePositive(split[1]); if (money != null && amount != null) { amount -= amount % money; if (amount < 0.01 || money < 0.01) { throw new SignException(_("moreThanZero")); } sign.setLine(index, FormatUtil.shortCurrency(money, ess) + ":" + FormatUtil.shortCurrency(amount, ess).substring(1)); return; } } if (split.length == 2 && !amountNeeded) { final int amount = getIntegerPositive(split[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")); } String newline = amount + " " + split[1] + ":0"; if ((newline + amount).length() > 15) { throw new SignException("Line can be too long!"); } sign.setLine(index, newline); return; } if (split.length == 3 && amountNeeded) { final int stackamount = getIntegerPositive(split[0]); int amount = getIntegerPositive(split[2]); amount -= amount % stackamount; 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")); } sign.setLine(index, stackamount + " " + split[1] + ":" + amount); return; } throw new SignException(_("invalidSignLine", index + 1)); }