Add /fountain command. Fountain implementation

This commit is contained in:
Shaun Bennett 2016-04-19 13:57:19 +10:00
parent fed9f675d3
commit 5a667a7d31
8 changed files with 266 additions and 36 deletions

View File

@ -1,52 +1,62 @@
package mineplex.core.brawl.fountain;
import java.util.concurrent.atomic.AtomicLong;
import mineplex.core.common.util.UtilText;
import mineplex.core.hologram.Hologram;
import mineplex.core.hologram.HologramManager;
import mineplex.core.stats.StatsManager;
import mineplex.serverdata.redis.counter.Counter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
/**
* 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;
// Manager Injections
private final HologramManager _hologramManager;
private final StatsManager _statsManager;
// Redis repository to store the count
private FountainRedisRepository _redisRepository;
private final String _name;
private final String _dataKey;
private final Location _location;
private final Hologram _hologram;
private final Counter _counter;
public Fountain(String dataKey, long goal)
public Fountain(Location location, String name, String dataKey, long goal, HologramManager hologramManager, StatsManager statsManager)
{
_goal = goal;
_redisRepository = new FountainRedisRepository(dataKey);
_hologramManager = hologramManager;
_statsManager = statsManager;
updateCount();
_name = name;
_dataKey = dataKey;
_location = location;
_hologram = new Hologram(hologramManager, location.clone().add(0, 2, 0), name);
_counter = new Counter(dataKey, goal);
updateHologram();
}
public void updateCount()
protected void updateHologram()
{
_count.set(_redisRepository.getCount());
_cacheLastUpdated = System.currentTimeMillis();
String progressBar = UtilText.getProgress(null, _counter.getFillPercent(), null, false);
_hologram.setText(_name, progressBar);
}
public long getCount()
public void increment(Player player, long amount)
{
return _count.get();
_statsManager.incrementStat(player, "Global.Fountain." + getDataKey(), amount);
_counter.increment(amount);
updateHologram();
}
public long getGoal()
public String getName()
{
return _goal;
return _name;
}
public double getFillPercent()
public String getDataKey()
{
return Math.min(100, (((double) getCount()) / _goal));
return _dataKey;
}
}

View File

@ -1,9 +1,17 @@
package mineplex.core.brawl.fountain;
import mineplex.core.MiniPlugin;
import mineplex.core.brawl.fountain.command.FountainCommand;
import mineplex.core.common.util.C;
import mineplex.core.hologram.HologramManager;
import mineplex.core.stats.StatsManager;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.serverdata.redis.counter.Counter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;
/**
@ -11,13 +19,26 @@ import org.bukkit.plugin.java.JavaPlugin;
*/
public class FountainManager extends MiniPlugin
{
private HologramManager _hologramManager;
private StatsManager _statsManager;
private Fountain _gemFountain;
public FountainManager(JavaPlugin plugin)
public FountainManager(JavaPlugin plugin, HologramManager hologramManager, StatsManager statsManager)
{
super("Fountain", plugin);
super("Counter", plugin);
_gemFountain = new Fountain("GemFountain", 1000000);
_hologramManager = hologramManager;
_statsManager = statsManager;
_gemFountain = new Fountain(new Location(Bukkit.getWorld("world"), -32.5, 72, -23.5),
C.cGreen + "Gem Fountain", "GemFountain", 1000000, _hologramManager, _statsManager);
}
@Override
public void addCommands()
{
addCommand(new FountainCommand(this));
}
@EventHandler
@ -26,6 +47,11 @@ public class FountainManager extends MiniPlugin
if (event.getType() != UpdateType.SLOW)
return;
_gemFountain.updateCount();
_gemFountain.updateHologram();
}
public Fountain getGemFountain()
{
return _gemFountain;
}
}

View File

@ -0,0 +1,44 @@
package mineplex.core.brawl.fountain.command;
import mineplex.core.brawl.fountain.FountainManager;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import org.bukkit.entity.Player;
/**
* @author Shaun Bennett
*/
public class AddCommand extends CommandBase<FountainManager>
{
public AddCommand(FountainManager plugin)
{
super(plugin, Rank.ADMIN, "add");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args == null || args.length != 1)
{
help(caller);
return;
}
try
{
long amount = Long.parseLong(args[0]);
Plugin.getGemFountain().increment(caller, amount);
}
catch (NumberFormatException ex)
{
help(caller);
}
}
private void help(Player player)
{
UtilPlayer.message(player, F.help("/fountain add", "<amount>", Rank.ADMIN));
}
}

View File

@ -0,0 +1,25 @@
package mineplex.core.brawl.fountain.command;
import mineplex.core.brawl.fountain.FountainManager;
import mineplex.core.command.MultiCommandBase;
import mineplex.core.common.Rank;
import org.bukkit.entity.Player;
/**
* @author Shaun Bennett
*/
public class FountainCommand extends MultiCommandBase<FountainManager>
{
public FountainCommand(FountainManager plugin)
{
super(plugin, Rank.ADMIN, "fountain");
AddCommand(new AddCommand(plugin));
}
@Override
protected void Help(Player caller, String[] args)
{
}
}

View File

@ -0,0 +1,26 @@
package mineplex.core.brawl.fountain.gui;
import mineplex.core.account.CoreClientManager;
import mineplex.core.brawl.fountain.FountainManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.shop.page.ShopPageBase;
import org.bukkit.entity.Player;
/**
* @author Shaun Bennett
*/
public class FountainPage extends ShopPageBase<FountainManager, FountainShop>
{
public FountainPage(FountainManager plugin, FountainShop shop, CoreClientManager clientManager, DonationManager donationManager, Player player)
{
super(plugin, shop, clientManager, donationManager, "Counter Keeper", player);
buildPage();
}
@Override
protected void buildPage()
{
}
}

View File

@ -0,0 +1,25 @@
package mineplex.core.brawl.fountain.gui;
import mineplex.core.account.CoreClientManager;
import mineplex.core.brawl.fountain.FountainManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.shop.ShopBase;
import mineplex.core.shop.page.ShopPageBase;
import org.bukkit.entity.Player;
/**
* @author Shaun Bennett
*/
public class FountainShop extends ShopBase<FountainManager>
{
public FountainShop(FountainManager plugin, CoreClientManager clientManager, DonationManager donationManager)
{
super(plugin, clientManager, donationManager, "Counter Shop");
}
@Override
protected ShopPageBase<FountainManager, ? extends ShopBase<FountainManager>> buildPagesFor(Player player)
{
return new FountainPage(getPlugin(), this, getClientManager(), getDonationManager(), player);
}
}

View File

@ -0,0 +1,70 @@
package mineplex.serverdata.redis.counter;
import java.util.concurrent.atomic.AtomicLong;
/**
* A counter represents an atomic number that is trying to hit a goal. Once the counter
* reaches it's goal you can either reset it or let it stay completed for an indefinite time.
*
* @author Shaun Bennett
*/
public class Counter
{
// 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 final long _goal;
// The unique key to reference this counter
private final String _dataKey;
// Redis repository to store the count
private CounterRedisRepository _redisRepository;
public Counter(String dataKey, long goal)
{
_goal = goal;
_dataKey = dataKey;
_redisRepository = new CounterRedisRepository(dataKey);
updateCount();
}
public void updateCount()
{
updateCount(_redisRepository.getCount());
}
protected void updateCount(long newCount)
{
_count.set(newCount);
_cacheLastUpdated = System.currentTimeMillis();
}
public void increment(long amount)
{
long newCount = _redisRepository.incrementCount(amount);
updateCount(newCount);
}
public long getCount()
{
return _count.get();
}
public long getGoal()
{
return _goal;
}
public double getFillPercent()
{
return Math.min(100, (((double) getCount()) / _goal));
}
public String getDataKey()
{
return _dataKey;
}
}

View File

@ -1,18 +1,18 @@
package mineplex.core.brawl.fountain;
package mineplex.serverdata.redis.counter;
import mineplex.serverdata.Region;
import mineplex.serverdata.redis.RedisRepository;
import redis.clients.jedis.Jedis;
/**
* Redis repository to store the count for {@link Fountain}
* Redis repository to store the count for {@link Counter}
* @author Shaun Bennett
*/
public class FountainRedisRepository extends RedisRepository
public class CounterRedisRepository extends RedisRepository
{
private String _dataKey;
public FountainRedisRepository(String dataKey)
public CounterRedisRepository(String dataKey)
{
super(Region.ALL);
@ -31,6 +31,10 @@ public class FountainRedisRepository extends RedisRepository
{
count = Long.parseLong(jedis.get(getKey()));
}
catch (NumberFormatException ex)
{
ex.printStackTrace();
}
return count;
}