-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add BukkitGUIProvider and make BukkitGUI standalone
- Loading branch information
Showing
4 changed files
with
149 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/provider/HolderBukkitGUIProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/provider/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |