Refactoring of the signs

Todo: Eco signs, Protection signs

New permission: essentials.signs.[signname].break
This commit is contained in:
snowleo 2011-06-08 03:18:33 +02:00
parent 775c8d34dd
commit bcf81d9f0a
16 changed files with 622 additions and 75 deletions

View File

@ -5,27 +5,27 @@ import org.bukkit.inventory.ItemStack;
public class Charge
{
private final String command;
private final Double costs;
private final ItemStack items;
private final IEssentials ess;
private final transient String command;
private final transient Double costs;
private final transient ItemStack items;
private final transient IEssentials ess;
public Charge(String command, IEssentials ess)
public Charge(final String command, final IEssentials ess)
{
this(command, null, null, ess);
}
public Charge(double money, IEssentials ess)
public Charge(final double money, final IEssentials ess)
{
this(null, money, null, ess);
}
public Charge(ItemStack items, IEssentials ess)
public Charge(final ItemStack items, final IEssentials ess)
{
this(null, null, items, ess);
}
private Charge(String command, Double money, ItemStack item, IEssentials ess)
private Charge(final String command, final Double money, final ItemStack item, final IEssentials ess)
{
this.command = command;
this.costs = money;
@ -33,46 +33,40 @@ public class Charge
this.ess = ess;
}
public void isAffordableFor(IUser user) throws Exception
public void isAffordableFor(final IUser user) throws ChargeException
{
double mon = user.getMoney();
if (costs != null)
final double mon = user.getMoney();
if (costs != null
&& mon < costs
&& !user.isAuthorized("essentials.eco.loan"))
{
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
if (items != null)
if (items != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, items))
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
{
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
}
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
}
if (command != null && !command.isEmpty())
if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command)
&& mon < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
&& !user.isAuthorized("essentials.eco.loan"))
{
if (user.isAuthorized("essentials.nocommandcost.all")
|| user.isAuthorized("essentials.nocommandcost." + command))
{
return;
}
double cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
}
public void charge(IUser user) throws Exception
public void charge(final IUser user) throws ChargeException
{
double mon = user.getMoney();
if (costs != null)
{
final double mon = user.getMoney();
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
user.takeMoney(costs);
}
@ -80,23 +74,20 @@ public class Charge
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
{
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
}
InventoryWorkaround.removeItem(user.getInventory(), true, items);
user.updateInventory();
}
if (command != null && !command.isEmpty())
if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command))
{
if (user.isAuthorized("essentials.nocommandcost.all")
|| user.isAuthorized("essentials.nocommandcost." + command))
{
return;
}
double cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
final double mon = user.getMoney();
final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
user.takeMoney(cost);
}

View File

@ -0,0 +1,15 @@
package com.earth2me.essentials;
public class ChargeException extends Exception
{
public ChargeException(final String message)
{
super(message);
}
public ChargeException(final String message, final Throwable throwable)
{
super(message, throwable);
}
}

View File

@ -474,21 +474,7 @@ public class Essentials extends JavaPlugin implements IEssentials
}
catch (Throwable ex)
{
sender.sendMessage(Util.format("errorWithMessage", ex.getMessage()));
LogRecord lr = new LogRecord(Level.WARNING, Util.format("errorCallingCommand", commandLabel));
lr.setThrown(ex);
if (getSettings().isDebug())
{
logger.log(lr);
}
else
{
if (enableErrorLogging)
{
errorHandler.publish(lr);
errorHandler.flush();
}
}
showError(sender, ex, commandLabel);
return true;
}
}
@ -499,6 +485,25 @@ public class Essentials extends JavaPlugin implements IEssentials
}
}
public void showError(final CommandSender sender, final Throwable exception, final String commandLabel)
{
sender.sendMessage(Util.format("errorWithMessage", exception.getMessage()));
final LogRecord logRecord = new LogRecord(Level.WARNING, Util.format("errorCallingCommand", commandLabel));
logRecord.setThrown(exception);
if (getSettings().isDebug())
{
logger.log(logRecord);
}
else
{
if (enableErrorLogging)
{
errorHandler.publish(logRecord);
errorHandler.flush();
}
}
}
public void loadBanList()
{
//I don't like this but it needs to be done until CB fixors

View File

@ -220,11 +220,7 @@ public class EssentialsBlockListener extends BlockListener
}
catch (Throwable ex)
{
user.sendMessage(Util.format("errorWithMessage", ex.getMessage()));
if (ess.getSettings().isDebug())
{
logger.log(Level.WARNING, ex.getMessage(), ex);
}
ess.showError(user, ex, "onSignChange");
}
}

View File

@ -73,4 +73,6 @@ public interface IEssentials
EssentialsDependancyChecker getDependancyChecker();
IPermissionsHandler getPermissionsHandler();
void showError(final CommandSender sender, final Throwable exception, final String commandLabel);
}

View File

@ -101,11 +101,7 @@ public class Teleport implements Runnable
}
catch (Throwable ex)
{
user.sendMessage(Util.format("errorWithMessage", ex.getMessage()));
if (ess.getSettings().isDebug())
{
logger.log(Level.WARNING, ex.getMessage(), ex);
}
ess.showError(user.getBase(), ex, "teleport");
}
return;
}

View File

@ -50,11 +50,7 @@ public class Commandtpall extends EssentialsCommand
}
catch (Exception ex)
{
sender.sendMessage(Util.format("errorWithMessage", ex.getMessage()));
if (ess.getSettings().isDebug())
{
logger.log(Level.WARNING, ex.getMessage(), ex);
}
ess.showError(sender, ex, getName());
}
}
}

View File

@ -0,0 +1,257 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge;
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"))
{
return false;
}
boolean ret;
try
{
ret = onSignCreate(sign, user, getUsername(user), ess);
}
catch (SignException ex)
{
ess.showError(user, ex, signName);
ret = false;
}
if (ret)
{
sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName));
}
return ret;
}
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")
&& 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")
&& onSignBreak(sign, user, getUsername(user), ess);
}
catch (SignException ex)
{
ess.showError(user, ex, signName);
return false;
}
}
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
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;
}
protected final void validateCharge(final ISign sign, final int index) throws SignException
{
final String line = sign.getLine(index);
if (line.isEmpty())
{
return;
}
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
if (isMoney)
{
final double quantity = Double.parseDouble(line.substring(1));
if (quantity <= 0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
sign.setLine(index, Util.formatCurrency(quantity));
}
else
{
final String[] split = line.split("[ :-]+", 2);
if (split.length != 2)
{
throw new SignException(Util.i18n("invalidCharge"));
}
final int quantity = Integer.parseInt(split[0]);
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);
}
}
protected final ItemStack getItemStack(final String itemName) throws SignException
{
try
{
return ItemDb.get(itemName);
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
}
protected final Charge getCharge(final ISign sign, final int index, final IEssentials ess) throws SignException
{
final String line = sign.getLine(index);
if (line.isEmpty())
{
return new Charge(signName.toLowerCase() + "sign", ess);
}
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
if (isMoney)
{
final double quantity = Double.parseDouble(line.substring(1));
if (quantity <= 0)
{
throw new SignException(Util.i18n("moreThanZero"));
}
return new Charge(quantity, ess);
}
else
{
final String[] split = line.split("[ :-]+", 2);
if (split.length != 2)
{
throw new SignException(Util.i18n("invalidCharge"));
}
final int quantity = Integer.parseInt(split[0]);
if (quantity <= 1)
{
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);
}
}
}
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);
}
}

View File

@ -0,0 +1,21 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
public class SignBalance extends EssentialsSign
{
public SignBalance()
{
super("Balance");
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
player.sendMessage(Util.format("balance", player.getMoney()));
return true;
}
}

View File

@ -0,0 +1,24 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import net.minecraft.server.InventoryPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer;
public class SignDisposal extends EssentialsSign
{
public SignDisposal()
{
super("Disposal");
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess)
{
final CraftInventoryPlayer inv = new CraftInventoryPlayer(new InventoryPlayer(player.getHandle()));
inv.clear();
player.showInventory(inv);
return true;
}
}

View File

@ -0,0 +1,15 @@
package com.earth2me.essentials.signs;
public class SignException extends Exception
{
public SignException(final String message)
{
super(message);
}
public SignException(final String message, final Throwable throwable)
{
super(message, throwable);
}
}

View File

@ -0,0 +1,37 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.ItemDb;
import com.earth2me.essentials.User;
import net.minecraft.server.InventoryPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer;
import org.bukkit.inventory.ItemStack;
public class SignFree extends EssentialsSign
{
public SignFree()
{
super("Free");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
getItemStack(sign.getLine(1));
return true;
}
@Override
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 CraftInventoryPlayer inv = new CraftInventoryPlayer(new InventoryPlayer(player.getHandle()));
inv.clear();
item.setAmount(9 * 4 * 64);
inv.addItem(item);
player.showInventory(inv);
return true;
}
}

View File

@ -0,0 +1,34 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
public class SignHeal extends EssentialsSign
{
public SignHeal()
{
super("Heal");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
validateCharge(sign, 1);
return true;
}
@Override
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);
charge.isAffordableFor(player);
player.setHealth(20);
player.sendMessage(Util.i18n("youAreHealed"));
charge.charge(player);
return true;
}
}

View File

@ -0,0 +1,32 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import java.util.List;
public class SignMail extends EssentialsSign
{
public SignMail()
{
super("Mail");
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
final List<String> mail = player.getMails();
if (mail.isEmpty())
{
player.sendMessage(Util.i18n("noNewMail"));
return false;
}
for (String s : mail)
{
player.sendMessage(s);
}
player.sendMessage(Util.i18n("markMailAsRead"));
return true;
}
}

View File

@ -0,0 +1,57 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
public class SignTime extends EssentialsSign
{
public SignTime()
{
super("Time");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
validateCharge(sign, 2);
final String timeString = sign.getLine(1);
if ("Day".equalsIgnoreCase(timeString))
{
sign.setLine(1, "§2Day");
return true;
}
if ("Night".equalsIgnoreCase(timeString))
{
sign.setLine(1, "§2Night");
return true;
}
throw new SignException(Util.i18n("onlyDayNight"));
}
@Override
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);
charge.isAffordableFor(player);
final String timeString = sign.getLine(1);
long time = player.getWorld().getTime();
time -= time % 24000;
if ("§2Day".equalsIgnoreCase(timeString))
{
player.getWorld().setTime(time + 24000);
charge.charge(player);
return true;
}
if ("§2Night".equalsIgnoreCase(timeString))
{
player.getWorld().setTime(time + 37700);
charge.charge(player);
return true;
}
throw new SignException(Util.i18n("onlyDayNight"));
}
}

View File

@ -0,0 +1,69 @@
package com.earth2me.essentials.signs;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
public class SignWarp extends EssentialsSign
{
public SignWarp()
{
super("Warp");
}
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
{
validateCharge(sign, 3);
final String warpName = sign.getLine(1);
if (warpName.isEmpty())
{
sign.setLine(1, "§dWarp name!");
return false;
}
else
{
try
{
ess.getWarps().getWarp(warpName);
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
final String group = sign.getLine(2);
if ("Everyone".equalsIgnoreCase(group))
{
sign.setLine(2, "§2Everyone");
}
return true;
}
}
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
final String warpName = sign.getLine(1);
final String group = sign.getLine(2);
if ((!group.isEmpty()
&& ("§2Everyone".equals(group)
|| player.inGroup(group)))
|| (!ess.getSettings().getPerWarpPermission() || player.isAuthorized("essentials.warp." + warpName)))
{
final Charge charge = getCharge(sign, 3, ess);
try
{
player.getTeleport().warp(warpName, charge);
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
return true;
}
return false;
}
}