Add location cache

Signed-off-by: Etzelia <etzelia@hotmail.com>
grief
Etzelia 2020-07-30 20:52:00 -05:00
parent fa9eb9a1d2
commit d070f47d2d
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
1 changed files with 17 additions and 3 deletions

View File

@ -5,7 +5,9 @@ import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -25,6 +27,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class BlockBreakListener implements Listener {
private final MineAlert plugin;
private final HashSet<Location> cache = new HashSet<>();
private final Map<Player, List<BlockEvent>> map = new HashMap<>();
private final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
@ -38,14 +41,16 @@ public class BlockBreakListener implements Listener {
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getType(), true));
if (cache.contains(event.getBlock().getLocation())) return;
queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getType(), event.getBlock().getLocation(), true));
int radius = this.plugin.getConfig().getInt("ore.radius", 3);
for (int x = -radius; x < radius; x++) {
for (int y = -radius; y < radius; y++) {
for (int z = -radius; z < radius; z++) {
if (x == 0 && y == 0 && z == 0) continue;
queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getRelative(x, y, z).getType(), false));
Block block = event.getBlock().getRelative(x, y, z);
queue.add(new BlockEvent(event.getPlayer(), block.getType(), block.getLocation(), false));
}
}
}
@ -55,6 +60,8 @@ public class BlockBreakListener implements Listener {
while (this.plugin.isEnabled()) {
BlockEvent event = this.queue.poll();
if (event != null) {
if (cache.contains(event.getLocation())) continue;
cache.add(event.getLocation());
for (String s: this.plugin.getConfig().getConfigurationSection("ore.blocks").getKeys(false)) {
if (Lang.getMaterialKey(event.getMaterial()).equals(s)) {
addStrike(event);
@ -132,6 +139,7 @@ public class BlockBreakListener implements Listener {
"ore.purge"
);
if (new Date(e.getTime().getTime() + purge).before(now)) {
cache.remove(e.getLocation());
events.remove();
}
}
@ -194,12 +202,14 @@ public class BlockBreakListener implements Listener {
class BlockEvent {
private final Player player;
private final Material material;
private final Location location;
private final Boolean parent;
private final Date time;
BlockEvent(Player player, Material material, Boolean parent) {
BlockEvent(Player player, Material material, Location location, Boolean parent) {
this.player = player;
this.material = material;
this.location = location;
this.parent = parent;
this.time = Calendar.getInstance().getTime();
}
@ -212,6 +222,10 @@ class BlockEvent {
return material;
}
public Location getLocation() {
return location;
}
public Boolean isParent() {
return parent;
}