From 25404d6c9288ed560b77d1737735a6bacab79ff3 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Wed, 21 Oct 2020 18:57:54 +0200 Subject: [PATCH] Fix fire extinguishing (only on Spigot servers) Fixes #274 --- .../nl/matsv/viabackwards/BukkitPlugin.java | 8 +++-- .../listener/FireExtinguishListener.java | 36 +++++++++++++++++++ .../listener/LecternInteractListener.java | 6 ++-- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 bukkit/src/main/java/nl/matsv/viabackwards/listener/FireExtinguishListener.java diff --git a/bukkit/src/main/java/nl/matsv/viabackwards/BukkitPlugin.java b/bukkit/src/main/java/nl/matsv/viabackwards/BukkitPlugin.java index 9f7f4091..9895cb2d 100644 --- a/bukkit/src/main/java/nl/matsv/viabackwards/BukkitPlugin.java +++ b/bukkit/src/main/java/nl/matsv/viabackwards/BukkitPlugin.java @@ -11,6 +11,7 @@ package nl.matsv.viabackwards; import nl.matsv.viabackwards.api.ViaBackwardsPlatform; +import nl.matsv.viabackwards.listener.FireExtinguishListener; import nl.matsv.viabackwards.listener.LecternInteractListener; import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.Via; @@ -27,8 +28,11 @@ public class BukkitPlugin extends JavaPlugin implements ViaBackwardsPlatform { } private void onServerLoaded() { - if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_14.getId()) { - BukkitViaLoader loader = (BukkitViaLoader) Via.getManager().getLoader(); + BukkitViaLoader loader = (BukkitViaLoader) Via.getManager().getLoader(); + if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_16.getVersion()) { + loader.storeListener(new FireExtinguishListener(this)).register(); + } + if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_14.getVersion()) { loader.storeListener(new LecternInteractListener(this)).register(); } } diff --git a/bukkit/src/main/java/nl/matsv/viabackwards/listener/FireExtinguishListener.java b/bukkit/src/main/java/nl/matsv/viabackwards/listener/FireExtinguishListener.java new file mode 100644 index 00000000..604dd94f --- /dev/null +++ b/bukkit/src/main/java/nl/matsv/viabackwards/listener/FireExtinguishListener.java @@ -0,0 +1,36 @@ +package nl.matsv.viabackwards.listener; + +import nl.matsv.viabackwards.BukkitPlugin; +import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener; + +public class FireExtinguishListener extends ViaBukkitListener { + + public FireExtinguishListener(BukkitPlugin plugin) { + super(plugin, Protocol1_15_2To1_16.class); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onLecternInteract(PlayerInteractEvent event) { + if (event.getAction() != Action.LEFT_CLICK_BLOCK) return; + + Block block = event.getClickedBlock(); + if (block == null) return; + + Player player = event.getPlayer(); + if (!isOnPipe(player)) return; + + Block relative = block.getRelative(event.getBlockFace()); + if (relative.getType() == Material.FIRE) { + event.setCancelled(true); + relative.setType(Material.AIR); + } + } +} diff --git a/bukkit/src/main/java/nl/matsv/viabackwards/listener/LecternInteractListener.java b/bukkit/src/main/java/nl/matsv/viabackwards/listener/LecternInteractListener.java index 63e4df97..d8b3b7ff 100644 --- a/bukkit/src/main/java/nl/matsv/viabackwards/listener/LecternInteractListener.java +++ b/bukkit/src/main/java/nl/matsv/viabackwards/listener/LecternInteractListener.java @@ -21,12 +21,12 @@ public class LecternInteractListener extends ViaBukkitListener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onLecternInteract(PlayerInteractEvent event) { - Player player = event.getPlayer(); - if (!isOnPipe(player)) return; - Block block = event.getClickedBlock(); if (block == null || block.getType() != Material.LECTERN) return; + Player player = event.getPlayer(); + if (!isOnPipe(player)) return; + Lectern lectern = (Lectern) block.getState(); ItemStack book = lectern.getInventory().getItem(0); if (book == null) return;