Skip to content

Commit

Permalink
Add basic GUI framework
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 27, 2024
1 parent 46fa18c commit acb5a5b
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.cjburkey.claimchunk.data.newdata.*;
import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler;
import com.cjburkey.claimchunk.event.*;
import com.cjburkey.claimchunk.gui.CCGuiHandler;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;
import com.cjburkey.claimchunk.layer.PlaceholderInitLayer;
import com.cjburkey.claimchunk.layer.PrereqsInitLayer;
Expand Down Expand Up @@ -108,6 +109,7 @@ public final class ClaimChunk extends JavaPlugin implements IClaimChunkPlugin {
// The main handler (may not always be here, please don't rely on this)
@Getter private MainHandler mainHandler;
@Getter private ChunkOutlineHandler chunkOutlineHandler;
@Getter private CCGuiHandler guiHandler;

// Config conversion storage
private FromPre0023 fromPre0023;
Expand Down Expand Up @@ -533,10 +535,13 @@ private void setupConfig() {
}

private void setupEvents() {
guiHandler = new CCGuiHandler();

// Register all the event handlers
getServer().getPluginManager().registerEvents(new PlayerConnectionHandler(this), this);
getServer().getPluginManager().registerEvents(new PlayerMovementHandler(this), this);
getServer().getPluginManager().registerEvents(new WorldProfileEventHandler(this), this);
getServer().getPluginManager().registerEvents(guiHandler, this);
}

private void setupNewCommands() {
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.cjburkey.claimchunk.gui;

import com.cjburkey.claimchunk.Utils;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;

import java.util.HashMap;
import java.util.UUID;

/**
* The handler behind ClaimChunk GUI screens. This class acts as the interface for opening GUIs as
* well as the event listener for players clicking in/closing the screens.
*
* @since 0.0.26
*/
public class CCGuiHandler implements Listener {

private final HashMap<UUID, CCOpenGui> openGuis = new HashMap<>();

@EventHandler
public void onGuiClick(InventoryClickEvent e) {
UUID uuid = e.getWhoClicked().getUniqueId();
CCOpenGui openGui = openGuis.get(uuid);
if (openGui != null) {
if (e.getInventory().equals(openGui.inventory())) {
openGui.gui()
.onClick(
openGui.inventory(),
(Player) e.getWhoClicked(),
e.getSlot(),
e.getClick(),
e.getCurrentItem());
}
e.setCancelled(true);
}
}

@EventHandler
public void onGuiClose(InventoryCloseEvent e) {
if (openGuis.containsKey(e.getPlayer().getUniqueId())) {
closeGui((Player) e.getPlayer());
}
}

@SuppressWarnings("unused")
public void openGui(Player player, ICCGui gui) {
if (openGuis.containsKey(player.getUniqueId())) {
closeGui(player);
}
openGuis.put(player.getUniqueId(), new CCOpenGui(gui, createAndShowGui(player, gui)));
gui.onOpen(openGuis.get(player.getUniqueId()).inventory(), player);
}

public void closeGui(Player player) {
if (openGuis.containsKey(player.getUniqueId())) {
openGuis.get(player.getUniqueId())
.gui()
.onClose(openGuis.get(player.getUniqueId()).inventory(), player);
openGuis.remove(player.getUniqueId());
}
}

private static Inventory createAndShowGui(Player player, ICCGui gui) {
int rowCount = Math.min(Math.max(gui.getRows(), 1), 6);
Inventory inventory =
Bukkit.createInventory(player, rowCount * 9, Utils.color(gui.getName()));
player.openInventory(inventory);
return inventory;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/CCOpenGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cjburkey.claimchunk.gui;

import org.bukkit.inventory.Inventory;

record CCOpenGui(ICCGui gui, Inventory inventory) {}
59 changes: 59 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.cjburkey.claimchunk.gui;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Interface representing a ClaimChunk GUI screen.
*
* @since 0.0.26
*/
public interface ICCGui {

/**
* Called when this screen is shown to a player.
*
* @param inventory The inventory behind this GUI
* @param player The player the GUI is shown to
*/
void onOpen(@NotNull Inventory inventory, @NotNull Player player);

/**
* Called when this screen is being closed.
*
* @param inventory The inventory behind this GUI
* @param player The player closing the GUI
*/
void onClose(@NotNull Inventory inventory, @NotNull Player player);

/**
* Called when the player clicks on a given slot within the inventory.
*
* @param inventory The inventory behind this GUI
* @param player The player clicking in the GUI being shown to them.
* @param slot The index of the slot being clicked.
* @param clickType Which type of click the player performed.
* @param stack The stack on which the player clicked.
*/
void onClick(
@NotNull Inventory inventory,
@NotNull Player player,
int slot,
@NotNull ClickType clickType,
@Nullable ItemStack stack);

/**
* @return The name to be shown at the top of the GUI
*/
@NotNull
String getName();

/**
* @return The number of rows this GUI should have
*/
int getRows();
}
36 changes: 36 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.cjburkey.claimchunk.gui.screens;

import com.cjburkey.claimchunk.gui.ICCGui;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public class MainMenu implements ICCGui {

@Override
public void onOpen(@NotNull Inventory inventory, @NotNull Player player) {}

@Override
public void onClose(@NotNull Inventory inventory, @NotNull Player player) {}

@Override
public void onClick(
@NotNull Inventory inventory,
@NotNull Player player,
int slot,
@NotNull ClickType clickType,
@NotNull ItemStack stack) {}

@Override
public @NotNull String getName() {
return "ClaimChunk GUI";
}

@Override
public int getRows() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public final class V2JsonMessages {
public String cmdClaim = "Claim the chunk you're standing in";
public String cmdHelp = "Display ClaimChunk help (for [command], if supplied)";
public String cmdInfo = "Display information about the current chunk";
public String cmdGui = "Open the ClaimChunk GUI";
public String cmdList = "Display a paginated list of all your claims in the world";
public String cmdName = "Change the name that appears when someone enters your land";
public String cmdReload = "Reload the config for ClaimChunk";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public ClaimChunkBaseCommand(ClaimChunk claimChunk) {
// `/chunk unclaim`
new CommandStr(new UnclaimCmd(claimChunk), "unclaim"),
// `/chunk scan`
new CommandStr(new ScanCmd(claimChunk), "scan"));
new CommandStr(new ScanCmd(claimChunk), "scan"),
// `/chunk gui`
new CommandStr(new GuiCmd(claimChunk), "gui"));

// Admin commands
registerCmds(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cjburkey.claimchunk.smartcommand.sub.ply;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.gui.screens.MainMenu;
import com.cjburkey.claimchunk.smartcommand.CCSubCommand;

import de.goldmensch.commanddispatcher.Executor;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @since 0.0.23
*/
public class GuiCmd extends CCSubCommand {

public GuiCmd(ClaimChunk claimChunk) {
super(claimChunk, Executor.PLAYER, true, "player");
}

@Override
public @Nullable String getDescription() {
return claimChunk.getMessages().cmdGui;
}

@Override
public CCArg[] getPermittedArguments() {
return new CCArg[0];
}

@Override
public int getRequiredArguments() {
return 0;
}

@Override
public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender executor, String[] args) {
claimChunk.getGuiHandler().openGui((Player) executor, new MainMenu());
return true;
}
}

0 comments on commit acb5a5b

Please sign in to comment.