Skip to content

Commit

Permalink
Check inventory view on inventory click
Browse files Browse the repository at this point in the history
Check the inventory view or which inventory, the top or bottom, that is
clicked on. Fixes players clicked on items in their inventory activating
functions in the custom inventory.
  • Loading branch information
virustotalop committed Jan 24, 2019
1 parent 7cbfc42 commit 6eba58b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.clubobsidian.dynamicgui.entity.PlayerWrapper;
import com.clubobsidian.dynamicgui.event.InventoryEvent;
import com.clubobsidian.dynamicgui.gui.InventoryView;
import com.clubobsidian.dynamicgui.inventory.InventoryWrapper;
import com.clubobsidian.dynamicgui.inventory.ItemStackWrapper;
import com.clubobsidian.trident.Cancelable;
Expand All @@ -27,13 +28,15 @@ public class InventoryClickEvent extends InventoryEvent implements Cancelable {
private ItemStackWrapper<?> itemStackWrapper;
private int slot;
private Click click;
private InventoryView view;
private boolean cancelled = false;
public InventoryClickEvent(PlayerWrapper<?> playerWrapper, InventoryWrapper<?> inventoryWrapper, ItemStackWrapper<?> itemStackWrapper, int slot, Click click)
public InventoryClickEvent(PlayerWrapper<?> playerWrapper, InventoryWrapper<?> inventoryWrapper, ItemStackWrapper<?> itemStackWrapper, int slot, Click click, InventoryView view)
{
super(playerWrapper, inventoryWrapper);
this.itemStackWrapper = itemStackWrapper;
this.slot = slot;
this.click = click;
this.view = view;
}

public ItemStackWrapper<?> getItemStackWrapper()
Expand All @@ -46,6 +49,11 @@ public int getSlot()
return this.slot;
}

public InventoryView getView()
{
return this.view;
}

public Click getClick()
{
return this.click;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.clubobsidian.dynamicgui.gui;

public enum InventoryView {

TOP,
BOTTOM;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent;
import com.clubobsidian.dynamicgui.function.Function;
import com.clubobsidian.dynamicgui.gui.Gui;
import com.clubobsidian.dynamicgui.gui.InventoryView;
import com.clubobsidian.dynamicgui.gui.Slot;
import com.clubobsidian.dynamicgui.inventory.ItemStackWrapper;
import com.clubobsidian.dynamicgui.manager.dynamicgui.GuiManager;
Expand Down Expand Up @@ -52,6 +53,11 @@ public void invClick(final InventoryClickEvent e)
return;
}

if(e.getView() != InventoryView.TOP)
{
return;
}

if(e.getClick() == null) //For other types of clicks besides left, right, middle
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.clubobsidian.dynamicgui.entity.PlayerWrapper;
import com.clubobsidian.dynamicgui.entity.bukkit.BukkitPlayerWrapper;
import com.clubobsidian.dynamicgui.event.inventory.Click;
import com.clubobsidian.dynamicgui.gui.InventoryView;
import com.clubobsidian.dynamicgui.inventory.InventoryWrapper;
import com.clubobsidian.dynamicgui.inventory.ItemStackWrapper;
import com.clubobsidian.dynamicgui.inventory.bukkit.BukkitInventoryWrapper;
Expand All @@ -46,6 +47,12 @@ public void onInventoryClick(InventoryClickEvent e)
{
return;
}

InventoryView view = InventoryView.TOP;
if(e.getRawSlot() >= e.getInventory().getSize())
{
view = InventoryView.BOTTOM;
}

if(e.getWhoClicked() instanceof Player)
{
Expand All @@ -69,7 +76,7 @@ public void onInventoryClick(InventoryClickEvent e)
itemStackWrapper = new BukkitItemStackWrapper<ItemStack>(itemStack);
}

com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent clickEvent = new com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent(playerWrapper, inventoryWrapper, itemStackWrapper, slot, clickType);
com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent clickEvent = new com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent(playerWrapper, inventoryWrapper, itemStackWrapper, slot, clickType, view);
DynamicGui.get().getEventManager().callEvent(clickEvent);
if(clickEvent.isCanceled())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.clubobsidian.dynamicgui.entity.PlayerWrapper;
import com.clubobsidian.dynamicgui.entity.sponge.SpongePlayerWrapper;
import com.clubobsidian.dynamicgui.event.inventory.Click;
import com.clubobsidian.dynamicgui.gui.InventoryView;
import com.clubobsidian.dynamicgui.inventory.InventoryWrapper;
import com.clubobsidian.dynamicgui.inventory.ItemStackWrapper;
import com.clubobsidian.dynamicgui.inventory.sponge.SpongeInventoryWrapper;
Expand Down Expand Up @@ -91,10 +92,16 @@ else if (e instanceof ClickInventoryEvent.Secondary)
DynamicGui.get().getLogger().info("SlotIndex: " + slotIndexClicked);
DynamicGui.get().getLogger().info("ItemStack: " + itemStack);

InventoryView view = InventoryView.TOP;
if(slotIndexClicked >= inventory.size())
{
view = InventoryView.BOTTOM;
}

PlayerWrapper<?> playerWrapper = new SpongePlayerWrapper<Player>(player);
InventoryWrapper<?> inventoryWrapper = new SpongeInventoryWrapper<Inventory>(inventory);

com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent clickEvent = new com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent(playerWrapper, inventoryWrapper, itemStackWrapper, slotIndexClicked, clickType);
com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent clickEvent = new com.clubobsidian.dynamicgui.event.inventory.InventoryClickEvent(playerWrapper, inventoryWrapper, itemStackWrapper, slotIndexClicked, clickType, view);
DynamicGui.get().getEventManager().callEvent(clickEvent);
if(clickEvent.isCanceled())
{
Expand Down

0 comments on commit 6eba58b

Please sign in to comment.