Search in sources :

Example 11 with ModularUserService

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());
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) ChangeNicknameEvent(io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent) Text(org.spongepowered.api.text.Text) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) Subject(org.spongepowered.api.service.permission.Subject) UserStorageService(org.spongepowered.api.service.user.UserStorageService) NicknameException(io.github.nucleuspowered.nucleus.api.exceptions.NicknameException) NicknameUserDataModule(io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)

Example 12 with ModularUserService

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();
}
Also used : MessageUserDataModule(io.github.nucleuspowered.nucleus.modules.message.datamodules.MessageUserDataModule) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService)

Example 13 with ModularUserService

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();
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) NicknameUserDataModule(io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)

Example 14 with ModularUserService

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));
        }
    }
}
Also used : ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) CoreUserDataModule(io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule) Listener(org.spongepowered.api.event.Listener) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener)

Example 15 with ModularUserService

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;
}
Also used : JailGeneralDataModule(io.github.nucleuspowered.nucleus.modules.jail.datamodules.JailGeneralDataModule) CoreUserDataModule(io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) NoSuchLocationException(io.github.nucleuspowered.nucleus.api.exceptions.NoSuchLocationException) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Vector3d(com.flowpowered.math.vector.Vector3d) Locatable(org.spongepowered.api.world.Locatable) JailData(io.github.nucleuspowered.nucleus.modules.jail.data.JailData) Map(java.util.Map) FlyUserDataModule(io.github.nucleuspowered.nucleus.modules.fly.datamodules.FlyUserDataModule) NucleusJailService(io.github.nucleuspowered.nucleus.api.service.NucleusJailService) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) Util(io.github.nucleuspowered.nucleus.Util) Subject(org.spongepowered.api.service.permission.Subject) Location(org.spongepowered.api.world.Location) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) JailUserDataModule(io.github.nucleuspowered.nucleus.modules.jail.datamodules.JailUserDataModule) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Collection(java.util.Collection) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) UUID(java.util.UUID) Instant(java.time.Instant) Maps(com.google.common.collect.Maps) JailEvent(io.github.nucleuspowered.nucleus.modules.jail.events.JailEvent) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) Cause(org.spongepowered.api.event.cause.Cause) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) ChronoUnit(java.time.temporal.ChronoUnit) World(org.spongepowered.api.world.World) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Inmate(io.github.nucleuspowered.nucleus.api.nucleusdata.Inmate) Player(org.spongepowered.api.entity.living.player.Player) ModularGeneralService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularGeneralService) FlyUserDataModule(io.github.nucleuspowered.nucleus.modules.fly.datamodules.FlyUserDataModule) Context(org.spongepowered.api.service.context.Context) Player(org.spongepowered.api.entity.living.player.Player) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) JailEvent(io.github.nucleuspowered.nucleus.modules.jail.events.JailEvent) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) JailUserDataModule(io.github.nucleuspowered.nucleus.modules.jail.datamodules.JailUserDataModule)

Aggregations

ModularUserService (io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService)17 CoreUserDataModule (io.github.nucleuspowered.nucleus.modules.core.datamodules.CoreUserDataModule)6 User (org.spongepowered.api.entity.living.player.User)5 Player (org.spongepowered.api.entity.living.player.Player)4 Listener (org.spongepowered.api.event.Listener)4 Text (org.spongepowered.api.text.Text)4 NamedLocation (io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation)3 MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)3 JailData (io.github.nucleuspowered.nucleus.modules.jail.data.JailData)3 JailUserDataModule (io.github.nucleuspowered.nucleus.modules.jail.datamodules.JailUserDataModule)3 Duration (java.time.Duration)3 Instant (java.time.Instant)3 ServiceChangeListener (io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener)2 FlyUserDataModule (io.github.nucleuspowered.nucleus.modules.fly.datamodules.FlyUserDataModule)2 JailEvent (io.github.nucleuspowered.nucleus.modules.jail.events.JailEvent)2 NicknameUserDataModule (io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)2 Optional (java.util.Optional)2 Subject (org.spongepowered.api.service.permission.Subject)2 UserStorageService (org.spongepowered.api.service.user.UserStorageService)2 Location (org.spongepowered.api.world.Location)2