Essentials/Essentials/src/com/earth2me/essentials/User.java

447 lines
10 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.register.payment.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class User extends UserData implements Comparable<User>, IReplyTo, IUser
{
private boolean justPortaled = false;
private CommandSender replyTo = null;
private transient User teleportRequester;
private transient boolean teleportRequestHere;
private transient final Teleport teleport;
2011-08-27 15:30:56 +02:00
private transient long lastOnlineActivity = System.currentTimeMillis();;
private transient long lastActivity;
private boolean hidden = false;
private transient boolean godStateBeforeAfk;
User(final Player base, final IEssentials ess)
{
super(base, ess);
teleport = new Teleport(this, ess);
godStateBeforeAfk = isGodModeEnabled();
}
User update(final Player base)
{
setBase(base);
return this;
}
@Override
public boolean isAuthorized(final IEssentialsCommand cmd)
{
return isAuthorized(cmd, "essentials.");
}
@Override
public boolean isAuthorized(final IEssentialsCommand cmd, final String permissionPrefix)
{
return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName()));
}
@Override
public boolean isAuthorized(final String node)
{
if (isOp())
{
return true;
}
if (isJailed())
{
return false;
}
return ess.getPermissionsHandler().hasPermission(this, node);
}
public void healCooldown() throws Exception
{
final Calendar now = new GregorianCalendar();
if (getLastHealTimestamp() > 0)
{
final double cooldown = ess.getSettings().getHealCooldown();
final 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"))
{
throw new Exception(Util.format("timeBeforeHeal", Util.formatDateDiff(cooldownTime.getTimeInMillis())));
}
}
setLastHealTimestamp(now.getTimeInMillis());
}
@Override
public void giveMoney(final double value)
2011-07-08 13:29:06 +02:00
{
giveMoney(value, null);
}
public void giveMoney(final double value, final CommandSender initiator)
{
if (value == 0)
{
return;
}
setMoney(getMoney() + value);
sendMessage(Util.format("addedToAccount", Util.formatCurrency(value, ess)));
2011-07-08 13:29:06 +02:00
if (initiator != null)
{
initiator.sendMessage(Util.format("addedToOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()));
2011-07-08 13:29:06 +02:00
}
}
public void payUser(final User reciever, final double value) throws Exception
{
if (value == 0)
{
return;
}
if (canAfford(value))
{
setMoney(getMoney() - value);
reciever.setMoney(reciever.getMoney() + value);
sendMessage(Util.format("moneySentTo", Util.formatCurrency(value, ess), reciever.getDisplayName()));
reciever.sendMessage(Util.format("moneyRecievedFrom", Util.formatCurrency(value, ess), getDisplayName()));
}
else
{
throw new Exception(Util.i18n("notEnoughMoney"));
}
}
@Override
public void takeMoney(final double value)
2011-07-08 13:29:06 +02:00
{
takeMoney(value, null);
}
public void takeMoney(final double value, final CommandSender initiator)
{
if (value == 0)
{
return;
}
setMoney(getMoney() - value);
sendMessage(Util.format("takenFromAccount", Util.formatCurrency(value, ess)));
2011-07-08 13:29:06 +02:00
if (initiator != null)
{
initiator.sendMessage(Util.format("takenFromOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()));
2011-07-08 13:29:06 +02:00
}
}
public boolean canAfford(final double cost)
{
final double mon = getMoney();
return mon >= cost || isAuthorized("essentials.eco.loan");
}
public void dispose()
{
this.base = new OfflinePlayer(getName(), ess);
}
public boolean getJustPortaled()
{
return justPortaled;
}
public void setJustPortaled(final boolean value)
{
justPortaled = value;
}
@Override
public void setReplyTo(final CommandSender user)
{
replyTo = user;
}
@Override
public CommandSender getReplyTo()
{
return replyTo;
}
@Override
public int compareTo(final User other)
{
return ChatColor.stripColor(this.getDisplayName()).compareToIgnoreCase(ChatColor.stripColor(other.getDisplayName()));
}
@Override
public boolean equals(final Object object)
{
if (!(object instanceof User))
{
return false;
}
return ChatColor.stripColor(this.getDisplayName()).equalsIgnoreCase(ChatColor.stripColor(((User)object).getDisplayName()));
}
@Override
public int hashCode()
{
return ChatColor.stripColor(this.getDisplayName()).hashCode();
}
public Boolean canSpawnItem(final int itemId)
{
return !ess.getSettings().itemSpawnBlacklist().contains(itemId);
}
public Location getHome() throws Exception
{
return getHome(getHomes().get(0));
}
public void setHome()
{
setHome("home", getLocation());
}
public void setHome(final String name)
{
setHome(name, getLocation());
}
@Override
public void setLastLocation()
{
setLastLocation(getLocation());
}
public void requestTeleport(final User player, final boolean here)
{
teleportRequester = player;
teleportRequestHere = here;
}
public User getTeleportRequest()
{
return teleportRequester;
}
public boolean isTeleportRequestHere()
{
return teleportRequestHere;
}
public String getNick()
{
final StringBuilder nickname = new StringBuilder();
final String nick = getNickname();
if (ess.getSettings().isCommandDisabled("nick") || nick == null || nick.isEmpty() || nick.equals(getName()))
{
nickname.append(getName());
}
else
{
nickname.append(ess.getSettings().getNicknamePrefix()).append(nick);
}
if (isOp())
{
try
{
nickname.insert(0, ess.getSettings().getOperatorColor().toString());
nickname.append("§f");
}
catch (Exception e)
{
}
}
if (ess.getSettings().addPrefixSuffix())
{
final String prefix = ess.getPermissionsHandler().getPrefix(this).replace('&', '§').replace("{WORLDNAME}", this.getWorld().getName());
final String suffix = ess.getPermissionsHandler().getSuffix(this).replace('&', '§').replace("{WORLDNAME}", this.getWorld().getName());
nickname.insert(0, prefix);
nickname.append(suffix);
if (suffix.length() < 2 || !suffix.substring(suffix.length() - 2, suffix.length() - 1).equals("§"))
{
nickname.append("§f");
}
}
return nickname.toString();
}
public Teleport getTeleport()
{
return teleport;
}
public long getLastOnlineActivity()
{
return lastOnlineActivity;
}
public void setLastOnlineActivity(final long timestamp)
{
lastOnlineActivity = timestamp;
}
@Override
public double getMoney()
{
if (ess.getPaymentMethod().hasMethod())
{
try
{
final Method method = ess.getPaymentMethod().getMethod();
if (!method.hasAccount(this.getName()))
{
throw new Exception();
}
final Method.MethodAccount account = ess.getPaymentMethod().getMethod().getAccount(this.getName());
return account.balance();
}
catch (Throwable ex)
{
}
}
return super.getMoney();
}
@Override
public void setMoney(final double value)
{
if (ess.getPaymentMethod().hasMethod())
{
try
{
final Method method = ess.getPaymentMethod().getMethod();
if (!method.hasAccount(this.getName()))
{
throw new Exception();
}
final Method.MethodAccount account = ess.getPaymentMethod().getMethod().getAccount(this.getName());
account.set(value);
}
catch (Throwable ex)
{
}
}
super.setMoney(value);
}
@Override
public void setAfk(final boolean set)
{
this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : set);
if (set && !isAfk() && ess.getSettings().getFreezeAfkPlayers()) {
godStateBeforeAfk = isGodModeEnabled();
setGodModeEnabled(true);
}
if (!set && isAfk() && ess.getSettings().getFreezeAfkPlayers()) {
setGodModeEnabled(godStateBeforeAfk);
}
super.setAfk(set);
}
@Override
public boolean toggleAfk()
{
final boolean now = super.toggleAfk();
this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now);
return now;
}
public boolean isHidden()
{
return hidden;
}
public void setHidden(final boolean hidden)
{
this.hidden = hidden;
}
public void checkJailTimeout(final long currentTime)
{
if (getJailTimeout() > 0 && getJailTimeout() < currentTime && isJailed())
{
setJailTimeout(0);
setJailed(false);
sendMessage(Util.i18n("haveBeenReleased"));
setJail(null);
try
{
getTeleport().back();
}
catch (Exception ex)
{
}
}
}
public void checkMuteTimeout(final long currentTime)
{
if (getMuteTimeout() > 0 && getMuteTimeout() < currentTime && isMuted())
{
setMuteTimeout(0);
sendMessage(Util.i18n("canTalkAgain"));
setMuted(false);
}
}
public void checkBanTimeout(final long currentTime)
{
if (getBanTimeout() > 0 && getBanTimeout() < currentTime && ess.getBans().isNameBanned(getName()))
{
setBanTimeout(0);
ess.getBans().unbanByName(getName());
}
}
public void updateActivity()
{
if (isAfk())
{
setAfk(false);
ess.broadcastMessage(getName(), Util.format("userIsNotAway", getDisplayName()));
return;
}
lastActivity = System.currentTimeMillis();
}
public void checkActivity()
{
final long autoafkkick = ess.getSettings().getAutoAfkKick();
if (autoafkkick > 0 && lastActivity + autoafkkick * 1000 < System.currentTimeMillis()
&& !isAuthorized("essentials.kick.exempt") && !isAuthorized("essentials.afk.kickexempt"))
{
final String kickReason = Util.format("autoAfkKickReason", autoafkkick/60.0);
kickPlayer(kickReason);
for (Player player : ess.getServer().getOnlinePlayers())
{
final User user = ess.getUser(player);
if (user.isAuthorized("essentials.kick.notify"))
{
player.sendMessage(Util.format("playerKicked", Console.NAME, getName(), kickReason));
}
}
}
final long autoafk = ess.getSettings().getAutoAfk();
2011-08-27 15:30:56 +02:00
if (!isAfk() && autoafk > 0 && lastActivity + autoafk * 1000 < System.currentTimeMillis())
{
setAfk(true);
ess.broadcastMessage(getName(), Util.format("userIsAway", getDisplayName()));
}
}
2011-07-08 13:29:06 +02:00
}