use of org.spongepowered.api.world.PortalAgent in project ProjectWorlds by trentech.
the class EventManager method onMoveEntityEventPortal.
@Listener
public void onMoveEntityEventPortal(MoveEntityEvent.Teleport.Portal event, @Getter("getTargetEntity") Player player) {
World from = event.getFromTransform().getExtent();
World to = event.getToTransform().getExtent();
String toName;
if (to.getName().equals("DIM-1")) {
toName = from.getGameRule("netherPortal").get();
if (toName.equals("DIM-1") || toName.equalsIgnoreCase("default")) {
return;
}
} else if (to.getName().equals("DIM1")) {
toName = from.getGameRule("endPortal").get();
if (toName.equals("DIM1") || toName.equalsIgnoreCase("default")) {
return;
}
} else {
return;
}
Optional<World> optionalWorld = Sponge.getServer().getWorld(toName);
if (!optionalWorld.isPresent()) {
return;
}
World world = optionalWorld.get();
Optional<Location<World>> optionalLocation = TeleportManager.getRandomLocation(world, 2000);
Location<World> location;
if (!optionalLocation.isPresent()) {
location = world.getSpawnLocation();
} else {
location = optionalLocation.get();
}
PortalAgent portalAgent = event.getPortalAgent();
optionalLocation = portalAgent.findPortal(location);
if (!optionalLocation.isPresent()) {
optionalLocation = portalAgent.createPortal(location);
if (!optionalLocation.isPresent()) {
event.setCancelled(true);
return;
}
}
location = optionalLocation.get();
Transform<World> transform = new Transform<>(location.getExtent(), location.getPosition());
event.setToTransform(transform);
event.setUsePortalAgent(true);
}
Aggregations