1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-09-30 16:19:11 +02:00

Re-Implemented User class

This commit is contained in:
snowleo
2011-12-10 03:04:10 +01:00
parent de12c5c6d1
commit f0dabdd4b1
57 changed files with 1420 additions and 259 deletions

View File

@@ -1,25 +1,60 @@
package com.earth2me.essentials.user;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.Console;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import com.earth2me.essentials.Teleport;
import com.earth2me.essentials.Util;
import com.earth2me.essentials.api.IGroups;
import com.earth2me.essentials.api.ISettings;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.register.payment.Method;
import com.earth2me.essentials.settings.WorldOptions;
import static com.earth2me.essentials.I18n._;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Logger;
import lombok.Cleanup;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class User extends UserBase implements IUser
{
@Getter
@Setter
private CommandSender replyTo = null;
private transient User teleportRequester;
private transient boolean teleportRequestHere;
@Getter
private transient final Teleport teleport;
@Getter
private transient long teleportRequestTime;
@Getter
@Setter
private transient long lastOnlineActivity;
private transient long lastActivity = System.currentTimeMillis();
@Getter
@Setter
private boolean hidden = false;
private transient Location afkPosition;
private static final Logger logger = Bukkit.getLogger();
public User(final Player base, final IEssentials ess)
{
super(base, ess);
teleport = new Teleport(this, (com.earth2me.essentials.IEssentials)ess);
}
public User(final OfflinePlayer offlinePlayer, final IEssentials ess)
{
super(offlinePlayer, ess);
teleport = new Teleport(this, (com.earth2me.essentials.IEssentials)ess);
}
public void example()
@@ -37,45 +72,144 @@ public class User extends UserBase implements IUser
user.getData().setMoney(10 + money);
}
@Override
public long getLastTeleportTimestamp()
{
acquireReadLock();
try
{
return getData().getTimestamps().get("lastteleport");
}
finally
{
unlock();
}
}
@Override
public boolean isAuthorized(String node)
{
throw new UnsupportedOperationException("Not supported yet.");
if (!isOnlineUser())
{
return false;
}
if (getData().isJailed())
{
return false;
}
return ess.getPermissionsHandler().hasPermission(base, node);
}
@Override
public boolean isAuthorized(IEssentialsCommand cmd)
{
throw new UnsupportedOperationException("Not supported yet.");
return isAuthorized(cmd, "essentials.");
}
@Override
public boolean isAuthorized(IEssentialsCommand cmd, String permissionPrefix)
{
throw new UnsupportedOperationException("Not supported yet.");
return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName()));
}
public void checkCooldown(final UserData.TimestampType cooldownType,final double cooldown, final boolean set, final String bypassPermission) throws CooldownException
{
final Calendar now = new GregorianCalendar();
if (getTimestamp(cooldownType) > 0)
{
final Calendar cooldownTime = new GregorianCalendar();
cooldownTime.setTimeInMillis(getTimestamp(cooldownType));
cooldownTime.add(Calendar.SECOND, (int)cooldown);
cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0));
if (cooldownTime.after(now) && !isAuthorized(bypassPermission))
{
throw new CooldownException(Util.formatDateDiff(cooldownTime.getTimeInMillis()));
}
}
if (set) {
setTimestamp(cooldownType, now.getTimeInMillis());
}
}
@Override
public void setLastTeleportTimestamp(long time)
public void giveMoney(final double value)
{
giveMoney(value, null);
}
public void giveMoney(final double value, final CommandSender initiator)
{
if (value == 0)
{
return;
}
acquireWriteLock();
try
{
setMoney(getMoney() + value);
sendMessage(_("addedToAccount", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess)));
if (initiator != null)
{
initiator.sendMessage(_("addedToOthersAccount", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess), this.getDisplayName()));
}
}
finally
{
unlock();
}
}
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(_("moneySentTo", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess), reciever.getDisplayName()));
reciever.sendMessage(_("moneyRecievedFrom", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess), getDisplayName()));
}
else
{
throw new Exception(_("notEnoughMoney"));
}
}
@Override
public void takeMoney(final double value)
{
takeMoney(value, null);
}
public void takeMoney(final double value, final CommandSender initiator)
{
if (value == 0)
{
return;
}
setMoney(getMoney() - value);
sendMessage(_("takenFromAccount", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess)));
if (initiator != null)
{
initiator.sendMessage(_("takenFromOthersAccount", Util.formatCurrency(value, (com.earth2me.essentials.IEssentials)ess), this.getDisplayName()));
}
}
public boolean canAfford(final double cost)
{
final double mon = getMoney();
return mon >= cost || isAuthorized("essentials.eco.loan");
}
public void setHome()
{
setHome("home", getLocation());
}
public void setHome(final String name)
{
setHome(name, getLocation());
}
@Override
public void setLastLocation()
{
acquireWriteLock();
try
{
getData().getTimestamps().put("lastteleport", time);
getData().setLastLocation(getLocation());
}
finally
{
@@ -83,13 +217,60 @@ public class User extends UserBase implements IUser
}
}
@Override
public Location getLastLocation()
public void requestTeleport(final User player, final boolean here)
{
teleportRequestTime = System.currentTimeMillis();
teleportRequester = player;
teleportRequestHere = here;
}
public User getTeleportRequest()
{
return teleportRequester;
}
public boolean isTeleportRequestHere()
{
return teleportRequestHere;
}
public String getNick(boolean addprefixsuffix)
{
acquireReadLock();
try
{
return getData().getLastLocation();
final String nick = getData().getNickname();
@Cleanup
final ISettings settings = ess.getSettings();
settings.acquireReadLock();
@Cleanup
final IGroups groups = ess.getGroups();
groups.acquireReadLock();
// default: {PREFIX}{NICKNAMEPREFIX}{NAME}{SUFFIX}
String displayname = settings.getData().getChat().getDisplaynameFormat();
if (settings.getData().getCommands().isDisabled("nick") || nick == null || nick.isEmpty() || nick.equals(getName()))
{
displayname = displayname.replace("{NAME}", getName());
}
else
{
displayname = displayname.replace("{NAME}", nick);
displayname = displayname.replace("{NICKNAMEPREFIX}", settings.getData().getChat().getNicknamePrefix());
}
if (displayname.contains("{PREFIX}"))
{
displayname = displayname.replace("{PREFIX}", groups.getPrefix(this));
}
if (displayname.contains("{SUFFIX}"))
{
displayname = displayname.replace("{SUFFIX}", groups.getSuffix(this));
}
displayname = displayname.replace("{WORLDNAME}", this.getWorld().getName());
displayname = displayname.replace('&', '§');
displayname = displayname.concat("§f");
return displayname;
}
finally
{
@@ -97,13 +278,141 @@ public class User extends UserBase implements IUser
}
}
public void setDisplayNick()
{
String name = getNick(true);
setDisplayName(name);
if (name.length() > 16)
{
name = getNick(false);
}
if (name.length() > 16)
{
name = name.substring(0, name.charAt(15) == '§' ? 15 : 16);
}
try
{
setPlayerListName(name);
}
catch (IllegalArgumentException e)
{
logger.info("Playerlist for " + name + " was not updated. Use a shorter displayname prefix.");
}
}
@Override
public String getDisplayName()
{
return super.getDisplayName() == null ? super.getName() : super.getDisplayName();
}
@Override
public void updateDisplayName()
{
@Cleanup
final ISettings settings = ess.getSettings();
settings.acquireReadLock();
if (isOnlineUser() && settings.getData().getChat().getChangeDisplayname())
{
setDisplayNick();
}
}
@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);
}
public void setAfk(final boolean set)
{
acquireWriteLock();
try
{
this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : set);
if (set && !getData().isAfk())
{
afkPosition = getLocation();
}
getData().setAfk(set);
}
finally
{
unlock();
}
}
@Override
public boolean toggleAfk()
{
final boolean now = super.toggleAfk();
this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now);
return now;
}
//Returns true if status expired during this check
public boolean checkJailTimeout(final long currentTime)
{
acquireReadLock();
try
{
return getData().getMoney();
if (getTimestamp(UserData.TimestampType.JAIL) > 0 && getTimestamp(UserData.TimestampType.JAIL) < currentTime && getData().isJailed())
{
acquireWriteLock();
setTimestamp(UserData.TimestampType.JAIL, 0);
getData().setJailed(false);
sendMessage(_("haveBeenReleased"));
getData().setJail(null);
try
{
teleport.back();
}
catch (Exception ex)
{
}
return true;
}
return false;
}
finally
{
@@ -111,13 +420,21 @@ public class User extends UserBase implements IUser
}
}
@Override
public void takeMoney(double value)
//Returns true if status expired during this check
public boolean checkMuteTimeout(final long currentTime)
{
acquireWriteLock();
acquireReadLock();
try
{
getData().setMoney(getData().getMoney() - value);
if (getTimestamp(UserData.TimestampType.MUTE) > 0 && getTimestamp(UserData.TimestampType.MUTE) < currentTime && getData().isMuted())
{
acquireWriteLock();
setTimestamp(UserData.TimestampType.MUTE, 0);
sendMessage(_("canTalkAgain"));
getData().setMuted(false);
return true;
}
return false;
}
finally
{
@@ -125,13 +442,116 @@ public class User extends UserBase implements IUser
}
}
@Override
public void giveMoney(double value)
//Returns true if status expired during this check
public boolean checkBanTimeout(final long currentTime)
{
acquireWriteLock();
acquireReadLock();
try
{
getData().setMoney(getData().getMoney() + value);
if (getData().getBan() != null && getData().getBan().getTimeout() > 0 && getData().getBan().getTimeout() < currentTime && isBanned())
{
acquireWriteLock();
getData().setBan(null);
setBanned(false);
return true;
}
return false;
}
finally
{
unlock();
}
}
public void updateActivity(final boolean broadcast)
{
acquireReadLock();
try
{
if (getData().isAfk())
{
acquireWriteLock();
getData().setAfk(false);
if (broadcast && !hidden)
{
ess.broadcastMessage(this, _("userIsNotAway", getDisplayName()));
}
}
lastActivity = System.currentTimeMillis();
}
finally
{
unlock();
}
}
public void checkActivity()
{
@Cleanup
final ISettings settings = ess.getSettings();
settings.acquireReadLock();
final long autoafkkick = settings.getData().getCommands().getAfk().getAutoAFKKick();
if (autoafkkick > 0 && lastActivity > 0 && (lastActivity + (autoafkkick * 1000)) < System.currentTimeMillis()
&& !hidden && !isAuthorized("essentials.kick.exempt") && !isAuthorized("essentials.afk.kickexempt"))
{
final String kickReason = _("autoAfkKickReason", autoafkkick / 60.0);
lastActivity = 0;
kickPlayer(kickReason);
for (Player player : ess.getServer().getOnlinePlayers())
{
final IUser user = ess.getUser(player);
if (user.isAuthorized("essentials.kick.notify"))
{
player.sendMessage(_("playerKicked", Console.NAME, getName(), kickReason));
}
}
}
final long autoafk = settings.getData().getCommands().getAfk().getAutoAFK();
acquireReadLock();
try
{
if (!getData().isAfk() && autoafk > 0 && lastActivity + autoafk * 1000 < System.currentTimeMillis() && isAuthorized("essentials.afk"))
{
setAfk(true);
if (!hidden)
{
ess.broadcastMessage(this, _("userIsAway", getDisplayName()));
}
}
}
finally
{
unlock();
}
}
public Location getAfkPosition()
{
return afkPosition;
}
public boolean toggleGodModeEnabled()
{
if (!isGodModeEnabled())
{
setFoodLevel(20);
}
return super.toggleGodmode();
}
public boolean isGodModeEnabled()
{
acquireReadLock();
try
{
@Cleanup
final ISettings settings = ess.getSettings();
settings.acquireReadLock();
return (getData().isGodmode()
&& !settings.getData().getWorldOptions(getLocation().getWorld().getName()).isGodmode())
|| (getData().isAfk() && settings.getData().getCommands().getAfk().isFreezeAFKPlayers());
}
finally
{
@@ -142,27 +562,17 @@ public class User extends UserBase implements IUser
@Override
public String getGroup()
{
throw new UnsupportedOperationException("Not supported yet.");
return ess.getPermissionsHandler().getGroup(base);
}
@Override
public void setLastLocation()
public boolean inGroup(final String group)
{
acquireWriteLock();
try
{
getData().setLastLocation(base.getLocation());
}
finally
{
unlock();
}
return ess.getPermissionsHandler().inGroup(base, group);
}
@Override
public Location getHome(String name) throws Exception
public boolean canBuild()
{
throw new UnsupportedOperationException("Not supported yet.");
return ess.getPermissionsHandler().canBuild(base, getGroup());
}
@Override
@@ -172,28 +582,8 @@ public class User extends UserBase implements IUser
}
@Override
public boolean isHidden()
public Location getHome(String name) throws Exception
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Teleport getTeleport()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setJail(final String jail)
{
acquireWriteLock();
try
{
getData().setJail(jail);
}
finally
{
unlock();
}
}
}