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