Search in sources :

Example 1 with InteractEvent

use of org.spongepowered.api.event.action.InteractEvent 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)

Aggregations

Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 CommandPermissionHandler (io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler)1 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 PowertoolCommand (io.github.nucleuspowered.nucleus.modules.powertool.commands.PowertoolCommand)1 PowertoolUserDataModule (io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)1 Sponge (org.spongepowered.api.Sponge)1 HandTypes (org.spongepowered.api.data.type.HandTypes)1 Player (org.spongepowered.api.entity.living.player.Player)1 Listener (org.spongepowered.api.event.Listener)1 InteractEvent (org.spongepowered.api.event.action.InteractEvent)1 InteractBlockEvent (org.spongepowered.api.event.block.InteractBlockEvent)1 InteractEntityEvent (org.spongepowered.api.event.entity.InteractEntityEvent)1 Root (org.spongepowered.api.event.filter.cause.Root)1 Exclude (org.spongepowered.api.event.filter.type.Exclude)1 ItemType (org.spongepowered.api.item.ItemType)1