Skip to content

Commit

Permalink
get from world if respawn anchors work
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 7, 2024
1 parent 5ce3b51 commit 3b8d3ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.cryptomorin.xseries.XMaterial;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.WorldUtil;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -59,7 +59,7 @@ private void onAnchorBreak(PlayerInteractEvent event) {
final ItemStack interactItem = event.getItem();
if (interactItem == null || interactItem.getType() != GLOWSTONE) return;
final Player player = event.getPlayer();
if (player.getWorld().getEnvironment() == World.Environment.NORMAL) return;
if (WorldUtil.isRespawnAnchorWorks(player.getWorld())) return;

if (breakCooldowns.contains(player.getUniqueId())) {
event.setCancelled(true);
Expand All @@ -74,7 +74,7 @@ private void onAnchorPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
if (event.getBlock().getType() != RESPAWN_ANCHOR) return;
final Player player = event.getPlayer();
if (player.getWorld().getEnvironment() == World.Environment.NORMAL) return;
if (WorldUtil.isRespawnAnchorWorks(player.getWorld())) return;

if (placeCooldowns.contains(player.getUniqueId())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,51 @@
import me.xginko.aef.AnarchyExploitFixes;
import org.bukkit.World;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.invoke.MethodHandle;

public class WorldUtil {

private static Method getMinWorldHeight;
private static final MethodHandle GET_MIN_WORLD_HEIGHT, RESPAWNANCHOR_WORKS;
private static final boolean GET_MIN_WORLD_HEIGHT_AVAILABLE, RESPAWN_ANCHOR_WORKS_AVAILABLE;

static {
GET_MIN_WORLD_HEIGHT_AVAILABLE = Crafty.hasMethod(World.class, "getMinHeight");
GET_MIN_WORLD_HEIGHT = Crafty.findMethod(World.class, "getMinHeight", int.class);
RESPAWN_ANCHOR_WORKS_AVAILABLE = Crafty.hasMethod(World.class, "isRespawnAnchorWorks");
RESPAWNANCHOR_WORKS = Crafty.findMethod(World.class, "isRespawnAnchorWorks", boolean.class);
}

public static int getMinWorldHeight(World world) {
if (!GET_MIN_WORLD_HEIGHT_AVAILABLE) {
return getMinWorldHeightFromConfig(world);
}

try {
getMinWorldHeight = World.class.getMethod("getMinHeight");
} catch (NoSuchMethodException e) {
getMinWorldHeight = null;
return (int) GET_MIN_WORLD_HEIGHT.invoke(world);
} catch (Throwable t) {
AnarchyExploitFixes.prefixedLogger().error("Error getting min world height from world '{}'.", world.getName(), t);
return getMinWorldHeightFromConfig(world);
}
}

public static int getMinWorldHeight(World world) {
if (getMinWorldHeight == null) {
if (world.getEnvironment() == World.Environment.NORMAL) {
return AnarchyExploitFixes.config().overworld_floor_min_y;
} else {
return AnarchyExploitFixes.config().nether_floor_min_y;
}
private static int getMinWorldHeightFromConfig(World world) {
if (world.getEnvironment() == World.Environment.NORMAL) {
return AnarchyExploitFixes.config().overworld_floor_min_y;
} else {
return AnarchyExploitFixes.config().nether_floor_min_y;
}
}

public static boolean isRespawnAnchorWorks(World world) {
if (!RESPAWN_ANCHOR_WORKS_AVAILABLE) {
return world.getEnvironment() == World.Environment.NORMAL;
}

try {
return (int) getMinWorldHeight.invoke(world);
} catch (InvocationTargetException | IllegalAccessException e) {
getMinWorldHeight = null;
return getMinWorldHeight(world);
return (boolean) RESPAWNANCHOR_WORKS.invoke(world);
} catch (Throwable t) {
AnarchyExploitFixes.prefixedLogger().error("Error checking if respawn anchors work in world '{}'.", world.getName(), t);
return world.getEnvironment() == World.Environment.NORMAL;
}
}
}

0 comments on commit 3b8d3ba

Please sign in to comment.