org.bukkit.event.inventory.InventoryOpenEvent Java Examples

The following examples show how to use org.bukkit.event.inventory.InventoryOpenEvent. 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: PlayerListenerTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldCancelInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    given(transaction.getTitle()).willReturn("Spawn");
    given(settings.getProperty(RestrictionSettings.UNRESTRICTED_INVENTORIES)).willReturn(Collections.emptySet());
    InventoryOpenEvent event = new InventoryOpenEvent(transaction);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);

    // when
    listener.onPlayerInventoryOpen(event);

    // then
    assertThat(event.isCancelled(), equalTo(true));
    verify(player).closeInventory();
}
 
Example #2
Source File: PlayerListenerTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldAllowInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    InventoryOpenEvent event = new InventoryOpenEvent(transaction);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(false);

    // when
    listener.onPlayerInventoryOpen(event);

    // then
    assertThat(event.isCancelled(), equalTo(false));
    verifyNoInteractions(bukkitService);
}
 
Example #3
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public static Container callInventoryOpenEvent(EntityPlayerMP player, Container container, boolean cancelled) {
    if (player.openContainer != player.inventoryContainer) { // fire INVENTORY_CLOSE if one already open
        player.connection.processCloseWindow(new CPacketCloseWindow(player.openContainer.windowId));
    }

    CraftServer server = player.world.getServer();
    CraftPlayer craftPlayer = player.getBukkitEntity();
    player.openContainer.transferTo(container, craftPlayer);

    InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView());
    event.setCancelled(cancelled);
    server.getPluginManager().callEvent(event);

    if (event.isCancelled()) {
        container.transferTo(player.openContainer, craftPlayer);
        return null;
    }

    return container;
}
 
Example #4
Source File: PlayerEventListener.java    From Hawk with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpenServerSide(InventoryOpenEvent e) {
    HumanEntity hE = e.getPlayer();
    if(!(hE instanceof Player))
        return;

    //Fixes issues regarding the client not releasing item usage when a server inventory is opened.
    //Consumables may not have this issue.
    HawkPlayer pp = hawk.getHawkPlayer((Player) hE);
    pp.sendSimulatedAction(new Runnable() {
        @Override
        public void run() {
            pp.setBlocking(false);
            pp.setPullingBow(false);
            pp.setInventoryOpen((byte)2);
        }
    });
}
 
Example #5
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void storageMinecraftInventoryOpen(InventoryOpenEvent event) {
    InventoryHolder holder = event.getInventory().getHolder();
    Player player = (Player) event.getPlayer();
    if ((!(holder instanceof StorageMinecart)) && (!(holder instanceof HopperMinecart))) {
        return;
    }
    Entity entity = (Entity) holder;
    if (plugin.getLWC().isProtectable(entity.getType())) {
        if (!plugin.getLWC().hasPermission(player, "lwc.protect")
                && plugin.getLWC().hasPermission(player, "lwc.deny") && !plugin.getLWC().isAdmin(player)
                && !plugin.getLWC().isMod(player)) {
            plugin.getLWC().sendLocale(player, "protection.interact.error.blocked");
            event.setCancelled(true);
            return;
        }
        if (onPlayerEntityInteract((Player) event.getPlayer(), entity, event.isCancelled())) {
            event.setCancelled(true);
        }
    }
}
 
Example #6
Source File: PlayerListener.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryOpenEvent(InventoryOpenEvent event) {
	if (event.getInventory() instanceof DoubleChestInventory) {
		DoubleChestInventory doubleInv = (DoubleChestInventory)event.getInventory();
					
		Chest leftChest = (Chest)doubleInv.getHolder().getLeftSide();			
		/*Generate a new player 'switch' event for the left and right chests. */
		PlayerInteractEvent interactLeft = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, leftChest.getBlock(), null);
		BlockListener.OnPlayerSwitchEvent(interactLeft);
		
		if (interactLeft.isCancelled()) {
			event.setCancelled(true);
			return;
		}
		
		Chest rightChest = (Chest)doubleInv.getHolder().getRightSide();
		PlayerInteractEvent interactRight = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, rightChest.getBlock(), null);
		BlockListener.OnPlayerSwitchEvent(interactRight);
		
		if (interactRight.isCancelled()) {
			event.setCancelled(true);
			return;
		}			
	}
}
 
Example #7
Source File: ExploitEvents.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onVillagerTrade(InventoryOpenEvent event) {
    if (!plugin.getWorldManager().isSkyAssociatedWorld(event.getPlayer().getWorld())) {
        return;
    }
    if (!(event.getPlayer() instanceof Player)) {
        return;
    }
    if (!anyVillagerTradingAllowed && event.getInventory().getType() == InventoryType.MERCHANT) {
        event.setCancelled(true);
        event.getPlayer().sendMessage(tr("\u00a7eVillager-trading isn't allowed."));
        return;
    }
    if (visitorVillagerTradingProtected
            && event.getPlayer() instanceof Player
            && !(event.getPlayer().hasPermission("usb.mod.bypassprotection"))
            && event.getInventory().getType() == InventoryType.MERCHANT
            && !plugin.playerIsOnIsland((Player)event.getPlayer())) {
        event.setCancelled(true);
        event.getPlayer().sendMessage(tr("\u00a7eTrading isn't allowed on other islands. Do it in spawn."));
    }
}
 
Example #8
Source File: WorldGuardListener.java    From ShopChest with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onUseBlock(WrappedUseBlockEvent event) {
    if (Config.enableWorldGuardIntegration) {
        Player player = event.getPlayer();

        if (event.getOriginalEvent() instanceof PlayerInteractEvent) {
            Block block = event.getBlocks().get(0);
            Material type = block.getType();
            
            if (type == Material.CHEST || type == Material.TRAPPED_CHEST) {
                if (isAllowed(player, block.getLocation())) {
                    event.setResult(Result.ALLOW);
                }
            }
        } else if (event.getOriginalEvent() instanceof InventoryOpenEvent) {
            InventoryOpenEvent orig = (InventoryOpenEvent) event.getOriginalEvent();

            if (orig.getInventory().getHolder() instanceof Chest) {
                if (isAllowed(player, ((Chest) orig.getInventory().getHolder()).getLocation())) {
                    event.setResult(Result.ALLOW);
                }
            }
        }
    }
}
 
Example #9
Source File: ListenerInventories.java    From CombatLogX with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority=EventPriority.HIGH, ignoreCancelled=true)
public void onOpenInventory(InventoryOpenEvent e) {
    FileConfiguration config = this.expansion.getConfig("cheat-prevention.yml");
    if(!config.getBoolean("inventories.close-on-tag")) return;

    HumanEntity human = e.getPlayer();
    if(!(human instanceof Player)) return;

    Player player = (Player) human;
    ICombatManager manager = this.plugin.getCombatManager();
    if(!manager.isInCombat(player)) return;

    e.setCancelled(true);
    String message = this.plugin.getLanguageMessageColoredWithPrefix("cheat-prevention.inventory.no-opening");
    this.plugin.sendMessage(player, message);
}
 
Example #10
Source File: WorthListener.java    From factions-top with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void checkWorth(InventoryOpenEvent event) {
    // Do nothing if a player did not open the inventory or if chest events
    // are disabled.
    if (!(event.getPlayer() instanceof Player) || plugin.getSettings().isDisableChestEvents()) {
        return;
    }

    Inventory inventory = event.getInventory();

    // Set all default worth values for this chest.
    if (inventory.getHolder() instanceof DoubleChest) {
        DoubleChest chest = (DoubleChest) inventory.getHolder();
        checkWorth((Chest) chest.getLeftSide());
        checkWorth((Chest) chest.getRightSide());
    }

    if (inventory.getHolder() instanceof Chest) {
        checkWorth((Chest) inventory.getHolder());
    }
}
 
Example #11
Source File: CraftEventFactory.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
public static net.minecraft.inventory.Container callInventoryOpenEvent(net.minecraft.entity.player.EntityPlayerMP player, net.minecraft.inventory.Container container, boolean closeInv) {
    if (player.openContainer != player.inventoryContainer && closeInv) { // fire INVENTORY_CLOSE if one already open
// Cauldron end
        player.playerNetServerHandler.processCloseWindow(new net.minecraft.network.play.client.C0DPacketCloseWindow(player.openContainer.windowId));
    }

    CraftServer server = player.worldObj.getServer();
    CraftPlayer craftPlayer = player.getBukkitEntity();
    // Cauldron start - vanilla compatibility
    try {
        player.openContainer.transferTo(container, craftPlayer);
    }
    catch (AbstractMethodError e) {
        // do nothing
    }
    // Cauldron end
    InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView());
    if (container.getBukkitView() != null) server.getPluginManager().callEvent(event); // Cauldron - allow vanilla mods to bypass

    if (event.isCancelled()) {
        container.transferTo(player.openContainer, craftPlayer);
        // Cauldron start - handle close for modded containers
        if (!closeInv) { // fire INVENTORY_CLOSE if one already open
            player.openContainer = container; // make sure the right container is processed
            player.closeScreen();
            player.openContainer = player.inventoryContainer;
        }
        // Cauldron end
        return null;
    }

    return container;
}
 
Example #12
Source File: PlayerListener.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerInventoryOpen(InventoryOpenEvent event) {
    final HumanEntity player = event.getPlayer();

    if (listenerService.shouldCancelEvent(player)
        && !isInventoryWhitelisted(event.getView())) {
        event.setCancelled(true);

        /*
         * @note little hack cause InventoryOpenEvent cannot be cancelled for
         * real, cause no packet is sent to server by client for the main inv
         */
        bukkitService.scheduleSyncDelayedTask(player::closeInventory, 1);
    }
}
 
Example #13
Source File: GuiActionHandler.java    From FunnyGuilds with Apache License 2.0 5 votes vote down vote up
@EventHandler
public void onOpen(final InventoryOpenEvent e) {
    GuiWindow window = GuiWindow.getWindow(e.getView().getTitle());
    if (window != null) {
        window.handleOpen(e);
    }
}
 
Example #14
Source File: RewardListener.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    if (!(event.getPlayer() instanceof Player)) {
        return;
    }

    InventoryView inventory = event.getView();

    DGameWorld gameWorld = (DGameWorld) plugin.getGameWorld(event.getPlayer().getWorld());

    if (gameWorld == null) {
        return;
    }

    if (!(ContainerAdapter.isValidContainer(inventory.getTopInventory()))) {
        return;
    }

    for (RewardChest rewardChest : gameWorld.getRewardChests()) {
        if (!rewardChest.getBlock().equals(ContainerAdapter.getHolderBlock(inventory.getTopInventory().getHolder()))) {
            continue;
        }

        rewardChest.onOpen((Player) event.getPlayer());
        event.setCancelled(true);
        break;
    }

    if (!plugin.getMainConfig().getOpenInventories() && !DPermission.hasPermission(event.getPlayer(), DPermission.INSECURE)) {
        World world = event.getPlayer().getWorld();
        if (event.getInventory().getType() != InventoryType.CREATIVE && plugin.getEditWorld(world) != null) {
            event.setCancelled(true);
        }
    }
}
 
Example #15
Source File: PlayerListener.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    if (event.isCancelled() || !(event.getPlayer() instanceof Player)) {
        return;
    }

    Player player = (Player) event.getPlayer();
    if (Main.isPlayerInGame(player)) {
        GamePlayer gProfile = Main.getPlayerGameProfile(player);
        if (gProfile.getGame().getStatus() == GameStatus.RUNNING) {
            if (gProfile.isSpectator) {
                // TODO spectator compass exclude
                event.setCancelled(true);
                return;
            }
            if (event.getInventory().getType() == InventoryType.ENCHANTING
                    || event.getInventory().getType() == InventoryType.CRAFTING
                    || event.getInventory().getType() == InventoryType.ANVIL
                    || event.getInventory().getType() == InventoryType.BREWING
                    || event.getInventory().getType() == InventoryType.FURNACE
                    || event.getInventory().getType() == InventoryType.WORKBENCH) {
                if (!gProfile.getGame().getOriginalOrInheritedCrafting()) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
 
Example #16
Source File: NPCInteractions.java    From EliteMobs with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {

    if (!event.getInventory().getType().equals(InventoryType.MERCHANT)) return;

    for (NPCEntity npcEntity : EntityTracker.getNPCEntities())
        if (event.getInventory().getName().equals(npcEntity.getName())) {
            event.setCancelled(true);
            return;
        }

}
 
Example #17
Source File: GuiInject.java    From AACAdditionPro with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onJoin(InventoryOpenEvent event)
{
    if (event.getView().getTitle().startsWith(AAC_GUI_TITLE)
        && event.getView().getType() == InventoryType.CHEST
        && InternalPermission.hasPermission(event.getPlayer(), InternalPermission.AAC_MANAGE))
    {
        event.getInventory().setItem(16, AACADDITIONPRO_ITEMSTACK);
    }
}
 
Example #18
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
private void onInventoryOpen(Event event, Location location, Object target, HumanEntity player) {
    GDCauseStackManager.getInstance().pushCause(player);
    if (event instanceof InventoryOpenEvent) {
        final InventoryOpenEvent inventoryEvent = (InventoryOpenEvent) event;
        target = inventoryEvent.getView().getType();
    }
    if (!GDFlags.INTERACT_INVENTORY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUID())) {
        return;
    }

    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_INVENTORY.getName(), target, player.getWorld().getUID())) {
        return;
    }

    String targetId = GDPermissionManager.getInstance().getPermissionIdentifier(target);
    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.startTiming();
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final GDPermissionUser user = PermissionHolderCache.getInstance().getOrCreateUser(player.getUniqueId());
    if (user.getInternalPlayerData() != null && user.getInternalPlayerData().eventResultCache != null && user.getInternalPlayerData().eventResultCache.checkEventResultCache(claim, Flags.INTERACT_BLOCK_SECONDARY.getName()) == Tristate.TRUE) {
        GDPermissionManager.getInstance().processResult(claim, Flags.INTERACT_INVENTORY.getPermission(), "cache", Tristate.TRUE, user);
        GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTiming();
        return;
    }
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_INVENTORY, player, target, user, TrustTypes.CONTAINER, true);
    if (result == Tristate.FALSE) {
        final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_INVENTORY_OPEN,
                ImmutableMap.of(
                "player", claim.getOwnerDisplayName(),
                "block", targetId));
        GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
        ((Cancellable) event).setCancelled(true);
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTiming();
}
 
Example #19
Source File: ItemModifyMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
  ItemStack[] contents = event.getInventory().getContents();
  for (int i = 0; i < contents.length; i++) {
    if (applyRules(contents[i])) {
      event.getInventory().setItem(i, contents[i]);
    }
  }
}
 
Example #20
Source File: PlayerListener.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    if (event.isCancelled() || !(event.getPlayer() instanceof Player)) {
        return;
    }

    Player player = (Player) event.getPlayer();
    if (Main.isPlayerInGame(player)) {
        GamePlayer gProfile = Main.getPlayerGameProfile(player);
        if (gProfile.getGame().getStatus() == GameStatus.RUNNING) {
            if (gProfile.isSpectator) {
                // TODO spectator compass exclude
                event.setCancelled(true);
                return;
            }
            if (event.getInventory().getType() == InventoryType.ENCHANTING
                    || event.getInventory().getType() == InventoryType.CRAFTING
                    || event.getInventory().getType() == InventoryType.ANVIL
                    || event.getInventory().getType() == InventoryType.BREWING
                    || event.getInventory().getType() == InventoryType.FURNACE
                    || event.getInventory().getType() == InventoryType.WORKBENCH) {
                if (!gProfile.getGame().getOriginalOrInheritedCrafting()) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
 
Example #21
Source File: FillListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) {
    final MatchPlayer opener = playerFinder.getParticipant(event.getActor());
    if(opener == null) return;

    final Inventory inventory = event.getInventory();
    final Predicate<Filter> passesFilter = passesFilter(inventory.getHolder());
    if(passesFilter == null) return;

    logger.fine(() -> opener.getName() + " opened a " + inventory.getHolder().getClass().getSimpleName());

    // Find all Fillers that apply to the holder of the opened inventory
    final List<Filler> fillers = this.fillers.stream()
                                             .filter(filler -> passesFilter.test(filler.filter()))
                                             .collect(Collectors.toImmutableList());
    if(fillers.isEmpty()) return;

    logger.fine(() -> "Found fillers " + fillers.stream()
                                                .map(Filler::identify)
                                                .collect(java.util.stream.Collectors.joining(", ")));

    // Find all Caches that the opened inventory is part of
    final List<Fillable> fillables = new ArrayList<>();
    for(Cache cache : caches) {
        if(passesFilter.test(cache.region()) && passesFilter.test(cache.filter())) {
            fillables.add(new FillableCache(cache));
        }
    }
    // If the inventory is not in any Cache, just fill it directly
    if(fillables.isEmpty()) {
        fillables.add(new FillableInventory(inventory));
    }

    fillables.forEach(fillable -> fillable.fill(opener, fillers));
}
 
Example #22
Source File: DataUpdaterEvents.java    From AACAdditionPro with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onInventoryOpen(final InventoryOpenEvent event)
{
    // Removed theUser.getPlayer().getOpenInventory().getType() != InventoryType.CRAFTING.
    final User user = UserManager.getUser(event.getPlayer().getUniqueId());

    if (user != null) {
        user.getTimestampMap().updateTimeStamp(TimestampKey.INVENTORY_OPENED);
    }
}
 
Example #23
Source File: CutCleanListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void openInventoryEvent(InventoryOpenEvent e){
    if (!unlimitedLapis) return;

    if (e.getInventory() instanceof EnchantingInventory){
        e.getInventory().setItem(1, lapis);
    }
}
 
Example #24
Source File: NoDurability.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onInventoryOpen(InventoryOpenEvent event, ItemStack stack) {
	stack.setDurability((short) 0);		
}
 
Example #25
Source File: WoolMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) {
  // Register container blocks when they are opened
  this.registerContainer(event.getInventory());
}
 
Example #26
Source File: ThreadSafetyListener.java    From LagMonitor with MIT License 4 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent inventoryOpenEvent) {
    checkSafety(inventoryOpenEvent);
}
 
Example #27
Source File: ItemMods.java    From CardinalPGM with MIT License 4 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    applyRules(event.getInventory());
}
 
Example #28
Source File: GuiWindow.java    From FunnyGuilds with Apache License 2.0 4 votes vote down vote up
public void handleOpen(final InventoryOpenEvent e) {
    if (this.openHandler != null) {
        this.openHandler.accept(e);
    }
}
 
Example #29
Source File: GuiWindow.java    From FunnyGuilds with Apache License 2.0 4 votes vote down vote up
public void setOpenEvent(final Consumer<InventoryOpenEvent> e) {
    this.openHandler = e;
}
 
Example #30
Source File: ItemModifyMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    event.getInventory().contents().forEach(this::applyRules);
}