From f234cdd73e6fea9b612be7d498637e0ad37e3888 Mon Sep 17 00:00:00 2001 From: ceze88 Date: Fri, 7 Apr 2023 22:43:31 +0200 Subject: [PATCH] Move ChunkLoadEvent to separate ChunkListener class --- .../ultimatestacker/UltimateStacker.java | 1 + .../listeners/BlockListeners.java | 8 ------ .../listeners/ChunkListener.java | 28 +++++++++++++++++++ .../stackable/block/BlockStackManager.java | 3 +- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/songoda/ultimatestacker/listeners/ChunkListener.java diff --git a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java index a92b12e..6cfa4e7 100644 --- a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java +++ b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java @@ -281,6 +281,7 @@ public class UltimateStacker extends SongodaPlugin { } } }); + getServer().getPluginManager().registerEvents(new ChunkListener(this), this); } public void addExp(Player player, EntityStack stack) { diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/BlockListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/BlockListeners.java index 3c6722a..cbf8f51 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/BlockListeners.java @@ -47,14 +47,6 @@ public class BlockListeners implements Listener { this.plugin = plugin; } - @EventHandler - public void onChunkLoad(ChunkLoadEvent event) { - if (!Settings.STACK_BLOCKS.getBoolean()) return; - Chunk chunk = event.getChunk(); - BlockStackManager blockStackManager = plugin.getBlockStackManager(); - blockStackManager.getStacks().stream().filter(stack -> stack.getLocation().getChunk().equals(chunk)).forEach(plugin::updateHologram); - } - @EventHandler(priority = EventPriority.HIGHEST) public void onBlockInteract(PlayerInteractEvent event) { if (event.useInteractedBlock() == Event.Result.DENY) return; diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/ChunkListener.java b/src/main/java/com/songoda/ultimatestacker/listeners/ChunkListener.java new file mode 100644 index 0000000..8866462 --- /dev/null +++ b/src/main/java/com/songoda/ultimatestacker/listeners/ChunkListener.java @@ -0,0 +1,28 @@ +package com.songoda.ultimatestacker.listeners; + +import com.songoda.ultimatestacker.UltimateStacker; +import com.songoda.ultimatestacker.settings.Settings; +import com.songoda.ultimatestacker.stackable.block.BlockStackManager; +import org.bukkit.Chunk; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.ChunkLoadEvent; + +public class ChunkListener implements Listener { + + private final BlockStackManager blockStackManager; + private final UltimateStacker plugin; + + public ChunkListener(UltimateStacker plugin) { + this.blockStackManager = plugin.getBlockStackManager(); + this.plugin = plugin; + } + + @EventHandler + public void onChunkLoad(ChunkLoadEvent event) { + if (!Settings.STACK_BLOCKS.getBoolean()) return; + Chunk chunk = event.getChunk(); + BlockStackManager blockStackManager = plugin.getBlockStackManager(); + blockStackManager.getStacks().stream().filter(stack -> stack.getLocation().getChunk().equals(chunk)).forEach(plugin::updateHologram); + } +} diff --git a/src/main/java/com/songoda/ultimatestacker/stackable/block/BlockStackManager.java b/src/main/java/com/songoda/ultimatestacker/stackable/block/BlockStackManager.java index 136bb63..30474f4 100644 --- a/src/main/java/com/songoda/ultimatestacker/stackable/block/BlockStackManager.java +++ b/src/main/java/com/songoda/ultimatestacker/stackable/block/BlockStackManager.java @@ -8,10 +8,11 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class BlockStackManager { - private final Map registeredBlocks = new HashMap<>(); + private final Map registeredBlocks = new ConcurrentHashMap<>(); public void addBlocks(Map blocks) { this.registeredBlocks.putAll(blocks);