-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acb5a5b
commit a755313
Showing
2 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.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,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
38
src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.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 |
---|---|---|
@@ -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) -> {}); | ||
} | ||
} |