org.spongepowered.api.event.item.inventory.ClickInventoryEvent Java Examples

The following examples show how to use org.spongepowered.api.event.item.inventory.ClickInventoryEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Inventory create(String inventoryName, Player player)
{
    UUID uuid = player.getUniqueId();
    if (!inventories.containsKey(uuid))
    {
        try
        {
            EventListener listener = new EventListener(player, inventoryName);
            Inventory chestInventory = Inventory.builder().of(InventoryArchetypes.CHEST).withCarrier(player)
                    .property(InventoryDimension.PROPERTY_NAME, new InventoryDimension(9, this.height))
                    .property(InventoryTitle.PROPERTY_NAME, new InventoryTitle(this.title))
                    .listener(InteractInventoryEvent.Close.class, listener::fireCloseEvent)
                    .listener(InteractInventoryEvent.Open.class, listener::fireOpenEvent)
                    .listener(ClickInventoryEvent.class, listener::fireClickEvent)
                    .build(this.plugin);
            inventories.put(uuid, chestInventory);
            return chestInventory;
        }
        catch (Exception e)
        {
            this.logger.error("There is something wrong with the GUI configuration (" + inventoryName + ")", e);
        }
    }
    return inventories.get(uuid);
}
 
Example #2
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void fireClickEvent(ClickInventoryEvent e)
{
    Optional<Player> optional = Sponge.getServer().getPlayer(playerUniqueId);
    if (optional.isPresent())
    {
        Player player = optional.get();
        CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> Boolean.TRUE, executorService);
        for (SlotTransaction slotTransaction : e.getTransactions())
        {
            Slot slot = slotTransaction.getSlot();
            Container targetContainer = e.getTargetInventory();
            SlotIndex pos = Iterables.getOnlyElement(slot.parent().getProperties(slot, SlotIndex.class));
            if (slot.parent().equals(targetContainer) && slotToListen.matches(pos))
            {
                e.setCancelled(true);
                if (actionIntervalManager.allowAction(player, acceptableActionIntervalTick))
                {
                    int index = Objects.requireNonNull(pos.getValue());
                    future = future.thenCompose(b -> this.runCommand(e, player, index).thenApply(a -> a && b));
                }
            }
        }
        future.thenApply(keepInventoryOpen -> keepInventoryOpen || !plugin.getDispatcher().close(name, player));
    }
}
 
Example #3
Source File: FlagGui.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
private void applyFlag(String flag, ItemStack item, ClickInventoryEvent event) {
    if (this.region.setFlag(RedProtect.get().getVersionHelper().getCause(this.player), flag, !this.region.getFlagBool(flag))) {
        RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("cmdmanager.region.flag.set").replace("{flag}", "'" + flag + "'") + " " + this.region.getFlagBool(flag));

        if (!this.region.getFlagBool(flag)) {
            item.remove(Keys.ITEM_ENCHANTMENTS);
        } else {
            item = RedProtect.get().getVersionHelper().offerEnchantment(item);
        }
        item.offer(Keys.HIDE_ENCHANTMENTS, true);
        item.offer(Keys.HIDE_ATTRIBUTES, true);

        List<Text> lore = new ArrayList<>(Arrays.asList(
                Text.joinWith(Text.of(" "), RedProtect.get().guiLang.getFlagString("value"), RedProtect.get().guiLang.getFlagString(region.getFlags().get(flag).toString())),
                RedProtect.get().getUtil().toText("&0" + flag)));
        lore.addAll(RedProtect.get().guiLang.getFlagDescription(flag));
        item.offer(Keys.ITEM_LORE, lore);

        event.getCursorTransaction().setCustom(ItemStackSnapshot.NONE);
        event.getTransactions().get(0).getSlot().offer(item);

        RedProtect.get().getVersionHelper().removeGuiItem(this.player);

        RedProtect.get().logger.addLog("(World " + this.region.getWorld() + ") Player " + player.getName() + " CHANGED flag " + flag + " of region " + this.region.getName() + " to " + this.region.getFlagString(flag));
    }
}
 
Example #4
Source File: InventoryListener.java    From UltimateCore with MIT License 6 votes vote down vote up
@Listener
@Exclude({ClickInventoryEvent.Close.class, ClickInventoryEvent.Open.class})
public void onInteract(ClickInventoryEvent event, @Root Player p) {
    try {
        //Get target inventory owner
        Inventory inv = event.getTargetInventory();
        if (!(inv instanceof CarriedInventory)) return;
        CarriedInventory cinv = (CarriedInventory) inv;
        if (!cinv.getCarrier().isPresent() || !(cinv.getCarrier().get() instanceof User)) return;
        User t = (User) cinv.getCarrier().get();

        //Check if player is in invsee & Cancel event if player doesn't have permission
        UltimateUser up = UltimateCore.get().getUserService().getUser(p);
        if (up.get(InventoryKeys.INVSEE_TARGET).isPresent() && up.get(InventoryKeys.INVSEE_TARGET).get().equals(t.getUniqueId())) {
            if (!p.hasPermission("uc.inventory.invsee.modify")) {
                event.getCursorTransaction().setValid(false);
                event.setCancelled(true);
            }
        }
    } catch (Exception ignore) {
    }
}
 
Example #5
Source File: HuskyUI.java    From HuskyUI-Plugin with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handle inventory clicks
 * @param event clickinvevent
 */
@Listener(order = Order.PRE)
public void onItemClick(ClickInventoryEvent event){
    try {
        if (event instanceof ClickInventoryEvent.Primary ||
                event instanceof ClickInventoryEvent.Secondary ||
                event instanceof ClickInventoryEvent.Shift ||
                event instanceof ClickInventoryEvent.Creative) {

            ItemStack affected;
            affected = event.getTransactions().get(0).getOriginal().createStack();
            if (event instanceof ClickInventoryEvent.Shift && (affected.getType() == ItemTypes.AIR || affected.getType() == ItemTypes.NONE)) {
                affected = event.getTransactions().get(0).getDefault().createStack();
            }
            Optional<Integer> potentialID = registry.getElementIDFromItemStack(affected);
            if (potentialID.isPresent()) {
                if (registry.elementExists(potentialID.get())) {
                    if (registry.isElementAuto(potentialID.get())) {
                        if (event.getTransactions().get(0).getSlot().parent().getArchetype().equals(InventoryArchetypes.PLAYER)) {
                            if (registry.isElementFixedAuto(potentialID.get())) {
                                event.setCancelled(true);
                            }
                        } else {
                            event.setCancelled(true);
                        }
                    }
                }
            }
        }
    }catch(Exception ignored){}
}
 
Example #6
Source File: Sponge5ArmorListener.java    From ViaVersion with MIT License 5 votes vote down vote up
@Listener
public void onInventoryClick(ClickInventoryEvent e, @Root Player player) {
    for (SlotTransaction transaction : e.getTransactions()) {
        if (ArmorType.isArmor(transaction.getFinal().getType().getId()) ||
                ArmorType.isArmor(e.getCursorTransaction().getFinal().getType().getId())) {
            sendDelayedArmorUpdate(player);
            break;
        }
    }
}
 
Example #7
Source File: Sponge4ArmorListener.java    From ViaVersion with MIT License 5 votes vote down vote up
@Listener
public void onInventoryClick(ClickInventoryEvent e, @Root Player player) {
    for (SlotTransaction transaction : e.getTransactions()) {
        if (ArmorType.isArmor(transaction.getFinal().getType().getId()) ||
                ArmorType.isArmor(e.getCursorTransaction().getFinal().getType().getId())) {
            sendDelayedArmorUpdate(player);
            break;
        }
    }
}
 
Example #8
Source File: FlagGui.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void onInventoryClick(ClickInventoryEvent event) {
    if (event.getTargetInventory().getName().get().equals(this.inv.getName().get())) {

        if (this.editable) {
            return;
        }

        if (event.getTransactions().size() > 0) {
            Transaction<ItemStackSnapshot> clickTransaction = event.getTransactions().get(0);

            ItemStack item = clickTransaction.getOriginal().createStack();

            if (!RedProtect.get().getVersionHelper().getItemType(item).equals(ItemTypes.NONE) && item.get(Keys.ITEM_LORE).isPresent()) {
                String flag = item.get(Keys.ITEM_LORE).get().get(1).toPlain().replace("ยง0", "");
                if (RedProtect.get().config.getDefFlags().contains(flag)) {
                    if (RedProtect.get().config.configRoot().flags_configuration.change_flag_delay.enable) {
                        if (RedProtect.get().config.configRoot().flags_configuration.change_flag_delay.flags.contains(flag)) {
                            if (!RedProtect.get().changeWait.contains(this.region.getName() + flag)) {
                                applyFlag(flag, item, event);
                                RedProtect.get().getUtil().startFlagChanger(this.region.getName(), flag, this.player);
                            } else {
                                RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("gui.needwait.tochange").replace("{seconds}", RedProtect.get().config.configRoot().flags_configuration.change_flag_delay.seconds + ""));
                                event.setCancelled(true);
                            }
                            return;
                        } else {
                            applyFlag(flag, item, event);
                            return;
                        }
                    } else {
                        applyFlag(flag, item, event);
                        return;
                    }
                }
                event.setCancelled(true);
            }
        }
    }
}
 
Example #9
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ClickStatus(ClickInventoryEvent e)
{
    this.isShift = e instanceof ClickInventoryEvent.Shift;
    this.isPrimary = e instanceof ClickInventoryEvent.Primary;
    this.isSecondary = e instanceof ClickInventoryEvent.Secondary;
}
 
Example #10
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 4 votes vote down vote up
private CompletableFuture<Boolean> runCommand(ClickInventoryEvent e, Player player, int slotIndex)
{
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    List<VirtualChestItem> items = VirtualChestInventory.this.items.get(slotIndex);

    int size = items.size();
    List<CompletableFuture<?>> setFutures = new ArrayList<>(size);
    List<Supplier<CompletableFuture<?>>> clearFutures = new ArrayList<>(size);

    try (Timing ignored = VirtualChestTimings.updateAndRefreshMapping(name, slotIndex).startTiming())
    {
        String playerName = player.getName();
        ClickStatus status = new ClickStatus(e);
        String keyString = slotIndexToKey(slotIndex);
        VirtualChestPermissionManager permissionManager = plugin.getPermissionManager();

        logger.debug("Player {} tries to click the chest GUI at {} with {}", playerName, keyString, status);

        for (VirtualChestItem item : items)
        {
            UUID actionUUID = UUID.randomUUID();
            List<String> list = item.getIgnoredPermissions();
            setFutures.add(permissionManager.setIgnored(player, actionUUID, list));
            clearFutures.add(() ->
            {
                if (future.isDone() || !item.matchRequirements(player, slotIndex, playerName))
                {
                    return permissionManager.clearIgnored(player, actionUUID);
                }

                boolean record = recordManager.filter(name, VirtualChestInventory.this);
                Optional<VirtualChestActionDispatcher> optional = item.getAction(status);
                if (record)
                {
                    recordManager.recordSlotClick(actionUUID, name, slotIndex, status, player);
                    logger.debug("Player {} now submits an action: {}", playerName, actionUUID);
                }

                if (!optional.isPresent())
                {
                    future.complete(true);
                    return permissionManager.clearIgnored(player, actionUUID);
                }

                Tuple<Boolean, CompletableFuture<CommandResult>> tuple;
                tuple = optional.get().runCommand(plugin, actionUUID, player, record);

                future.complete(tuple.getFirst());
                return tuple.getSecond().thenCompose(r -> permissionManager.clearIgnored(player, actionUUID));
            });
        }
    }

    CompletableFuture.allOf(setFutures.toArray(new CompletableFuture<?>[0]))
            .thenComposeAsync(v -> CompletableFuture.allOf(clearFutures.stream().map(Supplier::get)
                    .toArray(CompletableFuture[]::new)), executorService).thenRun(() -> future.complete(true));

    return future;
}
 
Example #11
Source File: PreventListener.java    From FlexibleLogin with MIT License 4 votes vote down vote up
@Exclude(NumberPress.class)
@Listener(order = Order.FIRST, beforeModifications = true)
public void onInventoryClick(ClickInventoryEvent clickInventoryEvent, @First Player player) {
    checkLoginStatus(clickInventoryEvent, player);
}