Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
run: chmod +x ./gradlew

- name: Build with Gradle Wrapper
run: ./gradlew build -PStaticStudiosUsername=github -PStaticStudiosPassword=${{ secrets.REPOSITORY_SECRET }}
run: ./gradlew :menus:build -PStaticStudiosUsername=github -PStaticStudiosPassword=${{ secrets.REPOSITORY_SECRET }}
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
distribution: 'temurin'
cache: 'gradle'
- name: Publish to Private Repository
run: ./gradlew publish -PStaticStudiosUsername=github -PStaticStudiosPassword=${{ secrets.REPOSITORY_SECRET }}
run: ./gradlew :menus:publish -PStaticStudiosUsername=github -PStaticStudiosPassword=${{ secrets.REPOSITORY_SECRET }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

run
TestCommand.java
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.staticstudios.menus.history.MenuHistory;
import net.staticstudios.menus.listener.MenuListener;
import net.staticstudios.menus.parser.MenuRegistry;
import net.staticstudios.menus.parser.YamlMenuParser;
import net.staticstudios.menus.viewer.MenuViewer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -17,11 +19,15 @@ public class StaticMenus {
private static Function<Player, MenuViewer> getViewerFunction;
private static JavaPlugin plugin;
private static MiniMessage miniMessage;
private static MenuRegistry registry;
private static YamlMenuParser parser;

public static void enable(JavaPlugin plugin, MiniMessage miniMessage, Function<Player, MenuViewer> getViewerFunction) {
StaticMenus.plugin = plugin;
StaticMenus.getViewerFunction = getViewerFunction;
StaticMenus.miniMessage = miniMessage;
StaticMenus.registry = new MenuRegistry();
StaticMenus.parser = new YamlMenuParser();

Bukkit.getPluginManager().registerEvents(new MenuListener(), plugin);
}
Expand All @@ -41,4 +47,12 @@ public static MiniMessage getMiniMessage() {
public static MenuViewer getViewer(Player player) {
return getViewerFunction.apply(player);
}

public static MenuRegistry getRegistry() {
return registry;
}

public static YamlMenuParser getParser() {
return parser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.function.Function;

public interface ButtonAction {
public interface ButtonAction extends ViewerAction {
static ButtonAction openMenu(Menu menu) {
return new OpenMenuAction(menuViewer -> menu);
}
Expand All @@ -14,9 +14,19 @@ static ButtonAction openMenu(Function<MenuViewer, Menu> menuSupplier) {
return new OpenMenuAction(menuSupplier);
}

static ButtonAction closeMenu() {
return new CloseMenuAction();
}

static ButtonAction goBack() {
return new GoBackAction();
}

void invoke(MenuViewer viewer);
static ButtonAction sendMessage(String message) {
return new SendMessageAction(message);
}

static ButtonAction command(String command) {
return new CommandAction(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.staticstudios.menus.action;

import net.staticstudios.menus.viewer.MenuViewer;

public class CloseMenuAction implements ButtonAction {
@Override
public void invoke(MenuViewer viewer) {
viewer.closeMenu();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.staticstudios.menus.action;

import net.staticstudios.menus.viewer.MenuViewer;

public class CommandAction implements ButtonAction {
private final String command;

protected CommandAction(String command) {
this.command = command;
}

@Override
public void invoke(MenuViewer viewer) {
viewer.getPlayer().performCommand(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected GoBackAction() {
@Override
public void invoke(MenuViewer viewer) {
StaticMenus.getHistory(viewer).pop();
Menu menuToOpen = StaticMenus.getHistory(viewer).peek();
Menu menuToOpen = StaticMenus.getHistory(viewer).isEmpty() ? null : StaticMenus.getHistory(viewer).peek();

if (menuToOpen == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.staticstudios.menus.action;

import net.staticstudios.menus.viewer.MenuViewer;

public class SendMessageAction implements ButtonAction {
private final String message;

protected SendMessageAction(String message) {
this.message = message;
}

@Override
public void invoke(MenuViewer viewer) {
viewer.sendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.staticstudios.menus.viewer.MenuViewer;

public interface MenuAction {
public interface ViewerAction {

void invoke(MenuViewer viewer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.history.MenuHistory;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
import org.bukkit.inventory.InventoryHolder;
Expand Down Expand Up @@ -60,7 +61,8 @@ default void updateButton(int slot, Function<@NotNull Button, @NotNull Button> u
* Open the menu
*/
default void open() {
Menu previous = StaticMenus.getHistory(getViewer()).peek();
MenuHistory history = StaticMenus.getHistory(getViewer());
Menu previous = history.isEmpty() ? null : history.peek();
open(false, previous == null || !previous.getId().equals(this.getId())); //if we are opening a new menu, push it to the history
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.kyori.adventure.text.Component;
import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.action.MenuAction;
import net.staticstudios.menus.action.ViewerAction;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
Expand All @@ -18,7 +18,7 @@
public class PagedMenu implements Menu {
private final Component inventoryTitle;
private final String id;
private final Map<Action, List<MenuAction>> actions;
private final Map<Action, List<ViewerAction>> actions;
private final List<Button> buttons;
private final MenuViewer viewer;
private final MenuOptions options;
Expand All @@ -33,7 +33,7 @@ public class PagedMenu implements Menu {
private int previousPageButtonIndex = -1;
private List<Button[]> currentPageButtons;

public PagedMenu(String id, MenuViewer viewer, Component inventoryTitle, @NotNull Map<Action, List<MenuAction>> actions, String template, char marker, Button fallback, boolean shrinkToFit, List<Button> buttons, @NotNull Map<Character, Button> buttonMappings, @NotNull MenuOptions options) {
public PagedMenu(String id, MenuViewer viewer, Component inventoryTitle, @NotNull Map<Action, List<ViewerAction>> actions, String template, char marker, Button fallback, boolean shrinkToFit, List<Button> buttons, @NotNull Map<Character, Button> buttonMappings, @NotNull MenuOptions options) {
this.inventoryTitle = inventoryTitle;
this.id = id;
this.actions = actions;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void open(boolean clearHistory, boolean pushToHistory) {
StaticMenus.getHistory(viewer).replace(this);
}

actions.getOrDefault(Action.OPEN, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
actions.getOrDefault(Menu.Action.OPEN, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
viewer.getPlayer().openInventory(getInventory());
}

Expand All @@ -129,7 +129,7 @@ public int getPage() {
public void previousPage() {
if (page > 0) {
page--;
actions.getOrDefault(Action.PREVIOUS_PAGE, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
actions.getOrDefault(Menu.Action.PREVIOUS_PAGE, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
updateInventory();
}
}
Expand All @@ -139,7 +139,7 @@ public void nextPage() {

if (page < maxPage) {
page++;
actions.getOrDefault(Action.NEXT_PAGE, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
actions.getOrDefault(Menu.Action.NEXT_PAGE, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
updateInventory();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.kyori.adventure.text.Component;
import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.action.MenuAction;
import net.staticstudios.menus.action.ViewerAction;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
Expand All @@ -12,7 +12,7 @@

public class PagedMenuBuilder implements Cloneable, MenuBuilder {
private final boolean mutable;
private final Map<Menu.Action, List<MenuAction>> actions = new HashMap<>();
private final Map<Menu.Action, List<ViewerAction>> actions = new HashMap<>();
private final Map<Character, Button> buttonMappings = new HashMap<>();
private final List<Button> buttons = new ArrayList<>();
private final String template;
Expand Down Expand Up @@ -75,9 +75,9 @@ public PagedMenuBuilder title(String title) {
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onOpen(MenuAction action) {
public PagedMenuBuilder onOpen(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.OPEN, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.OPEN, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.OPEN, actions);
return builder;
Expand All @@ -89,9 +89,9 @@ public PagedMenuBuilder onOpen(MenuAction action) {
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onClose(MenuAction action) {
public PagedMenuBuilder onClose(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.CLOSE, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.CLOSE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.CLOSE, actions);
return builder;
Expand All @@ -103,9 +103,9 @@ public PagedMenuBuilder onClose(MenuAction action) {
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onNextPage(MenuAction action) {
public PagedMenuBuilder onNextPage(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.NEXT_PAGE, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.NEXT_PAGE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.NEXT_PAGE, actions);
return builder;
Expand All @@ -117,9 +117,9 @@ public PagedMenuBuilder onNextPage(MenuAction action) {
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onPreviousPage(MenuAction action) {
public PagedMenuBuilder onPreviousPage(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.PREVIOUS_PAGE, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.PREVIOUS_PAGE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.PREVIOUS_PAGE, actions);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.kyori.adventure.text.Component;
import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.action.MenuAction;
import net.staticstudios.menus.action.ViewerAction;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
Expand All @@ -16,14 +16,14 @@
public class SimpleMenu implements Menu {
private final Component inventoryTitle;
private final String id;
private final Map<Action, List<MenuAction>> actions;
private final Map<Action, List<ViewerAction>> actions;
private final int size;
private final List<Button> buttons;
private final MenuViewer viewer;
private final MenuOptions options;
private Inventory inventory;

public SimpleMenu(String id, MenuViewer viewer, Component inventoryTitle, int size, @NotNull Map<Action, List<MenuAction>> actions, @NotNull List<Button> buttons, @NotNull MenuOptions options) {
public SimpleMenu(String id, MenuViewer viewer, Component inventoryTitle, int size, @NotNull Map<Action, List<ViewerAction>> actions, @NotNull List<Button> buttons, @NotNull MenuOptions options) {
this.inventoryTitle = inventoryTitle;
this.id = id;
this.actions = actions;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void open(boolean clearHistory, boolean pushToHistory) {
StaticMenus.getHistory(viewer).replace(this);
}

actions.getOrDefault(Action.OPEN, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
actions.getOrDefault(Menu.Action.OPEN, List.of()).forEach(menuAction -> menuAction.invoke(viewer));
viewer.getPlayer().openInventory(getInventory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.kyori.adventure.text.Component;
import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.action.MenuAction;
import net.staticstudios.menus.action.ViewerAction;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
Expand All @@ -15,7 +15,7 @@

public class SimpleMenuBuilder implements Cloneable, MenuBuilder {
private final boolean mutable;
private final Map<Menu.Action, List<MenuAction>> actions = new HashMap<>();
private final Map<Menu.Action, List<ViewerAction>> actions = new HashMap<>();
private String id;
private int size = -1;
private Component title;
Expand Down Expand Up @@ -115,9 +115,9 @@ public SimpleMenuBuilder setButton(int index, Button button) {
* @param action The action
* @return The builder
*/
public SimpleMenuBuilder onOpen(MenuAction action) {
public SimpleMenuBuilder onOpen(ViewerAction action) {
SimpleMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.OPEN, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.OPEN, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.OPEN, actions);
return builder;
Expand All @@ -129,9 +129,9 @@ public SimpleMenuBuilder onOpen(MenuAction action) {
* @param action The action
* @return The builder
*/
public SimpleMenuBuilder onClose(MenuAction action) {
public SimpleMenuBuilder onClose(ViewerAction action) {
SimpleMenuBuilder builder = clone();
List<MenuAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.CLOSE, new ArrayList<>()));
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.CLOSE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.CLOSE, actions);
return builder;
Expand Down
Loading