1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-10-01 00:26:48 +02:00

Refactoring to create less redundant code

This commit is contained in:
snowleo
2011-12-06 15:38:14 +01:00
parent 51390a9698
commit a7097df231
7 changed files with 330 additions and 175 deletions

View File

@@ -1,19 +1,16 @@
package com.earth2me.essentials.user;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter;
import com.earth2me.essentials.storage.StorageObject;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.commands.IEssentialsCommand;
import lombok.Cleanup;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
// this is a prototype for locking userdata
public class User extends UserBase implements IOfflineUser
{
private transient UserData data = new UserData();
private final transient ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
public class User extends UserBase implements IUser
{
public User(final Player base, final IEssentials ess)
{
super(base, ess);
@@ -24,43 +21,6 @@ public class User extends UserBase implements IOfflineUser
super(offlinePlayer, ess);
}
@Override
public UserData getData()
{
return data;
}
@Override
public void aquireReadLock()
{
rwl.readLock().lock();
}
@Override
public void aquireWriteLock()
{
while (rwl.getReadHoldCount() > 0)
{
rwl.readLock().unlock();
}
rwl.writeLock().lock();
rwl.readLock().lock();
}
@Override
public void close()
{
if (rwl.isWriteLockedByCurrentThread())
{
rwl.writeLock().unlock();
scheduleSaving();
}
while (rwl.getReadHoldCount() > 0)
{
rwl.readLock().unlock();
}
}
public void example()
{
// Cleanup will call close at the end of the function
@@ -68,37 +28,151 @@ public class User extends UserBase implements IOfflineUser
final User user = this;
// read lock allows to read data from the user
user.aquireReadLock();
user.acquireReadLock();
final double money = user.getData().getMoney();
// write lock allows only one thread to modify the data
user.aquireWriteLock();
user.acquireWriteLock();
user.getData().setMoney(10 + money);
}
private void scheduleSaving()
@Override
public long getLastTeleportTimestamp()
{
new UserDataWriter();
acquireReadLock();
try
{
return getData().getTimestamps().get("lastteleport");
}
finally
{
unlock();
}
}
private class UserDataWriter extends AbstractDelayedYamlFileWriter
@Override
public boolean isAuthorized(String node)
{
public UserDataWriter()
{
super(ess, ess.getUserMap().getUserFile(User.this.getName()));
}
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public StorageObject getObject()
{
aquireReadLock();
return getData();
}
@Override
public boolean isAuthorized(IEssentialsCommand cmd)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void onFinish()
@Override
public boolean isAuthorized(IEssentialsCommand cmd, String permissionPrefix)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setLastTeleportTimestamp(long time)
{
acquireWriteLock();
try
{
close();
getData().getTimestamps().put("lastteleport", time);
}
finally
{
unlock();
}
}
@Override
public Location getLastLocation()
{
acquireReadLock();
try
{
return getData().getLastLocation();
}
finally
{
unlock();
}
}
@Override
public double getMoney()
{
acquireReadLock();
try
{
return getData().getMoney();
}
finally
{
unlock();
}
}
@Override
public void takeMoney(double value)
{
acquireWriteLock();
try
{
getData().setMoney(getData().getMoney() - value);
}
finally
{
unlock();
}
}
@Override
public void giveMoney(double value)
{
acquireWriteLock();
try
{
getData().setMoney(getData().getMoney() + value);
}
finally
{
unlock();
}
}
@Override
public String getGroup()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setLastLocation()
{
acquireWriteLock();
try
{
getData().setLastLocation(base.getLocation());
}
finally
{
unlock();
}
}
@Override
public Location getHome(String name) throws Exception
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Location getHome(Location loc) throws Exception
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean isHidden()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}