Search in sources :

Example 1 with Order

use of org.spongepowered.api.event.Order in project SpongeForge by SpongePowered.

the class SpongeModEventManager method post.

// Uses Forge mixins
public boolean post(Event spongeEvent, net.minecraftforge.fml.common.eventhandler.Event forgeEvent, IEventListener[] listeners, boolean useCauseStackManager) {
    checkNotNull(forgeEvent, "forgeEvent");
    if (spongeEvent == null) {
        // Fired by Forge
        spongeEvent = ((IMixinEvent) forgeEvent).createSpongeEvent();
    }
    boolean isNotSameEvent = spongeEvent != forgeEvent;
    RegisteredListener.Cache listenerCache = getHandlerCache(spongeEvent);
    // Fire events to plugins before modifications
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), true, false, useCauseStackManager);
    }
    if (isNotSameEvent) {
        syncToForge(forgeEvent, spongeEvent);
    }
    // If plugin cancelled event before modifications, ignore mods
    if (listeners.length > 0 && !forgeEvent.isCanceled()) {
        for (IEventListener listener : listeners) {
            try {
                if (listener instanceof IMixinASMEventHandler) {
                    IMixinASMEventHandler modListener = (IMixinASMEventHandler) listener;
                    modListener.getTimingsHandler().startTimingIfSync();
                    listener.invoke(forgeEvent);
                    modListener.getTimingsHandler().stopTimingIfSync();
                } else {
                    listener.invoke(forgeEvent);
                }
            } catch (Throwable throwable) {
                this.logger.error("Encountered an exception while processing a Forge event listener", throwable);
            }
        }
        if (isNotSameEvent) {
            syncToSponge(forgeEvent, spongeEvent);
        }
    }
    // Fire events to plugins after modifications (default)
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), false, false, useCauseStackManager);
    }
    if (isNotSameEvent) {
        syncToForge(forgeEvent, spongeEvent);
    }
    return forgeEvent.isCancelable() && forgeEvent.isCanceled();
}
Also used : Order(org.spongepowered.api.event.Order) IEventListener(net.minecraftforge.fml.common.eventhandler.IEventListener) IMixinASMEventHandler(org.spongepowered.mod.interfaces.IMixinASMEventHandler) RegisteredListener(org.spongepowered.common.event.RegisteredListener)

Example 2 with Order

use of org.spongepowered.api.event.Order in project Nucleus by NucleusPowered.

the class CommandSpyListener method onCommand.

@Listener(order = Order.LAST)
public void onCommand(SendCommandEvent event, @Root Player player) {
    if (!player.hasPermission(this.exemptTarget)) {
        boolean isInList = false;
        if (!this.listIsEmpty) {
            String command = event.getCommand().toLowerCase();
            Optional<? extends CommandMapping> oc = Sponge.getCommandManager().get(command, player);
            Set<String> cmd;
            // If the command exists, then get all aliases.
            cmd = oc.map(commandMapping -> commandMapping.getAllAliases().stream().map(String::toLowerCase).collect(Collectors.toSet())).orElseGet(() -> Sets.newHashSet(command));
            isInList = this.config.getCommands().stream().map(String::toLowerCase).anyMatch(cmd::contains);
        }
        // If the command is in the list, report it.
        if (isInList == this.config.isUseWhitelist()) {
            List<Player> playerList = Sponge.getServer().getOnlinePlayers().stream().filter(x -> !x.getUniqueId().equals(player.getUniqueId())).filter(x -> x.hasPermission(this.basePermission)).filter(x -> Nucleus.getNucleus().getUserDataManager().getUnchecked(x).get(CommandSpyUserDataModule.class).isCommandSpy()).collect(Collectors.toList());
            if (!playerList.isEmpty()) {
                Text prefix = this.config.getTemplate().getForCommandSource(player);
                TextParsingUtils.StyleTuple st = TextParsingUtils.getLastColourAndStyle(prefix, null);
                Text messageToSend = prefix.toBuilder().append(Text.of(st.colour, st.style, "/", event.getCommand(), Util.SPACE, event.getArguments())).build();
                playerList.forEach(x -> x.sendMessage(messageToSend));
            }
        }
    }
}
Also used : CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSpyConfig(io.github.nucleuspowered.nucleus.modules.commandspy.config.CommandSpyConfig) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) CommandMapping(org.spongepowered.api.command.CommandMapping) CommandSpyModule(io.github.nucleuspowered.nucleus.modules.commandspy.CommandSpyModule) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Root(org.spongepowered.api.event.filter.cause.Root) List(java.util.List) CommandSpyConfigAdapter(io.github.nucleuspowered.nucleus.modules.commandspy.config.CommandSpyConfigAdapter) Text(org.spongepowered.api.text.Text) Order(org.spongepowered.api.event.Order) Optional(java.util.Optional) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) CommandSpyCommand(io.github.nucleuspowered.nucleus.modules.commandspy.commands.CommandSpyCommand) Util(io.github.nucleuspowered.nucleus.Util) CommandSpyUserDataModule(io.github.nucleuspowered.nucleus.modules.commandspy.datamodules.CommandSpyUserDataModule) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) SendCommandEvent(org.spongepowered.api.event.command.SendCommandEvent) Player(org.spongepowered.api.entity.living.player.Player) CommandSpyUserDataModule(io.github.nucleuspowered.nucleus.modules.commandspy.datamodules.CommandSpyUserDataModule) Text(org.spongepowered.api.text.Text) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) Listener(org.spongepowered.api.event.Listener)

Example 3 with Order

use of org.spongepowered.api.event.Order 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 4 with Order

use of org.spongepowered.api.event.Order in project Almura by AlmuraDev.

the class ServerNickManager method playerMove.

@Listener(order = Order.LAST)
public void playerMove(final MoveEntityEvent.Teleport event, @Getter("getTargetEntity") final Player player) throws IllegalAccessException {
    if (differentExtent(event.getFromTransform(), event.getToTransform())) {
        this.game.getServiceManager().provide(NucleusNicknameService.class).ifPresent((service) -> {
            service.getNickname(player).ifPresent((nick) -> {
                final String oldNick = TextSerializers.LEGACY_FORMATTING_CODE.serialize(nick);
                // Tell the Forge mods
                final String newNick = ForgeEventFactory.getPlayerDisplayName((EntityPlayer) event.getTargetEntity(), TextSerializers.LEGACY_FORMATTING_CODE.serialize(nick));
                if (!oldNick.equals(newNick)) {
                    try {
                        service.setNickname(player, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(newNick));
                    } catch (NicknameException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    displayNameField.set(player, newNick);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            });
            // Send the joining player's nickname to others
            Text nickname = service.getNickname(player).orElse(null);
            if (nickname == null) {
                nickname = Text.of(player.getName());
            } else {
                nickname = Text.of("~" + nickname.toPlain());
            }
            final ClientboundNucleusNameChangeMappingPacket joiningPlayerPacket = this.getMappingMessage(player, nickname);
            this.game.getServer().getOnlinePlayers().stream().filter((players) -> !players.getUniqueId().equals(player.getUniqueId())).forEach((players) -> this.network.sendTo(players, joiningPlayerPacket));
            Task.builder().delayTicks(40).execute(t -> {
                // Send everyone's nicknames to the joining player
                this.network.sendTo(player, this.getMappingMessage(service));
            }).submit(this.container);
        });
    }
}
Also used : Getter(org.spongepowered.api.event.filter.Getter) NucleusChangeNicknameEvent(io.github.nucleuspowered.nucleus.api.events.NucleusChangeNicknameEvent) ForgeEventFactory(net.minecraftforge.event.ForgeEventFactory) Game(org.spongepowered.api.Game) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) NucleusNicknameService(io.github.nucleuspowered.nucleus.api.service.NucleusNicknameService) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) NicknameException(io.github.nucleuspowered.nucleus.api.exceptions.NicknameException) Text(org.spongepowered.api.text.Text) Order(org.spongepowered.api.event.Order) Map(java.util.Map) Task(org.spongepowered.api.scheduler.Task) ClientboundNucleusNameMappingsPacket(com.almuradev.almura.feature.nick.network.ClientboundNucleusNameMappingsPacket) GameState(org.spongepowered.api.GameState) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ClientboundNucleusNameChangeMappingPacket(com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) UUID(java.util.UUID) Field(java.lang.reflect.Field) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) ChannelBinding(org.spongepowered.api.network.ChannelBinding) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) Witness(com.almuradev.core.event.Witness) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Transform(org.spongepowered.api.entity.Transform) World(org.spongepowered.api.world.World) ChannelId(org.spongepowered.api.network.ChannelId) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) Singleton(com.google.inject.Singleton) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) NucleusNicknameService(io.github.nucleuspowered.nucleus.api.service.NucleusNicknameService) ClientboundNucleusNameChangeMappingPacket(com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket) NicknameException(io.github.nucleuspowered.nucleus.api.exceptions.NicknameException) Text(org.spongepowered.api.text.Text) Listener(org.spongepowered.api.event.Listener)

Example 5 with Order

use of org.spongepowered.api.event.Order in project SpongeForge by SpongePowered.

the class SpongeModEventManager method post.

// Uses SpongeForgeEventFactory (required for any events shared in SpongeCommon)
private boolean post(Event spongeEvent, Class<? extends net.minecraftforge.fml.common.eventhandler.Event> clazz, boolean useCauseStackManager) {
    RegisteredListener.Cache listenerCache = getHandlerCache(spongeEvent);
    SpongeForgeEventFactory.handlePrefireLogic(spongeEvent);
    // Fire events to plugins before modifications
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), true, false, useCauseStackManager);
    }
    boolean cancelled = false;
    if (spongeEvent instanceof Cancellable) {
        cancelled = ((Cancellable) spongeEvent).isCancelled();
    }
    if (!cancelled) {
        spongeEvent = SpongeForgeEventFactory.callForgeEvent(spongeEvent, clazz);
    }
    // Fire events to plugins after modifications (default)
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), false, false, useCauseStackManager);
    }
    return spongeEvent instanceof Cancellable && ((Cancellable) spongeEvent).isCancelled();
}
Also used : Order(org.spongepowered.api.event.Order) Cancellable(org.spongepowered.api.event.Cancellable) RegisteredListener(org.spongepowered.common.event.RegisteredListener)

Aggregations

Order (org.spongepowered.api.event.Order)5 Player (org.spongepowered.api.entity.living.player.Player)3 Listener (org.spongepowered.api.event.Listener)3 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)2 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)2 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)2 Map (java.util.Map)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Sponge (org.spongepowered.api.Sponge)2 Transform (org.spongepowered.api.entity.Transform)2 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)2 ClientboundNucleusNameChangeMappingPacket (com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket)1 ClientboundNucleusNameMappingsPacket (com.almuradev.almura.feature.nick.network.ClientboundNucleusNameMappingsPacket)1 NetworkConfig (com.almuradev.almura.shared.network.NetworkConfig)1 Witness (com.almuradev.core.event.Witness)1 Vector3d (com.flowpowered.math.vector.Vector3d)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1