Skip to content

Commit

Permalink
reuse method in netherroof
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 2, 2024
1 parent 02ebb73 commit 1781da3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
Expand All @@ -22,7 +22,6 @@
public class NetherRoof implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final int ceilingY;
private final boolean safe_teleport_enabled;

public NetherRoof() {
Expand All @@ -31,7 +30,6 @@ public NetherRoof() {
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);
this.ceilingY = config.nether_ceiling_max_y;
}

@Override
Expand Down Expand Up @@ -59,40 +57,38 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onTeleport(PlayerTeleportEvent event) {
final Location teleportDestination = event.getTo();
if (!teleportDestination.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (teleportDestination.getY() >= ceilingY) {
if (event.getPlayer().hasPermission("anarchyexploitfixes.netherroofbypass")) return;
if (
LocationUtil.isNetherCeiling(event.getTo())
&& !event.getPlayer().hasPermission("anarchyexploitfixes.netherroofbypass")
) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (event.getTo().getY() >= ceilingY || player.getLocation().getY() >= ceilingY) {
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;
teleportFromCeiling(player);
if (
LocationUtil.isNetherCeiling(player.getLocation())
&& !player.hasPermission("anarchyexploitfixes.netherroofbypass")
) {
this.teleportFromCeiling(player);
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
if (!player.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (event.getBlock().getY() > ceilingY) {
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;

if (LocationUtil.isNetherCeiling(event.getBlock().getLocation())) {
event.setCancelled(true);
if (player.getLocation().getY() >= ceilingY) {
teleportFromCeiling(player);
}
}

if (LocationUtil.isNetherCeiling(player.getLocation())) {
this.teleportFromCeiling(player);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import net.kyori.adventure.text.TextReplacementConfig;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
Expand All @@ -15,7 +16,6 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -77,20 +77,15 @@ private void onCreatureSpawn(CreatureSpawnEvent event) {

final Integer disabledRadius = worldsAndTheirRadiuses.get(world);
Location witherLocation = wither.getLocation();
if (!isInDisabledRange(witherLocation, disabledRadius)) return;
if (LocationUtil.getDistanceTo00(witherLocation) > disabledRadius) return;

event.setCancelled(true);

if (playersShouldBeInformed) {
for (Player nearbyPlayer : witherLocation.getNearbyPlayers(8, 8, 8)) {
nearbyPlayer.sendMessage(AnarchyExploitFixes.getLang(nearbyPlayer.locale()).preventions_witherSpawningDisabledInRadius
.replaceText(TextReplacementConfig.builder().matchLiteral("%radius%").replacement(disabledRadius.toString()).build())
);
.replaceText(TextReplacementConfig.builder().matchLiteral("%radius%").replacement(disabledRadius.toString()).build()));
}
}
}

private static boolean isInDisabledRange(Location witherLocation, Integer disabledRadius) {
return new Point(witherLocation.getBlockX(), witherLocation.getBlockZ()).distance(new Point(0, 0)) < disabledRadius;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public static double getXYDistance(Location from, Location to) {

public static boolean isNetherCeiling(Location location) {
return location.getWorld().getEnvironment().equals(World.Environment.NETHER)
&& location.y() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y - 1;
&& location.y() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
Expand All @@ -19,21 +19,17 @@

public class NetherRoof implements AnarchyExploitFixesModule, Listener {

private final int ceilingY;
private final Material AIR, NETHER_PORTAL, NETHERRACK;
private final boolean safe_teleport_enabled;

private final Material air, nether_portal, netherrack;

public NetherRoof() {
shouldEnable();
this.AIR = XMaterial.AIR.parseMaterial();
this.NETHER_PORTAL = XMaterial.NETHER_PORTAL.parseMaterial();
this.NETHERRACK = XMaterial.NETHERRACK.parseMaterial();
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);
this.ceilingY = config.nether_ceiling_max_y;

this.air = XMaterial.AIR.parseMaterial();
this.nether_portal = XMaterial.NETHER_PORTAL.parseMaterial();
this.netherrack = XMaterial.NETHERRACK.parseMaterial();
}

@Override
Expand All @@ -57,38 +53,38 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("preventions.prevent-nether-roof.enable", true);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onTeleport(PlayerTeleportEvent event) {
final Location teleportDestination = event.getTo();
if (!teleportDestination.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (teleportDestination.getY() >= ceilingY) {
if (event.getPlayer().hasPermission("anarchyexploitfixes.netherroofbypass")) return;
if (
LocationUtil.isNetherCeiling(event.getTo())
&& !event.getPlayer().hasPermission("anarchyexploitfixes.netherroofbypass")
) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (event.getTo().getY() >= ceilingY || player.getLocation().getY() >= ceilingY) {
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;
teleportFromCeiling(player);
if (
LocationUtil.isNetherCeiling(player.getLocation())
&& !player.hasPermission("anarchyexploitfixes.netherroofbypass")
) {
this.teleportFromCeiling(player);
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
if (!player.getWorld().getEnvironment().equals(World.Environment.NETHER)) return;

if (event.getBlock().getY() > ceilingY) {
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;
if (player.hasPermission("anarchyexploitfixes.netherroofbypass")) return;

if (LocationUtil.isNetherCeiling(event.getBlock().getLocation())) {
event.setCancelled(true);
teleportFromCeiling(player);
}

if (LocationUtil.isNetherCeiling(player.getLocation())) {
this.teleportFromCeiling(player);
}
}

Expand All @@ -102,33 +98,33 @@ private void teleportFromCeiling(Player player) {
// Check block above for liquid or falling block
Block blockAboveHead = teleportDestination.clone().add(0,2,0).getBlock();
if (blockAboveHead.isLiquid() || blockAboveHead.getType().hasGravity())
blockAboveHead.setType(netherrack, false);
blockAboveHead.setType(NETHERRACK, false);

// Create air pocket for player
Block blockAtPlayerLegs = teleportDestination.getBlock();
if (!blockAtPlayerLegs.getType().equals(air) && !blockAtPlayerLegs.getType().equals(nether_portal))
blockAtPlayerLegs.setType(air, false);
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))
blockAtPlayerTorso.setType(air, false);
if (!blockAtPlayerTorso.getType().equals(AIR) && !blockAtPlayerTorso.getType().equals(NETHER_PORTAL))
blockAtPlayerTorso.setType(AIR, false);

// Check all sides of air pocket for liquids and fill with netherrack
for (int i = 0; i < 2; i++) {
Block airPocketBlock = blockAtPlayerLegs.getRelative(BlockFace.UP, i);
if (airPocketBlock.getRelative(BlockFace.NORTH).isLiquid())
airPocketBlock.getRelative(BlockFace.NORTH).setType(netherrack, false);
airPocketBlock.getRelative(BlockFace.NORTH).setType(NETHERRACK, false);
if (airPocketBlock.getRelative(BlockFace.EAST).isLiquid())
airPocketBlock.getRelative(BlockFace.EAST).setType(netherrack, false);
airPocketBlock.getRelative(BlockFace.EAST).setType(NETHERRACK, false);
if (airPocketBlock.getRelative(BlockFace.SOUTH).isLiquid())
airPocketBlock.getRelative(BlockFace.SOUTH).setType(netherrack, false);
airPocketBlock.getRelative(BlockFace.SOUTH).setType(NETHERRACK, false);
if (airPocketBlock.getRelative(BlockFace.WEST).isLiquid())
airPocketBlock.getRelative(BlockFace.WEST).setType(netherrack, false);
airPocketBlock.getRelative(BlockFace.WEST).setType(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);
blockBelowFeet.setType(NETHERRACK, false);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -13,7 +14,6 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -72,20 +72,15 @@ private void onCreatureSpawn(CreatureSpawnEvent event) {

final Integer disabledRadius = worldsAndTheirRadiuses.get(world);
final Location witherLocation = entity.getLocation();
if (!isInDisabledRange(witherLocation, disabledRadius)) return;
if (LocationUtil.getDistanceTo00(witherLocation) > disabledRadius) return;

event.setCancelled(true);

if (playersShouldBeInformed) {
for (Player nearbyPlayer : witherLocation.getNearbyPlayers(8, 8, 8)) {
nearbyPlayer.sendMessage(AnarchyExploitFixes.getLang(nearbyPlayer.getLocale()).withers_SpawningDisabledInRadius
.replace("%radius%", disabledRadius.toString())
);
.replace("%radius%", disabledRadius.toString()));
}
}
}

private static boolean isInDisabledRange(Location witherLocation, Integer disabledRadius) {
return new Point(witherLocation.getBlockX(), witherLocation.getBlockZ()).distance(new Point(0, 0)) < disabledRadius;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public static double getXYDistance(Location from, Location to) {

public static boolean isNetherCeiling(Location location) {
return location.getWorld().getEnvironment().equals(World.Environment.NETHER)
&& location.getY() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y - 1;
&& location.getY() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y;
}
}

0 comments on commit 1781da3

Please sign in to comment.