From 444fc3f5bd88c2b8fca6d8bac3b6c50f845a75c7 Mon Sep 17 00:00:00 2001 From: Matz Hilven Date: Mon, 1 Jul 2024 14:36:37 +0200 Subject: [PATCH 1/4] init --- .../com/slampvp/factory/FactoryServer.java | 2 + .../command/generic/gamemode/Menu.java | 24 +++++++ .../com/slampvp/factory/common/menu/Menu.java | 46 +++++++++++++ .../slampvp/factory/common/menu/MenuItem.java | 27 ++++++++ .../factory/common/menu/MenuListener.java | 66 +++++++++++++++++++ .../factory/common/menu/MenuManager.java | 36 ++++++++++ .../slampvp/factory/common/menu/TestMenu.java | 64 ++++++++++++++++++ .../factory/player/PlayerListener.java | 1 - 8 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java create mode 100644 src/main/java/com/slampvp/factory/common/menu/Menu.java create mode 100644 src/main/java/com/slampvp/factory/common/menu/MenuItem.java create mode 100644 src/main/java/com/slampvp/factory/common/menu/MenuListener.java create mode 100644 src/main/java/com/slampvp/factory/common/menu/MenuManager.java create mode 100644 src/main/java/com/slampvp/factory/common/menu/TestMenu.java diff --git a/src/main/java/com/slampvp/factory/FactoryServer.java b/src/main/java/com/slampvp/factory/FactoryServer.java index 7c612d2..d159e3a 100644 --- a/src/main/java/com/slampvp/factory/FactoryServer.java +++ b/src/main/java/com/slampvp/factory/FactoryServer.java @@ -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; @@ -41,6 +42,7 @@ public static void main(String[] args) { instanceContainer.setTimeRate(0); new PlayerListener(instanceContainer); + new MenuListener(); PlotManager.getInstance().init(); DatabaseManager.getInstance().init(); diff --git a/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java b/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java new file mode 100644 index 0000000..346bae2 --- /dev/null +++ b/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java @@ -0,0 +1,24 @@ +package com.slampvp.factory.command.generic.gamemode; + +import com.slampvp.factory.command.Command; +import com.slampvp.factory.command.FactoryCommand; +import com.slampvp.factory.common.menu.TestMenu; +import com.slampvp.factory.player.Rank; +import net.minestom.server.entity.GameMode; +import net.minestom.server.entity.Player; + +@Command(description = "Open menu.", usage = "/menu", minimumRank = Rank.ADMIN, playerOnly = true) +public class Menu extends FactoryCommand { + public Menu() { + super("menu"); + } + + @Override + public void init() { + addSyntax((sender, context) -> { + Player player = (Player) sender; + + new TestMenu().open(player); + }); + } +} diff --git a/src/main/java/com/slampvp/factory/common/menu/Menu.java b/src/main/java/com/slampvp/factory/common/menu/Menu.java new file mode 100644 index 0000000..4456223 --- /dev/null +++ b/src/main/java/com/slampvp/factory/common/menu/Menu.java @@ -0,0 +1,46 @@ +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 items(); + + default Optional 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) { + } + +} diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java new file mode 100644 index 0000000..0000eb7 --- /dev/null +++ b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java @@ -0,0 +1,27 @@ +package com.slampvp.factory.common.menu; + +import net.minestom.server.event.inventory.InventoryPreClickEvent; +import net.minestom.server.item.ItemStack; + +import java.util.List; + +public interface MenuItem { + + List slots(); + + ItemStack itemStack(); + + default ClickAction action() { + return new ClickAction() { + }; + } + + interface ClickAction { + default boolean canEquip() { + return false; + } + + default void onClick(InventoryPreClickEvent event) { + } + } +} diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuListener.java b/src/main/java/com/slampvp/factory/common/menu/MenuListener.java new file mode 100644 index 0000000..cab6e4e --- /dev/null +++ b/src/main/java/com/slampvp/factory/common/menu/MenuListener.java @@ -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 optionalMenu = MenuManager.getInstance().getOpenMenu(player); + + if (optionalMenu.isEmpty()) { + return; + } + + Menu menu = optionalMenu.get(); + int slot = event.getSlot(); + + Optional optionalMenuItem = menu.itemBySlot(slot); + + if (optionalMenuItem.isEmpty()) { + return; + } + + MenuItem item = optionalMenuItem.get(); + + if (!item.action().canEquip()) { + event.setCancelled(true); + } + + item.action().onClick(event); + }); + + globalEventHandler.addListener(InventoryOpenEvent.class, event -> { + Player player = event.getPlayer(); + Optional 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 optionalMenu = MenuManager.getInstance().getOpenMenu(player); + + if (optionalMenu.isEmpty()) { + return; + } + + Menu menu = optionalMenu.get(); + menu.onClose(event); + }); + } +} diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuManager.java b/src/main/java/com/slampvp/factory/common/menu/MenuManager.java new file mode 100644 index 0000000..0e8de9c --- /dev/null +++ b/src/main/java/com/slampvp/factory/common/menu/MenuManager.java @@ -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 openMenus; + + private MenuManager() { + this.openMenus = new HashMap<>(); + } + + public static synchronized MenuManager getInstance() { + if (instance == null) { + instance = new MenuManager(); + } + return instance; + } + + public Optional 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()); + } +} diff --git a/src/main/java/com/slampvp/factory/common/menu/TestMenu.java b/src/main/java/com/slampvp/factory/common/menu/TestMenu.java new file mode 100644 index 0000000..79a6f4d --- /dev/null +++ b/src/main/java/com/slampvp/factory/common/menu/TestMenu.java @@ -0,0 +1,64 @@ +package com.slampvp.factory.common.menu; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.minestom.server.event.inventory.InventoryCloseEvent; +import net.minestom.server.event.inventory.InventoryOpenEvent; +import net.minestom.server.event.inventory.InventoryPreClickEvent; +import net.minestom.server.inventory.InventoryType; +import net.minestom.server.item.ItemStack; +import net.minestom.server.item.Material; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class TestMenu implements Menu { + @Override + public @NotNull TextComponent name() { + return Component.text("test"); + } + + @Override + public @NotNull InventoryType type() { + return InventoryType.CHEST_4_ROW; + } + + @Override + public void onOpen(InventoryOpenEvent event) { + event.getPlayer().sendMessage("open"); + } + + @Override + public void onClose(InventoryCloseEvent event) { + event.getPlayer().sendMessage("close"); + } + + @Override + public @NotNull List items() { + return List.of( + new MenuItem() { + @Override + public List slots() { + return IntStream.range(0,9).boxed().toList(); + } + + @Override + public ItemStack itemStack() { + return ItemStack.of(Material.ACACIA_BOAT); + } + + @Override + public ClickAction action() { + return new ClickAction() { + @Override + public void onClick(InventoryPreClickEvent event) { + System.out.println("click"); + } + }; + } + } + ); + } +} diff --git a/src/main/java/com/slampvp/factory/player/PlayerListener.java b/src/main/java/com/slampvp/factory/player/PlayerListener.java index ea53210..c1d1a76 100644 --- a/src/main/java/com/slampvp/factory/player/PlayerListener.java +++ b/src/main/java/com/slampvp/factory/player/PlayerListener.java @@ -11,7 +11,6 @@ import net.minestom.server.instance.InstanceContainer; public class PlayerListener { - public PlayerListener(InstanceContainer instanceContainer) { GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler(); From ce5c4b1624887fb48e7c5124f529a349f57904d0 Mon Sep 17 00:00:00 2001 From: Matz Hilven Date: Mon, 1 Jul 2024 14:57:40 +0200 Subject: [PATCH 2/4] impr: menu item api --- .../com/slampvp/factory/common/menu/Menu.java | 1 - .../slampvp/factory/common/menu/MenuItem.java | 52 ++++++++++++++++--- .../slampvp/factory/common/menu/TestMenu.java | 28 ++-------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/slampvp/factory/common/menu/Menu.java b/src/main/java/com/slampvp/factory/common/menu/Menu.java index 4456223..10f38e6 100644 --- a/src/main/java/com/slampvp/factory/common/menu/Menu.java +++ b/src/main/java/com/slampvp/factory/common/menu/Menu.java @@ -42,5 +42,4 @@ default void onOpen(InventoryOpenEvent event) { default void onClose(InventoryCloseEvent event) { } - } diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java index 0000eb7..0baccd5 100644 --- a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java +++ b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java @@ -1,22 +1,60 @@ package com.slampvp.factory.common.menu; +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 interface MenuItem { +public record MenuItem(List slots, ItemStack itemStack, ClickAction action) { + public static Function CLOSE = slot -> + new MenuItem(slot, ItemStack.of(Material.BARRIER), event -> { + Player player = event.getPlayer(); + player.closeInventory(); + MenuManager.getInstance().clearOpenMenu(player); + }); - List slots(); + 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 consumer) { + this(stream.boxed().toList(), itemStack, consumer); + } - ItemStack itemStack(); + public MenuItem(List slots, ItemStack itemStack) { + this(slots, itemStack, new ClickAction() { + }); + } + + public MenuItem(List slots, ItemStack itemStack, Consumer consumer) { + this(slots, itemStack, new ClickAction() { + @Override + public void onClick(InventoryPreClickEvent event) { + consumer.accept(event); + } + }); + } + + public MenuItem(int slot, ItemStack itemStack) { + this(List.of(slot), itemStack, new ClickAction() { + }); + } - default ClickAction action() { - return new ClickAction() { - }; + public MenuItem(int slot, ItemStack itemStack, Consumer consumer) { + this(List.of(slot), itemStack, consumer); } - interface ClickAction { + public interface ClickAction { default boolean canEquip() { return false; } diff --git a/src/main/java/com/slampvp/factory/common/menu/TestMenu.java b/src/main/java/com/slampvp/factory/common/menu/TestMenu.java index 79a6f4d..3da7e5d 100644 --- a/src/main/java/com/slampvp/factory/common/menu/TestMenu.java +++ b/src/main/java/com/slampvp/factory/common/menu/TestMenu.java @@ -4,14 +4,12 @@ import net.kyori.adventure.text.TextComponent; import net.minestom.server.event.inventory.InventoryCloseEvent; import net.minestom.server.event.inventory.InventoryOpenEvent; -import net.minestom.server.event.inventory.InventoryPreClickEvent; import net.minestom.server.inventory.InventoryType; import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; import org.jetbrains.annotations.NotNull; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.IntStream; public class TestMenu implements Menu { @@ -38,27 +36,11 @@ public void onClose(InventoryCloseEvent event) { @Override public @NotNull List items() { return List.of( - new MenuItem() { - @Override - public List slots() { - return IntStream.range(0,9).boxed().toList(); - } - - @Override - public ItemStack itemStack() { - return ItemStack.of(Material.ACACIA_BOAT); - } - - @Override - public ClickAction action() { - return new ClickAction() { - @Override - public void onClick(InventoryPreClickEvent event) { - System.out.println("click"); - } - }; - } - } + new MenuItem( + IntStream.range(0, 9), + ItemStack.of(Material.ACACIA_BOAT) + ), + MenuItem.CLOSE.apply(31) ); } } From a0b9adc733f2f5738fb6d0f2a8efdf8d1039e804 Mon Sep 17 00:00:00 2001 From: Matz Hilven Date: Mon, 1 Jul 2024 15:02:42 +0200 Subject: [PATCH 3/4] impr: more constructors & remove test menu --- .../command/generic/gamemode/Menu.java | 24 ---------- .../slampvp/factory/common/menu/MenuItem.java | 23 ++++++++-- .../factory/common/menu/MenuListener.java | 2 +- .../slampvp/factory/common/menu/TestMenu.java | 46 ------------------- 4 files changed, 21 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java delete mode 100644 src/main/java/com/slampvp/factory/common/menu/TestMenu.java diff --git a/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java b/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java deleted file mode 100644 index 346bae2..0000000 --- a/src/main/java/com/slampvp/factory/command/generic/gamemode/Menu.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.slampvp.factory.command.generic.gamemode; - -import com.slampvp.factory.command.Command; -import com.slampvp.factory.command.FactoryCommand; -import com.slampvp.factory.common.menu.TestMenu; -import com.slampvp.factory.player.Rank; -import net.minestom.server.entity.GameMode; -import net.minestom.server.entity.Player; - -@Command(description = "Open menu.", usage = "/menu", minimumRank = Rank.ADMIN, playerOnly = true) -public class Menu extends FactoryCommand { - public Menu() { - super("menu"); - } - - @Override - public void init() { - addSyntax((sender, context) -> { - Player player = (Player) sender; - - new TestMenu().open(player); - }); - } -} diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java index 0baccd5..d27a948 100644 --- a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java +++ b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java @@ -28,7 +28,11 @@ public MenuItem(IntStream stream, ItemStack itemStack, ClickAction action) { } public MenuItem(IntStream stream, ItemStack itemStack, Consumer consumer) { - this(stream.boxed().toList(), itemStack, consumer); + this(stream.boxed().toList(), itemStack, true, consumer); + } + + public MenuItem(IntStream stream, ItemStack itemStack, boolean cancel, Consumer consumer) { + this(stream.boxed().toList(), itemStack, cancel, consumer); } public MenuItem(List slots, ItemStack itemStack) { @@ -37,7 +41,16 @@ public MenuItem(List slots, ItemStack itemStack) { } public MenuItem(List slots, ItemStack itemStack, Consumer consumer) { + this(slots, itemStack, true, consumer); + } + + public MenuItem(List slots, ItemStack itemStack, boolean cancel, Consumer consumer) { this(slots, itemStack, new ClickAction() { + @Override + public boolean cancel() { + return cancel; + } + @Override public void onClick(InventoryPreClickEvent event) { consumer.accept(event); @@ -54,9 +67,13 @@ public MenuItem(int slot, ItemStack itemStack, Consumer this(List.of(slot), itemStack, consumer); } + public MenuItem(int slot, ItemStack itemStack, boolean cancel, Consumer consumer) { + this(List.of(slot), itemStack, cancel, consumer); + } + public interface ClickAction { - default boolean canEquip() { - return false; + default boolean cancel() { + return true; } default void onClick(InventoryPreClickEvent event) { diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuListener.java b/src/main/java/com/slampvp/factory/common/menu/MenuListener.java index cab6e4e..fc949fc 100644 --- a/src/main/java/com/slampvp/factory/common/menu/MenuListener.java +++ b/src/main/java/com/slampvp/factory/common/menu/MenuListener.java @@ -32,7 +32,7 @@ public MenuListener() { MenuItem item = optionalMenuItem.get(); - if (!item.action().canEquip()) { + if (item.action().cancel()) { event.setCancelled(true); } diff --git a/src/main/java/com/slampvp/factory/common/menu/TestMenu.java b/src/main/java/com/slampvp/factory/common/menu/TestMenu.java deleted file mode 100644 index 3da7e5d..0000000 --- a/src/main/java/com/slampvp/factory/common/menu/TestMenu.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.slampvp.factory.common.menu; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.minestom.server.event.inventory.InventoryCloseEvent; -import net.minestom.server.event.inventory.InventoryOpenEvent; -import net.minestom.server.inventory.InventoryType; -import net.minestom.server.item.ItemStack; -import net.minestom.server.item.Material; -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.IntStream; - -public class TestMenu implements Menu { - @Override - public @NotNull TextComponent name() { - return Component.text("test"); - } - - @Override - public @NotNull InventoryType type() { - return InventoryType.CHEST_4_ROW; - } - - @Override - public void onOpen(InventoryOpenEvent event) { - event.getPlayer().sendMessage("open"); - } - - @Override - public void onClose(InventoryCloseEvent event) { - event.getPlayer().sendMessage("close"); - } - - @Override - public @NotNull List items() { - return List.of( - new MenuItem( - IntStream.range(0, 9), - ItemStack.of(Material.ACACIA_BOAT) - ), - MenuItem.CLOSE.apply(31) - ); - } -} From 7b531bbfec871e2fd44acc86f5625fa2794004fc Mon Sep 17 00:00:00 2001 From: Matz Hilven Date: Mon, 1 Jul 2024 15:05:08 +0200 Subject: [PATCH 4/4] fix: style muenu item --- .../slampvp/factory/common/menu/MenuItem.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java index d27a948..d9a43cb 100644 --- a/src/main/java/com/slampvp/factory/common/menu/MenuItem.java +++ b/src/main/java/com/slampvp/factory/common/menu/MenuItem.java @@ -1,5 +1,8 @@ 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; @@ -12,11 +15,14 @@ public record MenuItem(List slots, ItemStack itemStack, ClickAction action) { public static Function CLOSE = slot -> - new MenuItem(slot, ItemStack.of(Material.BARRIER), event -> { - Player player = event.getPlayer(); - player.closeInventory(); - MenuManager.getInstance().clearOpenMenu(player); - }); + 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() {