Search in sources :

Example 1 with GlobalSpawnConfig

use of io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig in project Nucleus by NucleusPowered.

the class SpawnListener method onRespawn.

@Listener(order = Order.EARLY)
public void onRespawn(RespawnPlayerEvent event) {
    if (event.isBedSpawn() && !this.spawnConfig.isRedirectBedSpawn()) {
        // Nope, we don't care.
        return;
    }
    GlobalSpawnConfig sc = spawnConfig.getGlobalSpawn();
    World world = event.getToTransform().getExtent();
    // Get the world.
    if (sc.isOnRespawn()) {
        Optional<WorldProperties> oworld = sc.getWorld();
        if (oworld.isPresent()) {
            world = Sponge.getServer().getWorld(oworld.get().getUniqueId()).orElse(world);
        }
    }
    Location<World> spawn = world.getSpawnLocation().add(0.5, 0, 0.5);
    Transform<World> to = new Transform<>(spawn);
    // Compare current transform to spawn - set rotation.
    Nucleus.getNucleus().getWorldDataManager().getWorld(world).ifPresent(x -> x.get(SpawnWorldDataModule.class).getSpawnRotation().ifPresent(y -> event.setToTransform(to.setRotation(y))));
}
Also used : GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) CoreUserDataModule(io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) Vector3d(com.flowpowered.math.vector.Vector3d) SpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.SpawnConfig) UserStorageService(org.spongepowered.api.service.user.UserStorageService) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) Order(org.spongepowered.api.event.Order) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) SpawnConfigAdapter(io.github.nucleuspowered.nucleus.modules.spawn.config.SpawnConfigAdapter) Location(org.spongepowered.api.world.Location) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) SpawnGeneralDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnGeneralDataModule) Transform(org.spongepowered.api.entity.Transform) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) RespawnPlayerEvent(org.spongepowered.api.event.entity.living.humanoid.player.RespawnPlayerEvent) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) World(org.spongepowered.api.world.World) Transform(org.spongepowered.api.entity.Transform) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Listener(org.spongepowered.api.event.Listener)

Example 2 with GlobalSpawnConfig

use of io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig in project Nucleus by NucleusPowered.

the class SpawnListener method onJoin.

@Listener
public void onJoin(ClientConnectionEvent.Login loginEvent) {
    UUID pl = loginEvent.getProfile().getUniqueId();
    boolean first = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl).get(CoreUserDataModule.class).isStartedFirstJoin();
    try {
        if (first) {
            // first spawn.
            Optional<Transform<World>> ofs = Nucleus.getNucleus().getGeneralService().get(SpawnGeneralDataModule.class).getFirstSpawn();
            // setting the location the player safely. If this cannot be done in either case, send them to world spawn.
            if (ofs.isPresent()) {
                NucleusTeleportHandler.StandardTeleportMode mode = spawnConfig.isSafeTeleport() ? NucleusTeleportHandler.StandardTeleportMode.SAFE_TELEPORT : NucleusTeleportHandler.StandardTeleportMode.WALL_CHECK;
                Optional<Location<World>> location = plugin.getTeleportHandler().getSafeLocation(null, ofs.get().getLocation(), mode);
                if (location.isPresent()) {
                    loginEvent.setToTransform(new Transform<>(location.get().getExtent(), process(location.get().getPosition()), ofs.get().getRotation()));
                    return;
                }
                WorldProperties w = Sponge.getServer().getDefaultWorld().get();
                loginEvent.setToTransform(new Transform<>(Sponge.getServer().getWorld(w.getUniqueId()).get(), w.getSpawnPosition().toDouble().add(0.5, 0, 0.5)));
                // We don't want to boot them elsewhere.
                return;
            }
        }
    } catch (Exception e) {
        if (Nucleus.getNucleus().isDebugMode()) {
            e.printStackTrace();
        }
    }
    // Throw them to the default world spawn if the config suggests so.
    User user = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).getOrCreate(loginEvent.getProfile());
    if (spawnConfig.isSpawnOnLogin() && !user.hasPermission(spawnExempt)) {
        GlobalSpawnConfig sc = spawnConfig.getGlobalSpawn();
        World world = loginEvent.getFromTransform().getExtent();
        if (sc.isOnLogin() && sc.getWorld().isPresent()) {
            world = Sponge.getServer().getWorld(sc.getWorld().get().getUniqueId()).orElse(world);
        }
        Location<World> lw = world.getSpawnLocation().add(0.5, 0, 0.5);
        Optional<Location<World>> safe = plugin.getTeleportHandler().getSafeLocation(null, lw, spawnConfig.isSafeTeleport() ? NucleusTeleportHandler.StandardTeleportMode.SAFE_TELEPORT_ASCENDING : NucleusTeleportHandler.StandardTeleportMode.NO_CHECK);
        if (safe.isPresent()) {
            try {
                Optional<Vector3d> ov = Nucleus.getNucleus().getWorldDataManager().getWorld(world.getUniqueId()).get().get(SpawnWorldDataModule.class).getSpawnRotation();
                if (ov.isPresent()) {
                    loginEvent.setToTransform(new Transform<>(safe.get().getExtent(), process(safe.get().getPosition()), ov.get()));
                    return;
                }
            } catch (Exception e) {
            // 
            }
            loginEvent.setToTransform(new Transform<>(process(safe.get())));
        }
    }
}
Also used : GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) User(org.spongepowered.api.entity.living.player.User) SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) World(org.spongepowered.api.world.World) CoreUserDataModule(io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule) UserStorageService(org.spongepowered.api.service.user.UserStorageService) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) SpawnGeneralDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnGeneralDataModule) Vector3d(com.flowpowered.math.vector.Vector3d) UUID(java.util.UUID) Transform(org.spongepowered.api.entity.Transform) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 3 with GlobalSpawnConfig

use of io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig in project Nucleus by NucleusPowered.

the class SpawnCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    boolean force = args.hasAny("f");
    GlobalSpawnConfig gsc = sc.getGlobalSpawn();
    WorldProperties wp = args.<WorldProperties>getOne(key).orElseGet(() -> gsc.isOnSpawnCommand() ? gsc.getWorld().orElse(src.getWorld().getProperties()) : src.getWorld().getProperties());
    Optional<World> ow = Sponge.getServer().loadWorld(wp.getUniqueId());
    if (!ow.isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.noworld"));
    } else if (sc.isPerWorldPerms() && !permissions.testSuffix(src, "worlds." + ow.get().getName().toLowerCase())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.nopermsworld", ow.get().getName()));
    }
    Transform<World> worldTransform = SpawnHelper.getSpawn(wp, plugin, src);
    SendToSpawnEvent event = new SendToSpawnEvent(worldTransform, src, CauseStackHelper.createCause(src));
    if (Sponge.getEventManager().post(event)) {
        if (event.getCancelReason().isPresent()) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.reason", event.getCancelReason().get()));
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.noreason"));
    }
    // If we don't have a rotation, then use the current rotation
    NucleusTeleportHandler.TeleportResult result = this.plugin.getTeleportHandler().teleportPlayer(src, SpawnHelper.getSpawn(ow.get().getProperties(), this.plugin, src), !force && this.sc.isSafeTeleport());
    if (result.isSuccess()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.success", wp.getWorldName()));
        return CommandResult.success();
    }
    if (result == NucleusTeleportHandler.TeleportResult.FAILED_NO_LOCATION) {
        throw ReturnMessageException.fromKey("command.spawn.fail", wp.getWorldName());
    }
    throw ReturnMessageException.fromKey("command.spawn.cancelled", wp.getWorldName());
}
Also used : GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) SendToSpawnEvent(io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

NucleusTeleportHandler (io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler)3 GlobalSpawnConfig (io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig)3 World (org.spongepowered.api.world.World)3 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 CoreUserDataModule (io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule)2 SpawnGeneralDataModule (io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnGeneralDataModule)2 SpawnWorldDataModule (io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule)2 UUID (java.util.UUID)2 Transform (org.spongepowered.api.entity.Transform)2 User (org.spongepowered.api.entity.living.player.User)2 Listener (org.spongepowered.api.event.Listener)2 UserStorageService (org.spongepowered.api.service.user.UserStorageService)2 Location (org.spongepowered.api.world.Location)2 Maps (com.google.common.collect.Maps)1 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 PermissionRegistry (io.github.nucleuspowered.nucleus.internal.PermissionRegistry)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)1