From 8fdbaa5c38411d49d0f0be92f3bde41c73daa3ac Mon Sep 17 00:00:00 2001 From: Euphyllia Date: Fri, 12 Jul 2024 08:40:55 +0200 Subject: [PATCH] =?UTF-8?q?Portal=20World=20on=20Folia=20=F0=9F=8E=89=20(#?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/hsgamer/morefoworld/WorldUtil.java | 4 +- .../morefoworld/listener/PortalListener.java | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/hsgamer/morefoworld/WorldUtil.java b/src/main/java/me/hsgamer/morefoworld/WorldUtil.java index d3ee128..2a56d8c 100644 --- a/src/main/java/me/hsgamer/morefoworld/WorldUtil.java +++ b/src/main/java/me/hsgamer/morefoworld/WorldUtil.java @@ -193,8 +193,8 @@ public static FeedbackWorld addWorld(WorldCreator creator) { } if (console.options.has("forceUpgrade")) { - net.minecraft.server.Main.convertWorldButItWorks( - actualDimension, worldSession, DataFixers.getDataFixer(), iregistrycustom_dimension, worlddimension.generator().getTypeNameForDataFixer(), console.options.has("eraseCache"), console.options.has("recreateRegionFiles") + net.minecraft.server.Main.forceUpgrade( + worldSession, DataFixers.getDataFixer(), console.options.has("eraseCache"), () -> true, iregistrycustom_dimension, console.options.has("recreateRegionFiles") ); } diff --git a/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java b/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java index a410a54..c19f9f6 100644 --- a/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java +++ b/src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java @@ -2,6 +2,7 @@ import io.github.projectunified.minelib.plugin.base.BasePlugin; import io.github.projectunified.minelib.plugin.listener.ListenerComponent; +import io.papermc.paper.event.entity.EntityInsideBlockEvent; import io.papermc.paper.event.entity.EntityPortalReadyEvent; import me.hsgamer.morefoworld.DebugComponent; import me.hsgamer.morefoworld.config.PortalConfig; @@ -15,9 +16,12 @@ import org.bukkit.event.player.PlayerTeleportEvent; import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public class PortalListener extends ListenerComponent { private DebugComponent debug; + private final ConcurrentHashMap portalTeleportCache = new ConcurrentHashMap<>(); public PortalListener(BasePlugin plugin) { super(plugin); @@ -113,4 +117,66 @@ public void onEntityPortal(EntityPortalEvent event) { } }); } + + @EventHandler(ignoreCancelled = true) + public void onEntityInsidePortal(final EntityInsideBlockEvent event) { + Block block = event.getBlock(); + Entity entity = event.getEntity(); + 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 -> { + + debug.debug("End portal"); + + Optional worldOptional = plugin.get(PortalConfig.class).getWorldFromEndPortal(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)); + } + }); + } + } + portalTeleportCache.remove(entity.getUniqueId()); + }, null, 1L); + } }