Implementation for basic Fountains
This commit is contained in:
parent
9c2cd6a883
commit
fed9f675d3
@ -0,0 +1,52 @@
|
||||
package mineplex.core.brawl.fountain;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* A fountain represents an atomic counter that is trying to hit a goal. Once the fountain
|
||||
* reaches it's goal you can either reset it or let it stay completed for an indefinite time.
|
||||
*
|
||||
* @author Shaun Bennett
|
||||
*/
|
||||
public class Fountain
|
||||
{
|
||||
// Cached count for the fountain
|
||||
private AtomicLong _count = new AtomicLong(0);
|
||||
// The System.currentTimeMillis() when cached count was last updated
|
||||
private long _cacheLastUpdated;
|
||||
// The goal that this fountain is trying to reach
|
||||
private long _goal;
|
||||
|
||||
// Redis repository to store the count
|
||||
private FountainRedisRepository _redisRepository;
|
||||
|
||||
public Fountain(String dataKey, long goal)
|
||||
{
|
||||
_goal = goal;
|
||||
_redisRepository = new FountainRedisRepository(dataKey);
|
||||
|
||||
updateCount();
|
||||
}
|
||||
|
||||
public void updateCount()
|
||||
{
|
||||
_count.set(_redisRepository.getCount());
|
||||
_cacheLastUpdated = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getCount()
|
||||
{
|
||||
return _count.get();
|
||||
}
|
||||
|
||||
public long getGoal()
|
||||
{
|
||||
return _goal;
|
||||
}
|
||||
|
||||
public double getFillPercent()
|
||||
{
|
||||
return Math.min(100, (((double) getCount()) / _goal));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package mineplex.core.brawl.fountain;
|
||||
|
||||
import mineplex.core.MiniPlugin;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
/**
|
||||
* @author Shaun Bennett
|
||||
*/
|
||||
public class FountainManager extends MiniPlugin
|
||||
{
|
||||
private Fountain _gemFountain;
|
||||
|
||||
public FountainManager(JavaPlugin plugin)
|
||||
{
|
||||
super("Fountain", plugin);
|
||||
|
||||
_gemFountain = new Fountain("GemFountain", 1000000);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void updateFountainCount(UpdateEvent event)
|
||||
{
|
||||
if (event.getType() != UpdateType.SLOW)
|
||||
return;
|
||||
|
||||
_gemFountain.updateCount();
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package mineplex.core.brawl.fountain;
|
||||
|
||||
import mineplex.serverdata.Region;
|
||||
import mineplex.serverdata.redis.RedisRepository;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
/**
|
||||
* Redis repository to store the count for {@link Fountain}
|
||||
* @author Shaun Bennett
|
||||
*/
|
||||
public class FountainRedisRepository extends RedisRepository
|
||||
{
|
||||
private String _dataKey;
|
||||
|
||||
public FountainRedisRepository(String dataKey)
|
||||
{
|
||||
super(Region.ALL);
|
||||
|
||||
_dataKey = dataKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current count inside the fountain
|
||||
* @return The current count for the fountain
|
||||
*/
|
||||
public long getCount()
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
try (Jedis jedis = getResource(false))
|
||||
{
|
||||
count = Long.parseLong(jedis.get(getKey()));
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the current fountain count by {@code increment} and then return the latest
|
||||
* count of the fountain. This is handled in an atomic process through redis
|
||||
* @param increment Amount to increment the fountain by
|
||||
* @return The new count for the fountain
|
||||
*/
|
||||
public long incrementCount(long increment)
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
try (Jedis jedis = getResource(true))
|
||||
{
|
||||
count = jedis.incrBy(getKey(), increment);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private String getKey()
|
||||
{
|
||||
return getKey(_dataKey);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user