Skip to content

Commit

Permalink
refactor portal workaround
Browse files Browse the repository at this point in the history
only teleport if linked
  • Loading branch information
HSGamer committed Aug 23, 2024
1 parent 96efbce commit e306a48
Showing 1 changed file with 18 additions and 50 deletions.
68 changes: 18 additions & 50 deletions src/main/java/me/hsgamer/morefoworld/listener/PortalListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}

Expand Down Expand Up @@ -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<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 -> {
Optional<World> 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<World> 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);
Expand Down

0 comments on commit e306a48

Please sign in to comment.