From d2d44c07bc55491c635f207063024437ca4bc084 Mon Sep 17 00:00:00 2001 From: Ddggdd135 <66861021+JWJUN233233@users.noreply.github.com> Date: Sat, 4 May 2024 01:50:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=83=A8=E5=88=86=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=B9=BD=E7=81=B5=E6=96=B9=E5=9D=97=E4=BA=A7=E7=94=9F=20(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../networks/listeners/BlockListener.java | 35 +++++++++++++++++++ .../networks/managers/ListenerManager.java | 2 ++ .../slimefun/network/NetworkObject.java | 5 --- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/sefiraat/networks/listeners/BlockListener.java diff --git a/src/main/java/io/github/sefiraat/networks/listeners/BlockListener.java b/src/main/java/io/github/sefiraat/networks/listeners/BlockListener.java new file mode 100644 index 00000000..bc481742 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/listeners/BlockListener.java @@ -0,0 +1,35 @@ +package io.github.sefiraat.networks.listeners; + +import io.github.sefiraat.networks.NetworkStorage; +import io.github.sefiraat.networks.network.NodeDefinition; +import io.github.sefiraat.networks.network.NodeType; +import io.github.sefiraat.networks.slimefun.network.NetworkController; +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; + +public class BlockListener implements Listener { + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBlockBreak(BlockBreakEvent e) { + removeNetwork(e.getBlock().getLocation()); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPlace(BlockPlaceEvent e) { + removeNetwork(e.getBlock().getLocation()); + } + + private void removeNetwork(Location location) { + NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(location); + if (definition == null) return; + if (definition.getNode() == null) return; + NetworkStorage.removeNode(location); + + if (definition.getNode().getNodeType() == NodeType.CONTROLLER) { + NetworkController.wipeNetwork(location); + } + } +} diff --git a/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java b/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java index 11b23074..6d48d1ee 100644 --- a/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java +++ b/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java @@ -1,6 +1,7 @@ package io.github.sefiraat.networks.managers; import io.github.sefiraat.networks.Networks; +import io.github.sefiraat.networks.listeners.BlockListener; import io.github.sefiraat.networks.listeners.ExplosiveToolListener; import org.bukkit.event.Listener; @@ -8,6 +9,7 @@ public class ListenerManager { public ListenerManager() { addListener(new ExplosiveToolListener()); + addListener(new BlockListener()); } private void addListener(Listener listener) { diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java index fca8fa99..f6e41735 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java @@ -82,11 +82,6 @@ protected void onBreak(@Nonnull BlockBreakEvent event) { blockMenu.dropItems(location, i); } } - NetworkStorage.removeNode(location); - - if (this.nodeType == NodeType.CONTROLLER) { - NetworkController.wipeNetwork(location); - } Slimefun.getDatabaseManager().getBlockDataController().removeBlock(location); }