Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 2 additions & 6 deletions src/main/java/fr/openmc/api/menulib/template/ConfirmMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -110,10 +109,7 @@ public void onClose(InventoryCloseEvent event) {

loreDeny.add(Component.text("§e§lCLIQUEZ ICI POUR REFUSER"));

ItemStack refuseBtn = OMCRegistry.CUSTOM_ITEMS.get("omc_menus:refuse_btn").getBest();
ItemStack acceptBtn = OMCRegistry.CUSTOM_ITEMS.get("omc_menus:accept_btn").getBest();

inventory.put(posDenyBtn, new ItemMenuBuilder(this, refuseBtn, itemMeta -> {
inventory.put(posDenyBtn, new ItemMenuBuilder(this, OMCRegistry.CUSTOM_ITEMS.REFUSE_BTN, itemMeta -> {
itemMeta.displayName(Component.text("§cRefuser"));
itemMeta.lore(loreDeny);
}).setOnClick(event -> {
Expand All @@ -126,7 +122,7 @@ public void onClose(InventoryCloseEvent event) {
}
}));

inventory.put(posAcceptBtn, new ItemMenuBuilder(this, acceptBtn, itemMeta -> {
inventory.put(posAcceptBtn, new ItemMenuBuilder(this, OMCRegistry.CUSTOM_ITEMS.ACCEPT_BTN, itemMeta -> {
itemMeta.displayName(Component.text("§aAccepter"));
itemMeta.lore(loreAccept);
}).setOnClick(event -> {
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/fr/openmc/api/menulib/template/ItemMenuTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package fr.openmc.api.menulib.template;

import fr.openmc.api.menulib.Menu;
import fr.openmc.api.menulib.utils.ItemMenuBuilder;
import fr.openmc.core.OMCRegistry;
import fr.openmc.core.features.mailboxes.menu.HomeMailbox;
import fr.openmc.core.registry.items.CustomItem;
import fr.openmc.core.utils.text.messages.TranslationManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;

import java.util.List;
import java.util.function.Function;

public class ItemMenuTemplate {
public static final Function<Menu, ItemMenuBuilder> BTN_PREVIOUS_PAGE_WHITE = (menu) ->
new ItemMenuBuilder(menu, OMCRegistry.CUSTOM_ITEMS.MAILBOX_ARROW_LEFT, meta ->
meta.displayName(Component.text("⬅ Page précédente",
NamedTextColor.GOLD, TextDecoration.BOLD).decoration(TextDecoration.ITALIC, false))
).setPreviousPageButton();

public static final Function<Menu, ItemMenuBuilder> BTN_NEXT_PAGE_WHITE = (menu) ->
new ItemMenuBuilder(menu, OMCRegistry.CUSTOM_ITEMS.MAILBOX_ARROW_RIGHT, meta ->
meta.displayName(Component.text("Page suivante ➡",
NamedTextColor.GOLD, TextDecoration.BOLD).decoration(TextDecoration.ITALIC, false))
).setNextPageButton();

public static final Function<Menu, ItemMenuBuilder> BTN_PREVIOUS_PAGE_ORANGE = (menu) ->
new ItemMenuBuilder(menu, OMCRegistry.CUSTOM_ITEMS.ICON_BACK_ORANGE, meta ->
meta.displayName(TranslationManager.translation("messages.menus.previous_page"))
).setPreviousPageButton();

public static final Function<Menu, ItemMenuBuilder> BTN_NEXT_PAGE_ORANGE = (menu) ->
new ItemMenuBuilder(menu, OMCRegistry.CUSTOM_ITEMS.ICON_NEXT_ORANGE, meta ->
meta.displayName(TranslationManager.translation("messages.menus.next_page"))
).setNextPageButton();

public static final Function<Menu, ItemMenuBuilder> BTN_CANCEL = (menu) ->
new ItemMenuBuilder(menu, OMCRegistry.CUSTOM_ITEMS.ICON_CANCEL, meta ->
meta.displayName(TranslationManager.translation("messages.menus.close"))
).setCloseButton();

public static final Function<Menu, ItemMenuBuilder> BTN_CLOSE = (menu) ->
btn(menu, "✘", "Annuler", OMCRegistry.CUSTOM_ITEMS.MAILBOX_CANCEL_BTN, NamedTextColor.DARK_RED, true)
.setCloseButton();

public static final Function<Menu, ItemMenuBuilder> BTN_MAILBOX_ACCEPT = (menu) ->
btn(menu, "✔", "Accepter", OMCRegistry.CUSTOM_ITEMS.MAILBOX_ACCEPT_BTN, NamedTextColor.DARK_GREEN, true);

public static final Function<Menu, ItemMenuBuilder> BTN_MAILBOX_SEND = (menu) ->
btn(menu, "✉", "Envoyer", OMCRegistry.CUSTOM_ITEMS.MAILBOX_SEND, NamedTextColor.DARK_AQUA, true);

public static final Function<Menu, ItemMenuBuilder> BTN_MAILBOX_HOME = (menu) ->
new ItemMenuBuilder(menu, Material.CHEST, meta -> {
meta.displayName(Component.text("⬅ Home", NamedTextColor.GOLD, TextDecoration.BOLD).decoration(TextDecoration.ITALIC, false));
meta.setMaxStackSize(1);
}).setOnClick(e -> new HomeMailbox(menu.getOwner()).open());


public static ItemMenuBuilder btn(Menu menu, String symbol, String name, List<Component> lore, CustomItem customItem, NamedTextColor color, boolean bold) {
ItemMenuBuilder item = btn(menu, symbol, name, customItem, color, bold);

item.editMeta(
meta -> meta.lore(lore)
);
return item;
}

public static ItemMenuBuilder btn(Menu menu, String symbol, String name, CustomItem customItem, NamedTextColor color, boolean bold) {
Component itemName = Component.text("[", NamedTextColor.DARK_GRAY)
.append(Component.text(symbol, color))
.append(Component.text("]", NamedTextColor.DARK_GRAY))
.append(Component.text(" " + name, color));

return new ItemMenuBuilder(menu, customItem, meta -> {
meta.displayName(itemName
.decorate(TextDecoration.BOLD)
.decoration(TextDecoration.ITALIC, false)
.decoration(TextDecoration.BOLD, bold));
meta.setMaxStackSize(1);
});
}
}
58 changes: 56 additions & 2 deletions src/main/java/fr/openmc/api/menulib/utils/ItemMenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.openmc.api.menulib.MenuLib;
import fr.openmc.api.menulib.PaginatedMenu;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import fr.openmc.core.registry.items.CustomItem;
import fr.openmc.core.utils.bukkit.ItemBuilder;
import fr.openmc.core.utils.text.messages.MessageType;
import fr.openmc.core.utils.text.messages.MessagesManager;
Expand Down Expand Up @@ -48,9 +49,24 @@ public ItemMenuBuilder(Menu itemMenu, Material material) {
this(itemMenu, material, null, false);
}

/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link CustomItem}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customItem The {@link CustomItem} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customItem) {
this(itemMenu, customItem.getBest(), null, false);
}


/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link Material}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and material,
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
Expand All @@ -62,7 +78,22 @@ public ItemMenuBuilder(Menu itemMenu, Material material, boolean isBackButton) {
this(itemMenu, material, null, isBackButton);
this.backButton = isBackButton;
}


/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link CustomItem}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customItem The {@link CustomItem} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customItem, boolean isBackButton) {
this(itemMenu, customItem.getBest(), null, isBackButton);
this.backButton = isBackButton;
}

/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link ItemStack}.
* This constructor initializes the {@code ItemBuilder} to create items with the given menu and item,
Expand Down Expand Up @@ -164,6 +195,24 @@ public ItemMenuBuilder(Menu itemMenu, ItemStack item, Consumer<ItemMeta> itemMet
this.backButton = isBackButton;
}

/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link CustomItem},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a specific item to define
* the base configuration, and a consumer for applying additional metadata customizations to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customitem The {@link CustomItem} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customitem, Consumer<ItemMeta> itemMeta) {
super(customitem.getBest());
this.itemMenu = itemMenu;
}

/**
* Sets the unique identifier for the item using the specified {@code itemId}.
* The identifier is stored in the item's {@link PersistentDataContainer} as a
Expand Down Expand Up @@ -275,6 +324,11 @@ public ItemMenuBuilder setPreviousPageButton() {
return this;
}

public ItemMenuBuilder setBackButton() {
this.backButton = true;
return this;
}

@SuppressWarnings("UnstableApiUsage")
public ItemMenuBuilder hide(DataComponentType... typesToHide) {
super.hide(typesToHide);
Expand Down
94 changes: 52 additions & 42 deletions src/main/java/fr/openmc/core/OMCRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import fr.openmc.core.bootstrap.integration.OMCLogger;
import fr.openmc.core.bootstrap.registries.LifecycleRegistry;
import fr.openmc.core.bootstrap.registries.RegistryContext;
import fr.openmc.core.bootstrap.registries.RegistryLoadingType;
import fr.openmc.core.registry.ambient.CustomAmbientRegistry;
import fr.openmc.core.registry.enchantments.CustomEnchantmentRegistry;
import fr.openmc.core.registry.items.CustomItemRegistry;
Expand All @@ -11,69 +13,77 @@
import io.papermc.paper.plugin.bootstrap.BootstrapContext;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("UnstableApiUsage")
public final class OMCRegistry {

public static final CustomItemRegistry CUSTOM_ITEMS = new CustomItemRegistry();
public static final CustomMobRegistry CUSTOM_MOBS = new CustomMobRegistry();
public static final CustomEnchantmentRegistry CUSTOM_ENCHANTS = new CustomEnchantmentRegistry();
public static final CustomLootTableRegistry CUSTOM_LOOT_TABLES = new CustomLootTableRegistry();
public static final CustomLootboxRegistry CUSTOM_LOOTBOXES = new CustomLootboxRegistry();
public static final CustomAmbientRegistry CUSTOM_AMBIENTS = new CustomAmbientRegistry();
public static CustomItemRegistry CUSTOM_ITEMS;
public static CustomMobRegistry CUSTOM_MOBS;
public static CustomEnchantmentRegistry CUSTOM_ENCHANTS;
public static CustomLootTableRegistry CUSTOM_LOOT_TABLES;
public static CustomAmbientRegistry CUSTOM_AMBIENTS;
public static CustomLootboxRegistry CUSTOM_LOOTBOXES;

private static final List<LifecycleRegistry> ALL = List.of(
CUSTOM_ITEMS,
CUSTOM_MOBS,
CUSTOM_ENCHANTS,
CUSTOM_LOOT_TABLES,
CUSTOM_LOOTBOXES,
CUSTOM_AMBIENTS

private static final List<RegistryContext> ALL = List.of(
new RegistryContext(
() -> CUSTOM_ITEMS = new CustomItemRegistry(),
RegistryLoadingType.AFTER_IA),
new RegistryContext(() -> CUSTOM_MOBS = new CustomMobRegistry(),
RegistryLoadingType.AFTER_IA),
new RegistryContext(
() -> CUSTOM_ENCHANTS = new CustomEnchantmentRegistry(),
RegistryLoadingType.BOOTSTRAP, RegistryLoadingType.AFTER_IA),
new RegistryContext(
() -> CUSTOM_LOOT_TABLES = new CustomLootTableRegistry(),
RegistryLoadingType.AFTER_IA),
new RegistryContext(
() -> CUSTOM_AMBIENTS = new CustomAmbientRegistry(),
RegistryLoadingType.BOOTSTRAP),
new RegistryContext(
() -> CUSTOM_LOOTBOXES = new CustomLootboxRegistry(),
RegistryLoadingType.AFTER_IA)
);

private OMCRegistry() {}

public static void bootstrapAll(BootstrapContext context) {
for (LifecycleRegistry r : OMCRegistry.ALL) {
if (isOverridden(r, "bootstrap", BootstrapContext.class)) {
try {
r.bootstrap(context);
} catch (IOException e) {
OMCLogger.errorFormatted("Erreur lors du chargement du registre '{}' lors du bootstrap", r.getClass().getSimpleName());
OMCLogger.error(e.getMessage());
}
OMCLogger.successFormatted("Registre {} chargé pendant le bootstrap", r.getClass().getSimpleName());
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.BOOTSTRAP)) continue;

LifecycleRegistry r = ctx.registry().get();
try {
r.bootstrap(context);
} catch (IOException e) {
OMCLogger.errorFormatted("Erreur lors du chargement du registre '{}' lors du bootstrap", r.getClass().getSimpleName());
OMCLogger.error(e.getMessage());
}
OMCLogger.successFormatted("Registre {} chargé pendant le bootstrap", r.getClass().getSimpleName());
}
}

public static void initAll() {
for (LifecycleRegistry r : OMCRegistry.ALL) {
if (isOverridden(r, "init")) {
r.init();
OMCLogger.successFormatted("Registre {} chargé pendant le runtime", r.getClass().getSimpleName());
}
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.RUNTIME)) continue;

LifecycleRegistry r = ctx.registry().get();
r.init();
OMCLogger.successFormatted("Registre {} chargé pendant le runtime", r.getClass().getSimpleName());
}
}

public static void postInitAll() {
for (LifecycleRegistry r : OMCRegistry.ALL) {
if (isOverridden(r, "postInit")) {
r.postInit();
OMCLogger.successFormatted("Registre {} chargé après ItemsAdder", r.getClass().getSimpleName());
}
}
}
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.AFTER_IA)) continue;

private static boolean isOverridden(LifecycleRegistry r, String methodName, Class<?>... args) {
try {
return !r.getClass()
.getMethod(methodName, args)
.getDeclaringClass()
.equals(LifecycleRegistry.class);
} catch (NoSuchMethodException e) {
return false;
LifecycleRegistry r = ctx.registry().get();
r.postInit();
OMCLogger.successFormatted("Registre {} chargé après ItemsAdder", r.getClass().getSimpleName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
public interface KeyedRegistry<K, V> {
K key(V registryObject);

void register(K key, V value);
V register(K key, V value);

default void register(V value) {
register(key(value), value);
default V register(V value) {
return register(key(value), value);
}

default void register(V... values) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/fr/openmc/core/bootstrap/registries/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class Registry<K, V> implements LifecycleRegistry {

protected final Map<K, V> entries = new HashMap<>();

public void register(K key, V value) {
public V register(K key, V value) {
entries.put(key, value);
return value;
}

public V get(K key) {
return entries.get(key);
public Optional<V> get(K key) {
return Optional.ofNullable(entries.get(key));
}

public V getOrThrow(K key) {
return get(key).orElseThrow(() -> new IllegalArgumentException("No entry found for key: " + key));
}

public Collection<K> keys() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.openmc.core.bootstrap.registries;

import java.util.function.Supplier;

/**
* Création d'un context pour les registres.
* prends un registre à initialiser, et les conditions d'initialisations du registre.
* Creer pour éviter des erreurs de {@link org.bukkit.Registry} lié au Material ou autre, qui ne sont pas encore initialisé lors du bootstrap.
* @param registry le constructeur du registre
* @param loadingTypes les conditions de chargements
*/
public record RegistryContext(Supplier<LifecycleRegistry> registry, RegistryLoadingType... loadingTypes) {
}
Loading
Loading