From e306a4826c1800b324010922b3e92d85fa4abcb0 Mon Sep 17 00:00:00 2001 From: hsgamer Date: Fri, 23 Aug 2024 19:04:39 +0700 Subject: [PATCH] refactor portal workaround only teleport if linked --- .../morefoworld/listener/PortalListener.java | 68 +++++-------------- 1 file changed, 18 insertions(+), 50 deletions(-) diff --git a/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java b/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java index c19f9f6..8880911 100644 --- a/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java +++ b/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java @@ -78,14 +78,6 @@ public void onPlayerPortal(PlayerPortalEvent event) { worldOptional.ifPresent(world -> { Location clone = to.clone(); clone.setWorld(world); - - event.setCancelled(true); - if (world.getEnvironment() == World.Environment.THE_END) { - teleportToEnd(event.getPlayer(), clone); - debug.debug("Teleport to " + clone); - } else { - event.getPlayer().teleportAsync(clone).thenRun(() -> debug.debug("Teleported to " + clone)); - } }); } @@ -125,56 +117,32 @@ public void onEntityInsidePortal(final EntityInsideBlockEvent event) { Material blockTypeInside = block.getType(); Location from = entity.getLocation(); - if (!blockTypeInside.equals(Material.NETHER_PORTAL) && !blockTypeInside.equals(Material.END_PORTAL)) { - return; - } - - debug.debug("Preparing for teleportation..."); - - event.setCancelled(true); - if (portalTeleportCache.containsKey(entity.getUniqueId())) { debug.debug("The entity is being teleported"); return; } - portalTeleportCache.put(entity.getUniqueId(), blockTypeInside); - entity.getScheduler().execute(plugin, () -> { - switch (blockTypeInside) { - case NETHER_PORTAL -> { - - debug.debug("Nether portal"); - - Optional worldOptional = plugin.get(PortalConfig.class).getWorldFromNetherPortal(from.getWorld()); - - worldOptional.ifPresent(world -> { - Location clone = from.clone(); - clone.setWorld(world); - if (world.getEnvironment() == World.Environment.THE_END) { - teleportToEnd(event.getEntity(), clone); - debug.debug("Teleport to " + clone); - } else { - event.getEntity().teleportAsync(clone).thenRun(() -> debug.debug("Teleported to " + clone)); - } - }); - } - case END_PORTAL -> { + Optional toWorldOptional = switch (blockTypeInside) { + case NETHER_PORTAL -> plugin.get(PortalConfig.class).getWorldFromNetherPortal(from.getWorld()); + case END_PORTAL -> plugin.get(PortalConfig.class).getWorldFromEndPortal(from.getWorld()); + default -> Optional.empty(); + }; + if (toWorldOptional.isEmpty()) return; + World toWorld = toWorldOptional.get(); - debug.debug("End portal"); + portalTeleportCache.put(entity.getUniqueId(), blockTypeInside); - Optional worldOptional = plugin.get(PortalConfig.class).getWorldFromEndPortal(from.getWorld()); + event.setCancelled(true); - worldOptional.ifPresent(world -> { - Location clone = from.clone(); - clone.setWorld(world); - if (world.getEnvironment() == World.Environment.THE_END) { - teleportToEnd(event.getEntity(), clone); - debug.debug("Teleport to " + clone); - } else { - event.getEntity().teleportAsync(clone).thenRun(() -> debug.debug("Teleported to " + clone)); - } - }); - } + entity.getScheduler().execute(plugin, () -> { + if (toWorld.getEnvironment() == World.Environment.THE_END) { + teleportToEnd(entity, new Location(toWorld, 0.5, 64, 0.5)); + } else if (toWorld.getEnvironment() == World.Environment.NETHER) { + Location to = new Location(toWorld, from.getX() / 8, from.getY(), from.getZ() / 8); + entity.teleportAsync(to).thenRun(() -> debug.debug("Teleported to " + to)); + } else { + Location to = new Location(toWorld, from.getX(), from.getY(), from.getZ()); + entity.teleportAsync(to).thenRun(() -> debug.debug("Teleported to " + to)); } portalTeleportCache.remove(entity.getUniqueId()); }, null, 1L);