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..91975992 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/listeners/BlockListener.java @@ -0,0 +1,34 @@ +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; + 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); }