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();
}
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));
}
}
}
}
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))));
}
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);
});
}
}
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();
}
Aggregations