Skip to content

Commit

Permalink
Make abstract inventory menu class
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 27, 2024
1 parent acb5a5b commit a755313
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 23 deletions.
94 changes: 94 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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;

/**
* An abstract wrapper for {@link ICCGui} to make creating inventory menus easier.
*
* @since 0.0.26
*/
public abstract class GuiMenuScreen implements ICCGui {

/** Anonymous callback for item click event. */
@FunctionalInterface
public interface GuiItemAction {

/** Called when the item is clicked. */
void onClick(
@NotNull Inventory inventory,
@NotNull Player player,
int slot,
@NotNull ClickType clickType,
@NotNull ItemStack stack);
}

/** Wrapper around an interactive item in the GUI menu. */
record GuiItemWrapper(@NotNull ItemStack stack, @NotNull GuiItemAction action) {}

private final int rowCount;
private final GuiItemWrapper[] actions;
private final String name;

protected GuiMenuScreen(int rowCount, @NotNull String name) {
this.rowCount = Math.min(Math.max(rowCount, 1), 6);
this.actions = new GuiItemWrapper[this.rowCount * 9];
this.name = name;
}

/**
* Add an interactive button item to this inventory GUI.
*
* @param slot The slot of the inventory in which to put the item.
* @param stack The stack that represents this action.
* @param action The on-click callback
*/
protected void addInteractiveButton(
int slot, @NotNull ItemStack stack, @NotNull GuiItemAction action) {
if (slot >= 0 && slot < this.actions.length) {
this.actions[slot] = new GuiItemWrapper(stack, action);
}
}

@Override
public void onOpen(@NotNull Inventory inventory, @NotNull Player player) {
for (int slot = 0; slot < actions.length; slot++) {
GuiItemWrapper action = actions[slot];
if (action != null) {
inventory.setItem(slot, action.stack());
}
}
}

@Override
public void onClick(
@NotNull Inventory inventory,
@NotNull Player player,
int slot,
@NotNull ClickType clickType,
@Nullable ItemStack stack) {
assert slot >= 0 && slot < actions.length;

GuiItemWrapper action = actions[slot];
if (action != null && stack != null) {
action.action().onClick(inventory, player, slot, clickType, stack);
}
}

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

@Override
public @NotNull String getName() {
return name;
}

@Override
public int getRows() {
return rowCount;
}
}
38 changes: 15 additions & 23 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
package com.cjburkey.claimchunk.gui.screens;

import com.cjburkey.claimchunk.gui.ICCGui;
import com.cjburkey.claimchunk.gui.GuiMenuScreen;

import org.bukkit.Material;
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 {
public class MainMenu extends GuiMenuScreen {

@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";
public MainMenu() {
super(1, "ClaimChunk Menu");
addButtons();
}

@Override
public int getRows() {
return 0;
private void addButtons() {
addInteractiveButton(
0,
new ItemStack(Material.PAPER),
(Inventory inventory,
Player player,
int slot,
ClickType clickType,
ItemStack stack) -> {});
}
}

0 comments on commit a755313

Please sign in to comment.