Skip to content

Commit

Permalink
add BukkitGUIProvider and make BukkitGUI standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Oct 25, 2024
1 parent a02b6e2 commit b5e641a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

import static me.hsgamer.hscore.bukkit.gui.BukkitGUIUtils.normalizeToChestSize;

/**
* The GUI for Bukkit
*/
public class BukkitGUI extends GUI implements InventoryHolder {
public class BukkitGUI extends GUI {
private final Inventory inventory;
private final InventorySize inventorySize;

Expand All @@ -36,28 +32,21 @@ public BukkitGUI(Inventory inventory) {
}

/**
* Create a GUI
* Open the inventory for the player
*
* @param inventoryType the inventory type
* @param size the size of the inventory if the inventory type is CHEST
* @param title the title of the inventory
* @param player the player
*/
public BukkitGUI(InventoryType inventoryType, int size, String title) {
if (inventoryType == InventoryType.CHEST) {
this.inventory = Bukkit.createInventory(this, normalizeToChestSize(size), title);
} else {
this.inventory = Bukkit.createInventory(this, inventoryType, title);
}
this.inventorySize = new BukkitInventorySize(inventory);
public void open(Player player) {
player.openInventory(inventory);
}

/**
* Open the inventory for the player
* Get the inventory of the GUI
*
* @param player the player
* @return the inventory
*/
public void open(Player player) {
player.openInventory(inventory);
public Inventory getInventory() {
return inventory;
}

@Override
Expand Down Expand Up @@ -87,9 +76,4 @@ public void stop() {
super.stop();
inventory.clear();
}

@Override
public Inventory getInventory() {
return inventory;
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,73 @@
package me.hsgamer.hscore.bukkit.gui;
package me.hsgamer.hscore.bukkit.gui.provider;

import me.hsgamer.hscore.bukkit.gui.BukkitGUI;
import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent;
import me.hsgamer.hscore.bukkit.gui.event.BukkitCloseEvent;
import me.hsgamer.hscore.bukkit.gui.event.BukkitDragEvent;
import me.hsgamer.hscore.bukkit.gui.event.BukkitOpenEvent;
import me.hsgamer.hscore.ui.property.Initializable;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;

/**
* The Listener for {@link BukkitGUI}
* Need to register this for others to work.
* The base class to provide {@link BukkitGUI}
*/
public class BukkitGUIListener implements Listener {
private final Plugin plugin;

private BukkitGUIListener(Plugin plugin, EventPriority clickPriority, EventPriority dragPriority, EventPriority openPriority, EventPriority closePriority) {
this.plugin = plugin;
registerEvent(InventoryClickEvent.class, clickPriority, this::onInventoryClick);
registerEvent(InventoryDragEvent.class, dragPriority, this::onInventoryDrag);
registerEvent(InventoryOpenEvent.class, openPriority, this::onInventoryOpen);
registerEvent(InventoryCloseEvent.class, closePriority, this::onInventoryClose);
registerEvent(PluginDisableEvent.class, EventPriority.MONITOR, this::onPluginDisable);
}
public abstract class BukkitGUIProvider implements Initializable, Listener {
protected final Plugin plugin;
private final EventPriority clickPriority;
private final EventPriority dragPriority;
private final EventPriority openPriority;
private final EventPriority closePriority;

/**
* Register the listener
* Create a new provider
*
* @param plugin the plugin
* @param clickPriority the priority of the click event
* @param dragPriority the priority of the drag event
* @param openPriority the priority of the open event
* @param closePriority the priority of the close event
* @param clickPriority the priority for click event
* @param dragPriority the priority for drag event
* @param openPriority the priority for open event
* @param closePriority the priority for close event
*/
public static BukkitGUIListener init(Plugin plugin, EventPriority clickPriority, EventPriority dragPriority, EventPriority openPriority, EventPriority closePriority) {
return new BukkitGUIListener(plugin, clickPriority, dragPriority, openPriority, closePriority);
protected BukkitGUIProvider(Plugin plugin, EventPriority clickPriority, EventPriority dragPriority, EventPriority openPriority, EventPriority closePriority) {
this.plugin = plugin;
this.clickPriority = clickPriority;
this.dragPriority = dragPriority;
this.openPriority = openPriority;
this.closePriority = closePriority;
}

/**
* Register the listener
* Create a new provider with the default priority
*
* @param plugin the plugin
*/
public static BukkitGUIListener init(Plugin plugin) {
return init(plugin, EventPriority.LOW, EventPriority.LOW, EventPriority.NORMAL, EventPriority.NORMAL);
protected BukkitGUIProvider(Plugin plugin) {
this(plugin, EventPriority.LOW, EventPriority.LOW, EventPriority.NORMAL, EventPriority.NORMAL);
}

private static void handleIfDisplay(InventoryEvent event, Consumer<BukkitGUI> consumer) {
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof BukkitGUI) {
consumer.accept((BukkitGUI) holder);
/**
* Get the GUI from the inventory
*
* @param inventory the inventory
*
* @return the GUI or null if not found
*/
protected abstract @Nullable BukkitGUI getGUI(Inventory inventory);

private void handleIfDisplay(InventoryEvent event, Consumer<BukkitGUI> consumer) {
Inventory inventory = event.getInventory();
BukkitGUI gui = getGUI(inventory);
if (gui != null) {
consumer.accept(gui);
}
}

Expand Down Expand Up @@ -94,9 +104,16 @@ private void onInventoryDrag(InventoryDragEvent event) {
handleIfDisplay(event, gui -> gui.handleEvent(new BukkitDragEvent(event)));
}

private void onPluginDisable(PluginDisableEvent event) {
if (event.getPlugin().equals(plugin)) {
HandlerList.unregisterAll(this);
}
@Override
public void init() {
registerEvent(InventoryClickEvent.class, clickPriority, this::onInventoryClick);
registerEvent(InventoryOpenEvent.class, openPriority, this::onInventoryOpen);
registerEvent(InventoryCloseEvent.class, closePriority, this::onInventoryClose);
registerEvent(InventoryDragEvent.class, dragPriority, this::onInventoryDrag);
}

@Override
public void stop() {
HandlerList.unregisterAll(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package me.hsgamer.hscore.bukkit.gui.provider;

import me.hsgamer.hscore.bukkit.gui.BukkitGUI;
import org.bukkit.Bukkit;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import static me.hsgamer.hscore.bukkit.gui.BukkitGUIUtils.normalizeToChestSize;

/**
* The {@link BukkitGUI} provider that uses {@link InventoryHolder} to store the GUI
*/
public class HolderBukkitGUIProvider extends BukkitGUIProvider {
/**
* {@inheritDoc}
*/
public HolderBukkitGUIProvider(Plugin plugin) {
super(plugin);
}

/**
* {@inheritDoc}
*/
public HolderBukkitGUIProvider(Plugin plugin, EventPriority clickPriority, EventPriority dragPriority, EventPriority openPriority, EventPriority closePriority) {
super(plugin, clickPriority, dragPriority, openPriority, closePriority);
}

/**
* Create a new {@link BukkitGUI}
*
* @param inventoryType the type of the inventory
* @param size the size of the inventory (only for CHEST)
* @param title the title of the inventory
*
* @return the new {@link BukkitGUI}
*/
public BukkitGUI create(InventoryType inventoryType, int size, String title) {
Holder holder = new Holder(plugin);
Inventory inventory;
if (inventoryType == InventoryType.CHEST) {
inventory = Bukkit.createInventory(holder, normalizeToChestSize(size), title);
} else {
inventory = Bukkit.createInventory(holder, inventoryType, title);
}
BukkitGUI gui = new BukkitGUI(inventory);
holder.gui = gui;
return gui;
}

@Override
protected @Nullable BukkitGUI getGUI(Inventory inventory) {
InventoryHolder inventoryHolder = inventory.getHolder();
if (!(inventoryHolder instanceof Holder)) {
return null;
}
Holder holder = (Holder) inventoryHolder;

if (holder.plugin != this.plugin) {
return null;
}

return holder.gui;
}

private static class Holder implements InventoryHolder {
private final Plugin plugin;
private BukkitGUI gui;

private Holder(Plugin plugin) {
this.plugin = plugin;
}

@Override
public Inventory getInventory() {
if (gui == null) {
throw new IllegalStateException("GUIHolder is not initialized");
}
return gui.getInventory();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the providers for the {@link me.hsgamer.hscore.bukkit.gui.BukkitGUI}
*/
package me.hsgamer.hscore.bukkit.gui.provider;

0 comments on commit b5e641a

Please sign in to comment.