From b78a7258edc4167f88a8d740fbb2579b7748ecfa Mon Sep 17 00:00:00 2001 From: xGinko Date: Wed, 7 Feb 2024 12:58:46 +0100 Subject: [PATCH] improve tp down logic in anti-nether-roof --- .../modules/preventions/NetherRoof.java | 20 +++++++++------- .../modules/preventions/NetherRoof.java | 24 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java index 75fc99a0b..930644f9c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java @@ -25,14 +25,13 @@ public class NetherRoof implements AnarchyExploitFixesModule, Listener { + private static final Iterable CARDINAL_FACES = Set.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST); private final AnarchyExploitFixes plugin; - private final Iterable CARDINAL_FACES; private final boolean safe_teleport_enabled; public NetherRoof() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); - this.CARDINAL_FACES = Set.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("preventions.prevent-nether-roof.enable", "Prevent players from going above the nether roof."); this.safe_teleport_enabled = config.getBoolean("preventions.prevent-nether-roof.safely-teleport-players", true); @@ -131,10 +130,10 @@ private void teleportFromCeiling(Player player) { player.getScheduler().run(plugin, safeTeleport -> { // Check block above for liquid or falling block Block blockAboveHead = teleportDestination.clone().add(0, 2, 0).getBlock(); - if (blockAboveHead.getType().hasGravity() || (!blockAboveHead.isSolid() && !blockAboveHead.getType().equals(Material.NETHER_PORTAL))) + if (isUnsafe(blockAboveHead) && !blockAboveHead.getType().equals(Material.NETHER_PORTAL)) blockAboveHead.setType(Material.NETHERRACK, false); - // Create air pocket for player + // Create an air pocket for the player Block blockAtPlayerLegs = teleportDestination.getBlock(); if (!blockAtPlayerLegs.getType().equals(Material.AIR) && !blockAtPlayerLegs.getType().equals(Material.NETHER_PORTAL)) blockAtPlayerLegs.setType(Material.AIR, false); @@ -146,17 +145,20 @@ private void teleportFromCeiling(Player player) { for (int i = 0; i < 2; i++) { Block airPocketBlock = blockAtPlayerLegs.getRelative(BlockFace.UP, i); for (BlockFace face : CARDINAL_FACES) { - if (airPocketBlock.getRelative(face).isLiquid()) - airPocketBlock.getRelative(face).setType(Material.NETHERRACK, false); + Block around = airPocketBlock.getRelative(face); + if (isUnsafe(around)) around.setType(Material.NETHERRACK, false); } } // Create block below feet if not solid Block blockBelowFeet = blockAtPlayerLegs.getRelative(BlockFace.DOWN); - if (!blockBelowFeet.isSolid() && !blockBelowFeet.getType().equals(Material.NETHER_PORTAL)) - blockBelowFeet.setType(Material.NETHERRACK, false); + if (isUnsafe(blockBelowFeet) || blockBelowFeet.getType().equals(Material.NETHER_PORTAL)) + blockBelowFeet.setType(Material.NETHERRACK, true); }, null); }); } -} + private static boolean isUnsafe(Block block) { + return block.isLiquid() || block.getType().hasGravity() || !block.getType().isSolid(); + } +} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java index e402d4ab7..d9df6567f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/NetherRoof.java @@ -1,6 +1,7 @@ package me.moomoo.anarchyexploitfixes.modules.preventions; import com.cryptomorin.xseries.XMaterial; +import com.google.common.collect.Sets; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; @@ -21,12 +22,10 @@ import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.vehicle.VehicleMoveEvent; -import java.util.Arrays; - public class NetherRoof implements AnarchyExploitFixesModule, Listener { + private static final Iterable CARDINAL_FACES = Sets.newHashSet(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST); private final Material AIR, NETHER_PORTAL, NETHERRACK; - private final Iterable CARDINAL_FACES; private final boolean safe_teleport_enabled; public NetherRoof() { @@ -34,7 +33,6 @@ public NetherRoof() { this.AIR = XMaterial.AIR.parseMaterial(); this.NETHER_PORTAL = XMaterial.NETHER_PORTAL.parseMaterial(); this.NETHERRACK = XMaterial.NETHERRACK.parseMaterial(); - this.CARDINAL_FACES = Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("preventions.prevent-nether-roof.enable", "Prevent players from going above the nether roof."); this.safe_teleport_enabled = config.getBoolean("preventions.prevent-nether-roof.safely-teleport-players", true); @@ -123,12 +121,12 @@ private void teleportFromCeiling(Player player) { if (!safe_teleport_enabled) return; // Check block above for liquid or falling block Block blockAboveHead = teleportDestination.clone().add(0,2,0).getBlock(); - if (blockAboveHead.isLiquid() || blockAboveHead.getType().hasGravity()) + if (isUnsafe(blockAboveHead)) blockAboveHead.setType(NETHERRACK, false); - // Create air pocket for player + // Create an air pocket for the player Block blockAtPlayerLegs = teleportDestination.getBlock(); - if (!blockAtPlayerLegs.getType().equals(AIR) && blockAtPlayerLegs.getType().equals(NETHER_PORTAL)) + if (!blockAtPlayerLegs.getType().equals(AIR) && !blockAtPlayerLegs.getType().equals(NETHER_PORTAL)) blockAtPlayerLegs.setType(AIR, false); Block blockAtPlayerTorso = blockAtPlayerLegs.getRelative(BlockFace.UP); if (!blockAtPlayerTorso.getType().equals(AIR) && !blockAtPlayerTorso.getType().equals(NETHER_PORTAL)) @@ -138,15 +136,19 @@ private void teleportFromCeiling(Player player) { for (int i = 0; i < 2; i++) { Block airPocketBlock = blockAtPlayerLegs.getRelative(BlockFace.UP, i); for (BlockFace face : CARDINAL_FACES) { - if (airPocketBlock.getRelative(face).isLiquid()) - airPocketBlock.getRelative(face).setType(Material.NETHERRACK, false); + Block around = airPocketBlock.getRelative(face); + if (isUnsafe(around)) around.setType(Material.NETHERRACK, false); } } // Create block below feet if not solid Block blockBelowFeet = blockAtPlayerLegs.getRelative(BlockFace.DOWN); - if (blockBelowFeet.isLiquid() || blockBelowFeet.getType().hasGravity()) - blockBelowFeet.setType(NETHERRACK, false); + if (isUnsafe(blockBelowFeet) || blockBelowFeet.getType().equals(NETHER_PORTAL)) + blockBelowFeet.setType(NETHERRACK, true); + } + + private static boolean isUnsafe(Block block) { + return block.isLiquid() || block.getType().hasGravity() || !block.getType().isSolid(); } }