Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions src/main/java/de/emilo/instantnetherportals/PortalListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.lang.reflect.Field;
Expand All @@ -24,7 +21,6 @@ public class PortalListener implements Listener {
private boolean reflectionReady = false;
private Field portalProcessField; // Entity -> PortalProcessor
private Field processorTimeField; // PortalProcessor -> int portalTime
private Field portalCooldownField; // Entity -> int portalCooldown
private Method getPortalWaitTimeMethod;
private Method getHandleMethod;

Expand All @@ -46,8 +42,8 @@ public void run() {
if (!config.isEnabled()) return;
for (Player player : plugin.getServer().getOnlinePlayers()) {
if (!player.hasPermission("instantnetherportals.use")) continue;
if (player.getPortalCooldown() > 0) continue;
if (!isInNetherPortal(player)) continue;
if (hasPortalCooldown(player)) continue;
acceleratePortal(player);
}
}
Expand All @@ -62,15 +58,15 @@ public void run() {
private void acceleratePortal(Player player) {
if (!ensureReflection(player)) return;

int maxTime = resolveMaxTime(player);
int delay = config.getTeleportDelay();
if (delay >= maxTime) return;

try {
Object nms = getHandleMethod.invoke(player);
Object pp = portalProcessField.get(nms); // PortalProcessor instance
if (pp == null) return; // Created next tick by the game

int maxTime = resolveMaxTime(nms);
int delay = config.getTeleportDelay();
if (delay >= maxTime) return;

int currentTime = processorTimeField.getInt(pp);
int targetTime = maxTime - delay;

Expand All @@ -83,16 +79,6 @@ private void acceleratePortal(Player player) {
}
}

// -----------------------------------------------------------------------
// Cleanup on successful portal teleport
// -----------------------------------------------------------------------

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent event) {
// portalProcess is set to null by the game after teleportation,
// so no manual cleanup needed – the tick task handles it automatically.
}

// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
Expand All @@ -104,16 +90,6 @@ private boolean isInNetherPortal(Player player) {
|| torso.getType() == Material.NETHER_PORTAL;
}

private boolean hasPortalCooldown(Player player) {
if (portalCooldownField == null || getHandleMethod == null) return false;
try {
Object nms = getHandleMethod.invoke(player);
return portalCooldownField.getInt(nms) > 0;
} catch (Exception e) {
return false;
}
}

// -----------------------------------------------------------------------
// NMS reflection (pure reflection – no NMS imports needed)
// -----------------------------------------------------------------------
Expand All @@ -133,7 +109,6 @@ private boolean ensureReflection(Player player) {
return false;
}

portalCooldownField = getDeclaredFieldSafe(entityClass, "portalCooldown");
getPortalWaitTimeMethod = findMethod(nms.getClass(), "getPortalWaitTime");

// In Paper 1.21.8 portalTime lives inside PortalProcessor
Expand All @@ -158,10 +133,9 @@ private boolean ensureReflection(Player player) {
return processorTimeField != null;
}

private int resolveMaxTime(Player player) {
private int resolveMaxTime(Object nms) {
if (getPortalWaitTimeMethod == null) return VANILLA_WAIT_TIME;
try {
Object nms = getHandleMethod.invoke(player);
int v = (int) getPortalWaitTimeMethod.invoke(nms);
return v > 0 ? v : VANILLA_WAIT_TIME;
} catch (Exception e) {
Expand Down