-
Notifications
You must be signed in to change notification settings - Fork 0
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
b8e5f7b
commit 527172e
Showing
6 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
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
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,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
88
src/main/java/com/slampvp/factory/common/menu/MenuItem.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,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
66
src/main/java/com/slampvp/factory/common/menu/MenuListener.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,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
36
src/main/java/com/slampvp/factory/common/menu/MenuManager.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,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()); | ||
} | ||
} |
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