Search in sources :

Example 1 with PortalAgent

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);
}
Also used : PortalAgent(org.spongepowered.api.world.PortalAgent) World(org.spongepowered.api.world.World) Transform(org.spongepowered.api.entity.Transform) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

Transform (org.spongepowered.api.entity.Transform)1 Listener (org.spongepowered.api.event.Listener)1 Location (org.spongepowered.api.world.Location)1 PortalAgent (org.spongepowered.api.world.PortalAgent)1 World (org.spongepowered.api.world.World)1