Skip to content

Commit

Permalink
feat: menu lib (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzHilven authored Jul 1, 2024
1 parent b8e5f7b commit 527172e
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/slampvp/factory/FactoryServer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.slampvp.factory;

import com.slampvp.factory.command.FactoryCommand;
import com.slampvp.factory.common.menu.MenuListener;
import com.slampvp.factory.database.DatabaseManager;
import com.slampvp.factory.player.PlayerListener;
import com.slampvp.factory.plot.PlotGenerator;
Expand Down Expand Up @@ -41,6 +42,7 @@ public static void main(String[] args) {
instanceContainer.setTimeRate(0);

new PlayerListener(instanceContainer);
new MenuListener();

PlotManager.getInstance().init();
DatabaseManager.getInstance().init();
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/slampvp/factory/common/menu/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.slampvp.factory.common.menu;

import net.kyori.adventure.text.TextComponent;
import net.minestom.server.entity.Player;
import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;

public interface Menu {
@NotNull
TextComponent name();

@NotNull
InventoryType type();

@NotNull
List<MenuItem> items();

default Optional<MenuItem> itemBySlot(int slot) {
return items().stream()
.filter(menuItem -> menuItem.slots().contains(slot))
.findFirst();
}

default void open(Player player) {
MenuManager.getInstance().setOpenMenu(player, this);

Inventory inventory = new Inventory(type(), name());

items().forEach(menuItem -> menuItem.slots().forEach(slot -> inventory.setItemStack(slot, menuItem.itemStack())));

player.openInventory(inventory);
}

default void onOpen(InventoryOpenEvent event) {
}

default void onClose(InventoryCloseEvent event) {
}
}
88 changes: 88 additions & 0 deletions src/main/java/com/slampvp/factory/common/menu/MenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.slampvp.factory.common.menu;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.server.entity.Player;
import net.minestom.server.event.inventory.InventoryPreClickEvent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;

public record MenuItem(List<Integer> slots, ItemStack itemStack, ClickAction action) {
public static Function<Integer, MenuItem> CLOSE = slot ->
new MenuItem(slot,
ItemStack.of(Material.BARRIER)
.withCustomName(Component.text("Close").color(NamedTextColor.DARK_RED).decorate(TextDecoration.BOLD)),
event -> {
Player player = event.getPlayer();
player.closeInventory();
MenuManager.getInstance().clearOpenMenu(player);
});

public MenuItem(IntStream stream, ItemStack itemStack) {
this(stream.boxed().toList(), itemStack, new ClickAction() {
});
}

public MenuItem(IntStream stream, ItemStack itemStack, ClickAction action) {
this(stream.boxed().toList(), itemStack, action);
}

public MenuItem(IntStream stream, ItemStack itemStack, Consumer<InventoryPreClickEvent> consumer) {
this(stream.boxed().toList(), itemStack, true, consumer);
}

public MenuItem(IntStream stream, ItemStack itemStack, boolean cancel, Consumer<InventoryPreClickEvent> consumer) {
this(stream.boxed().toList(), itemStack, cancel, consumer);
}

public MenuItem(List<Integer> slots, ItemStack itemStack) {
this(slots, itemStack, new ClickAction() {
});
}

public MenuItem(List<Integer> slots, ItemStack itemStack, Consumer<InventoryPreClickEvent> consumer) {
this(slots, itemStack, true, consumer);
}

public MenuItem(List<Integer> slots, ItemStack itemStack, boolean cancel, Consumer<InventoryPreClickEvent> consumer) {
this(slots, itemStack, new ClickAction() {
@Override
public boolean cancel() {
return cancel;
}

@Override
public void onClick(InventoryPreClickEvent event) {
consumer.accept(event);
}
});
}

public MenuItem(int slot, ItemStack itemStack) {
this(List.of(slot), itemStack, new ClickAction() {
});
}

public MenuItem(int slot, ItemStack itemStack, Consumer<InventoryPreClickEvent> consumer) {
this(List.of(slot), itemStack, consumer);
}

public MenuItem(int slot, ItemStack itemStack, boolean cancel, Consumer<InventoryPreClickEvent> consumer) {
this(List.of(slot), itemStack, cancel, consumer);
}

public interface ClickAction {
default boolean cancel() {
return true;
}

default void onClick(InventoryPreClickEvent event) {
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/slampvp/factory/common/menu/MenuListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.slampvp.factory.common.menu;

import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.event.inventory.InventoryPreClickEvent;

import java.util.Optional;

public class MenuListener {
public MenuListener() {
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();

globalEventHandler.addListener(InventoryPreClickEvent.class, event -> {
Player player = event.getPlayer();
Optional<Menu> optionalMenu = MenuManager.getInstance().getOpenMenu(player);

if (optionalMenu.isEmpty()) {
return;
}

Menu menu = optionalMenu.get();
int slot = event.getSlot();

Optional<MenuItem> optionalMenuItem = menu.itemBySlot(slot);

if (optionalMenuItem.isEmpty()) {
return;
}

MenuItem item = optionalMenuItem.get();

if (item.action().cancel()) {
event.setCancelled(true);
}

item.action().onClick(event);
});

globalEventHandler.addListener(InventoryOpenEvent.class, event -> {
Player player = event.getPlayer();
Optional<Menu> optionalMenu = MenuManager.getInstance().getOpenMenu(player);

if (optionalMenu.isEmpty()) {
return;
}

Menu menu = optionalMenu.get();
menu.onOpen(event);
});

globalEventHandler.addListener(InventoryCloseEvent.class, event -> {
Player player = event.getPlayer();
Optional<Menu> optionalMenu = MenuManager.getInstance().getOpenMenu(player);

if (optionalMenu.isEmpty()) {
return;
}

Menu menu = optionalMenu.get();
menu.onClose(event);
});
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/slampvp/factory/common/menu/MenuManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.slampvp.factory.common.menu;

import net.minestom.server.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public final class MenuManager {
private static MenuManager instance;
private final Map<UUID, Menu> openMenus;

private MenuManager() {
this.openMenus = new HashMap<>();
}

public static synchronized MenuManager getInstance() {
if (instance == null) {
instance = new MenuManager();
}
return instance;
}

public Optional<Menu> getOpenMenu(Player player) {
return Optional.ofNullable(openMenus.get(player.getUuid()));
}

public void setOpenMenu(Player player, Menu menu) {
openMenus.put(player.getUuid(), menu);
}

public void clearOpenMenu(Player player) {
openMenus.remove(player.getUuid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minestom.server.instance.InstanceContainer;

public class PlayerListener {

public PlayerListener(InstanceContainer instanceContainer) {
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();

Expand Down

0 comments on commit 527172e

Please sign in to comment.