use of io.github.nucleuspowered.nucleus.api.exceptions.NicknameException 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);
});
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NicknameException in project Almura by AlmuraDev.
the class ServerNickManager method onChangeNickname.
@Listener(order = Order.POST)
public void onChangeNickname(final NucleusChangeNicknameEvent event, @Getter("getTargetUser") final Player player) throws IllegalAccessException {
final EntityPlayerMP mcPlayer = (EntityPlayerMP) player;
final String oldNick = mcPlayer.getDisplayNameString();
final String newNick = TextSerializers.LEGACY_FORMATTING_CODE.serialize(event.getNewNickname());
if (!oldNick.equals(newNick)) {
// Tell the Forge mods
final String modNick = ForgeEventFactory.getPlayerDisplayName(mcPlayer, newNick);
final Text finalNick = TextSerializers.LEGACY_FORMATTING_CODE.deserialize(modNick);
if (!newNick.equals(modNick)) {
// Mod says our nick needs to be something else
this.game.getServiceManager().provide(NucleusNicknameService.class).ifPresent((service) -> {
try {
service.setNickname(player, finalNick);
} catch (NicknameException e) {
e.printStackTrace();
}
});
}
displayNameField.set(mcPlayer, modNick);
// Tell everyone about the new nick
this.network.sendToAll(this.getMappingMessage(player, Text.of("~" + finalNick.toPlain())));
}
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NicknameException 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);
});
}
}
Aggregations