Search in sources :

Example 1 with KitUserDataModule

use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.

the class KitResetUsageCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource player, CommandContext args) throws Exception {
    Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    User u = args.<User>getOne(user).get();
    KitUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(u).get(KitUserDataModule.class);
    if (Util.getKeyIgnoreCase(inu.getKitLastUsedTime(), kitInfo.getName()).isPresent()) {
        // Remove the key.
        inu.removeKitLastUsedTime(kitInfo.getName().toLowerCase());
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.resetuser.success", u.getName(), kitInfo.getName()));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.kit.resetuser.empty", u.getName(), kitInfo.getName());
}
Also used : KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) User(org.spongepowered.api.entity.living.player.User) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)

Example 2 with KitUserDataModule

use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.

the class KitHandler method redeemKit.

public RedeemResult redeemKit(Kit kit, Player player, boolean performChecks, boolean isMustGetAll, boolean isFirstJoin) throws KitRedeemException {
    KitUserDataModule user = Nucleus.getNucleus().getUserDataManager().get(player.getUniqueId()).get().get(KitUserDataModule.class);
    Optional<Instant> oi = Util.getValueIgnoreCase(user.getKitLastUsedTime(), kit.getName());
    Instant now = Instant.now();
    if (performChecks) {
        // If the kit was used before...
        if (oi.isPresent()) {
            // if it's one time only and the user does not have an exemption...
            if (kit.isOneTime() && !player.hasPermission(cph.getPermissionWithSuffix("exempt.onetime"))) {
                throw new KitRedeemException("Already redeemed", KitRedeemException.Reason.ALREADY_REDEEMED);
            }
            // bypass it...
            if (!cph.testCooldownExempt(player) && kit.getCooldown().map(Duration::getSeconds).orElse(0L) > 0) {
                // ...and we haven't reached the cooldown point yet...
                Instant timeForNextUse = oi.get().plus(kit.getCooldown().get());
                if (timeForNextUse.isAfter(now)) {
                    throw new KitRedeemException.Cooldown("Cooldown not expired", Duration.between(now, timeForNextUse));
                }
            }
        }
    }
    // Kit pre redeem
    Cause cause = CauseStackHelper.createCause(player);
    NucleusKitEvent.Redeem.Pre preEvent = new KitEvent.PreRedeem(cause, oi.orElse(null), kit, player);
    if (Sponge.getEventManager().post(preEvent)) {
        throw new KitRedeemException.PreCancelled(preEvent.getCancelMessage().orElse(null));
    }
    List<Optional<ItemStackSnapshot>> slotList = Lists.newArrayList();
    Util.getStandardInventory(player).slots().forEach(x -> slotList.add(x.peek().map(ItemStack::createSnapshot)));
    InventoryTransactionResult inventoryTransactionResult = EMPTY_ITR;
    if (!kit.getStacks().isEmpty()) {
        inventoryTransactionResult = addToStandardInventory(player, kit.getStacks(), isProcessTokens);
        if (!isFirstJoin && !inventoryTransactionResult.getRejectedItems().isEmpty() && isMustGetAll) {
            Inventory inventory = Util.getStandardInventory(player);
            // Slots
            Iterator<Inventory> slot = inventory.slots().iterator();
            // Slots to restore
            slotList.forEach(x -> {
                Inventory i = slot.next();
                i.clear();
                x.ifPresent(y -> i.offer(y.createStack()));
            });
            // My friend was playing No Man's Sky, I almost wrote "No free slots in suit inventory".
            throw new KitRedeemException("No free slots in player inventory", KitRedeemException.Reason.NO_SPACE);
        }
    }
    // If something was consumed, consider a success.
    if (inventoryTransactionResult.getType() == InventoryTransactionResult.Type.SUCCESS) {
        kit.redeemKitCommands(player);
        // permissions or cooldowns change later
        if (performChecks) {
            user.addKitLastUsedTime(kit.getName(), now);
        }
        Sponge.getEventManager().post(new KitEvent.PostRedeem(cause, oi.orElse(null), kit, player));
        return new KitRedeemResult(inventoryTransactionResult.getRejectedItems(), slotList.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()));
    } else {
        // Failed.
        Sponge.getEventManager().post(new KitEvent.FailedRedeem(cause, oi.orElse(null), kit, player));
        throw new KitRedeemException("No items were redeemed", KitRedeemException.Reason.UNKNOWN);
    }
}
Also used : KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) Optional(java.util.Optional) Instant(java.time.Instant) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException) Cause(org.spongepowered.api.event.cause.Cause) NucleusKitEvent(io.github.nucleuspowered.nucleus.api.events.NucleusKitEvent) KitEvent(io.github.nucleuspowered.nucleus.modules.kit.events.KitEvent) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 3 with KitUserDataModule

use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.

the class KitListener method onPlayerJoin.

@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event, @Root Player player) {
    loader.get(player).ifPresent(p -> {
        KitUserDataModule user = loader.get(player.getUniqueId()).get().get(KitUserDataModule.class);
        gds.getAutoRedeemable().stream().filter(k -> k.ignoresPermission() || (!this.isSepratePermissions && !player.hasPermission(PermissionRegistry.PERMISSIONS_PREFIX + "kits." + k.getName().toLowerCase()))).forEach(k -> {
            try {
                handler.redeemKit(k, player, true, this.mustGetAll);
            } catch (KitRedeemException e) {
            // player.sendMessage(e.getText());
            }
        });
    });
}
Also used : KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException) Getter(org.spongepowered.api.event.filter.Getter) Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) Lists(com.google.common.collect.Lists) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) EntityTypes(org.spongepowered.api.entity.EntityTypes) Text(org.spongepowered.api.text.Text) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) Exclude(org.spongepowered.api.event.filter.type.Exclude) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) UserDataManager(io.github.nucleuspowered.nucleus.dataservices.loaders.UserDataManager) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Root(org.spongepowered.api.event.filter.cause.Root) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) List(java.util.List) Container(org.spongepowered.api.item.inventory.Container) NucleusFirstJoinEvent(io.github.nucleuspowered.nucleus.api.events.NucleusFirstJoinEvent) World(org.spongepowered.api.world.World) KitService(io.github.nucleuspowered.nucleus.dataservices.KitService) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException) Listener(org.spongepowered.api.event.Listener)

Example 4 with KitUserDataModule

use of io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule in project Nucleus by NucleusPowered.

the class KitListCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    Set<String> kits = KIT_HANDLER.getKitNames();
    if (kits.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.empty"));
        return CommandResult.empty();
    }
    PaginationService paginationService = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    ArrayList<Text> kitText = Lists.newArrayList();
    final KitUserDataModule user = src instanceof Player ? Nucleus.getNucleus().getUserDataManager().getUnchecked(((Player) src).getUniqueId()).get(KitUserDataModule.class) : null;
    final boolean showHidden = kitPermissionHandler.testSuffix(src, "showhidden");
    Stream<String> kitStream = KIT_HANDLER.getKitNames(showHidden).stream();
    if (this.isSeparatePermissions) {
        kitStream = kitStream.filter(kit -> src.hasPermission(KitHandler.getPermissionForKit(kit.toLowerCase())));
    }
    kitStream.forEach(kit -> kitText.add(createKit(src, user, kit, KIT_HANDLER.getKit(kit).get())));
    PaginationList.Builder paginationBuilder = paginationService.builder().contents(kitText).title(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.kits")).padding(Text.of(TextColors.GREEN, "-"));
    paginationBuilder.sendTo(src);
    return CommandResult.success();
}
Also used : KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) Lists(com.google.common.collect.Lists) KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Duration(java.time.Duration) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) CommandResult(org.spongepowered.api.command.CommandResult) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Instant(java.time.Instant) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Stream(java.util.stream.Stream) Player(org.spongepowered.api.entity.living.player.Player) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) Player(org.spongepowered.api.entity.living.player.Player) PaginationList(org.spongepowered.api.service.pagination.PaginationList) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Aggregations

KitUserDataModule (io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule)4 Kit (io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)3 Lists (com.google.common.collect.Lists)2 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)2 KitRedeemException (io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)2 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)2 KitConfigAdapter (io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter)2 KitHandler (io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler)2 Instant (java.time.Instant)2 Player (org.spongepowered.api.entity.living.player.Player)2 Text (org.spongepowered.api.text.Text)2 Util (io.github.nucleuspowered.nucleus.Util)1 NucleusFirstJoinEvent (io.github.nucleuspowered.nucleus.api.events.NucleusFirstJoinEvent)1 NucleusKitEvent (io.github.nucleuspowered.nucleus.api.events.NucleusKitEvent)1 KitService (io.github.nucleuspowered.nucleus.dataservices.KitService)1 UserDataManager (io.github.nucleuspowered.nucleus.dataservices.loaders.UserDataManager)1 CommandPermissionHandler (io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler)1 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 PermissionRegistry (io.github.nucleuspowered.nucleus.internal.PermissionRegistry)1 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)1