Search in sources :

Example 11 with Kit

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Kit in project Nucleus by NucleusPowered.

the class KitListener method onPlayerInteractInventoryClose.

@Listener
public void onPlayerInteractInventoryClose(final InteractInventoryEvent.Close event, @Root final Player player, @Getter("getTargetInventory") final Container inventory) {
    handler.getCurrentlyOpenInventoryCommandKit(inventory).ifPresent(x -> {
        // Set the commands.
        Kit kitInfo = x.getFirst();
        List<String> c = Lists.newArrayList();
        // For each slot, is it a written book?
        x.getSecond().slots().forEach(slot -> slot.poll().ifPresent(item -> {
            if (item.getType().equals(ItemTypes.WRITTEN_BOOK)) {
                item.get(Keys.BOOK_PAGES).ifPresent(y -> c.add(fixup(y)));
            } else if (item.getType().equals(ItemTypes.WRITABLE_BOOK)) {
                item.get(Keys.BOOK_PAGES).ifPresent(page -> c.add(getCommandFromText(page)));
            } else {
                // Drop the item.
                item.get(Keys.ITEM_BLOCKSTATE).ifPresent(z -> {
                    World world = player.getLocation().getExtent();
                    Entity e = world.createEntity(EntityTypes.ITEM, player.getLocation().getPosition());
                    e.offer(Keys.ITEM_BLOCKSTATE, z);
                    world.spawnEntity(e);
                });
            }
            kitInfo.setCommands(c);
            handler.saveKit(kitInfo);
        }));
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.command.edit.success", kitInfo.getName()));
        handler.removeKitCommandInventoryFromListener(inventory);
    });
    handler.removeViewer(inventory);
}
Also used : 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) Entity(org.spongepowered.api.entity.Entity) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 12 with Kit

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Kit in project Nucleus by NucleusPowered.

the class KitArgument method parseValue.

@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
    String kitName = args.next();
    if (kitName.isEmpty()) {
        throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.kit.noname"));
    }
    Kit kit = kitHandler.getKit(kitName).orElseThrow(() -> args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.kit.noexist")));
    if (!checkPermission(source, kit)) {
        throw args.createError(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("args.kit.noperms"));
    }
    return kit;
}
Also used : Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) Nullable(javax.annotation.Nullable)

Example 13 with Kit

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Kit in project Nucleus by NucleusPowered.

the class KitGiveCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Kit kit = args.<Kit>getOne(KIT_PARAMETER).get();
    Player player = args.<Player>getOne(playerKey).get();
    boolean skip = args.hasAny("i");
    if (src instanceof Player && player.getUniqueId().equals(((Player) src).getUniqueId())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.give.self"));
    }
    Text playerName = Nucleus.getNucleus().getNameUtil().getName(player);
    Text kitName = Text.of(kit.getName());
    try {
        NucleusKitService.RedeemResult redeemResult = KIT_HANDLER.redeemKit(kit, player, !skip, this.mustGetAll);
        if (!redeemResult.rejected().isEmpty()) {
            // If we drop them, tell the user
            if (this.isDrop) {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithTextFormat("command.kit.give.itemsdropped", playerName));
                redeemResult.rejected().forEach(x -> Util.dropItemOnFloorAtLocation(x, player.getLocation()));
            } else {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithTextFormat("command.kit.give.fullinventory", playerName));
            }
        }
        src.sendMessage(this.plugin.getMessageProvider().getTextMessageWithTextFormat("command.kit.give.spawned", playerName, kitName));
        if (kit.isDisplayMessageOnRedeem()) {
            player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.spawned", kit.getName()));
        }
        return CommandResult.success();
    } catch (KitRedeemException ex) {
        switch(ex.getReason()) {
            case ALREADY_REDEEMED:
                throw ReturnMessageException.fromKeyText("command.kit.give.onetime.alreadyredeemed", kitName, playerName);
            case COOLDOWN_NOT_EXPIRED:
                KitRedeemException.Cooldown kre = (KitRedeemException.Cooldown) ex;
                throw ReturnMessageException.fromKeyText("command.kit.give.cooldown", playerName, Text.of(Util.getTimeStringFromSeconds(kre.getTimeLeft().getSeconds())), kitName);
            case PRE_EVENT_CANCELLED:
                KitRedeemException.PreCancelled krepe = (KitRedeemException.PreCancelled) ex;
                throw new ReturnMessageException(krepe.getCancelMessage().orElseGet(() -> (Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.kit.cancelledpre", kit.getName()))));
            case NO_SPACE:
                throw ReturnMessageException.fromKeyText("command.kit.give.fullinventorynosave", playerName);
            case UNKNOWN:
            default:
                throw ReturnMessageException.fromKeyText("command.kit.give.fail", playerName, kitName);
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) NucleusKitService(io.github.nucleuspowered.nucleus.api.service.NucleusKitService) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) Text(org.spongepowered.api.text.Text) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)

Example 14 with Kit

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Kit in project Nucleus by NucleusPowered.

the class KitHiddenCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource player, CommandContext args) throws Exception {
    Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    boolean b = args.<Boolean>getOne(toggle).get();
    // This Kit is a reference back to the version in list, so we don't need
    // to update it explicitly
    kitInfo.setHiddenFromList(b);
    KIT_HANDLER.saveKit(kitInfo);
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat(b ? "command.kit.hidden.off" : "command.kit.hidden.on", kitInfo.getName()));
    return CommandResult.success();
}
Also used : Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)

Example 15 with Kit

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Kit 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

Kit (io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)24 Text (org.spongepowered.api.text.Text)7 Player (org.spongepowered.api.entity.living.player.Player)6 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)5 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)4 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)4 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)4 KitFallbackBase (io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase)4 KitHandler (io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler)4 CommandResult (org.spongepowered.api.command.CommandResult)4 CommandContext (org.spongepowered.api.command.args.CommandContext)4 ItemTypes (org.spongepowered.api.item.ItemTypes)4 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)4 Lists (com.google.common.collect.Lists)3 Util (io.github.nucleuspowered.nucleus.Util)3 KitRedeemException (io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)3 KitArgument (io.github.nucleuspowered.nucleus.argumentparsers.KitArgument)3 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)3 KitConfigAdapter (io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter)3