Add BlockProgressBar for Gem Fountain

This commit is contained in:
Shaun Bennett 2016-04-26 15:26:07 +10:00
parent e8370579c0
commit cabf4c4c8b
4 changed files with 70 additions and 14 deletions

View File

@ -0,0 +1,57 @@
package mineplex.core.brawl.fountain;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
/**
* This class handled filling a vertical area with a specific block based off a percent
*
* @author Shaun Bennett
*/
public class BlockProgressBar
{
// Starting block for the block fill
private final Block _startBlock;
// Direction the blockfill takes place in
private final BlockFace _direction;
// Blocks in order from lowest to highes
private final Block[] _blocks;
// Material used to fill the blocks
private final Material _material;
public BlockProgressBar(Block startBlock, Material material, BlockFace direction)
{
_startBlock = startBlock;
_material = material;
_direction = direction;
// Add blocks to array
int i;
Block curr;
Block[] blocks = new Block[100]; // max of 100 to prevent blocking
for (i = 0, curr = startBlock; (curr.getType() == Material.AIR || curr.getType() == material) && i < blocks.length; i++)
{
blocks[i] = curr;
curr = curr.getRelative(direction);
}
_blocks = new Block[i];
System.arraycopy(blocks, 0, _blocks, 0, i);
}
// Update the blockfill based on fill percent
public void update(double percent)
{
double percentPerBlock = 1D / _blocks.length;
double check = 0;
for (int i = 0; i < _blocks.length; i++)
{
_blocks[i].setType(percent > check ? _material : Material.AIR);
check += percentPerBlock;
}
}
}

View File

@ -7,9 +7,10 @@ import mineplex.core.donation.DonationManager;
import mineplex.core.hologram.Hologram; import mineplex.core.hologram.Hologram;
import mineplex.core.hologram.HologramManager; import mineplex.core.hologram.HologramManager;
import mineplex.core.stats.StatsManager; import mineplex.core.stats.StatsManager;
import mineplex.serverdata.redis.counter.Counter;
import mineplex.serverdata.redis.counter.GoalCounter; import mineplex.serverdata.redis.counter.GoalCounter;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
@ -27,6 +28,7 @@ public class Fountain
private Location _lavaLocation; private Location _lavaLocation;
private final Hologram _hologram; private final Hologram _hologram;
private final GoalCounter _counter; private final GoalCounter _counter;
private final BlockProgressBar _blockProgressBar;
private final FountainShop _shop; private final FountainShop _shop;
@ -43,28 +45,27 @@ public class Fountain
_lavaLocation = lavaLocation; _lavaLocation = lavaLocation;
_hologram = new Hologram(hologramManager, location.clone().add(0, 2, 0), name).start(); _hologram = new Hologram(hologramManager, location.clone().add(0, 2, 0), name).start();
_counter = new GoalCounter(dataKey, 5000, goal); _counter = new GoalCounter(dataKey, 5000, goal);
_blockProgressBar = new BlockProgressBar(_lavaLocation.getBlock(), Material.LAVA, BlockFace.UP);
_shop = new FountainShop(this, fountainManager, clientManager, donationManager); _shop = new FountainShop(this, fountainManager, clientManager, donationManager);
updateHologram(); updateVisuals();
} }
protected void updateHologram() protected void updateVisuals()
{ {
String progressBar = UtilText.getProgress(null, _counter.getFillPercent(), null, false); double percent = _counter.getFillPercent();
String progressBar = UtilText.getProgress(null, percent, null, false);
_hologram.setText(_name, progressBar); _hologram.setText(_name, progressBar);
} _blockProgressBar.update(percent);
protected void updateCount()
{
updateHologram();
} }
public void increment(Player player, long amount) public void increment(Player player, long amount)
{ {
_statsManager.incrementStat(player, "Global.Fountain." + getDataKey(), amount); _statsManager.incrementStat(player, "Global.Fountain." + getDataKey(), amount);
_counter.addAndGet(amount); _counter.addAndGet(amount);
updateHologram(); updateVisuals();
} }
public String getName() public String getName()

View File

@ -53,10 +53,10 @@ public class FountainManager extends MiniPlugin
@EventHandler @EventHandler
public void updateFountainCount(UpdateEvent event) public void updateFountainCount(UpdateEvent event)
{ {
if (event.getType() != UpdateType.SLOW) if (event.getType() != UpdateType.SEC)
return; return;
_gemFountain.updateCount(); _gemFountain.updateVisuals();
} }
public Fountain getGemFountain() public Fountain getGemFountain()

View File

@ -27,8 +27,6 @@ public class Counter
_dataKey = dataKey; _dataKey = dataKey;
_syncTime = syncTime; _syncTime = syncTime;
_redisRepository = new CounterRedisRepository(dataKey); _redisRepository = new CounterRedisRepository(dataKey);
updateCount(_redisRepository.getCount());
} }
public Counter(String dataKey) public Counter(String dataKey)