Fixed Vein Tracking (#3)

Use UUID as key for map instead of player

+ Fixes issue where if a player re-logs, they get a new strike list.

Fixed ore vein tracking

+ All datasets are now thread safe
+ Blocks are checked if they are tracked before being put in the queue
+ Only tracked blocks are placed in the cache as well

Co-authored-by: Joey Hines <joey@ahines.net>
Reviewed-on: https://git.etztech.xyz/Minecraft/MineAlert/pulls/3
master
Joey Hines 2020-08-02 22:52:25 +02:00 committed by Etzelia
parent f667672823
commit 117a9c5607
1 changed files with 20 additions and 17 deletions

View File

@ -23,13 +23,14 @@ import xyz.etztech.minealert.MineAlert;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
public class OreAlertListener implements Listener { public class OreAlertListener implements Listener {
private final MineAlert plugin; private final MineAlert plugin;
private static final HashSet<Location> cache = new HashSet<>(); private static final Set<Location> cache = Collections.synchronizedSet(new HashSet<>());
private static final Map<Player, List<BlockEvent>> map = new HashMap<>(); private static final Map<UUID, List<BlockEvent>> map = new ConcurrentHashMap<>();
private static final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>(); private static final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
public OreAlertListener(MineAlert plugin) { public OreAlertListener(MineAlert plugin) {
@ -43,6 +44,8 @@ public class OreAlertListener implements Listener {
@EventHandler @EventHandler
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
if (cache.contains(event.getBlock().getLocation())) return; if (cache.contains(event.getBlock().getLocation())) return;
if (!isMaterialTracked(event.getBlock().getType())) return;
queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getType(), event.getBlock().getLocation(), true)); queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getType(), event.getBlock().getLocation(), true));
int radius = this.plugin.getConfig().getInt("ore.radius", 3); int radius = this.plugin.getConfig().getInt("ore.radius", 3);
@ -51,7 +54,8 @@ public class OreAlertListener implements Listener {
for (int z = -radius; z < radius; z++) { for (int z = -radius; z < radius; z++) {
if (x == 0 && y == 0 && z == 0) continue; if (x == 0 && y == 0 && z == 0) continue;
Block block = event.getBlock().getRelative(x, y, z); Block block = event.getBlock().getRelative(x, y, z);
if (cache.contains(block.getLocation())) return; if (cache.contains(block.getLocation())) continue;
if (!isMaterialTracked(block.getType())) continue;
queue.add(new BlockEvent(event.getPlayer(), block.getType(), block.getLocation(), false)); queue.add(new BlockEvent(event.getPlayer(), block.getType(), block.getLocation(), false));
} }
} }
@ -80,20 +84,19 @@ public class OreAlertListener implements Listener {
if (event != null) { if (event != null) {
if (cache.contains(event.getLocation())) continue; if (cache.contains(event.getLocation())) continue;
cache.add(event.getLocation()); cache.add(event.getLocation());
if (isMaterialTracked(event.getMaterial())) {
addStrike(event);
if (event.isParent()) { if (event.isParent()) {
addStrike(event);
check(event); check(event);
} }
} }
} }
} }
}
private void addStrike(BlockEvent event) { private void addStrike(BlockEvent event) {
List<BlockEvent> events = map.getOrDefault(event.getPlayer(), new ArrayList<>()); List<BlockEvent> events = map.getOrDefault(event.getPlayer().getUniqueId(), new ArrayList<>());
events.add(event); events.add(event);
map.put(event.getPlayer(), events); map.put(event.getPlayer().getUniqueId(), events);
} }
private void check(BlockEvent event) { private void check(BlockEvent event) {
@ -113,10 +116,10 @@ public class OreAlertListener implements Listener {
String.format("ore.blocks.%s.ping", blockKey), String.format("ore.blocks.%s.ping", blockKey),
"ore.ping" "ore.ping"
); );
purge(map.getOrDefault(event.getPlayer(), new ArrayList<>()).iterator()); purge(map.getOrDefault(event.getPlayer().getUniqueId(), new ArrayList<>()).iterator());
int strikes = 0; int strikes = 0;
for (BlockEvent e : map.getOrDefault(event.getPlayer(), new ArrayList<>())) { for (BlockEvent e : map.getOrDefault(event.getPlayer().getUniqueId(), new ArrayList<>())) {
if (e.isParent() && e.getMaterial().name().equals(event.getMaterial().name())) { if (e.isParent() && e.getMaterial().name().equals(event.getMaterial().name())) {
strikes++; strikes++;
} }
@ -135,10 +138,10 @@ public class OreAlertListener implements Listener {
} }
private void cleanup() { private void cleanup() {
for (Iterator<Player> it = map.keySet().iterator(); it.hasNext(); ) { for (Iterator<UUID> it = map.keySet().iterator(); it.hasNext(); ) {
Player player = it.next(); UUID playerUUID = it.next();
purge(map.get(player).iterator()); purge(map.get(playerUUID).iterator());
if (map.get(player).isEmpty()) { if (map.get(playerUUID).isEmpty()) {
it.remove(); it.remove();
} }
} }
@ -213,11 +216,11 @@ public class OreAlertListener implements Listener {
} }
} }
public static HashSet<Location> getCache() { public static Set<Location> getCache() {
return cache; return cache;
} }
public static Map<Player, List<BlockEvent>> getMap() { public static Map<UUID, List<BlockEvent>> getMap() {
return map; return map;
} }