Skip to content

Commit

Permalink
Portal World on Folia 🎉 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya authored Jul 12, 2024
1 parent e3376ee commit 8fdbaa5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/me/hsgamer/morefoworld/WorldUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
}

Expand Down
66 changes: 66 additions & 0 deletions src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UUID, Material> portalTeleportCache = new ConcurrentHashMap<>();

public PortalListener(BasePlugin plugin) {
super(plugin);
Expand Down Expand Up @@ -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<World> 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<World> 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);
}
}

0 comments on commit 8fdbaa5

Please sign in to comment.