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

The following examples show how to use org.spongepowered.api.event.item.inventory.InteractInventoryEvent. 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: PlayerEventHandler.java    From GriefDefender with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryClose(InteractInventoryEvent.Close event, @Root Player player) {
    final ItemStackSnapshot cursor = event.getCursorTransaction().getOriginal();
    if (cursor == ItemStackSnapshot.NONE || !GDFlags.ITEM_DROP || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUniqueId())) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_DROP.getName(), cursor, player.getWorld().getProperties())) {
        return;
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.startTimingIfSync();
    final Location<World> location = player.getLocation();
    final GDClaim claim = this.dataStore.getClaimAt(location);
    if (GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.ITEM_DROP, player, cursor, player, TrustTypes.ACCESSOR, true) == Tristate.FALSE) {
        final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_ITEM_DROP,
                ImmutableMap.of(
                "player", claim.getOwnerName(),
                "item", cursor.getType().getId()));
        GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
        event.setCancelled(true);
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.stopTimingIfSync();
}
 
Example #2
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 #3
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void fireCloseEvent(InteractInventoryEvent.Close e)
{
    Optional<Player> optional = Sponge.getServer().getPlayer(playerUniqueId);
    if (optional.isPresent())
    {
        Player player = optional.get();
        UUID actionUUID = UUID.randomUUID();
        ClassToInstanceMap<Context> context = getContextMap(player, actionUUID);
        boolean record = recordManager.filter(name, VirtualChestInventory.this);
        if (record)
        {
            recordManager.recordClose(actionUUID, name, player);
            logger.debug("Player {} closes the chest GUI", player.getName());
        }
        plugin.getVirtualChestActions().submitCommands(player, parsedCloseAction.stream(), context, record);

        actionIntervalManager.onClosingInventory(player);
    }
}
 
Example #4
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryClose(InteractInventoryEvent.Close event, @Root Player player) {
    final ItemStackSnapshot cursor = event.getCursorTransaction().getOriginal();
    if (cursor == ItemStackSnapshot.NONE || !GPFlags.ITEM_DROP || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), cursor, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.startTimingIfSync();
    final Location<World> location = player.getLocation();
    final GPClaim claim = this.dataStore.getClaimAt(location);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_DROP, player, cursor, player, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionItemDrop
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "item", cursor.getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        event.setCancelled(true);
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.stopTimingIfSync();
}
 
Example #5
Source File: InventoryListener.java    From Prism with MIT License 5 votes vote down vote up
/**
 * Saves event records when a player closes or opens an Inventory.
 *
 * @param event  InteractInventoryEvent
 * @param player Player
 */
@Listener(order = Order.POST)
public void onInteractInventory(InteractInventoryEvent event, @Root Player player) {
    if (!(event.getTargetInventory() instanceof CarriedInventory)
            || (!Prism.getInstance().getConfig().getEventCategory().isInventoryClose() && !Prism.getInstance().getConfig().getEventCategory().isInventoryOpen())) {
        return;
    }

    CarriedInventory<? extends Carrier> carriedInventory = (CarriedInventory<? extends Carrier>) event.getTargetInventory();
    if (carriedInventory.getCarrier().filter(Player.class::isInstance).isPresent()) {
        return;
    }

    // Get the location of the inventory otherwise fallback on the players location
    Location<World> location = carriedInventory.getCarrier()
            .filter(Locatable.class::isInstance)
            .map(Locatable.class::cast)
            .map(Locatable::getLocation)
            .orElse(player.getLocation());

    // Get the title of the inventory otherwise fallback on the class name
    String title = carriedInventory.getProperty(InventoryTitle.class, InventoryTitle.PROPERTY_NAME)
            .map(InventoryTitle::getValue)
            .map(Text::toPlain)
            .orElse(carriedInventory.getClass().getSimpleName());

    PrismRecord.EventBuilder eventBuilder = PrismRecord.create()
            .source(event.getCause())
            .container(title)
            .location(location);

    if (event instanceof InteractInventoryEvent.Close && Prism.getInstance().getConfig().getEventCategory().isInventoryClose()) {
        eventBuilder.event(PrismEvents.INVENTORY_CLOSE).buildAndSave();
    } else if (event instanceof InteractInventoryEvent.Open && Prism.getInstance().getConfig().getEventCategory().isInventoryOpen()) {
        eventBuilder.event(PrismEvents.INVENTORY_OPEN).buildAndSave();
    }
}
 
Example #6
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryOpen(InteractInventoryEvent.Open event, @First Player player) {
    if (!GDFlags.INTERACT_INVENTORY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUniqueId())) {
        return;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    if (blockSnapshot == BlockSnapshot.NONE) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_INVENTORY.getName(), blockSnapshot, player.getWorld().getProperties())) {
        return;
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.startTimingIfSync();
    final Location<World> location = blockSnapshot.getLocation().get();
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_INVENTORY, player, blockSnapshot, player, TrustTypes.CONTAINER, true);
    if (result == Tristate.FALSE) {
        final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_INVENTORY_OPEN,
                ImmutableMap.of(
                "player", claim.getOwnerName(),
                "block", blockSnapshot.getState().getType().getId()));
        GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
        NMSUtil.getInstance().closePlayerScreen(player);
        event.setCancelled(true);
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTimingIfSync();
}
 
Example #7
Source File: MobFlagGui.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void onCloseInventory(InteractInventoryEvent.Close event) {
    if (event.getTargetInventory().getName().get().equals(this.player.getInventory().getName().get())) {
        return;
    }

    StringBuilder str = new StringBuilder();

    event.getTargetInventory().forEach(inv -> {
        if (inv.capacity() == 54) {
            StreamSupport.stream(inv.slots().spliterator(), false)
                    .filter(i -> i.peek().isPresent()).map(i -> i.peek().get().getItem().getName()).distinct()
                    .forEach(i -> str.append(i).append(","));
        }
    });

    if (str.length() > 0) {
        region.setFlag(RedProtect.get().getVersionHelper().getCause(this.player), flag, str.toString().substring(0, str.toString().length() - 1));
        RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("cmdmanager.region.flag.set").replace("{flag}", "'" + flag + "'") + " " + region.getFlagString(flag));
        RedProtect.get().logger.addLog("(World " + region.getWorld() + ") Player " + player.getName() + " SET FLAG " + flag + " of region " + region.getName() + " to " + region.getFlagString(flag));
    } else {
        region.removeFlag(flag);
        RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("cmdmanager.region.flag.removed").replace("{flag}", flag).replace("{region}", region.getName()));
        RedProtect.get().logger.addLog("(World " + region.getWorld() + ") Player " + player.getName() + " REMOVED FLAG " + flag + " of region " + region.getName());
    }

    close(false);
}
 
Example #8
Source File: FlagGui.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void onCloseInventory(InteractInventoryEvent.Close event) {
    if (event.getTargetInventory().getName().get().equals(this.inv.getName().get())) {
        if (this.editable) {
            for (int i = 0; i < this.size; i++) {
                try {
                    int line = 0;
                    int slot = i;
                    if (i > 8) {
                        line = i / 9;
                        slot = i - (line * 9);
                    }
                    if (RedProtect.get().getVersionHelper().query(event.getTargetInventory(), slot, line).peek().isPresent()) {
                        final int fi = i;
                        ItemStack stack = RedProtect.get().getVersionHelper().query(event.getTargetInventory(), slot, line).peek().get();
                        stack.get(Keys.ITEM_LORE).ifPresent(ls -> {
                            String flag = ls.get(1).toPlain().replace("ยง0", "");
                            if (RedProtect.get().config.getDefFlags().contains(flag))
                                RedProtect.get().config.setGuiSlot(flag, fi);
                        });
                    }
                } catch (Exception e) {
                    RedProtect.get().lang.sendMessage(this.player, "gui.edit.error");
                    close(false);
                    CoreUtil.printJarVersion();
                    e.printStackTrace();
                    return;
                }
            }
            RedProtect.get().config.saveGui();
            RedProtect.get().lang.sendMessage(this.player, "gui.edit.ok");
        }
        close(false);
    }
}
 
Example #9
Source File: ItemFlagGui.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void onCloseInventory(InteractInventoryEvent.Close event) {
    if (event.getTargetInventory().getName().get().equals(this.player.getInventory().getName().get())) {
        return;
    }

    StringBuilder str = new StringBuilder();

    event.getTargetInventory().forEach(inv -> {
        if (inv.capacity() == 54) {
            StreamSupport.stream(inv.slots().spliterator(), false)
                    .filter(i -> i.peek().isPresent()).map(i -> i.peek().get().getItem().getName()).distinct()
                    .forEach(i -> str.append(i).append(","));
        }
    });

    if (str.length() > 0) {
        region.setFlag(RedProtect.get().getVersionHelper().getCause(this.player), flag, str.toString().substring(0, str.toString().length() - 1));
        RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("cmdmanager.region.flag.set").replace("{flag}", "'" + flag + "'") + " " + region.getFlagString(flag));
        RedProtect.get().logger.addLog("(World " + region.getWorld() + ") Player " + player.getName() + " SET FLAG " + flag + " of region " + region.getName() + " to " + region.getFlagString(flag));
    } else {
        region.removeFlag(flag);
        RedProtect.get().lang.sendMessage(player, RedProtect.get().lang.get("cmdmanager.region.flag.removed").replace("{flag}", flag).replace("{region}", region.getName()));
        RedProtect.get().logger.addLog("(World " + region.getWorld() + ") Player " + player.getName() + " REMOVED FLAG " + flag + " of region " + region.getName());
    }

    close(false);
}
 
Example #10
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryOpen(InteractInventoryEvent.Open event, @First Player player) {
    if (!GPFlags.INTERACT_INVENTORY || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    if (blockSnapshot == BlockSnapshot.NONE) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_INVENTORY.toString(), blockSnapshot, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.startTimingIfSync();
    final Location<World> location = blockSnapshot.getLocation().get();
    final GPClaim claim = this.dataStore.getClaimAt(location);
    final Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INVENTORY_OPEN, player, blockSnapshot, player, TrustType.CONTAINER, true);
    if (result == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionInventoryOpen
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "block", blockSnapshot.getState().getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        ((EntityPlayerMP) player).closeScreen();
        event.setCancelled(true);
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTimingIfSync();
}
 
Example #11
Source File: FactionChestImpl.java    From EagleFactions with MIT License 5 votes vote down vote up
private Inventory buildInventory(final List<SlotItem> slotItems)
{
    //Create inventory
    final Inventory inventory = Inventory.builder()
            .of(InventoryArchetypes.CHEST)
            .property(InventoryTitle.of(Text.of(TextColors.BLUE, Text.of("Faction's chest"))))
            .listener(InteractInventoryEvent.Close.class, (x) ->
            {
                final Faction faction = EagleFactionsPlugin.getPlugin().getFactionLogic().getFactionByName(factionName);
                if(faction != null)
                {
                    EagleFactionsPlugin.getPlugin().getFactionLogic().setChest(faction, this);
                }
            })
            .build(EagleFactionsPlugin.getPlugin());

    //Fill it with items
    int column = 1;
    int row = 1;

    for(final Inventory slot : inventory.slots())
    {
        ItemStack itemStack = getAtPosition(slotItems, row, column);
        if(itemStack != null)
            slot.offer(itemStack);

        column++;
        if(column > 9)
        {
            column = 1;
            row++;
        }
    }

    return inventory;
}
 
Example #12
Source File: WebHookService.java    From Web-API with MIT License 4 votes vote down vote up
@Listener(order = Order.POST)
public void onInteractInventory(InteractInventoryEvent.Close event) {
    notifyHooks(WebHookService.WebHookType.INVENTORY_CLOSE, event);
}
 
Example #13
Source File: WebHookService.java    From Web-API with MIT License 4 votes vote down vote up
@Listener(order = Order.POST)
public void onInteractInventory(InteractInventoryEvent.Open event) {
    notifyHooks(WebHookService.WebHookType.INVENTORY_OPEN, event);
}
 
Example #14
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void fireOpenEvent(InteractInventoryEvent.Open e)
{
    Optional<Player> optional = Sponge.getServer().getPlayer(playerUniqueId);
    if (optional.isPresent())
    {
        Player player = optional.get();
        UUID actionUUID = UUID.randomUUID();

        Container targetContainer = e.getTargetInventory();
        Inventory targetInventory = targetContainer.first();

        VirtualChestActions actions = plugin.getVirtualChestActions();
        ClassToInstanceMap<Context> context = getContextMap(player, actionUUID);
        Consumer<Inventory> updater = VirtualChestInventory.this.getUpdaterFor(name, player);

        if (recordManager.filter(name, VirtualChestInventory.this))
        {
            recordManager.recordOpen(actionUUID, name, player);
            logger.debug("Player {} opens the chest GUI", player.getName());
            actions.submitCommands(player, parsedOpenAction.stream(), context, true);
        }
        else
        {
            actions.submitCommands(player, parsedOpenAction.stream(), context, false);
        }

        if (updateIntervalTick > 0)
        {
            Task.Builder builder = Sponge.getScheduler().createTaskBuilder().execute(task ->
            {
                if (!dispatcher.isInventoryOpening(player, targetContainer))
                {
                    task.cancel();
                }
                updater.accept(targetInventory);
            });
            builder.delayTicks(updateIntervalTick).intervalTicks(updateIntervalTick).submit(plugin);
        }

        plugin.getScriptManager().onOpeningInventory(player);

        updater.accept(targetInventory);
    }
}
 
Example #15
Source File: PlayerListener.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void PlayerMoveInv(InteractInventoryEvent.Close e, @Root Player p) {
    RedProtect.get().getVersionHelper().removeGuiItem(p);
}
 
Example #16
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 onInventoryInteract(InteractInventoryEvent interactInventoryEvent, @First Player player) {
    checkLoginStatus(interactInventoryEvent, player);
}
 
Example #17
Source File: InventoryListener.java    From UltimateCore with MIT License 4 votes vote down vote up
@Listener
public void onCloseInventory(InteractInventoryEvent.Close event, @Root Player p) {
    //Player closed invsee
    UltimateUser up = UltimateCore.get().getUserService().getUser(p);
    up.offer(InventoryKeys.INVSEE_TARGET, null);
}