diff options
Diffstat (limited to 'Essentials/src/com/earth2me/essentials/User.java')
-rw-r--r-- | Essentials/src/com/earth2me/essentials/User.java | 746 |
1 files changed, 85 insertions, 661 deletions
diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 98209b0b9..3077a88e7 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -1,97 +1,31 @@ package com.earth2me.essentials; -import java.util.*; -import java.util.logging.*; -import java.io.*; -import org.bukkit.*; import com.earth2me.essentials.commands.IEssentialsCommand; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.logging.Logger; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.*; -import org.bukkit.inventory.ItemStack; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.SafeConstructor; -import org.yaml.snakeyaml.reader.UnicodeReader; +import org.bukkit.entity.Player; -public class User extends PlayerExtension implements Comparable<User>, IReplyTo +public class User extends UserData implements Comparable<User>, IReplyTo { private static final Logger logger = Logger.getLogger("Minecraft"); - private final Yaml yaml = new Yaml(new SafeConstructor()); - private boolean isLoaded = false; - private final File folder; - private Map<String, Object> data = new HashMap<String, Object>(); - private static Map<String, User> users = new HashMap<String, User>(); - private boolean teleEnabled = true; - private long lastTeleport = 0; - private long lastHeal = 0; private boolean justPortaled = false; - //private TimerTask teleTimer = null; - private int teleTimer = -1; - public Location lastLocation = null; private CommandSender replyTo = null; - private boolean isNew = false; - public String currentJail; - public ItemStack[] savedInventory; - private Map<String,Object> metadata = new HashMap<String,Object>(); + private User teleportRequester; + private boolean teleportRequestHere; + private Teleport teleport; + private long lastActivity; - private User(Player base) + User(Player base, Essentials ess) { - super(base); - this.folder = new File((Essentials.getStatic() == null ? new File(".") : Essentials.getStatic().getDataFolder()), "userdata"); - this.lastLocation = getBase().getLocation(); - load(); + super(base, ess); + teleport = new Teleport(this, ess); } - public static int size() - { - return users.size(); - } - - public static <T> User get(T base) - { - if (base instanceof Player) - return get((Player)base); - return null; - } - - public static <T extends Player> User get(T base) - { - if (base == null) - return null; - - if (base instanceof User) - return (User)base; - - if (users.containsKey(base.getName())) - return users.get(base.getName()).update(base); - - User u = new User(base); - users.put(u.getName(), u); - return u; - } - - public static <T> void charge(T base, IEssentialsCommand cmd) throws Exception - { - if (base instanceof Player) - User.get(base).charge(cmd); - } - - public boolean isNew() - { - return isNew; - } - - public void respawn(Spawn spawn) throws Exception - { - respawn(spawn, null); - } - - public void respawn(Spawn spawn, final String chargeFor) throws Exception - { - teleportTo(getSafeDestination(spawn.getSpawn(getGroup())), chargeFor); - } - - private User update(Player base) + User update(Player base) { setBase(base); return this; @@ -105,10 +39,14 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo public boolean isAuthorized(String node) { if (isOp()) + { return true; + } if (isJailed()) + { return false; + } try { @@ -117,204 +55,44 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo catch (Throwable ex) { String[] cmds = node.split("\\.", 2); - return !Essentials.getSettings().isCommandRestricted(cmds[cmds.length - 1]); + return !ess.getSettings().isCommandRestricted(cmds[cmds.length - 1]); } } - public boolean isTeleEnabled() - { - return teleEnabled; - } - - public boolean toggleTeleEnabled() - { - return teleEnabled = !teleEnabled; - } - - public void teleportCooldown(boolean justCheck) throws Exception - { - long now = Calendar.getInstance().getTimeInMillis(); - if (lastTeleport > 0) - { - long cooldown = Essentials.getSettings().getTeleportCooldown(); - long left = lastTeleport + cooldown - now; - if (left > 0 && !isOp() && !isAuthorized("essentials.teleport.cooldown.bypass")) - { - throw new Exception("Time before next teleport: " + Essentials.FormatTime(left)); - } - } - // if justCheck is set, don't update lastTeleport; we're just checking - if (!justCheck) lastTeleport = now; - } - - public void teleportCooldown() throws Exception - { - teleportCooldown(true); - } - public void healCooldown() throws Exception { - long now = Calendar.getInstance().getTimeInMillis(); - if (lastHeal > 0) - { - long cooldown = Essentials.getSettings().getHealCooldown(); - long left = lastHeal + cooldown - now; - if (left > 0 && !isOp() && !isAuthorized("essentials.heal.cooldown.bypass")) - { - throw new Exception("Time before next heal: " + Essentials.FormatTime(left)); - } - } - lastHeal = now; - } - - private void load() - { - if (isLoaded) return; - isLoaded = true; - - data = Essentials.getData(this); - - try - { - if (!folder.exists()) folder.mkdirs(); - File file = new File(folder, getName() + ".yml"); - if (!file.exists()) - { - isNew = true; - file.createNewFile(); - logger.info(getName() + " has logged in for the first time."); - } - - FileInputStream rx = new FileInputStream(file); - Map<String, Object> userData = (Map<String, Object>)yaml.load(new UnicodeReader(rx)); - if (userData != null) data.putAll(userData); - rx.close(); - } - catch (Throwable ex) - { - logger.log(Level.SEVERE, null, ex); - } - finally - { - if (data == null) data = new HashMap<String, Object>(); - } - } - - private void flush() - { - try - { - if (!folder.exists()) folder.mkdirs(); - File file = new File(folder, getName() + ".yml"); - if (!file.exists()) file.createNewFile(); - - FileWriter tx = new FileWriter(file); - tx.write(yaml.dump(data)); - tx.flush(); - tx.close(); - } - catch (Throwable ex) - { - logger.log(Level.SEVERE, null, ex); - } - } - - public boolean isGodModeEnabled() - { - load(); - return data.containsKey("godmode") && (Boolean)data.get("godmode"); - } - - public boolean toggleGodMode() - { - boolean retval = !isGodModeEnabled(); - data.put("godmode", retval); - flush(); - return retval; - } - - public boolean isMuted() - { - load(); - return data.containsKey("muted") && (Boolean)data.get("muted"); - } - - public boolean toggleMuted() - { - boolean retval = !isMuted(); - data.put("muted", retval); - flush(); - return retval; - } - - public boolean isJailed() - { - //load(); Do not load config everytime time! - return data.containsKey("jailed") && (Boolean)data.get("jailed"); - } - - public boolean toggleJailed() - { - boolean retval = !isJailed(); - data.put("jailed", retval); - flush(); - load(); - return retval; - } - - public double getMoney() - { - load(); - if (data.containsKey("money")) + Calendar now = new GregorianCalendar(); + if (getLastHealTimestamp() > 0) { - if (data.get("money") instanceof Number) + double cooldown = ess.getSettings().getHealCooldown(); + Calendar cooldownTime = new GregorianCalendar(); + cooldownTime.setTimeInMillis(getLastHealTimestamp()); + cooldownTime.add(Calendar.SECOND, (int)cooldown); + cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0)); + if (cooldownTime.after(now) && !isAuthorized("essentials.heal.cooldown.bypass")) { - return ((Number)data.get("money")).doubleValue(); - } - logger.log(Level.SEVERE, "Can't convert money value to double:" + data.get("money")); - } - - try - { - return com.nijiko.coelho.iConomy.iConomy.getBank().getAccount(getName()).getBalance(); - } - catch (Throwable ex) - { - try - { - Map<String, Object> idata = Essentials.getData(this); - return ((Number)idata.get("money")).doubleValue(); - } - catch (Throwable ex2) - { - return Essentials.getSettings().getStartingBalance(); + throw new Exception("Time before next heal: " + Util.formatDateDiff(cooldownTime.getTimeInMillis())); } } + setLastHealTimestamp(now.getTimeInMillis()); } - public void setMoney(double value) + public void giveMoney(double value) { - try + if (value == 0) { - com.nijiko.coelho.iConomy.iConomy.getBank().getAccount(getName()).setBalance(value); - } - catch (Throwable ex) - { - data.put("money", value); - flush(); + return; } - } - - public void giveMoney(double value) - { - if (value == 0) return; setMoney(getMoney() + value); sendMessage("§a$" + value + " has been added to your account."); } - public void payUser(User reciever, int value) throws Exception + public void payUser(User reciever, double value) throws Exception { - if (value == 0) return; + if (value == 0) + { + return; + } if (!canAfford(value)) { throw new Exception("You do not have sufficient funds."); @@ -330,7 +108,10 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo public void takeMoney(double value) { - if (value == 0) return; + if (value == 0) + { + return; + } setMoney(getMoney() - value); sendMessage("§c$" + value + " has been taken from your account."); } @@ -343,24 +124,28 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo return; } double mon = getMoney(); - double cost = Essentials.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd); - if (mon < cost && !isOp()) + double cost = ess.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd); + if (mon < cost && !isAuthorized("essentials.eco.loan")) + { throw new Exception("You do not have sufficient funds."); + } takeMoney(cost); } public void canAfford(String cmd) throws Exception { double mon = getMoney(); - double cost = Essentials.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd); - if (mon < cost && !isOp()) + double cost = ess.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd); + if (mon < cost && !isAuthorized("essentials.eco.loan")) + { throw new Exception("You do not have sufficient funds."); + } } public boolean canAfford(double cost) { double mon = getMoney(); - if (mon < cost && !isOp()) + if (mon < cost && !isAuthorized("essentials.eco.loan")) { return false; } @@ -375,312 +160,6 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo canAfford(cmd.getName()); } - public void cancelTeleport(boolean notifyUser) - { - if (teleTimer == -1) return; - try - { - getServer().getScheduler().cancelTask(teleTimer); - if (notifyUser) sendMessage("§cPending teleportation request cancelled."); - } - catch (Throwable ex) - { - } - finally - { - teleTimer = -1; - } - } - - public void cancelTeleport() - { - cancelTeleport(false); - } - - public void teleportTo(final Location loc, final String chargeFor) - { - - final long delay = Essentials.getSettings().getTeleportDelay(); - - if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass")) - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportCooldown(false); - teleportToNow(loc); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - return; - } - - cancelTeleport(); - sendMessage("§7Teleportation will commence in " + Essentials.FormatTime(delay) + ". Don't move."); - teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay) - { - public void DoTeleport() - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportToNow(loc); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - } - - public void DoCancel() - { - cancelTeleport(); - } - }, 10, 10); - } - - @Override - public void teleportTo(final Location loc) - { - teleportTo(loc, null); - } - - public void teleportTo(final Entity entity, final String chargeFor) - { - - final long delay = Essentials.getSettings().getTeleportDelay(); - - if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass")) - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportCooldown(false); - teleportToNow(entity); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - return; - } - - cancelTeleport(); - sendMessage("§7Teleportation will commence in " + Essentials.FormatTime(delay) + ". Don't move."); - teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay) - { - public void DoTeleport() - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportToNow(entity); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - } - - public void DoCancel() - { - cancelTeleport(); - } - }, 10, 10); - } - - @Override - public void teleportTo(final Entity entity) - { - teleportTo(entity, null); - } - - public Location getHome() throws Exception - { - return getHome(null); - } - - public Location getHome(String playerName) throws Exception - { - Map<String, Object> userData = new HashMap<String, Object>(); - if (playerName == null) - { - userData = data; - } - else - { - userData = Essentials.getData(playerName); - } - - if (userData.containsKey("home")) - { - List<Object> vals = (List<Object>)userData.get("home"); - World world = getServer() == null ? null : getServer().getWorlds().get(0); - if (vals.size() > 5 && getServer() != null) - { - world = getServer().getWorld((String)vals.get(5)); - } - if (world == null) - { - throw new Exception(); - } - return new Location( - world, - ((Number)vals.get(0)).doubleValue(), - ((Number)vals.get(1)).doubleValue(), - ((Number)vals.get(2)).doubleValue(), - ((Number)vals.get(3)).floatValue(), - ((Number)vals.get(4)).floatValue()); - } - - try - { - Map<String, Object> gdata = null; - if (playerName != null) - { - gdata = Essentials.getData(playerName); - } - else - { - gdata = Essentials.getData(this); - } - - List<Object> vals = (List<Object>)gdata.get("home"); - World world = getServer().getWorlds().get(0); - if (vals.size() > 5) - { - world = getServer().getWorld((String)vals.get(5)); - } - if (world == null) - { - throw new Exception(); - } - return new Location(world, - ((Number)vals.get(0)).doubleValue(), - ((Number)vals.get(1)).doubleValue(), - ((Number)vals.get(2)).doubleValue(), - ((Number)vals.get(3)).floatValue(), - ((Number)vals.get(4)).floatValue()); - } - catch (Throwable ex) - { - throw new Exception("You have not set a home."); - } - } - - public void teleportToHome(final String chargeFor) - { - teleportToHome(chargeFor, null); - } - - public void teleportToHome(final String chargeFor, String otherPlayer) - { - final long delay = Essentials.getSettings().getTeleportDelay(); - - Location loc = null; - try - {// check this first in case user hasn't set a home yet - if (otherPlayer == null) - { - loc = getHome(); - } - else - { - loc = getHome(otherPlayer); - } - } - catch (Throwable ex) - { - if (Essentials.getSettings().spawnIfNoHome()) - { - try - { - respawn(Essentials.getStatic().spawn, null); - return; - } - catch (Throwable rex) - { - sendMessage("§cTeleport: " + rex.getMessage()); - return; - } - } - - sendMessage("§cTeleport: " + ex.getMessage()); - return; - } - if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass")) - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportCooldown(false); - teleportToNow(loc); - sendMessage("§7Teleporting home..."); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - return; - } - - cancelTeleport(); - - sendMessage("§7Teleportation will commence in " - + Essentials.FormatTime(delay) + ". Don't move."); - teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay) - { - public void DoTeleport() - { - try - { - if (chargeFor != null) charge(chargeFor); - teleportToNow(getHome()); - } - catch (Throwable ex) - { - sendMessage("§cError: " + ex.getMessage()); - } - } - - public void DoCancel() - { - cancelTeleport(); - } - }, 10, 10); - } - - public void teleportToHome() - { - teleportToHome(null); - } - - public void teleportToNow(Location loc) throws Exception - { - cancelTeleport(); - lastLocation = getLocation(); - getBase().teleport(getSafeDestination(loc)); - } - - public void teleportToNow(Entity entity) - { - cancelTeleport(); - lastLocation = getLocation(); - getBase().teleport(entity); - } - - public void teleportBack(final String chargeFor) - { - teleportTo(lastLocation, chargeFor); - } - - public void teleportBack() - { - teleportBack(null); - } - public void dispose() { this.base = new OfflinePlayer(getName()); @@ -694,9 +173,6 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo public boolean getJustPortaled() { return justPortaled; - - - } public void setJustPortaled(boolean value) @@ -714,129 +190,77 @@ public class User extends PlayerExtension implements Comparable<User>, IReplyTo return replyTo; } - public void setHome() + public int compareTo(User t) { - setHome(getLocation()); + return ChatColor.stripColor(this.getDisplayName()).compareToIgnoreCase(ChatColor.stripColor(t.getDisplayName())); } - public void setHome(Location home) + public Boolean canSpawnItem(int itemId) { - List<Object> vals = new ArrayList<Object>(6); - vals.add(new Double(home.getX())); - vals.add(new Double(home.getY())); - vals.add(new Double(home.getZ())); - vals.add(new Double(home.getYaw())); - vals.add(new Double(home.getPitch())); - vals.add(home.getWorld() == null ? "world" : home.getWorld().getName()); - data.put("home", vals); - flush(); - - setCompassTarget(home); + return !ess.getSettings().itemSpawnBlacklist().contains(itemId); } - public String getNick() + public void setHome() { - Essentials ess = Essentials.getStatic(); - String name = Essentials.getSettings().isCommandDisabled("nick") ? getName() : ess.readNickname(this); - if (isOp() && ess.getConfiguration().getString("ops-name-color", "c").matches("^[0-9a-f]$")) - name = "§" + ess.getConfiguration().getString("ops-name-color", "c") + name + "§f"; - return name; + setHome(getLocation(), true); } - public void warpTo(String warp, final String chargeFor) throws Exception + public void setHome(boolean defaultHome) { - lastLocation = getLocation(); - Location loc = Essentials.getWarps().getWarp(warp); - teleportTo(loc, chargeFor); - sendMessage("§7Warping to " + warp + "."); + setHome(getLocation(), defaultHome); } - public void warpTo(String string) throws Exception + public void setLastLocation() { - warpTo(string, null); + setLastLocation(getLocation()); } - public void clearNewFlag() + public void requestTeleport(User player, boolean here) { - isNew = false; + teleportRequester = player; + teleportRequestHere = here; } - public int compareTo(User t) + public User getTeleportRequest() { - return ChatColor.stripColor(this.getDisplayName()).compareToIgnoreCase(ChatColor.stripColor(t.getDisplayName())); + return teleportRequester; } - public Boolean canSpawnItem(int itemId) + public boolean isTeleportRequestHere() { - return !Essentials.getSettings().itemSpawnBlacklist().contains(itemId); + return teleportRequestHere; } - @SuppressWarnings("unchecked") - public List<Integer> getUnlimited() + public String getNick() { - if (!data.containsKey("unlimited")) + String nickname = getNickname(); + if (ess.getSettings().isCommandDisabled("nick") || nickname == null || nickname.isEmpty() || nickname.equals(getName())) { - return new ArrayList<Integer>(); + nickname = getName(); } - return (List<Integer>)data.get("unlimited"); - } - - public boolean hasUnlimited(ItemStack stack) - { - return getUnlimited().contains(stack.getTypeId()); - } - - @SuppressWarnings("unchecked") - public void setUnlimited(ItemStack stack, boolean state) - { - List<Integer> items = getUnlimited(); - if (items.contains(stack.getTypeId())) + else { - items.remove(Integer.valueOf(stack.getTypeId())); + nickname = ess.getSettings().getNicknamePrefix() + nickname; } - if (state) + if (isOp()) { - items.add(stack.getTypeId()); + nickname = ess.getSettings().getOperatorColor().toString() + nickname + "§f"; } - data.put("unlimited", items); - flush(); + return nickname; } - public String getPowertool(ItemStack stack) + public Teleport getTeleport() { - if (!data.containsKey("powertools")) - { - return null; - } - @SuppressWarnings("unchecked") - Map<Integer, String> tools = (Map<Integer, String>)data.get("powertools"); - return tools.get(stack.getTypeId()); + return teleport; } - public void setPowertool(ItemStack stack, String command) + public long getLastActivity() { - Map<Integer, String> tools = new HashMap<Integer, String>(); - if (data.containsKey("powertools")) - { - tools = (Map<Integer, String>)data.get("powertools"); - } - if (command == null || command.trim().isEmpty()) - { - tools.remove(Integer.valueOf(stack.getTypeId())); - } - else - { - tools.put(Integer.valueOf(stack.getTypeId()), command.trim()); - } - data.put("powertools", tools); - flush(); - } - - public Map<String,Object> getMetadata() { - return metadata; + return lastActivity; } - - public void setMetadata(String key, Object value) { - metadata.put(key, value); + + public void setLastActivity(long timestamp) + { + lastActivity = timestamp; } -} +}
\ No newline at end of file |