Search in sources :

Example 16 with MessageChannel

use of org.spongepowered.api.text.channel.MessageChannel in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callDestructEntityEventDeath.

public static DestructEntityEvent.Death callDestructEntityEventDeath(EntityLivingBase entity, DamageSource source) {
    final MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter();
    MessageChannel originalChannel;
    MessageChannel channel;
    Text originalMessage;
    Optional<User> sourceCreator = Optional.empty();
    boolean messageCancelled = false;
    if (entity instanceof EntityPlayerMP) {
        originalChannel = channel = ((IMixinEntityPlayerMP) entity).getDeathMessageChannel();
    } else {
        originalChannel = MessageChannel.TO_NONE;
        channel = MessageChannel.TO_NONE;
    }
    if (source instanceof EntityDamageSource) {
        EntityDamageSource damageSource = (EntityDamageSource) source;
        IMixinEntity spongeEntity = (IMixinEntity) damageSource.getImmediateSource();
        if (spongeEntity != null) {
            sourceCreator = spongeEntity.getCreatorUser();
        }
    }
    originalMessage = SpongeTexts.toText(entity.getCombatTracker().getDeathMessage());
    formatter.getBody().add(new MessageEvent.DefaultBodyApplier(originalMessage));
    final boolean isMainThread = Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
    // checks need to be made here since entities can die on the client world.
    try (final StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
        if (isMainThread) {
            Sponge.getCauseStackManager().pushCause(source);
            if (sourceCreator.isPresent()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, sourceCreator.get());
            }
        }
        final Cause cause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), source);
        DestructEntityEvent.Death event = SpongeEventFactory.createDestructEntityEventDeath(cause, originalChannel, Optional.of(channel), formatter, (Living) entity, entity.world.getGameRules().getBoolean("keepInventory"), messageCancelled);
        SpongeImpl.postEvent(event);
        Text message = event.getMessage();
        if (!event.isMessageCancelled() && !message.isEmpty()) {
            event.getChannel().ifPresent(eventChannel -> eventChannel.send(entity, event.getMessage()));
        }
        return event;
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Text(org.spongepowered.api.text.Text) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) EntityDamageSource(net.minecraft.util.EntityDamageSource) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) Cause(org.spongepowered.api.event.cause.Cause) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP)

Example 17 with MessageChannel

use of org.spongepowered.api.text.channel.MessageChannel in project SpongeForge by SpongePowered.

the class MixinIntegratedServerAnonInner3 method beforeFirePlayerLoggedOut.

/**
 * @author Simon816
 *
 * PlayerQuitEvent must be fired manually just before playerLoggedOut.
 *
 * @see MixinPlayerList#onFirePlayerLoggedOutCall
 */
@ModifyArg(method = "run()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/management/PlayerList;playerLoggedOut(Lnet/minecraft/entity/player/EntityPlayerMP;)V"))
public EntityPlayerMP beforeFirePlayerLoggedOut(EntityPlayerMP playerIn) {
    Player player = (Player) playerIn;
    MessageChannel originalChannel = player.getMessageChannel();
    Sponge.getCauseStackManager().pushCause(player);
    ClientConnectionEvent.Disconnect event = SpongeEventFactory.createClientConnectionEventDisconnect(Sponge.getCauseStackManager().getCurrentCause(), originalChannel, Optional.of(originalChannel), new MessageEvent.MessageFormatter(), player, true);
    SpongeImpl.postEvent(event);
    // are quitting anyway
    return playerIn;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) ModifyArg(org.spongepowered.asm.mixin.injection.ModifyArg)

Example 18 with MessageChannel

use of org.spongepowered.api.text.channel.MessageChannel in project NewHonor-SpongePlugin by euOnmyoji.

the class NewHonor method onClientConnectionJoin.

@Listener
public void onClientConnectionJoin(ClientConnectionEvent.Join event) {
    Player p = event.getTargetEntity();
    PlayerData pd = new PlayerData(p);
    Task.builder().execute(() -> {
        if (!pd.init()) {
            logger.error("初始化玩家" + p.getName() + "," + p.getUniqueId() + ",头衔数据失败!");
        }
        doSomething(pd);
    }).async().delayTicks(20).name("newhonor - init Player" + p.getName()).submit(this);
    MessageChannel originalChannel = event.getOriginalChannel();
    MessageChannel newChannel = MessageChannel.combined(p.getMessageChannel(), originalChannel, mMessage);
    p.setMessageChannel(newChannel);
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) PlayerData(com.github.euonmyoji.newhonor.configuration.PlayerData) Listener(org.spongepowered.api.event.Listener)

Example 19 with MessageChannel

use of org.spongepowered.api.text.channel.MessageChannel in project LanternServer by LanternPowered.

the class NetworkSession method leavePlayer.

/**
 * Is called when the {@link LanternPlayer} leaves the
 * server and needs to be cleaned up.
 */
private void leavePlayer() {
    checkState(this.player != null, "The player must first be available!");
    final LanternWorld world = this.player.getWorld();
    if (world != null) {
        final CauseStack causeStack = CauseStack.current();
        causeStack.pushCause(this.player);
        // Close the open container
        this.player.getContainerSession().setRawOpenContainer(causeStack, null);
        final MessageChannel messageChannel = this.player.getMessageChannel();
        final Text quitMessage = t("multiplayer.player.left", this.player.getName());
        final ClientConnectionEvent.Disconnect event = SpongeEventFactory.createClientConnectionEventDisconnect(causeStack.getCurrentCause(), messageChannel, Optional.of(messageChannel), new MessageEvent.MessageFormatter(quitMessage), this.player, false);
        Sponge.getEventManager().post(event);
        if (!event.isMessageCancelled()) {
            event.getChannel().ifPresent(channel -> channel.send(this.player, event.getMessage()));
        }
        causeStack.popCause();
        // Remove the proxy user from the player and save the player data
        this.player.getProxyUser().setInternalUser(null);
        // Destroy the player entity
        this.player.remove(LanternEntity.RemoveState.DESTROYED);
        // Detach the player from the world
        this.player.setWorld(null);
        // Release the players entity id
        EntityProtocolManager.releaseEntityId(this.player.getNetworkId());
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) LanternWorld(org.lanternpowered.server.world.LanternWorld) Text(org.spongepowered.api.text.Text)

Example 20 with MessageChannel

use of org.spongepowered.api.text.channel.MessageChannel in project Nucleus by NucleusPowered.

the class AFKHandler method setAfk.

public boolean setAfk(Player player, Cause cause, boolean force) {
    if (!player.isOnline()) {
        return false;
    }
    UUID uuid = player.getUniqueId();
    AFKData a = data.compute(uuid, ((u, afkData) -> afkData == null ? new AFKData(u) : afkData));
    if (force) {
        a.isKnownAfk = false;
    } else if (a.isKnownAfk) {
        return false;
    }
    if (a.canGoAfk()) {
        // Don't accident undo setting AFK, remove any activity from the list.
        synchronized (lock) {
            activity.remove(uuid);
        }
        Tuple<Text, MessageChannel> ttmc = getAFKMessage(player, true);
        AFKEvents.To event = new AFKEvents.To(player, ttmc.getFirst(), ttmc.getSecond(), cause);
        Sponge.getEventManager().post(event);
        actionEvent(event, "command.afk.to.vanish");
        a.isKnownAfk = true;
        return true;
    }
    return false;
}
Also used : AFKConfig(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfig) ChatTypes(org.spongepowered.api.text.chat.ChatTypes) Keys(org.spongepowered.api.data.key.Keys) TextRepresentable(org.spongepowered.api.text.TextRepresentable) NoExceptionAutoClosable(io.github.nucleuspowered.nucleus.api.util.NoExceptionAutoClosable) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) Text(org.spongepowered.api.text.Text) Duration(java.time.Duration) Map(java.util.Map) Task(org.spongepowered.api.scheduler.Task) Util(io.github.nucleuspowered.nucleus.Util) NucleusAFKService(io.github.nucleuspowered.nucleus.api.service.NucleusAFKService) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) PluginContainer(org.spongepowered.api.plugin.PluginContainer) AFKConfigAdapter(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfigAdapter) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Tuple(org.spongepowered.api.util.Tuple) UUID(java.util.UUID) Instant(java.time.Instant) GuardedBy(javax.annotation.concurrent.GuardedBy) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AFKCommand(io.github.nucleuspowered.nucleus.modules.afk.commands.AFKCommand) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Player(org.spongepowered.api.entity.living.player.Player) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Text(org.spongepowered.api.text.Text) UUID(java.util.UUID) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents)

Aggregations

MessageChannel (org.spongepowered.api.text.channel.MessageChannel)24 Player (org.spongepowered.api.entity.living.player.Player)17 Text (org.spongepowered.api.text.Text)17 MessageEvent (org.spongepowered.api.event.message.MessageEvent)11 UUID (java.util.UUID)6 ClientConnectionEvent (org.spongepowered.api.event.network.ClientConnectionEvent)5 Instant (java.time.Instant)4 MessageChannelEvent (org.spongepowered.api.event.message.MessageChannelEvent)4 NucleusTextTemplateImpl (io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl)3 Optional (java.util.Optional)3 Sponge (org.spongepowered.api.Sponge)3 User (org.spongepowered.api.entity.living.player.User)3 Listener (org.spongepowered.api.event.Listener)3 Cause (org.spongepowered.api.event.cause.Cause)3 ChatTypes (org.spongepowered.api.text.chat.ChatTypes)3 Preconditions (com.google.common.base.Preconditions)2 HashMultimap (com.google.common.collect.HashMultimap)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Multimap (com.google.common.collect.Multimap)2