Search in sources :

Example 1 with ClientboundNucleusNameChangeMappingPacket

use of com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket in project Almura by AlmuraDev.

the class ServerNickManager method onClientConnectionEventJoin.

@Listener
public void onClientConnectionEventJoin(final ClientConnectionEvent.Join event) {
    this.game.getServiceManager().provide(NucleusNicknameService.class).ifPresent((service) -> {
        service.getNickname(event.getTargetEntity()).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(event.getTargetEntity(), TextSerializers.LEGACY_FORMATTING_CODE.deserialize(newNick));
                } catch (NicknameException e) {
                    e.printStackTrace();
                }
            }
            try {
                displayNameField.set(event.getTargetEntity(), newNick);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        // Send the joining player's nickname to others
        Text nickname = service.getNickname(event.getTargetEntity()).orElse(null);
        if (nickname == null) {
            nickname = Text.of(event.getTargetEntity().getName());
        } else {
            nickname = Text.of("~" + nickname.toPlain());
        }
        final ClientboundNucleusNameChangeMappingPacket joiningPlayerPacket = this.getMappingMessage(event.getTargetEntity(), nickname);
        this.game.getServer().getOnlinePlayers().stream().filter((player) -> !player.getUniqueId().equals(event.getTargetEntity().getUniqueId())).forEach((player) -> this.network.sendTo(player, joiningPlayerPacket));
        Task.builder().delayTicks(40).execute(t -> {
            // Send everyone's nicknames to the joining player
            this.network.sendTo(event.getTargetEntity(), 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 2 with ClientboundNucleusNameChangeMappingPacket

use of com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket 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)

Aggregations

ClientboundNucleusNameChangeMappingPacket (com.almuradev.almura.feature.nick.network.ClientboundNucleusNameChangeMappingPacket)2 ClientboundNucleusNameMappingsPacket (com.almuradev.almura.feature.nick.network.ClientboundNucleusNameMappingsPacket)2 NetworkConfig (com.almuradev.almura.shared.network.NetworkConfig)2 Witness (com.almuradev.core.event.Witness)2 Inject (com.google.inject.Inject)2 Singleton (com.google.inject.Singleton)2 NucleusChangeNicknameEvent (io.github.nucleuspowered.nucleus.api.events.NucleusChangeNicknameEvent)2 NicknameException (io.github.nucleuspowered.nucleus.api.exceptions.NicknameException)2 NucleusNicknameService (io.github.nucleuspowered.nucleus.api.service.NucleusNicknameService)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 ForgeEventFactory (net.minecraftforge.event.ForgeEventFactory)2 Game (org.spongepowered.api.Game)2 GameState (org.spongepowered.api.GameState)2 Transform (org.spongepowered.api.entity.Transform)2 Player (org.spongepowered.api.entity.living.player.Player)2