Refactoring: New Charge class

user.canAfford(String), user.canAfford(EssentialsCommand), user.charge(String), user.charge(EssentialsCommand) have been removed.
Teleport class has been changed to use the Charge class.
This also fixes some bugs, like the one with warp signs.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1512 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-05-22 18:53:23 +00:00
parent 011fc09b4f
commit 5ae48481bd
28 changed files with 208 additions and 117 deletions

View File

@ -0,0 +1,104 @@
package com.earth2me.essentials;
import com.earth2me.essentials.commands.EssentialsCommand;
import org.bukkit.inventory.ItemStack;
public class Charge
{
private String command = null;
private Double costs = null;
private ItemStack items = null;
private Essentials ess = Essentials.getStatic();
public Charge(String command)
{
this.command = command;
}
public Charge(double money)
{
this.costs = money;
}
public Charge(ItemStack items)
{
this.items = items;
}
public Charge(EssentialsCommand command)
{
this.command = command.getName();
}
public void isAffordableFor(User user) throws Exception
{
double mon = user.getMoney();
if (costs != null)
{
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
}
if (items != null)
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
{
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
}
}
if (command != null && !command.isEmpty())
{
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"));
}
}
}
public void charge(User user) throws Exception
{
double mon = user.getMoney();
if (costs != null)
{
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
user.takeMoney(costs);
user.sendMessage(Util.format("moneyTaken", Util.formatCurrency(costs)));
}
if (items != null)
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
{
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
}
InventoryWorkaround.removeItem(user.getInventory(), true, items);
user.updateInventory();
}
if (command != null && !command.isEmpty())
{
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"));
}
user.takeMoney(cost);
user.sendMessage(Util.format("moneyTaken", Util.formatCurrency(cost)));
}
}
}

View File

@ -2,7 +2,6 @@ package com.earth2me.essentials;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.minecraft.server.InventoryPlayer;
@ -163,7 +162,7 @@ public class EssentialsPlayerListener extends PlayerListener
event.setTo(loc);
try
{
user.getTeleport().teleport(loc, "portal");
user.getTeleport().teleport(loc, new Charge("portal"));
}
catch (Exception ex)
{
@ -456,22 +455,22 @@ public class EssentialsPlayerListener extends PlayerListener
{
if (sign.getLine(2).equals("§2Everyone"))
{
chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), "warpsign");
Charge charge = chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), charge);
return;
}
if (user.inGroup(sign.getLine(2)))
{
chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), "warpsign");
Charge charge = chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), charge);
return;
}
}
if (user.isAuthorized("essentials.signs.warp.use")
&& (!ess.getSettings().getPerWarpPermission() || user.isAuthorized("essentials.warp." + sign.getLine(1))))
{
chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), "warpsign");
Charge charge = chargeUserForWarp(sign, user);
user.getTeleport().warp(sign.getLine(1), charge);
}
return;
}
@ -482,7 +481,7 @@ public class EssentialsPlayerListener extends PlayerListener
}
}
private void chargeUserForWarp(Sign sign, User user) throws Exception
private Charge chargeUserForWarp(Sign sign, User user) throws Exception
{
if (!sign.getLine(3).isEmpty())
{
@ -495,24 +494,15 @@ public class EssentialsPlayerListener extends PlayerListener
}
if (m1)
{
if (user.getMoney() < q1)
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
user.takeMoney(q1);
user.sendMessage(Util.format("moneyTaken", Util.formatCurrency(q1)));
return new Charge(q1);
}
else
{
ItemStack i = ItemDb.get(l1[1], (int)q1);
if (!InventoryWorkaround.containsItem(user.getInventory(), true, i))
{
throw new Exception(Util.format("missingItems", (int)q1, l1[1]));
}
InventoryWorkaround.removeItem(user.getInventory(), true, i);
user.updateInventory();
return new Charge(i);
}
}
return new Charge("warpsign");
}
@Override

View File

@ -44,10 +44,10 @@ public class Teleport implements Runnable
private long initY;
private long initZ;
private Target teleportTarget;
private String chargeFor;
private Charge chargeFor;
private Essentials ess;
private void initTimer(long delay, Target target, String chargeFor)
private void initTimer(long delay, Target target, Charge chargeFor)
{
this.started = System.currentTimeMillis();
this.delay = delay;
@ -91,7 +91,7 @@ public class Teleport implements Runnable
now(teleportTarget);
if (chargeFor != null)
{
user.charge(chargeFor);
chargeFor.charge(user);
}
}
catch (Throwable ex)
@ -113,12 +113,12 @@ public class Teleport implements Runnable
this.ess = ess;
}
public void respawn(Spawn spawn, String chargeFor) throws Exception
public void respawn(Spawn spawn, Charge chargeFor) throws Exception
{
teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor);
}
public void warp(String warp, String chargeFor) throws Exception
public void warp(String warp, Charge chargeFor) throws Exception
{
Location loc = Essentials.getWarps().getWarp(warp);
teleport(new Target(loc), chargeFor);
@ -172,27 +172,28 @@ public class Teleport implements Runnable
cancel(false);
}
public void teleport(Location loc, String name) throws Exception
public void teleport(Location loc, Charge chargeFor) throws Exception
{
teleport(new Target(loc), name);
teleport(new Target(loc), chargeFor);
}
public void teleport(Entity entity, String name) throws Exception
public void teleport(Entity entity, Charge chargeFor) throws Exception
{
teleport(new Target(entity), name);
teleport(new Target(entity), chargeFor);
}
private void teleport(Target target, String chargeFor) throws Exception
private void teleport(Target target, Charge chargeFor) throws Exception
{
double delay = ess.getSettings().getTeleportDelay();
chargeFor.isAffordableFor(user);
cooldown(true);
if (delay <= 0 || user.isAuthorized("essentials.teleport.timer.bypass"))
{
now(target);
if (chargeFor != null)
{
user.charge(chargeFor);
chargeFor.charge(user);
}
return;
}
@ -224,7 +225,7 @@ public class Teleport implements Runnable
now(new Target(entity));
}
public void back(final String chargeFor) throws Exception
public void back(final Charge chargeFor) throws Exception
{
teleport(new Target(user.getLastLocation()), chargeFor);
}
@ -234,12 +235,12 @@ public class Teleport implements Runnable
back(null);
}
public void home(String chargeFor) throws Exception
public void home(Charge chargeFor) throws Exception
{
home(user, chargeFor);
}
public void home(User user, String chargeFor) throws Exception
public void home(User user, Charge chargeFor) throws Exception
{
Location loc = user.getHome(this.user.getLocation());
if (loc == null)

View File

@ -117,32 +117,6 @@ public class User extends UserData implements Comparable<User>, IReplyTo
sendMessage(Util.format("takenFromAccount", Util.formatCurrency(value)));
}
public void charge(String cmd) throws Exception
{
if (isAuthorized("essentials.nocommandcost.all")
|| isAuthorized("essentials.nocommandcost." + cmd))
{
return;
}
double mon = getMoney();
double cost = ess.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd);
if (mon < cost && !isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
takeMoney(cost);
}
public void canAfford(String cmd) throws Exception
{
double mon = getMoney();
double cost = ess.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd);
if (mon < cost && !isAuthorized("essentials.eco.loan"))
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
}
public boolean canAfford(double cost)
{
double mon = getMoney();
@ -156,21 +130,11 @@ public class User extends UserData implements Comparable<User>, IReplyTo
}
}
public void canAfford(IEssentialsCommand cmd) throws Exception
{
canAfford(cmd.getName());
}
public void dispose()
{
this.base = new OfflinePlayer(getName());
}
public void charge(IEssentialsCommand cmd) throws Exception
{
charge(cmd.getName());
}
public boolean getJustPortaled()
{
return justPortaled;

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.Server;
@ -15,8 +16,9 @@ public class Commandback extends EssentialsCommand
@Override
protected void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
user.canAfford(this);
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.sendMessage(Util.i18n("backUsageMsg"));
user.getTeleport().back(this.getName());
user.getTeleport().back(charge);
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
@ -15,7 +16,8 @@ public class Commandhome extends EssentialsCommand
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
user.canAfford(this);
Charge charge = new Charge(this);
charge.isAffordableFor(user);
if(args.length > 0 && user.isAuthorized("essentials.home.others"))
{
User u;
@ -31,9 +33,9 @@ public class Commandhome extends EssentialsCommand
{
throw new Exception(Util.i18n("playerNotFound"));
}
user.getTeleport().home(u, this.getName());
user.getTeleport().home(u, charge);
return;
}
user.getTeleport().home(this.getName());
user.getTeleport().home(charge);
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Location;
import org.bukkit.Server;
import com.earth2me.essentials.TargetBlock;
@ -35,7 +36,8 @@ public class Commandjump extends EssentialsCommand
throw new Exception(Util.i18n("jumpError"), ex);
}
user.canAfford(this);
user.getTeleport().teleport(loc, this.getName());
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.getTeleport().teleport(loc, charge);
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
@ -109,9 +110,10 @@ public class Commandkit extends EssentialsCommand
items = (List<String>)kit;
}
Charge charge = new Charge("kit-" + kitName);
try
{
user.canAfford("kit-" + kitName);
charge.isAffordableFor(user);
}
catch (Exception ex)
{
@ -139,8 +141,8 @@ public class Commandkit extends EssentialsCommand
}
try
{
user.charge(this);
user.charge("kit-" + kitName);
charge(user);
charge.charge(user);
}
catch (Exception ex)
{

View File

@ -28,7 +28,7 @@ public class Commandlightning extends EssentialsCommand
if (args.length < 1 & user != null)
{
user.getWorld().strikeLightning(user.getTargetBlock(null, 600).getLocation());
user.charge(this);
charge(user);
return;
}
@ -38,6 +38,10 @@ public class Commandlightning extends EssentialsCommand
return;
}
if (user != null)
{
charge(user);
}
for (Player p : server.matchPlayer(args[0]))
{
sender.sendMessage(Util.format("lightningUse", p.getDisplayName()));
@ -48,7 +52,5 @@ public class Commandlightning extends EssentialsCommand
p.sendMessage(Util.i18n("lightningSmited"));
}
}
if (user != null)
user.charge(this);
}
}

View File

@ -2,7 +2,6 @@ package com.earth2me.essentials.commands;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import com.earth2me.essentials.Essentials;
import org.bukkit.entity.Player;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
@ -67,7 +66,7 @@ public class Commandnick extends EssentialsCommand
}
}
user.charge(this);
charge(user);
user.setDisplayName(ess.getConfiguration().getString("nickname-prefix", "~") + nick);
user.setNickname(nick);
user.sendMessage(Util.format("nickSet", user.getDisplayName() + "§7."));

View File

@ -21,7 +21,7 @@ public class Commandrealname extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
String whois = args[0].toLowerCase();
user.charge(this);
charge(user);
for (Player p : server.getOnlinePlayers())
{
User u = ess.getUser(p);

View File

@ -22,7 +22,7 @@ public class Commandthunder extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
user.charge(this);
charge(user);
World world = user.getWorld();
boolean setThunder = args[0].equalsIgnoreCase("true");
if (args.length > 1)

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Location;
import org.bukkit.Server;
import com.earth2me.essentials.User;
@ -20,7 +21,7 @@ public class Commandtop extends EssentialsCommand
int topZ = user.getLocation().getBlockZ();
int topY = user.getWorld().getHighestBlockYAt(topX, topZ);
charge(user);
user.getTeleport().teleport(new Location(user.getWorld(), user.getLocation().getX(), topY + 1, user.getLocation().getZ()), this.getName());
user.getTeleport().teleport(new Location(user.getWorld(), user.getLocation().getX(), topY + 1, user.getLocation().getZ()), new Charge(this));
user.sendMessage(Util.i18n("teleportTop"));
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import com.earth2me.essentials.Console;
import org.bukkit.Server;
import com.earth2me.essentials.User;
@ -29,8 +30,9 @@ public class Commandtp extends EssentialsCommand
throw new Exception(Util.format("teleportDisabled", p.getDisplayName()));
}
user.sendMessage(Util.i18n("teleporting"));
user.canAfford(this);
user.getTeleport().teleport(p, this.getName());
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.getTeleport().teleport(p, charge);
break;
case 2:

View File

@ -13,7 +13,7 @@ public class Commandtpa extends EssentialsCommand
}
@Override
public void run(Server server, User player, String commandLabel, String[] args) throws Exception
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
if (args.length < 1)
{
@ -25,14 +25,14 @@ public class Commandtpa extends EssentialsCommand
{
throw new Exception(Util.format("teleportDisabled", p.getDisplayName()));
}
player.charge(this);
if (!p.isIgnoredPlayer(player.getName()))
charge(user);
if (!p.isIgnoredPlayer(user.getName()))
{
p.requestTeleport(player, false);
p.sendMessage(Util.format("teleportRequest", player.getDisplayName()));
p.requestTeleport(user, false);
p.sendMessage(Util.format("teleportRequest", user.getDisplayName()));
p.sendMessage(Util.i18n("typeTpaccept"));
p.sendMessage(Util.i18n("typeTpdeny"));
}
player.sendMessage(Util.format("requestSent", p.getDisplayName()));
user.sendMessage(Util.format("requestSent", p.getDisplayName()));
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
@ -22,17 +23,25 @@ public class Commandtpaccept extends EssentialsCommand
throw new Exception(Util.i18n("noPendingRequest"));
}
user.canAfford(this);
Charge charge = new Charge(this);
if (user.isTeleportRequestHere())
{
charge.isAffordableFor(user);
}
else
{
charge.isAffordableFor(p);
}
user.sendMessage(Util.i18n("requestAccepted"));
p.sendMessage(Util.i18n("requestAccepted"));
if (user.isTeleportRequestHere())
{
user.getTeleport().teleport(p, this.getName());
user.getTeleport().teleport(p, charge);
}
else
{
p.getTeleport().teleport(user, this.getName());
p.getTeleport().teleport(user, charge);
}
user.requestTeleport(null, false);
}

View File

@ -25,7 +25,7 @@ public class Commandtpahere extends EssentialsCommand
{
throw new Exception(Util.format("teleportDisabled", p.getDisplayName()));
}
user.charge(this);
charge(user);
p.requestTeleport(user, true);
p.sendMessage(Util.format("teleportHereRequest", user.getDisplayName()));
p.sendMessage(Util.i18n("typeTpaccept"));

View File

@ -21,7 +21,7 @@ public class Commandtpdeny extends EssentialsCommand
throw new Exception(Util.i18n("noPendingRequest"));
}
user.charge(this);
charge(user);
user.sendMessage(Util.i18n("requestDenied"));
p.sendMessage(Util.i18n("requestDenied"));
user.requestTeleport(null, false);

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
@ -20,7 +21,7 @@ public class Commandtphere extends EssentialsCommand
{
throw new Exception(Util.format("teleportDisabled", p.getDisplayName()));
}
p.getTeleport().teleport(user, commandLabel);
p.getTeleport().teleport(user, new Charge(this));
user.sendMessage(Util.i18n("teleporting"));
p.sendMessage(Util.i18n("teleporting"));
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Location;
import org.bukkit.Server;
import com.earth2me.essentials.User;
@ -25,8 +26,9 @@ public class Commandtppos extends EssentialsCommand
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
Location l = new Location(user.getWorld(), x, y, z);
user.canAfford(this);
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.sendMessage(Util.i18n("teleporting"));
user.getTeleport().teleport(l, this.getName());
user.getTeleport().teleport(l, charge);
}
}

View File

@ -66,7 +66,7 @@ public class Commandtree extends EssentialsCommand
boolean success = user.getWorld().generateTree(safeLocation, (TreeType)tree);
if (success)
{
user.charge(this);
charge(user);
user.sendMessage(Util.i18n("treeSpawned"));
}
else

View File

@ -1,11 +1,11 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import org.bukkit.Server;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import com.earth2me.essentials.Warps;
import org.bukkit.command.CommandSender;
public class Commandwarp extends EssentialsCommand
@ -74,16 +74,18 @@ public class Commandwarp extends EssentialsCommand
private void warpUser(User user, String name) throws Exception
{
Charge charge = new Charge(this);
charge.isAffordableFor(user);
if (ess.getSettings().getPerWarpPermission())
{
if (user.isAuthorized("essentials.warp." + name))
{
user.getTeleport().warp(name, this.getName());
user.getTeleport().warp(name, charge);
return;
}
user.sendMessage(Util.i18n("warpUsePermission"));
return;
}
user.getTeleport().warp(name, this.getName());
user.getTeleport().warp(name, charge);
}
}

View File

@ -23,7 +23,7 @@ public class Commandweather extends EssentialsCommand
boolean isStorm = args[0].equalsIgnoreCase("storm");
World world = user.getWorld();
user.charge(this);
charge(user);
if (args.length > 1)
{

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Server;
@ -68,7 +69,8 @@ public class Commandworld extends EssentialsCommand
Location loc = user.getLocation();
loc = new Location(world, loc.getBlockX() * factor + .5, loc.getBlockY(), loc.getBlockZ() * factor + .5);
user.canAfford(this);
user.getTeleport().teleport(loc, this.getName());
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.getTeleport().teleport(loc, charge);
}
}

View File

@ -45,7 +45,7 @@ public class Commandworth extends EssentialsCommand
throw new Exception(Util.i18n("itemCannotBeSold"));
}
user.charge(this);
charge(user);
user.sendMessage(is.getDurability() != 0
? Util.format("worthMeta",
is.getType().toString().toLowerCase().replace("_", ""),

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.Charge;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.Command;
@ -83,7 +84,8 @@ public abstract class EssentialsCommand implements IEssentialsCommand
{
if (sender instanceof Player)
{
ess.getUser((Player)sender).charge(this);
Charge charge = new Charge(this);
charge.charge(ess.getUser((Player)sender));
}
}
}

View File

@ -17,7 +17,7 @@ public class Commandsetspawn extends EssentialsCommand
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
user.charge(this);
charge(user);
String group = args.length > 0 ? getFinalArg(args, 0) : "default";
Essentials.getSpawn().setSpawn(user.getLocation(), group);
user.sendMessage(Util.format("spawnSet", group));

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.spawn;
import com.earth2me.essentials.Charge;
import org.bukkit.Server;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
@ -16,7 +17,8 @@ public class Commandspawn extends EssentialsCommand
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
user.canAfford(this);
user.getTeleport().respawn(Essentials.getSpawn(), this.getName());
Charge charge = new Charge(this);
charge.isAffordableFor(user);
user.getTeleport().respawn(Essentials.getSpawn(), charge);
}
}