Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.stefvanschie.inventoryframework.gui;

import com.github.stefvanschie.inventoryframework.pane.util.Slot;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;

public class GuiClickEvent {

@NotNull
private final InventoryClickEvent event;

/**
* The slot that was clicked, expressed relative to the top-left corner of the pane that received the click.
* For example, if a pane starts at column 3, row 1 of the inventory and the player clicks column 3, row 1,
* this slot will be (0, 0).
*/
@NotNull
private final Slot relativeSlot;

public GuiClickEvent(@NotNull InventoryClickEvent event, @NotNull Slot relativeSlot) {
this.event = event;
this.relativeSlot = relativeSlot;
}

@NotNull
public InventoryClickEvent getClickEvent() {
return event;
}

@NotNull
public Slot getRelativeSlot() {
return relativeSlot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryClickEvent;
import com.github.stefvanschie.inventoryframework.gui.GuiClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
Expand Down Expand Up @@ -70,7 +70,7 @@ public class GuiItem {
* An action for the inventory
*/
@Nullable
private Consumer<? super InventoryClickEvent> action;
private Consumer<? super GuiClickEvent> action;

/**
* List of item's properties
Expand Down Expand Up @@ -104,7 +104,7 @@ public class GuiItem {
* @see #GuiItem(ItemStack, Consumer)
* @since 0.10.8
*/
public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super InventoryClickEvent> action,
public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super GuiClickEvent> action,
@NotNull Plugin plugin) {
this(item, action, plugin.getLogger(), new NamespacedKey(plugin, "IF-uuid"));
}
Expand All @@ -118,7 +118,7 @@ public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super InventoryClic
* @since 0.10.8
*/
public GuiItem(@NotNull ItemStack item, @NotNull Plugin plugin) {
this(item, event -> {}, plugin);
this(item, (Consumer<? super GuiClickEvent>) null, plugin);
}

/**
Expand All @@ -127,7 +127,7 @@ public GuiItem(@NotNull ItemStack item, @NotNull Plugin plugin) {
* @param item the item stack
* @param action the action called whenever an interaction with this item happens
*/
public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super InventoryClickEvent> action) {
public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super GuiClickEvent> action) {
this(item, action, JavaPlugin.getProvidingPlugin(GuiItem.class));
}

Expand All @@ -137,7 +137,7 @@ public GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super InventoryClic
* @param item the item stack
*/
public GuiItem(@NotNull ItemStack item) {
this(item, event -> {});
this(item, (Consumer<? super GuiClickEvent>) null);
}

/**
Expand All @@ -150,7 +150,7 @@ public GuiItem(@NotNull ItemStack item) {
* @param key the key to identify this item with
* @since 0.10.10
*/
private GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super InventoryClickEvent> action,
private GuiItem(@NotNull ItemStack item, @Nullable Consumer<? super GuiClickEvent> action,
@NotNull Logger logger, @NotNull NamespacedKey key) {
this.logger = logger;
this.keyUUID = key;
Expand Down Expand Up @@ -181,14 +181,14 @@ public GuiItem copy() {
}

/**
* Calls the handler of the {@link InventoryClickEvent}
* Calls the handler of the {@link GuiClickEvent}
* if such a handler was specified in the constructor.
* Catches and logs all exceptions the handler might throw.
*
* @param event the event to handle
* @since 0.6.0
*/
public void callAction(@NotNull InventoryClickEvent event) {
public void callAction(@NotNull GuiClickEvent event) {
if (action == null) {
return;
}
Expand All @@ -197,8 +197,8 @@ public void callAction(@NotNull InventoryClickEvent event) {
action.accept(event);
} catch (Throwable t) {
this.logger.log(Level.SEVERE, "Exception while handling click event in inventory '"
+ InventoryViewUtil.getInstance().getTitle(event.getView()) + "', slot=" + event.getSlot() +
", item=" + item.getType(), t);
+ InventoryViewUtil.getInstance().getTitle(event.getClickEvent().getView()) + "', slot="
+ event.getClickEvent().getSlot() + ", item=" + item.getType(), t);
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ public void setItem(@NotNull ItemStack item) {
* @param action the action of this item
* @since 0.7.1
*/
public void setAction(@NotNull Consumer<InventoryClickEvent> action) {
public void setAction(@NotNull Consumer<GuiClickEvent> action) {
this.action = action;
}

Expand Down Expand Up @@ -333,7 +333,6 @@ public static GuiItem loadItem(@NotNull Object instance, @NotNull Element elemen
throw new XMLLoadException("Can't find material for '" + id + "'");
}

boolean hasDamage = element.hasAttribute("damage");
int amount = 1;

if (element.hasAttribute("amount")) {
Expand All @@ -348,7 +347,7 @@ public static GuiItem loadItem(@NotNull Object instance, @NotNull Element elemen

if (element.hasAttribute("damage")) {
try {
amount = Short.parseShort(element.getAttribute("damage"));
damage = Short.parseShort(element.getAttribute("damage"));
} catch (NumberFormatException exception) {
throw new XMLLoadException("Damage attribute is not a short", exception);
}
Expand Down Expand Up @@ -481,7 +480,7 @@ else if (elementItem.hasAttribute("id")) {
}
}

Consumer<InventoryClickEvent> action = null;
Consumer<GuiClickEvent> action = null;

if (element.hasAttribute("onClick")) {
String methodName = element.getAttribute("onClick");
Expand All @@ -498,19 +497,17 @@ else if (elementItem.hasAttribute("id")) {
if (parameterCount == 0) {
action = event -> {
try {
//because reflection with lambdas is stupid
method.setAccessible(true);
method.invoke(instance);
} catch (IllegalAccessException | InvocationTargetException exception) {
throw new XMLReflectionException(exception);
}
};
found = true;
} else if (parameterTypes[0].isAssignableFrom(InventoryClickEvent.class)) {
} else if (parameterTypes[0].isAssignableFrom(GuiClickEvent.class)) {
if (parameterCount == 1) {
action = event -> {
try {
//because reflection with lambdas is stupid
method.setAccessible(true);
method.invoke(instance, event);
} catch (IllegalAccessException | InvocationTargetException exception) {
Expand All @@ -533,14 +530,11 @@ else if (elementItem.hasAttribute("id")) {
if (correct) {
action = event -> {
try {
//don't ask me why we need to do this, just roll with it (actually I do know why, but it's stupid)
properties.add(0, event);

//because reflection with lambdas is stupid
method.setAccessible(true);
method.invoke(instance, properties.toArray(new Object[0]));

//since we'll append the event to the list next time again, we need to remove it here again
properties.remove(0);
} catch (IllegalAccessException | InvocationTargetException exception) {
throw new XMLReflectionException(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.stefvanschie.inventoryframework.exception.XMLLoadException;
import com.github.stefvanschie.inventoryframework.pane.util.GuiItemContainer;
import com.github.stefvanschie.inventoryframework.pane.util.Slot;
import com.github.stefvanschie.inventoryframework.gui.GuiClickEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -162,7 +163,11 @@ public boolean click(@NotNull Gui gui, @NotNull GuiComponent guiComponent, @NotN
return false;
}

callOnClick(event);
callOnClick(new GuiClickEvent(event, slot));

if (this.cachedPositions == null) {
return false;
}

boolean success = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.github.stefvanschie.inventoryframework.pane.util.Slot;
import com.github.stefvanschie.inventoryframework.util.GeometryUtil;
import org.bukkit.Material;
import com.github.stefvanschie.inventoryframework.gui.GuiClickEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -120,12 +122,14 @@ public GuiItemContainer display() {
if (getOrientation() == Orientation.HORIZONTAL) {
size = getHeight();
} else if (getOrientation() == Orientation.VERTICAL) {
size = getHeight();
size = getLength();
} else {
throw new IllegalStateException("Unknown orientation '" + getOrientation() + "'");
}

for (int vectorIndex = 0; vectorIndex < size && getItems().size() > itemIndex; vectorIndex++) {
List<GuiItem> allItems = getItems();

for (int vectorIndex = 0; vectorIndex < size && allItems.size() > itemIndex; vectorIndex++) {
boolean[] maskLine;

if (getOrientation() == Orientation.HORIZONTAL) {
Expand All @@ -149,18 +153,18 @@ public GuiItemContainer display() {
if (doesRepeat()) {
items = new GuiItem[enabled];
} else {
int remainingPositions = gapCount + (getItems().size() - itemIndex - 1) * (getGap() + 1) + 1;
int remainingPositions = gapCount + (allItems.size() - itemIndex - 1) * (getGap() + 1) + 1;

items = new GuiItem[Math.min(enabled, remainingPositions)];
}

for (int index = 0; index < items.length; index++) {
if (gapCount == 0) {
items[index] = getItems().get(itemIndex);
items[index] = allItems.get(itemIndex);

itemIndex++;

if (doesRepeat() && itemIndex >= getItems().size()) {
if (doesRepeat() && itemIndex >= allItems.size()) {
itemIndex = 0;
}

Expand Down Expand Up @@ -241,7 +245,7 @@ public boolean click(@NotNull Gui gui, @NotNull GuiComponent guiComponent, @NotN
return false;
}

callOnClick(event);
callOnClick(new GuiClickEvent(event, slot));

ItemStack itemStack = event.getCurrentItem();

Expand All @@ -255,7 +259,7 @@ public boolean click(@NotNull Gui gui, @NotNull GuiComponent guiComponent, @NotN
return false;
}

item.callAction(event);
item.callAction(new GuiClickEvent(event, slot));

return true;
}
Expand Down Expand Up @@ -452,6 +456,19 @@ public List<GuiItem> getItems() {
return items;
}

@Nullable
@Contract(pure = true)
public GuiItem getGuiItem(@NotNull Slot slot) {
int x = slot.getX(getLength());
int y = slot.getY(getLength());

if (x < 0 || x >= getLength() || y < 0 || y >= getHeight()) {
return null;
}

return display().getItem(x, y);
}

/**
* Gets the mask applied to this pane.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.stefvanschie.inventoryframework.pane.util.Slot;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import com.github.stefvanschie.inventoryframework.gui.GuiClickEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -309,7 +310,7 @@ public boolean click(@NotNull Gui gui, @NotNull GuiComponent guiComponent, @NotN
return false;
}

callOnClick(event);
callOnClick(new GuiClickEvent(event, slot));

boolean success = false;

Expand Down Expand Up @@ -405,7 +406,7 @@ public Collection<Pane> getPanes() {
@NotNull
@Contract(pure = true)
public Collection<Pane> getPanes(int page) {
if (page < 0 || this.page >= this.panes.size()) {
if (page < 0 || page >= this.panes.size()) {
throw new IllegalArgumentException("Invalid page");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.UUIDTagType;
import com.github.stefvanschie.inventoryframework.util.XMLUtil;
import com.github.stefvanschie.inventoryframework.gui.GuiClickEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -49,7 +50,7 @@ public abstract class Pane {
* The consumer that will be called once a players clicks in this pane
*/
@Nullable
protected Consumer<? super InventoryClickEvent> onClick;
protected Consumer<? super GuiClickEvent> onClick;

/**
* A unique identifier for panes to locate them by
Expand Down Expand Up @@ -225,8 +226,9 @@ public static void load(@NotNull Pane pane, @NotNull Object instance, @NotNull E
if (element.hasAttribute("field"))
XMLUtil.loadFieldAttribute(instance, element, pane);

if (element.hasAttribute("onClick"))
pane.setOnClick(XMLUtil.loadOnEventAttribute(instance, element, InventoryClickEvent.class, "onClick"));
if (element.hasAttribute("onClick")) {
pane.setOnClick(XMLUtil.loadOnEventAttribute(instance, element, GuiClickEvent.class, "onClick"));
}

if (element.hasAttribute("populate")) {
String attribute = element.getAttribute("populate");
Expand Down Expand Up @@ -346,7 +348,7 @@ public Priority getPriority() {
* @param onClick the consumer that gets called
* @since 0.4.0
*/
public void setOnClick(@Nullable Consumer<? super InventoryClickEvent> onClick) {
public void setOnClick(@Nullable Consumer<? super GuiClickEvent> onClick) {
this.onClick = onClick;
}

Expand All @@ -357,19 +359,19 @@ public void setOnClick(@Nullable Consumer<? super InventoryClickEvent> onClick)
* @param event the event to handle
* @since 0.6.0
*/
protected void callOnClick(@NotNull InventoryClickEvent event) {
protected void callOnClick(@NotNull GuiClickEvent event) {
if (onClick == null) {
return;
}


try {
onClick.accept(event);
} catch (Throwable t) {
throw new RuntimeException(
"Exception while handling click event in inventory '"
+ InventoryViewUtil.getInstance().getTitle(event.getView()) + "', slot=" + event.getSlot() +
", for " + getClass().getSimpleName() + ", length=" + length + ", height=" + height,
+ InventoryViewUtil.getInstance().getTitle(event.getClickEvent().getView()) + "', slot="
+ event.getClickEvent().getSlot() + ", for " + getClass().getSimpleName() + ", length=" + length
+ ", height=" + height,
t
);
}
Expand Down
Loading