Skip to content

Commit

Permalink
improve tp down logic in anti-nether-roof
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Feb 7, 2024
1 parent 4c897c3 commit b78a725
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@

public class NetherRoof implements AnarchyExploitFixesModule, Listener {

private static final Iterable<BlockFace> CARDINAL_FACES = Set.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST);
private final AnarchyExploitFixes plugin;
private final Iterable<BlockFace> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,20 +22,17 @@
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<BlockFace> CARDINAL_FACES = Sets.newHashSet(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST);
private final Material AIR, NETHER_PORTAL, NETHERRACK;
private final Iterable<BlockFace> CARDINAL_FACES;
private final boolean safe_teleport_enabled;

public NetherRoof() {
shouldEnable();
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);
Expand Down Expand Up @@ -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))
Expand All @@ -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();
}
}

0 comments on commit b78a725

Please sign in to comment.