Skip to content

Commit dfd60de

Browse files
authored
Merge pull request #230 from 1robie/developement
Minor improvements
2 parents d63dcc6 + 371488f commit dfd60de

File tree

10 files changed

+75
-43
lines changed

10 files changed

+75
-43
lines changed

API/src/main/java/fr/maxlego08/menu/api/attribute/AttributeWrapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.attribute.AttributeModifier;
88
import org.bukkit.inventory.EquipmentSlotGroup;
99
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
1011

1112
import java.util.Locale;
1213
import java.util.Map;
@@ -17,7 +18,11 @@
1718
* Value object for encapsulating all data needed for an attribute modifier on an item: the attribute, operation, amount, and slot.
1819
* Provides deserialization and conversion to Bukkit AttributeModifier.
1920
*/
20-
public record AttributeWrapper(Attribute attribute, AttributeModifier.Operation operation, double amount, EquipmentSlotGroup slot) {
21+
public record AttributeWrapper(Attribute attribute, AttributeModifier.Operation operation, double amount, EquipmentSlotGroup slot, @Nullable NamespacedKey namespacedKey) {
22+
23+
AttributeWrapper(Attribute attribute, AttributeModifier.Operation operation, double amount, EquipmentSlotGroup slot) {
24+
this(attribute, operation, amount, slot, null);
25+
}
2126

2227
public static AttributeWrapper deserialize(@NotNull Map<String, Object> attributeMap) {
2328
var attribute = Registry.ATTRIBUTE.get(Objects.requireNonNull(NamespacedKey.fromString(((String) attributeMap.get("attribute")).toLowerCase(Locale.ROOT))));
@@ -30,6 +35,6 @@ public static AttributeWrapper deserialize(@NotNull Map<String, Object> attribut
3035
}
3136

3237
public AttributeModifier toAttributeModifier(MenuPlugin plugin) {
33-
return new AttributeModifier(new NamespacedKey(plugin, UUID.randomUUID().toString()), amount, operation, slot);
38+
return new AttributeModifier(Objects.requireNonNullElseGet(this.namespacedKey, () -> new NamespacedKey(plugin, UUID.randomUUID().toString())), amount, operation, slot);
3439
}
3540
}

Common/src/main/java/fr/maxlego08/menu/ZMenuItemStack.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import fr.maxlego08.menu.api.utils.Placeholders;
2121
import fr.maxlego08.menu.common.context.ZBuildContext;
2222
import fr.maxlego08.menu.common.utils.ZUtils;
23-
import fr.maxlego08.menu.common.utils.itemstack.MenuItemStackFormMap;
2423
import fr.maxlego08.menu.common.utils.itemstack.MenuItemStackFromItemStack;
2524
import fr.maxlego08.menu.common.utils.nms.NmsVersion;
2625
import fr.maxlego08.menu.zcore.logger.Logger;
@@ -39,6 +38,7 @@
3938
import org.jetbrains.annotations.Nullable;
4039
import org.jspecify.annotations.NonNull;
4140

41+
import javax.annotation.Nullable;
4242
import java.io.File;
4343
import java.util.*;
4444

@@ -102,19 +102,6 @@ public static ZMenuItemStack fromItemStack(InventoryManager manager, ItemStack i
102102
return MenuItemStackFromItemStack.fromItemStack(manager, itemStack);
103103
}
104104

105-
/**
106-
* Build a MenuItemStack from a map.
107-
*
108-
* @param inventoryManager the inventoryManager of the item
109-
* @param file the file where the item is saved
110-
* @param path the path of the item in the file
111-
* @param map the map which contains the item data
112-
* @return the menuItemStack
113-
*/
114-
public static ZMenuItemStack fromMap(InventoryManager inventoryManager, File file, String path, Map<String, Object> map) {
115-
return MenuItemStackFormMap.fromMap(inventoryManager, file, path, map);
116-
}
117-
118105
/**
119106
* @return the inventoryManager
120107
*/
@@ -358,9 +345,7 @@ private void applyEnchantments(Material finalMaterial, ItemMeta itemMeta) {
358345

359346
private void applyFlags(ItemMeta itemMeta) {
360347
for (ItemFlag flag : this.flags) {
361-
if (flag != null) {
362-
itemMeta.addItemFlags(flag);
363-
}
348+
itemMeta.addItemFlags(flag);
364349
}
365350
}
366351

Common/src/main/java/fr/maxlego08/menu/common/enums/Permission.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ public enum Permission {
1010
ZMENU_CONVERT,
1111
ZMENU_LIST,
1212
ZMENU_TEST_DUPE,
13-
ZMENU_OPEN_ITEM,
1413
ZMENU_CREATE,
15-
ZMENU_DOWNLOAD,
16-
ZMENU_LOGIN,
17-
ZMENU_MARKETPLACE,
18-
ZMENU_INVENTORIES,
19-
ZMENU_DESCRIPTION,
14+
ZMENU_DOWNLOAD,
15+
ZMENU_LOGIN,
16+
ZMENU_MARKETPLACE,
17+
ZMENU_INVENTORIES,
18+
ZMENU_DESCRIPTION,
2019
ZMENU_OPEN_DIALOG,
2120
ZMENU_RELOAD_DIALOG,
2221
ZMENU_DUMPLOG,
2322
ZMENU_CONTRIBUTORS,
2423
ZMENU_GIVE_ITEM,
24+
ZMENU_GIVE_OPEN_ITEM,
25+
ZMENU_EDITOR,
26+
ZMENU_VERSION,
2527

2628
ZMENU_ADDONS,
2729
ZMENU_DOCUMENTATION;

src/main/java/fr/maxlego08/menu/ZInventoryManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import fr.maxlego08.menu.common.utils.ZUtils;
2828
import fr.maxlego08.menu.common.utils.cache.YamlFileCache;
2929
import fr.maxlego08.menu.common.utils.cache.YamlFileCacheEntry;
30-
import fr.maxlego08.menu.common.utils.itemstack.MenuItemStackFormMap;
3130
import fr.maxlego08.menu.common.utils.nms.ItemStackUtils;
3231
import fr.maxlego08.menu.common.utils.yaml.YamlParser;
3332
import fr.maxlego08.menu.hooks.dialogs.loader.body.ItemBodyLoader;
@@ -130,7 +129,9 @@ public MenuItemStack loadItemStack(YamlConfiguration configuration, String path,
130129

131130
@Override
132131
public MenuItemStack loadItemStack(File file, String path, Map<String, Object> map) {
133-
return MenuItemStackFormMap.fromMap(this, file, path, map);
132+
YamlConfiguration configuration = new YamlConfiguration();
133+
configuration.set("item", map);
134+
return new MenuItemStackLoader(this).load(configuration, "item", file);
134135
}
135136

136137
@Override

src/main/java/fr/maxlego08/menu/command/commands/CommandMenuEditor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package fr.maxlego08.menu.command.commands;
22

33
import fr.maxlego08.menu.ZMenuPlugin;
4-
import fr.maxlego08.menu.command.VCommand;
54
import fr.maxlego08.menu.api.utils.Message;
5+
import fr.maxlego08.menu.command.VCommand;
6+
import fr.maxlego08.menu.common.enums.Permission;
67
import fr.maxlego08.menu.zcore.utils.commands.CommandType;
78

89
public class CommandMenuEditor extends VCommand {
@@ -11,6 +12,7 @@ public CommandMenuEditor(ZMenuPlugin plugin) {
1112
super(plugin);
1213
this.addSubCommand("editor");
1314
this.setDescription(Message.DESCRIPTION_EDITOR);
15+
this.setPermission(Permission.ZMENU_EDITOR);
1416
}
1517

1618
@Override

src/main/java/fr/maxlego08/menu/command/commands/CommandMenuGiveOpenItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CommandMenuGiveOpenItem extends VCommand {
1919

2020
public CommandMenuGiveOpenItem(ZMenuPlugin plugin) {
2121
super(plugin);
22-
this.setPermission(Permission.ZMENU_OPEN_ITEM);
22+
this.setPermission(Permission.ZMENU_GIVE_OPEN_ITEM);
2323
this.setDescription(Message.DESCRIPTION_OPEN_ITEM);
2424
this.addSubCommand("giveopenitem");
2525
this.addSubCommand("goi");

src/main/java/fr/maxlego08/menu/command/commands/CommandMenuVersion.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package fr.maxlego08.menu.command.commands;
22

33
import fr.maxlego08.menu.ZMenuPlugin;
4-
import fr.maxlego08.menu.command.VCommand;
54
import fr.maxlego08.menu.api.utils.Message;
5+
import fr.maxlego08.menu.command.VCommand;
6+
import fr.maxlego08.menu.common.enums.Permission;
67
import fr.maxlego08.menu.zcore.utils.commands.CommandType;
78

89
public class CommandMenuVersion extends VCommand {
910

1011
public CommandMenuVersion(ZMenuPlugin plugin) {
1112
super(plugin);
1213
this.setDescription(Message.DESCRIPTION_VERSION);
14+
this.setPermission(Permission.ZMENU_VERSION);
1315
this.addSubCommand("version", "v", "ver");
1416
}
1517

src/main/java/fr/maxlego08/menu/loader/MenuItemStackLoader.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import fr.maxlego08.menu.api.loader.ItemComponentLoader;
1616
import fr.maxlego08.menu.api.utils.Loader;
1717
import fr.maxlego08.menu.api.utils.LoreType;
18-
import fr.maxlego08.menu.api.utils.TrimHelper;
1918
import fr.maxlego08.menu.common.context.ZBuildContext;
2019
import fr.maxlego08.menu.common.utils.ZUtils;
2120
import fr.maxlego08.menu.common.utils.nms.NmsVersion;
@@ -35,6 +34,7 @@
3534
import java.io.File;
3635
import java.io.IOException;
3736
import java.util.*;
37+
import java.util.stream.Collectors;
3838

3939
@SuppressWarnings("deprecation")
4040
public class MenuItemStackLoader extends ZUtils implements Loader<MenuItemStack> {
@@ -53,6 +53,9 @@ public MenuItemStackLoader(InventoryManager manager) {
5353
* Load ItemStack
5454
*/
5555
public MenuItemStack load(@NonNull YamlConfiguration configuration, @NonNull String path, Object... objects) {
56+
if (!path.isEmpty() && !path.endsWith("."))
57+
path = path + ".";
58+
5659

5760
File file = (File) objects[0];
5861

@@ -96,7 +99,9 @@ public MenuItemStack load(@NonNull YamlConfiguration configuration, @NonNull Str
9699
List<String> flagStrings = configuration.getStringList(path + "flags");
97100
List<ItemFlag> flags = new ArrayList<>(flagStrings.size());
98101
for (String flagName : flagStrings) {
99-
flags.add(this.getFlag(flagName));
102+
ItemFlag flag = this.getFlag(flagName);
103+
if (flag != null)
104+
flags.add(flag);
100105
}
101106
menuItemStack.setFlags(flags);
102107

@@ -520,18 +525,47 @@ private void loadNewItemStacks(ZMenuItemStack menuItemStack, YamlConfiguration c
520525
private void loadTrims(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path, File file) {
521526
boolean enableTrim = configuration.getBoolean(path + "trim.enable", false);
522527
if (enableTrim) {
523-
TrimHelper trimHelper = new TrimHelper();
524-
TrimPattern trimPattern = trimHelper.getTrimPatterns().get(configuration.getString(path + "trim.pattern", "").toLowerCase());
525-
if (trimPattern == null) {
528+
String patternKey = configuration.getString(path + "trim.pattern", "").toLowerCase();
529+
TrimPattern trimPattern = null;
530+
try {
531+
NamespacedKey patternNamespace = NamespacedKey.fromString(patternKey);
532+
if (patternNamespace != null) {
533+
trimPattern = Registry.TRIM_PATTERN.get(patternNamespace);
534+
}
535+
if (trimPattern == null) {
536+
enableTrim = false;
537+
String joinedNames = Registry.TRIM_PATTERN.stream()
538+
.map(TrimPattern::getKey)
539+
.map(NamespacedKey::toString)
540+
.collect(Collectors.joining(", "));
541+
Logger.info("Trim pattern '" + patternKey + "' was not found for item '" + file.getAbsolutePath() + "'. Available patterns: " + joinedNames, Logger.LogType.ERROR);
542+
}
543+
} catch (Exception e) {
526544
enableTrim = false;
527-
Bukkit.getLogger().severe("Trim pattern " + configuration.getString(path + "trim.pattern", "") + " was not found for item " + file.getAbsolutePath());
545+
Logger.info("Invalid namespace for trim pattern: '" + patternKey + "' in file '" + file.getAbsolutePath() + "'", Logger.LogType.ERROR);
528546
}
529-
TrimMaterial trimMaterial = trimHelper.getTrimMaterials().get(configuration.getString(path + "trim.material", "").toLowerCase());
530-
if (trimMaterial == null) {
547+
548+
String materialKey = configuration.getString(path + "trim.material", "").toLowerCase();
549+
TrimMaterial trimMaterial = null;
550+
try {
551+
NamespacedKey materialNamespace = NamespacedKey.fromString(materialKey);
552+
if (materialNamespace != null) {
553+
trimMaterial = Registry.TRIM_MATERIAL.get(materialNamespace);
554+
}
555+
if (trimMaterial == null) {
556+
enableTrim = false;
557+
String joinedNames = Registry.TRIM_MATERIAL.stream()
558+
.map(TrimMaterial::getKey)
559+
.map(NamespacedKey::toString)
560+
.collect(Collectors.joining(", "));
561+
Logger.info("Trim material '" + materialKey + "' was not found for item '" + file.getAbsolutePath() + "'. Available materials: " + joinedNames, Logger.LogType.ERROR);
562+
}
563+
} catch (Exception e) {
531564
enableTrim = false;
532-
Bukkit.getLogger().severe("Trim material " + configuration.getString(path + "trim.material", "") + " was not found for item " + file.getAbsolutePath());
565+
Logger.info("Invalid namespace for trim material: '" + materialKey + "' in file '" + file.getAbsolutePath() + "'", Logger.LogType.ERROR);
533566
}
534-
menuItemStack.setTrimConfiguration(new TrimConfiguration(enableTrim, trimMaterial, trimPattern));
567+
if (trimMaterial != null && trimPattern != null)
568+
menuItemStack.setTrimConfiguration(new TrimConfiguration(enableTrim, trimMaterial, trimPattern));
535569
}
536570
}
537571

src/main/java/fr/maxlego08/menu/loader/components/spigot/SpigotAttributeModifiersItemComponentLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public SpigotAttributeModifiersItemComponentLoader(MenuPlugin plugin){
5959
if (attribute == null) continue;
6060
try {
6161
AttributeModifier deserialize = AttributeModifier.deserialize(map);
62-
modifiersWrapper.add(new AttributeWrapper(attribute, deserialize.getOperation(), deserialize.getAmount(), deserialize.getSlotGroup()));
62+
modifiersWrapper.add(new AttributeWrapper(attribute, deserialize.getOperation(), deserialize.getAmount(), deserialize.getSlotGroup(), deserialize.getKey()));
6363
} catch (IllegalArgumentException e) {
6464
if (Configuration.enableDebug){
6565
Logger.info("Error deserializing attribute modifier for attribute " + attribute.name() + ": " + e.getMessage());

src/main/java/fr/maxlego08/menu/loader/components/spigot/SpigotEnchantmentGlintOverrideItemComponentLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public SpigotEnchantmentGlintOverrideItemComponentLoader(){
2020
@Override
2121
public @Nullable ItemComponent load(@NotNull MenuItemStackContext context, @NotNull File file, @NotNull YamlConfiguration configuration, @NotNull String path, @Nullable ConfigurationSection componentSection) {
2222
path = normalizePath(path);
23-
boolean hasGlint = configuration.getBoolean(path, false);
24-
return hasGlint ? new EnchantmentGlintOverrideComponent(true) : null;
23+
Object obj = configuration.get(path);
24+
if (obj == null) return null;
25+
return obj instanceof Boolean hasGlint ? new EnchantmentGlintOverrideComponent(hasGlint) : null;
2526
}
2627
}

0 commit comments

Comments
 (0)