Search in sources :

Example 1 with InventoryView

use of org.bukkit.inventory.InventoryView in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAllowInventoryOpen.

@Test
public void shouldAllowInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    given(transaction.getPlayer()).willReturn(player);
    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));
    verifyZeroInteractions(bukkitService);
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryView(org.bukkit.inventory.InventoryView) HumanEntity(org.bukkit.entity.HumanEntity) Test(org.junit.Test)

Example 2 with InventoryView

use of org.bukkit.inventory.InventoryView in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldCancelInventoryOpen.

@Test
public void shouldCancelInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    given(transaction.getPlayer()).willReturn(player);
    InventoryOpenEvent event = new InventoryOpenEvent(transaction);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    // when
    listener.onPlayerInventoryOpen(event);
    // then
    assertThat(event.isCancelled(), equalTo(true));
    TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
    verify(player).closeInventory();
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryView(org.bukkit.inventory.InventoryView) HumanEntity(org.bukkit.entity.HumanEntity) Test(org.junit.Test)

Example 3 with InventoryView

use of org.bukkit.inventory.InventoryView in project Glowstone by GlowstoneMC.

the class GlowHumanEntity method openInventory.

@Override
public InventoryView openInventory(Inventory inventory) {
    InventoryView view = new GlowInventoryView(this, inventory);
    openInventory(view);
    return view;
}
Also used : InventoryView(org.bukkit.inventory.InventoryView)

Example 4 with InventoryView

use of org.bukkit.inventory.InventoryView in project Glowstone by GlowstoneMC.

the class CreativeItemHandler method handle.

@Override
public void handle(GlowSession session, CreativeItemMessage message) {
    GlowPlayer player = session.getPlayer();
    GlowInventory inv = player.getInventory();
    // CraftBukkit does use a inventory view with both inventories set to the player's inventory
    // for the creative inventory as there is no second inventory (no crafting) visible for the client
    InventoryView view = player.getOpenInventory();
    int viewSlot = message.getSlot();
    int slot = view.convertSlot(viewSlot);
    ItemStack stack = ItemIds.sanitize(message.getItem());
    SlotType type = inv.getSlotType(slot);
    // only if creative mode
    if (player.getGameMode() != GameMode.CREATIVE) {
        player.kickPlayer("Illegal creative mode item selection");
        return;
    }
    // only if default (player) inventory
    if (!GlowInventoryView.isDefault(player.getOpenInventory())) {
        player.kickPlayer("Illegal creative mode item selection");
        return;
    }
    // clicking outside drops the item
    if (message.getSlot() < 0) {
        InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, SlotType.OUTSIDE, -999, stack));
        if (event.isCancelled()) {
            session.send(new SetWindowSlotMessage(-1, -1, stack));
        } else {
            player.drop(event.getCursor());
        }
        return;
    }
    // this happens quiet often as the client tends to update the whole inventory at once
    if (Objects.equals(stack, view.getItem(viewSlot))) {
        return;
    }
    InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, type, viewSlot, stack));
    if (event.isCancelled()) {
        // send original slot to player to prevent async inventories
        player.sendItemChange(viewSlot, view.getItem(viewSlot));
        // don't keep track of player's current item, just give them back what they tried to place
        session.send(new SetWindowSlotMessage(-1, -1, stack));
        return;
    }
    view.setItem(viewSlot, stack);
}
Also used : GlowInventoryView(net.glowstone.inventory.GlowInventoryView) InventoryView(org.bukkit.inventory.InventoryView) InventoryCreativeEvent(org.bukkit.event.inventory.InventoryCreativeEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) SetWindowSlotMessage(net.glowstone.net.message.play.inv.SetWindowSlotMessage) GlowInventory(net.glowstone.inventory.GlowInventory) ItemStack(org.bukkit.inventory.ItemStack) SlotType(org.bukkit.event.inventory.InventoryType.SlotType)

Aggregations

InventoryView (org.bukkit.inventory.InventoryView)4 HumanEntity (org.bukkit.entity.HumanEntity)2 InventoryOpenEvent (org.bukkit.event.inventory.InventoryOpenEvent)2 Test (org.junit.Test)2 GlowPlayer (net.glowstone.entity.GlowPlayer)1 GlowInventory (net.glowstone.inventory.GlowInventory)1 GlowInventoryView (net.glowstone.inventory.GlowInventoryView)1 SetWindowSlotMessage (net.glowstone.net.message.play.inv.SetWindowSlotMessage)1 InventoryCreativeEvent (org.bukkit.event.inventory.InventoryCreativeEvent)1 SlotType (org.bukkit.event.inventory.InventoryType.SlotType)1 ItemStack (org.bukkit.inventory.ItemStack)1