diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java index a7efd950b..471bfe8e2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java @@ -5,16 +5,20 @@ import mineplex.core.brawl.fountain.gui.FountainShop; import mineplex.core.common.SortedSchematicLoader; import mineplex.core.common.block.schematic.UtilSchematic; import mineplex.core.common.util.C; +import mineplex.core.common.util.Callback; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilText; import mineplex.core.donation.DonationManager; import mineplex.core.hologram.Hologram; import mineplex.core.hologram.HologramManager; import mineplex.core.stats.StatsManager; +import mineplex.core.thread.ThreadPool; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; +import mineplex.serverdata.Region; import mineplex.serverdata.redis.counter.GoalCounter; import mineplex.serverdata.redis.counter.GoalCounterListener; +import mineplex.serverdata.servers.ConnectionData; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -49,7 +53,7 @@ public class Fountain implements GoalCounterListener private final FountainShop _shop; - public Fountain(Location location, Location pasteLocation, String name, String dataKey, long goal, FountainManager fountainManager, + public Fountain(ConnectionData writeConnection, ConnectionData readConnection, Region region, Location location, Location pasteLocation, String name, String dataKey, long goal, FountainManager fountainManager, CoreClientManager clientManager, DonationManager donationManager, HologramManager hologramManager, StatsManager statsManager) { @@ -60,7 +64,7 @@ public class Fountain implements GoalCounterListener _dataKey = dataKey; _location = location; _hologram = new Hologram(hologramManager, location.clone().add(4, 3, -3), name).start(); - _counter = new GoalCounter(dataKey, 5000, goal); + _counter = new GoalCounter(writeConnection, readConnection, region, dataKey, goal); _counter.addListener(this); _brawlActive = false; // _blockProgressBar = new BlockProgressBar(_lavaLocation.getBlock(), Material.LAVA, BlockFace.UP); @@ -164,11 +168,16 @@ public class Fountain implements GoalCounterListener } } - public void increment(Player player, long amount) + public void increment(Player player, long amount, Callback callback) { _statsManager.incrementStat(player, getStatName(), amount); - _counter.addAndGet(amount); - updateVisuals(); + _statsManager.runAsync(() -> { + long count = _counter.addAndGet(amount); + _statsManager.runSync(() -> { + updateVisuals(); + if (callback != null) callback.run(count); + }); + }); } public long getAmountAdded(Player player) @@ -243,6 +252,11 @@ public class Fountain implements GoalCounterListener } } + public void updateCounter() + { + _counter.updateCount(); + } + public boolean isBrawlActive() { return _brawlActive; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java index 11e4cf821..2d7444df9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java @@ -14,6 +14,7 @@ import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.serverdata.Region; import mineplex.serverdata.redis.counter.Counter; +import mineplex.serverdata.servers.ConnectionData; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -51,8 +52,10 @@ public class FountainManager extends MiniPlugin World world = Bukkit.getWorlds().get(0);//-43.5, 66, -38.5 - int goal = !new File("eu.dat").exists() ? 200000000 : 20000000; - _gemFountain = new Fountain(new Location(world, -32.5, 72, -23.5), new Location(world, -43.5, 67, -38.5), + int goal = 70000000;//!new File("eu.dat").exists() ? 200000000 : 20000000; + _gemFountain = new Fountain(new ConnectionData("10.3.203.80", 6379, ConnectionData.ConnectionType.MASTER, "USRedis"), + new ConnectionData("10.3.203.80", 6377, ConnectionData.ConnectionType.SLAVE, "USRedis"), Region.ALL, + new Location(world, -32.5, 72, -23.5), new Location(world, -43.5, 67, -38.5), C.cGreen + "Gem Fountain", "GemFountain_01", goal, this, clientManager, donationManager, _hologramManager, _statsManager); } @@ -72,6 +75,15 @@ public class FountainManager extends MiniPlugin _gemFountain.updateVisuals(); } + @EventHandler + public void updateCounter(UpdateEvent event) + { + if (event.getType() != UpdateType.SEC_05) + return; + + runSync(_gemFountain::updateCounter); + } + @EventHandler public void onInteractAtEntity(PlayerInteractAtEntityEvent event) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/command/AddCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/command/AddCommand.java index 1be61d068..cd1bf65a5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/command/AddCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/command/AddCommand.java @@ -41,7 +41,7 @@ public class AddCommand extends CommandBase { if (result == TransactionResponse.Success) { - Plugin.getGemFountain().increment(caller, amount); + Plugin.getGemFountain().increment(caller, amount, null); UtilPlayer.message(caller, F.main("Fountain", "Added " + F.elem(amount) + " to the fountain!")); } else if (result == TransactionResponse.InsufficientFunds) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/gui/button/FountainAddButton.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/gui/button/FountainAddButton.java index 9460025c1..c37c8c551 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/gui/button/FountainAddButton.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/gui/button/FountainAddButton.java @@ -34,7 +34,7 @@ public class FountainAddButton implements IButton { public void run() { - _page.getPlugin().getGemFountain().increment(player, _gems); + _page.getPlugin().getGemFountain().increment(player, _gems, null); _page.refresh(); } }, _page, _salesPackage, CurrencyType.GEM, player)); diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/Counter.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/Counter.java index 208491b4b..af6a05043 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/Counter.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/Counter.java @@ -1,5 +1,8 @@ package mineplex.serverdata.redis.counter; +import mineplex.serverdata.Region; +import mineplex.serverdata.servers.ConnectionData; + import java.util.concurrent.atomic.AtomicLong; /** @@ -14,26 +17,25 @@ public class Counter private final AtomicLong _count = new AtomicLong(0); // The System.currentTimeMillis() when cached count was last updated private volatile long _lastUpdated; - // The maximum time to wait before syncing the count with repository - private final long _syncTime; // The unique key to reference this counter private final String _dataKey; // Redis repository to store the count private final CounterRedisRepository _redisRepository; - public Counter(String dataKey, long syncTime) + public Counter(ConnectionData writeConnection, ConnectionData readConnection, Region region, String dataKey) { _dataKey = dataKey; - _syncTime = syncTime; - _redisRepository = new CounterRedisRepository(dataKey); + _redisRepository = new CounterRedisRepository(writeConnection, readConnection, region, dataKey); } public Counter(String dataKey) { - this(dataKey, 5000); // 5 seconds + _dataKey = dataKey; + _redisRepository = new CounterRedisRepository(dataKey); } + /** * Add a value to the counter and return the new counter value. This method is thread-safe and interacts * directly with the atomic value stored in redis. The value returned from redis is then returned @@ -51,25 +53,26 @@ public class Counter } /** - * Get the latest count of the counter. This will use the last cached value for the counter, or if - * the last cached count hasn't been updated in the {@link #_syncTime} period it will pull the latest - * count from the redis repository and use that value. + * Get the latest cached count from the counter. This value will not be changed until {@link #addAndGet(long)} + * or {@link #updateCount} is called. * * @return The counter count */ public long getCount() { - if (System.currentTimeMillis() - _lastUpdated > _syncTime) - { - updateCount(_redisRepository.getCount()); - } - return _count.get(); } /** - * Reset the counter back to 0. This will take a maximum time of {@link #_syncTime} to propagate - * across all instances of this counter. Immediately updates the redis repository. + * Update the cached count to reflect the count in redis. This should be called async + */ + public void updateCount() + { + updateCount(_redisRepository.getCount()); + } + + /** + * Reset the counter back to 0. Immediately updates the redis repository. * * @return The value of the counter before it was reset */ diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/CounterRedisRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/CounterRedisRepository.java index 96a1cb0e3..69561aed8 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/CounterRedisRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/CounterRedisRepository.java @@ -2,6 +2,7 @@ package mineplex.serverdata.redis.counter; import mineplex.serverdata.Region; import mineplex.serverdata.redis.RedisRepository; +import mineplex.serverdata.servers.ConnectionData; import redis.clients.jedis.Jedis; /** @@ -12,13 +13,18 @@ public class CounterRedisRepository extends RedisRepository { private String _dataKey; + public CounterRedisRepository(ConnectionData writeConnection, ConnectionData readConnection, Region region, String dataKey) + { + super(writeConnection, readConnection, region); + + _dataKey = dataKey; + } + public CounterRedisRepository(String dataKey) { super(Region.ALL); _dataKey = dataKey; - -// setNX(); } /** diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/GoalCounter.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/GoalCounter.java index 8d14e56f0..38bb33d1a 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/GoalCounter.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/counter/GoalCounter.java @@ -1,5 +1,8 @@ package mineplex.serverdata.redis.counter; +import mineplex.serverdata.Region; +import mineplex.serverdata.servers.ConnectionData; + import java.util.ArrayList; import java.util.List; @@ -18,17 +21,31 @@ public class GoalCounter extends Counter private List _listeners; - public GoalCounter(String dataKey, long syncTime, long goal) + public GoalCounter(ConnectionData writeConnection, ConnectionData readConnection, Region region, String dataKey, long goal) { - super(dataKey, syncTime); + super(writeConnection, readConnection, region, dataKey); + init(goal); + } + + public GoalCounter(String dataKey, long goal) + { + super(dataKey); + + init(goal); + } + + private void init(long goal) + { _completed = false; _goal = goal; - _listeners = new ArrayList<>(); + + updateCount(); + _lastMilestone = (int) getFillPercent(); - update(); + updateMilestone(); } /** @@ -78,7 +95,7 @@ public class GoalCounter extends Counter /** * Update {@link #_completed} and notify listeners if it has completed */ - private void update() + private void updateMilestone() { int currentMilestone = (int) getFillPercent(); @@ -95,7 +112,7 @@ public class GoalCounter extends Counter { super.updateCount(newCount); - update(); + updateMilestone(); } }