Search in sources :

Example 1 with NicknameException

use of io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException in project Almura by AlmuraDev.

the class ServerNickManager method onChangeNickname.

// TODO - Update this for Nucleues' next release to use Post event
@Listener(order = Order.POST)
public void onChangeNickname(final NucleusChangeNicknameEvent.Pre event) {
    final Player player = event.getUser().getPlayer().orElse(null);
    if (player == null) {
        return;
    }
    this.scheduler.createTaskBuilder().delayTicks(// This is a "Pre" event, we want to get the value one tick later
    1).execute(() -> this.serviceManager.provide(NucleusNicknameService.class).ifPresent(service -> {
        final EntityPlayerMP mcPlayer = (EntityPlayerMP) player;
        final String oldNick = mcPlayer.getDisplayNameString();
        final String newNick = TextSerializers.LEGACY_FORMATTING_CODE.serialize(event.getNickname().get());
        if (!oldNick.equals(newNick)) {
            final String modNick = ForgeEventFactory.getPlayerDisplayName(mcPlayer, newNick);
            final Text finalNick = TextSerializers.LEGACY_FORMATTING_CODE.deserialize(modNick);
            if (!newNick.equals(modNick)) {
                try {
                    this.setNickname(service, player, finalNick);
                } catch (NicknameException e) {
                    e.printStackTrace();
                    return;
                }
                this.setForgeNickname(mcPlayer, modNick);
            }
            this.sendNicknameUpdate(service, player);
        }
    })).submit(this.container);
}
Also used : IMixinEntityPlayer(com.almuradev.almura.feature.nick.asm.mixin.iface.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) NucleusNicknameService(io.github.nucleuspowered.nucleus.api.module.nickname.NucleusNicknameService) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Text(org.spongepowered.api.text.Text) NicknameException(io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException) Listener(org.spongepowered.api.event.Listener)

Example 2 with NicknameException

use of io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException in project Almura by AlmuraDev.

the class ServerNickManager method onMoveEntityTeleportPlayer.

@Listener(order = Order.LAST)
public void onMoveEntityTeleportPlayer(final MoveEntityEvent.Teleport event, @Getter("getTargetEntity") final Player player) {
    if (!event.getFromTransform().getExtent().getUniqueId().equals(event.getToTransform().getExtent().getUniqueId())) {
        this.scheduler.createTaskBuilder().delayTicks(20).execute(() -> this.serviceManager.provide(NucleusNicknameService.class).ifPresent(service -> {
            service.getNickname(player).ifPresent(nick -> {
                final EntityPlayerMP mcPlayer = (EntityPlayerMP) player;
                final String oldNick = TextSerializers.LEGACY_FORMATTING_CODE.serialize(nick);
                final String newNick = ForgeEventFactory.getPlayerDisplayName(mcPlayer, oldNick);
                if (!oldNick.equals(newNick)) {
                    try {
                        this.setNickname(service, player, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(newNick));
                    } catch (NicknameException e) {
                        e.printStackTrace();
                        return;
                    }
                    this.setForgeNickname(mcPlayer, newNick);
                }
            });
            this.sendNicknameUpdate(service, player);
        })).submit(this.container);
    }
}
Also used : NucleusNicknameService(io.github.nucleuspowered.nucleus.api.module.nickname.NucleusNicknameService) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) NicknameException(io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException) Listener(org.spongepowered.api.event.Listener)

Example 3 with NicknameException

use of io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException in project Almura by AlmuraDev.

the class ServerboundNucleusNameChangePacketHandler method handleMessage.

@Override
public void handleMessage(final ServerboundNucleusNameChangePacket message, final RemoteConnection connection, final Platform.Type side) {
    if (side.isServer() && connection instanceof PlayerConnection && PacketUtil.checkThreadAndEnqueue((MinecraftServer) Sponge.getServer(), message, this, connection, side)) {
        final Player player = ((PlayerConnection) connection).getPlayer();
        if (message.nickname.isEmpty()) {
            // nullify the nicknamet if the server sends an empty chance packet.
            message.nickname = null;
        }
        final Text nickname = TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message.nickname == null ? player.getName() : message.nickname);
        // Validation checks
        if (message.nickname != null) {
            if (!player.hasPermission("nucleus.nick.base")) {
                // Nucleus Nickname Permission
                this.notificationManager.sendPopupNotification(player, Text.of("Nickname Error!"), Text.of("Insufficient Permissions!"), 5);
                return;
            }
            if (!NickUtil.nickNameRegex.matcher(nickname.toPlain()).matches()) {
                this.notificationManager.sendPopupNotification(player, Text.of("Nickname Error!"), Text.of("Invalid Character in Nickname!"), 5);
            }
            if (nickname.isEmpty() || nickname.toPlain().length() <= 3 || nickname.toPlain().length() > 20) {
                this.notificationManager.sendPopupNotification(player, Text.of("Nickname Error!"), Text.of("Validation of Nickname data failed!"), 5);
                return;
            }
        }
        this.nickManager.setForgeNickname((EntityPlayer) player, message.nickname == null ? player.getName() : message.nickname);
        this.game.getServiceManager().provide(NucleusNicknameService.class).ifPresent((service) -> {
            try {
                if (message.nickname == null || player.getName().equalsIgnoreCase(message.nickname)) {
                    // Detect if player name equals
                    // packet name, if it does, the client is attempting to remove the nickname.
                    this.nickManager.removeNickname(service, player);
                } else {
                    this.nickManager.setNickname(service, player, nickname);
                }
            } catch (NicknameException e) {
                e.printStackTrace();
                return;
            }
            this.nickManager.sendNicknameUpdate(service, player);
        });
        this.game.getServer().getOnlinePlayers().forEach(onlinePlayer -> {
            if (onlinePlayer.getUniqueId().equals(player.getUniqueId())) {
                if (message.nickname == null || player.getName().equalsIgnoreCase(message.nickname)) {
                    this.notificationManager.sendPopupNotification(onlinePlayer, Text.of("Nickname Removed!"), Text.of("You are now known as ", player.getName()), 5);
                } else {
                    this.notificationManager.sendPopupNotification(onlinePlayer, Text.of("New Nickname!"), Text.of("You are now known as ", message.nickname), 5);
                }
            } else {
                if (message.nickname == null || player.getName().equalsIgnoreCase(message.nickname)) {
                    this.notificationManager.sendPopupNotification(onlinePlayer, Text.of("Nickname removed!"), Text.of(player.getName() + " has removed their nickname."), 5);
                } else {
                    this.notificationManager.sendPopupNotification(onlinePlayer, Text.of("New Nickname!"), Text.of(player.getName() + " is now known as ", message.nickname), 5);
                }
            }
        });
    }
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) NucleusNicknameService(io.github.nucleuspowered.nucleus.api.module.nickname.NucleusNicknameService) Text(org.spongepowered.api.text.Text) NicknameException(io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException) PlayerConnection(org.spongepowered.api.network.PlayerConnection) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 4 with NicknameException

use of io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException in project Almura by AlmuraDev.

the class ServerNickManager method onClientConnectionEventJoin.

@Listener
public void onClientConnectionEventJoin(final ClientConnectionEvent.Join event, @Getter("getTargetEntity") final Player player) {
    this.scheduler.createTaskBuilder().delayTicks(20).execute(() -> this.serviceManager.provide(NucleusNicknameService.class).ifPresent(service -> {
        service.getNickname(player).ifPresent(nick -> {
            final EntityPlayerMP mcPlayer = (EntityPlayerMP) player;
            final String oldNick = TextSerializers.LEGACY_FORMATTING_CODE.serialize(nick);
            final String newNick = ForgeEventFactory.getPlayerDisplayName(mcPlayer, oldNick);
            if (!oldNick.equals(newNick)) {
                try {
                    this.setNickname(service, player, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(newNick));
                } catch (NicknameException e) {
                    e.printStackTrace();
                    return;
                }
                this.setForgeNickname(mcPlayer, newNick);
            }
        });
        this.sendNicknameUpdate(service, player);
    })).submit(this.container);
}
Also used : NucleusNicknameService(io.github.nucleuspowered.nucleus.api.module.nickname.NucleusNicknameService) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) NicknameException(io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException) Listener(org.spongepowered.api.event.Listener)

Aggregations

NucleusNicknameService (io.github.nucleuspowered.nucleus.api.module.nickname.NucleusNicknameService)4 NicknameException (io.github.nucleuspowered.nucleus.api.module.nickname.exception.NicknameException)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)3 Listener (org.spongepowered.api.event.Listener)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Player (org.spongepowered.api.entity.living.player.Player)2 Text (org.spongepowered.api.text.Text)2 IMixinEntityPlayer (com.almuradev.almura.feature.nick.asm.mixin.iface.IMixinEntityPlayer)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 PlayerConnection (org.spongepowered.api.network.PlayerConnection)1