|
@@ -12,6 +12,7 @@ import org.bukkit.inventory.Inventory;
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
|
+import org.jetbrains.annotations.ApiStatus;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -25,8 +26,11 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
private final String title;
|
|
private final String title;
|
|
|
private final int size;
|
|
private final int size;
|
|
|
private final Inventory inventory;
|
|
private final Inventory inventory;
|
|
|
-
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Slots with the action to trigger upon clicking
|
|
|
|
|
+ */
|
|
|
private final Map<Integer, BiConsumer<Player, ClickType>> slotActions = new HashMap<>();
|
|
private final Map<Integer, BiConsumer<Player, ClickType>> slotActions = new HashMap<>();
|
|
|
|
|
+
|
|
|
private final Stack<InventoryGUI> previousGuis = new Stack<>();
|
|
private final Stack<InventoryGUI> previousGuis = new Stack<>();
|
|
|
/** Slots that the player may edit (drag‑and‑drop, shift‑click, etc.) */
|
|
/** Slots that the player may edit (drag‑and‑drop, shift‑click, etc.) */
|
|
|
private final Set<Integer> editableSlots = new HashSet<>();
|
|
private final Set<Integer> editableSlots = new HashSet<>();
|
|
@@ -84,6 +88,7 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
onPlayerInventoryClick(player, event);
|
|
onPlayerInventoryClick(player, event);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
public void onPlayerInventoryClick(Player player, InventoryClickEvent event){}
|
|
public void onPlayerInventoryClick(Player player, InventoryClickEvent event){}
|
|
|
|
|
|
|
|
private void runClickActionIfPresent(Player player, int rawSlot, ClickType click) {
|
|
private void runClickActionIfPresent(Player player, int rawSlot, ClickType click) {
|
|
@@ -99,18 +104,39 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
inventory.setItem(slot, item);
|
|
inventory.setItem(slot, item);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param slot The slot Number
|
|
|
|
|
+ * @return The row this slot is in
|
|
|
|
|
+ */
|
|
|
protected int getRow(int slot) {
|
|
protected int getRow(int slot) {
|
|
|
return slot / 9;
|
|
return slot / 9;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param slot The slot Number
|
|
|
|
|
+ * @return The column this slot is in
|
|
|
|
|
+ */
|
|
|
protected int getColumn(int slot) {
|
|
protected int getColumn(int slot) {
|
|
|
return slot % 9;
|
|
return slot % 9;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param row The row Number
|
|
|
|
|
+ * @param column The column Number
|
|
|
|
|
+ * @return The slot this corresponds to
|
|
|
|
|
+ */
|
|
|
protected int getSlot(int row, int column) {
|
|
protected int getSlot(int row, int column) {
|
|
|
return row * 9 + column;
|
|
return row * 9 + column;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ */
|
|
|
protected List<Integer> computeRectangleSlots(int startSlot, int endSlot) {
|
|
protected List<Integer> computeRectangleSlots(int startSlot, int endSlot) {
|
|
|
|
|
|
|
|
List<Integer> slots = new ArrayList<>();
|
|
List<Integer> slots = new ArrayList<>();
|
|
@@ -132,6 +158,13 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
return slots;
|
|
return slots;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Copies the same Click Action onto multiple slots
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ * @param item The items to fill this Rectangle with
|
|
|
|
|
+ * @param action The action to perform upon clicking any of these slots
|
|
|
|
|
+ */
|
|
|
public void fillRectangleWithClickAction(int startSlot, int endSlot, ItemStack item, BiConsumer<Player, ClickType> action) {
|
|
public void fillRectangleWithClickAction(int startSlot, int endSlot, ItemStack item, BiConsumer<Player, ClickType> action) {
|
|
|
int startRow = getRow(startSlot);
|
|
int startRow = getRow(startSlot);
|
|
|
int startCol = getColumn(startSlot);
|
|
int startCol = getColumn(startSlot);
|
|
@@ -146,7 +179,12 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Copies the same {@link ItemStack} onto multiple slots
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ * @param item The items to fill this Rectangle with
|
|
|
|
|
+ */
|
|
|
public void fillRectangle(int startSlot, int endSlot, ItemStack item) {
|
|
public void fillRectangle(int startSlot, int endSlot, ItemStack item) {
|
|
|
int startRow = getRow(startSlot);
|
|
int startRow = getRow(startSlot);
|
|
|
int startCol = getColumn(startSlot);
|
|
int startCol = getColumn(startSlot);
|
|
@@ -161,6 +199,11 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets a full Rectangle to be editable
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ */
|
|
|
public void setRectangleEditable(int startSlot, int endSlot) {
|
|
public void setRectangleEditable(int startSlot, int endSlot) {
|
|
|
int startRow = getRow(startSlot);
|
|
int startRow = getRow(startSlot);
|
|
|
int startCol = getColumn(startSlot);
|
|
int startCol = getColumn(startSlot);
|
|
@@ -203,7 +246,6 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
ItemStack background = new ItemStack(material);
|
|
ItemStack background = new ItemStack(material);
|
|
|
ItemMeta meta = background.getItemMeta();
|
|
ItemMeta meta = background.getItemMeta();
|
|
|
meta.displayName(Component.text(displayName));
|
|
meta.displayName(Component.text(displayName));
|
|
|
- meta.setHideTooltip(true);
|
|
|
|
|
background.setItemMeta(meta);
|
|
background.setItemMeta(meta);
|
|
|
for (int i = 0; i < size; i++) {
|
|
for (int i = 0; i < size; i++) {
|
|
|
if (inventory.getItem(i) == null) {
|
|
if (inventory.getItem(i) == null) {
|
|
@@ -211,11 +253,18 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Quick way to open this GUI with the correct Listeners, so it actually works
|
|
|
|
|
+ * @param player The player to open the Inventory for
|
|
|
|
|
+ */
|
|
|
public void openWithListener(Player player){
|
|
public void openWithListener(Player player){
|
|
|
InventoryManager.openFor(player, this);
|
|
InventoryManager.openFor(player, this);
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* Opens this GUI for a player.
|
|
* Opens this GUI for a player.
|
|
|
|
|
+ * <br>NOTE: This doesn't make the Inventory responsive by itself, since it only displays the container to the player.
|
|
|
|
|
+ * <br>for that, use {@code openWithListener(player);}
|
|
|
*/
|
|
*/
|
|
|
public void open(Player player) {
|
|
public void open(Player player) {
|
|
|
buildContent();
|
|
buildContent();
|
|
@@ -272,7 +321,6 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Also need to update the overloaded version:
|
|
|
|
|
public void openPrevious(Player player, ClickType type) {
|
|
public void openPrevious(Player player, ClickType type) {
|
|
|
openPrevious(player);
|
|
openPrevious(player);
|
|
|
}
|
|
}
|
|
@@ -282,9 +330,23 @@ public abstract class InventoryGUI implements InventoryHolder {
|
|
|
return inventory;
|
|
return inventory;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Builds the Content of the Inventory, called everytime a player is shown the GUI
|
|
|
|
|
+ */
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
public abstract void buildContent();
|
|
public abstract void buildContent();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Usually only called when traversing through multiple GUIs
|
|
|
|
|
+ */
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
public abstract void update();
|
|
public abstract void update();
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * This interface can be implemented as well to handle cases where the player closes the inventory
|
|
|
|
|
+ * <br>You can e.g. stop this behavior etc.
|
|
|
|
|
+ */
|
|
|
|
|
+ @FunctionalInterface
|
|
|
public interface AutoCloseHandler {
|
|
public interface AutoCloseHandler {
|
|
|
void onClosedByPlayer(Player player);
|
|
void onClosedByPlayer(Player player);
|
|
|
}
|
|
}
|