Search in sources :

Example 1 with PowertoolUserDataModule

use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.

the class ListPowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    boolean toggle = inu.isPowertoolToggled();
    Map<String, List<String>> powertools = inu.getPowertools();
    if (powertools.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.none"));
        return CommandResult.success();
    }
    // Generate powertools.
    List<Text> mesl = powertools.entrySet().stream().sorted((a, b) -> a.getKey().compareToIgnoreCase(b.getKey())).map(k -> from(inu, src, k.getKey(), k.getValue())).collect(Collectors.toList());
    // Paginate the tools.
    Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.header", toggle ? "&aenabled" : "&cdisabled")).padding(Text.of(TextColors.YELLOW, "-")).contents(mesl).sendTo(src);
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ClickAction(org.spongepowered.api.text.action.ClickAction) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) List(java.util.List) Text(org.spongepowered.api.text.Text) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 2 with PowertoolUserDataModule

use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.

the class PowertoolListener method onUserInteract.

@Listener
@Exclude(InteractBlockEvent.class)
public void onUserInteract(final InteractEvent event, @Root Player player) {
    // No item in hand or no permission -> no powertool.
    if (!permissionRegistry.testBase(player) || !player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        return;
    }
    // Get the item and the user.
    ItemType item = player.getItemInHand(HandTypes.MAIN_HAND).get().getType();
    PowertoolUserDataModule user;
    try {
        user = Nucleus.getNucleus().getUserDataManager().getUnchecked(player).get(PowertoolUserDataModule.class);
    } catch (Exception e) {
        Nucleus.getNucleus().printStackTraceIfDebugMode(e);
        return;
    }
    // If the powertools are toggled on.
    if (user.isPowertoolToggled()) {
        // Execute all powertools if they exist.
        user.getPowertoolForItem(item).ifPresent(x -> {
            // Cancel the interaction.
            event.setCancelled(true);
            final Player interacting;
            if (event instanceof InteractEntityEvent && ((InteractEntityEvent) event).getTargetEntity() instanceof Player) {
                interacting = (Player) ((InteractEntityEvent) event).getTargetEntity();
            } else {
                interacting = null;
            }
            // Run each command.
            if (interacting == null && x.stream().allMatch(i -> i.contains("{{subject}}"))) {
                player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("powertool.playeronly"));
                return;
            }
            x.forEach(s -> {
                if (s.contains("{{subject}}")) {
                    if (interacting != null) {
                        s = s.replace("{{subject}}", interacting.getName());
                    } else {
                        // Don't execute when no subject is in the way.
                        return;
                    }
                }
                Sponge.getCommandManager().process(player, s);
            });
        });
    }
}
Also used : CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) PowertoolCommand(io.github.nucleuspowered.nucleus.modules.powertool.commands.PowertoolCommand) Sponge(org.spongepowered.api.Sponge) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Root(org.spongepowered.api.event.filter.cause.Root) HandTypes(org.spongepowered.api.data.type.HandTypes) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) InteractEvent(org.spongepowered.api.event.action.InteractEvent) Player(org.spongepowered.api.entity.living.player.Player) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) Exclude(org.spongepowered.api.event.filter.type.Exclude) Listener(org.spongepowered.api.event.Listener) ItemType(org.spongepowered.api.item.ItemType) Player(org.spongepowered.api.entity.living.player.Player) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) ItemType(org.spongepowered.api.item.ItemType) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Listener(org.spongepowered.api.event.Listener) Exclude(org.spongepowered.api.event.filter.type.Exclude)

Example 3 with PowertoolUserDataModule

use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.

the class DeletePowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    Optional<ItemStack> itemStack = src.getItemInHand(HandTypes.MAIN_HAND);
    if (!itemStack.isPresent()) {
        throw ReturnMessageException.fromKey("command.powertool.noitem");
    }
    PowertoolUserDataModule user = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    ItemType item = itemStack.get().getType();
    Optional<List<String>> cmds = user.getPowertoolForItem(item);
    if (cmds.isPresent() && !cmds.get().isEmpty()) {
        user.clearPowertool(item);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.removed", Text.of(item)));
    } else {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.nocmds", Text.of(item)));
    }
    return CommandResult.success();
}
Also used : ItemType(org.spongepowered.api.item.ItemType) List(java.util.List) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 4 with PowertoolUserDataModule

use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.

the class ListPowertoolCommand method from.

private Text from(final PowertoolUserDataModule inu, Player src, String powertool, List<String> commands) {
    Optional<ItemType> oit = Sponge.getRegistry().getType(ItemType.class, powertool);
    MessageProvider mp = plugin.getMessageProvider();
    // Create the click actions.
    ClickAction viewAction = TextActions.executeCallback(pl -> Util.getPaginationBuilder(src).title(mp.getTextMessageWithFormat("command.powertool.ind.header", powertool)).padding(Text.of(TextColors.GREEN, "-")).contents(commands.stream().map(x -> Text.of(TextColors.YELLOW, x)).collect(Collectors.toList())).sendTo(src));
    ClickAction deleteAction = TextActions.executeCallback(pl -> {
        inu.clearPowertool(powertool);
        pl.sendMessage(mp.getTextMessageWithFormat("command.powertool.removed", powertool));
    });
    TextColor tc = oit.map(itemType -> TextColors.YELLOW).orElse(TextColors.GRAY);
    // id - [View] - [Delete]
    return Text.builder().append(Text.of(tc, powertool)).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.view")).color(TextColors.YELLOW).onClick(viewAction).build()).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.delete")).color(TextColors.DARK_RED).onClick(deleteAction).build()).build();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ClickAction(org.spongepowered.api.text.action.ClickAction) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ClickAction(org.spongepowered.api.text.action.ClickAction) ItemType(org.spongepowered.api.item.ItemType) TextColor(org.spongepowered.api.text.format.TextColor)

Example 5 with PowertoolUserDataModule

use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.

the class PowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.powertool.noitem"));
    Optional<String> command = args.getOne(commandKey);
    PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    return command.map(s -> setPowertool(src, inu, itemStack.getType(), s)).orElseGet(() -> viewPowertool(src, inu, itemStack));
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Aggregations

PowertoolUserDataModule (io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)6 ItemType (org.spongepowered.api.item.ItemType)5 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)4 MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)4 List (java.util.List)4 Player (org.spongepowered.api.entity.living.player.Player)4 Util (io.github.nucleuspowered.nucleus.Util)3 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)3 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)3 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)3 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)3 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Sponge (org.spongepowered.api.Sponge)3 CommandResult (org.spongepowered.api.command.CommandResult)3 CommandContext (org.spongepowered.api.command.args.CommandContext)3 Text (org.spongepowered.api.text.Text)3 TextColors (org.spongepowered.api.text.format.TextColors)3 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)3