use of io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService in project Nucleus by NucleusPowered.
the class NicknameService method setNick.
private void setNick(User pl, Cause cause, Text nickname, boolean bypass) throws NicknameException {
String plain = nickname.toPlain().trim();
if (plain.isEmpty()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Does the user exist?
try {
Optional<User> match = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(nickname.toPlain().trim());
// The only person who can use such a name is oneself.
if (match.isPresent() && !match.get().getUniqueId().equals(pl.getUniqueId())) {
// Fail - cannot use another's name.
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nameinuse", plain), NicknameException.Type.NOT_OWN_IGN);
}
} catch (IllegalArgumentException ignored) {
// We allow some other nicknames too.
}
if (!bypass) {
// Giving subject must have the colour permissions and whatnot. Also,
// colour and color are the two spellings we support. (RULE BRITANNIA!)
Optional<Subject> os = cause.first(Subject.class);
if (os.isPresent()) {
stripPermissionless(os.get(), nickname);
}
if (!pattern.matcher(plain).matches()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nopattern", pattern.pattern()), NicknameException.Type.INVALID_PATTERN);
}
int strippedNameLength = plain.length();
// Do a regex remove to check minimum length requirements.
if (strippedNameLength < Math.max(min, 1)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Do a regex remove to check maximum length requirements. Will be at least the minimum length
if (strippedNameLength > Math.max(max, min)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.toolong"), NicknameException.Type.TOO_SHORT);
}
}
// Send an event
Text currentNickname = getNickname(pl).orElse(null);
ChangeNicknameEvent cne = new ChangeNicknameEvent(cause, currentNickname, nickname, pl);
if (Sponge.getEventManager().post(cne)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.eventcancel", pl.getName()), NicknameException.Type.EVENT_CANCELLED);
}
ModularUserService mus = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl);
NicknameUserDataModule nicknameUserDataModule = mus.get(NicknameUserDataModule.class);
nicknameUserDataModule.setNickname(nickname);
mus.set(nicknameUserDataModule);
mus.save();
Text set = nicknameUserDataModule.getNicknameAsText().get();
if (pl.isOnline()) {
pl.getPlayer().get().sendMessage(Text.builder().append(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.success.base")).append(Text.of(" - ", TextColors.RESET, set)).build());
}
}
use of io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService in project Nucleus by NucleusPowered.
the class MsgToggleCommand method executeCommand.
@Override
protected CommandResult executeCommand(Player src, CommandContext args) throws Exception {
ModularUserService mus = this.plugin.getUserDataManager().getUnchecked(src);
boolean flip = args.<Boolean>getOne(toggle).orElseGet(() -> !mus.get(MessageUserDataModule.class).isMsgToggle());
mus.get(MessageUserDataModule.class).setMsgToggle(flip);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.msgtoggle.success." + String.valueOf(flip)));
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService in project Nucleus by NucleusPowered.
the class NameUtil method getName.
/**
* Gets the display name from a {@link User} as Sponge sees it.
*
* @param player The {@link User} to get the data from.
* @return The {@link Text}
*/
public Text getName(User player) {
Preconditions.checkNotNull(player);
TextColor tc = getNameColour(player);
TextStyle ts = getNameStyle(player);
Optional<Text> dname = player.getPlayer().map(x -> x.get(Keys.DISPLAY_NAME).orElse(null));
Text.Builder tb = null;
if (plugin != null && plugin.isModuleLoaded(NicknameModule.ID)) {
Optional<ModularUserService> userService = plugin.getUserDataManager().get(player);
if (userService.isPresent()) {
Optional<Text> n = userService.get().get(NicknameUserDataModule.class).getNicknameWithPrefix();
if (n.isPresent()) {
tb = n.get().toBuilder();
}
}
} else if (dname.isPresent()) {
tb = dname.get().toBuilder();
}
if (tb == null) {
tb = Text.builder(player.getName());
}
tb.onHover(TextActions.showText(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("name.hover.ign", player.getName()))).build();
if (tc != TextColors.NONE && tb.getColor() == TextColors.NONE) {
List<Text> lt = tb.getChildren();
if (lt.isEmpty() || lt.get(0).getColor().equals(TextColors.NONE)) {
tb.color(tc);
}
}
if (!ts.isEmpty()) {
tb.style(ts);
}
return tb.build();
}
use of io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService in project Nucleus by NucleusPowered.
the class ConnectionMessagesListener method onPlayerLogin.
@Listener
public void onPlayerLogin(ClientConnectionEvent.Join joinEvent, @Getter("getTargetEntity") Player pl) {
if (joinEvent.isMessageCancelled() || (cmc.isDisableWithPermission() && pl.hasPermission(this.disablePermission))) {
joinEvent.setMessageCancelled(true);
return;
}
try {
ModularUserService nucleusUser = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl);
Optional<String> lastKnown = nucleusUser.get(CoreUserDataModule.class).getLastKnownName();
if (cmc.isDisplayPriorName() && !cmc.getPriorNameMessage().isEmpty() && !lastKnown.orElseGet(pl::getName).equalsIgnoreCase(pl.getName())) {
// Name change!
joinEvent.getChannel().orElse(MessageChannel.TO_ALL).send(plugin, cmc.getPriorNameMessage().getForCommandSource(pl, ImmutableMap.of("previousname", cs -> Optional.of(Text.of(lastKnown.get()))), Maps.newHashMap()));
}
} catch (Exception e) {
if (Nucleus.getNucleus().isDebugMode()) {
e.printStackTrace();
}
}
if (cmc.isModifyLoginMessage()) {
if (cmc.getLoginMessage().isEmpty()) {
joinEvent.setMessageCancelled(true);
} else {
joinEvent.setMessage(cmc.getLoginMessage().getForCommandSource(pl));
}
}
}
use of io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService in project Nucleus by NucleusPowered.
the class JailHandler method jailPlayer.
public boolean jailPlayer(User user, JailData data) {
ModularUserService modularUserService = plugin.getUserDataManager().getUnchecked(user);
JailUserDataModule jailUserDataModule = modularUserService.get(JailUserDataModule.class);
if (jailUserDataModule.getJailData().isPresent()) {
return false;
}
// Get the jail.
Optional<NamedLocation> owl = getJail(data.getJailName());
NamedLocation wl = owl.filter(x -> x.getLocation().isPresent()).orElseGet(() -> {
if (!getJails().isEmpty()) {
return null;
}
return getJails().entrySet().stream().findFirst().get().getValue();
});
if (wl == null) {
return false;
}
jailUserDataModule.setJailData(data);
if (user.isOnline()) {
Sponge.getScheduler().createSyncExecutor(plugin).execute(() -> {
Player player = user.getPlayer().get();
plugin.getTeleportHandler().teleportPlayer(player, owl.get().getLocation().get(), owl.get().getRotation(), NucleusTeleportHandler.StandardTeleportMode.NO_CHECK, Sponge.getCauseStackManager().getCurrentCause());
modularUserService.get(FlyUserDataModule.class).setFlying(false);
});
} else {
jailUserDataModule.setJailOnNextLogin(true);
}
this.jailDataCache.put(user.getUniqueId(), new Context(NucleusJailService.JAIL_CONTEXT, data.getJailName()));
Sponge.getEventManager().post(new JailEvent.Jailed(user, CauseStackHelper.createCause(Util.getObjectFromUUID(data.getJailerInternal())), data.getJailName(), TextSerializers.FORMATTING_CODE.deserialize(data.getReason()), data.getRemainingTime().orElse(null)));
return true;
}
Aggregations