refactor gui item creation

This commit is contained in:
alexcrea 2026-07-07 15:22:22 +02:00
parent 23c7b0c753
commit a9af606ae9
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C
3 changed files with 123 additions and 127 deletions

View file

@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -39,116 +40,142 @@ public class MainConfigGui extends ChestGui {
GuiGlobalItems.addBackgroundItem(pane); GuiGlobalItems.addBackgroundItem(pane);
// Basic config item // Basic config item
ItemStack basicConfigItemstack = new ItemStack(Material.COMMAND_BLOCK); var basicConfigItem = basicConfigItem(new BasicConfigGui(packetManager));
ItemMeta basicConfigMeta = basicConfigItemstack.getItemMeta();
assert basicConfigMeta != null;
basicConfigMeta.setDisplayName("§aBasic Config Menu");
basicConfigMeta.setLore(Collections.singletonList("§7Click here to open basic config menu"));
basicConfigItemstack.setItemMeta(basicConfigMeta);
GuiItem basicConfigItem = GuiGlobalItems.goToGuiItem(basicConfigItemstack, new BasicConfigGui(packetManager));
pane.bindItem('1', basicConfigItem); pane.bindItem('1', basicConfigItem);
// enchant level limit item // enchant level limit item
ItemStack enchantLimitItemstack = new ItemStack(Material.ENCHANTED_BOOK); var enchantLimitItem = enchantLimitItem(new EnchantLimitConfigGui());
ItemMeta enchantLimitMeta = enchantLimitItemstack.getItemMeta();
assert enchantLimitMeta != null;
enchantLimitMeta.setDisplayName("§aEnchantment Level Limit");
enchantLimitMeta.setLore(Collections.singletonList("§7Click here to open enchantment level limit menu"));
enchantLimitItemstack.setItemMeta(enchantLimitMeta);
GuiItem enchantLimitItem = GuiGlobalItems.goToGuiItem(enchantLimitItemstack, new EnchantLimitConfigGui());
pane.bindItem('2', enchantLimitItem); pane.bindItem('2', enchantLimitItem);
// enchant level limit item // enchant level limit item
ItemStack enchantMergeLimitItemstack = new ItemStack(Material.ENCHANTED_BOOK); var enchantMergeLimitItem = enchantMergeLimitItem(new EnchantMergeLimitConfigGui());
ItemMeta enchantMergeLimitMeta = enchantMergeLimitItemstack.getItemMeta();
assert enchantMergeLimitMeta != null;
enchantMergeLimitMeta.setDisplayName("§aEnchantment Merge Limit");
enchantMergeLimitMeta.setLore(Collections.singletonList("§7Click here to open enchantment merge limit menu"));
enchantMergeLimitItemstack.setItemMeta(enchantMergeLimitMeta);
GuiItem enchantMergeLimitItem = GuiGlobalItems.goToGuiItem(enchantMergeLimitItemstack, new EnchantMergeLimitConfigGui());
pane.bindItem('3', enchantMergeLimitItem); pane.bindItem('3', enchantMergeLimitItem);
// enchant cost item // enchant cost item
ItemStack enchantCostItemstack = new ItemStack(Material.EXPERIENCE_BOTTLE); var enchantCostItem = enchantCostItem(new EnchantCostConfigGui());
ItemMeta enchantCostMeta = enchantCostItemstack.getItemMeta();
assert enchantCostMeta != null;
enchantCostMeta.setDisplayName("§aEnchantment Cost");
enchantCostMeta.setLore(Collections.singletonList("§7Click here to open enchantment costs menu"));
enchantCostItemstack.setItemMeta(enchantCostMeta);
GuiItem enchantCostItem = GuiGlobalItems.goToGuiItem(enchantCostItemstack, new EnchantCostConfigGui());
pane.bindItem('4', enchantCostItem); pane.bindItem('4', enchantCostItem);
// Enchantment Conflicts item // Enchantment Conflicts item
ItemStack enchantConflictItemstack = new ItemStack(Material.OAK_FENCE); var enchantConflictItem = enchantConflictItem(EnchantConflictGui.getInstance());
ItemMeta enchantConflictMeta = enchantConflictItemstack.getItemMeta();
assert enchantConflictMeta != null;
enchantConflictMeta.setDisplayName("§aEnchantment Conflict");
enchantConflictMeta.setLore(Collections.singletonList("§7Click here to open enchantment conflict menu"));
enchantConflictItemstack.setItemMeta(enchantConflictMeta);
GuiItem enchantConflictItem = GuiGlobalItems.goToGuiItem(enchantConflictItemstack, EnchantConflictGui.getInstance());
pane.bindItem('5', enchantConflictItem); pane.bindItem('5', enchantConflictItem);
// Group config items // Group config items
ItemStack groupItemstack = new ItemStack(Material.CHEST); var groupConfigItem = groupConfigItem(GroupConfigGui.getInstance());
ItemMeta groupMeta = groupItemstack.getItemMeta();
assert groupMeta != null;
groupMeta.setDisplayName("§aItem Groups");
groupMeta.setLore(Collections.singletonList("§7Click here to open item group menu"));
groupItemstack.setItemMeta(groupMeta);
GuiItem groupConfigItem = GuiGlobalItems.goToGuiItem(groupItemstack, GroupConfigGui.getInstance());
pane.bindItem('6', groupConfigItem); pane.bindItem('6', groupConfigItem);
// Unit repair item // Unit repair item
ItemStack unirRepairItemstack = new ItemStack(Material.DIAMOND); var unitRepairItem = unitRepairItem(UnitRepairConfigGui.getInstance());
ItemMeta unitRepairMeta = unirRepairItemstack.getItemMeta();
assert unitRepairMeta != null;
unitRepairMeta.setDisplayName("§aUnit Repair");
unitRepairMeta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu"));
unirRepairItemstack.setItemMeta(unitRepairMeta);
GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, UnitRepairConfigGui.getInstance());
pane.bindItem('7', unitRepairItem); pane.bindItem('7', unitRepairItem);
// Custom recipe item // Custom recipe item
ItemStack customRecipeItemstack = new ItemStack(Material.CRAFTING_TABLE); var customRecipeItem = customRecipeItem(CustomRecipeConfigGui.getInstance());
ItemMeta customRecipeMeta = customRecipeItemstack.getItemMeta();
assert customRecipeMeta != null;
customRecipeMeta.setDisplayName("§aCustom recipes");
customRecipeMeta.setLore(Collections.singletonList("§7Click here to open anvil custom recipe menu"));
customRecipeItemstack.setItemMeta(customRecipeMeta);
GuiItem customRecipeItem = GuiGlobalItems.goToGuiItem(customRecipeItemstack, CustomRecipeConfigGui.getInstance());
pane.bindItem('8', customRecipeItem); pane.bindItem('8', customRecipeItem);
// quit item // quit item
ItemStack quitItemstack = new ItemStack(Material.BARRIER); pane.bindItem('Q', quitItem());
ItemMeta quitMeta = quitItemstack.getItemMeta(); }
assert quitMeta != null;
quitMeta.setDisplayName("§cQuit"); public static GuiItem basicConfigItem(Gui target) {
quitItemstack.setItemMeta(quitMeta); var item = new ItemStack(Material.COMMAND_BLOCK);
var meta = item.getItemMeta();
assert meta != null;
GuiItem quitItem = new GuiItem(quitItemstack, event -> { meta.setDisplayName("§aBasic Config Menu");
meta.setLore(Collections.singletonList("§7Click here to open basic config menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem enchantLimitItem(Gui target) {
var item = new ItemStack(Material.ENCHANTED_BOOK);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aEnchantment Level Limit");
meta.setLore(Collections.singletonList("§7Click here to open enchantment level limit menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem enchantMergeLimitItem(Gui target) {
ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
ItemMeta meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aEnchantment Merge Limit");
meta.setLore(Collections.singletonList("§7Click here to open enchantment merge limit menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem enchantCostItem(Gui target) {
ItemStack item = new ItemStack(Material.EXPERIENCE_BOTTLE);
ItemMeta meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aEnchantment Cost");
meta.setLore(Collections.singletonList("§7Click here to open enchantment costs menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem enchantConflictItem(Gui target) {
var item = new ItemStack(Material.OAK_FENCE);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aEnchantment Conflict");
meta.setLore(Collections.singletonList("§7Click here to open enchantment conflict menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem groupConfigItem(Gui target) {
var item = new ItemStack(Material.CHEST);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aItem Groups");
meta.setLore(Collections.singletonList("§7Click here to open item group menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem unitRepairItem(Gui target) {
var item = new ItemStack(Material.DIAMOND);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aUnit Repair");
meta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem customRecipeItem(Gui target) {
var item = new ItemStack(Material.CRAFTING_TABLE);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§aCustom recipes");
meta.setLore(Collections.singletonList("§7Click here to open anvil custom recipe menu"));
item.setItemMeta(meta);
return GuiGlobalItems.goToGuiItem(item, target);
}
public static GuiItem quitItem() {
var item = new ItemStack(Material.BARRIER);
var meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName("§cQuit");
item.setItemMeta(meta);
return new GuiItem(item, event -> {
event.setCancelled(true); event.setCancelled(true);
event.getWhoClicked().closeInventory(); event.getWhoClicked().closeInventory();
}, CustomAnvil.instance); }, CustomAnvil.instance);
pane.bindItem('Q', quitItem);
} }
} }

View file

@ -12,14 +12,13 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MaterialUtil; import xyz.alexcrea.cuanvil.util.MaterialUtil;
import xyz.alexcrea.cuanvil.util.UnitRepairUtil; import xyz.alexcrea.cuanvil.util.UnitRepairUtil;
import java.util.Collections;
public class ItemConfigGui extends ChestGui { public class ItemConfigGui extends ChestGui {
public EnchantConflictGui enchantConflictGui; public EnchantConflictGui enchantConflictGui;
@ -33,7 +32,7 @@ public class ItemConfigGui extends ChestGui {
Pattern pattern = new Pattern( Pattern pattern = new Pattern(
"0000D0000", "0000D0000",
"056000780", "056000780",
"000000000" "Q00000000"
); );
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern); PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
addPane(pane); addPane(pane);
@ -49,53 +48,23 @@ public class ItemConfigGui extends ChestGui {
pane.bindItem('D', new GuiItem(displayItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); pane.bindItem('D', new GuiItem(displayItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance));
// Enchantment Conflicts item // Enchantment Conflicts item
ItemStack enchantConflictItemstack = new ItemStack(Material.OAK_FENCE); GuiItem enchantConflictItem = MainConfigGui.enchantConflictItem(getEnchantConflictGui(material));
ItemMeta enchantConflictMeta = enchantConflictItemstack.getItemMeta();
assert enchantConflictMeta != null;
enchantConflictMeta.setDisplayName("§aEnchantment Conflict");
enchantConflictMeta.setLore(Collections.singletonList("§7Click here to open enchantment conflict menu"));
enchantConflictItemstack.setItemMeta(enchantConflictMeta);
GuiItem enchantConflictItem = GuiGlobalItems.goToGuiItem(enchantConflictItemstack, getEnchantConflictGui(material));
pane.bindItem('5', enchantConflictItem); pane.bindItem('5', enchantConflictItem);
// Group config items // Group config items
ItemStack groupItemstack = new ItemStack(Material.CHEST); GuiItem groupConfigItem = MainConfigGui.groupConfigItem(getGroupConfigGui(material));
ItemMeta groupMeta = groupItemstack.getItemMeta();
assert groupMeta != null;
groupMeta.setDisplayName("§aItem Groups");
groupMeta.setLore(Collections.singletonList("§7Click here to open item group menu"));
groupItemstack.setItemMeta(groupMeta);
GuiItem groupConfigItem = GuiGlobalItems.goToGuiItem(groupItemstack, getGroupConfigGui(material));
pane.bindItem('6', groupConfigItem); pane.bindItem('6', groupConfigItem);
// Unit repair item // Unit repair item
ItemStack unirRepairItemstack = new ItemStack(Material.DIAMOND); GuiItem unitRepairItem = MainConfigGui.unitRepairItem(getUnitRepairConfigGui(material));
ItemMeta unitRepairMeta = unirRepairItemstack.getItemMeta();
assert unitRepairMeta != null;
unitRepairMeta.setDisplayName("§aUnit Repair");
unitRepairMeta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu"));
unirRepairItemstack.setItemMeta(unitRepairMeta);
GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, getUnitRepairConfigGui(material));
pane.bindItem('7', unitRepairItem); pane.bindItem('7', unitRepairItem);
// Custom recipe item // Custom recipe item
ItemStack customRecipeItemstack = new ItemStack(Material.CRAFTING_TABLE); GuiItem customRecipeItem = MainConfigGui.customRecipeItem(getCustomRecipeConfigGui(material));
ItemMeta customRecipeMeta = customRecipeItemstack.getItemMeta();
assert customRecipeMeta != null;
customRecipeMeta.setDisplayName("§aCustom recipes");
customRecipeMeta.setLore(Collections.singletonList("§7Click here to open anvil custom recipe menu"));
customRecipeItemstack.setItemMeta(customRecipeMeta);
GuiItem customRecipeItem = GuiGlobalItems.goToGuiItem(customRecipeItemstack, getCustomRecipeConfigGui(material));
pane.bindItem('8', customRecipeItem); pane.bindItem('8', customRecipeItem);
// quit item
pane.bindItem('Q', MainConfigGui.quitItem());
} }
private EnchantConflictGui getEnchantConflictGui(@NotNull NamespacedKey material) { private EnchantConflictGui getEnchantConflictGui(@NotNull NamespacedKey material) {
@ -139,8 +108,8 @@ public class ItemConfigGui extends ChestGui {
if (customRecipeConfigGui == null) { if (customRecipeConfigGui == null) {
customRecipeConfigGui = new CustomRecipeConfigGui(this); customRecipeConfigGui = new CustomRecipeConfigGui(this);
customRecipeConfigGui.setFilter(recipe -> { customRecipeConfigGui.setFilter(recipe -> {
if(isMaterial(recipe.getLeftItem(), material)) return true; if (isMaterial(recipe.getLeftItem(), material)) return true;
if(isMaterial(recipe.getRightItem(), material)) return true; if (isMaterial(recipe.getRightItem(), material)) return true;
return isMaterial(recipe.getResultItem(), material); return isMaterial(recipe.getResultItem(), material);
} }
); );
@ -151,7 +120,7 @@ public class ItemConfigGui extends ChestGui {
} }
private boolean isMaterial(@Nullable ItemStack item, @NotNull NamespacedKey material) { private boolean isMaterial(@Nullable ItemStack item, @NotNull NamespacedKey material) {
if(item == null) return false; if (item == null) return false;
return material.equals(MaterialUtil.INSTANCE.getCustomType(item)); return material.equals(MaterialUtil.INSTANCE.getCustomType(item));
} }

View file

@ -86,7 +86,7 @@ class EditConfigExecutor : CASubCommand() {
private fun processItem(sender: HumanEntity) { private fun processItem(sender: HumanEntity) {
val item = sender.inventory.itemInMainHand val item = sender.inventory.itemInMainHand
if(item.isAir) { if (item.isAir) {
sender.sendMessage("Cannot configure the item in hand") sender.sendMessage("Cannot configure the item in hand")
return return
} }