Skip to content

Commit d512986

Browse files
authored
Added support for the new CustomModelDataComponent (#195)
1 parent 08fbb34 commit d512986

File tree

10 files changed

+447
-176
lines changed

10 files changed

+447
-176
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
# Compile only
3-
spigot = "1.21.4-R0.1-SNAPSHOT"
3+
spigot = "1.21.5-R0.1-SNAPSHOT"
44
vault = "1.7.1"
55
authlib = "1.5.25"
66
headdb = "1.3.2"

src/main/java/com/extendedclip/deluxemenus/DeluxeMenus.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public void onLoad() {
6666
return;
6767
}
6868

69-
this.debug(DebugLevel.HIGHEST, Level.WARNING, "Could not setup a NMS hook for your server version!");
69+
this.debug(
70+
DebugLevel.HIGHEST,
71+
Level.WARNING,
72+
"Could not setup a NMS hook for your server version! The following Item options will not work: nbt_int, nbt_ints, nbt_string and nbt_strings."
73+
);
7074
}
7175

7276
@Override

src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import com.extendedclip.deluxemenus.action.ClickActionTask;
77
import com.extendedclip.deluxemenus.action.ClickHandler;
88
import com.extendedclip.deluxemenus.hooks.ItemHook;
9-
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
109
import com.extendedclip.deluxemenus.menu.Menu;
1110
import com.extendedclip.deluxemenus.menu.MenuHolder;
1211
import com.extendedclip.deluxemenus.menu.MenuItem;
12+
import com.extendedclip.deluxemenus.menu.options.CustomModelDataComponent;
13+
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
1314
import com.extendedclip.deluxemenus.menu.options.MenuItemOptions;
1415
import com.extendedclip.deluxemenus.menu.options.MenuOptions;
1516
import com.extendedclip.deluxemenus.requirement.HasExpRequirement;
@@ -632,6 +633,13 @@ private Map<Integer, TreeMap<Integer, MenuItem>> loadMenuItems(FileConfiguration
632633
.tooltipStyle(c.getString(currentPath + "tooltip_style", null))
633634
.itemModel(c.getString(currentPath + "item_model", null));
634635

636+
if (c.contains(currentPath + "model_data_component") && c.isConfigurationSection(currentPath + "model_data_component")) {
637+
final ConfigurationSection modelDataComponent = c.getConfigurationSection(currentPath + "model_data_component");
638+
if (modelDataComponent != null) {
639+
builder.customModelDataComponent(CustomModelDataComponent.builder().colors(modelDataComponent.getStringList("colors")).flags(modelDataComponent.getStringList("flags")).floats(modelDataComponent.getStringList("floats")).strings(modelDataComponent.getStringList("strings")));
640+
}
641+
}
642+
635643
// Lore Append Mode
636644
if (c.contains(currentPath + "lore_append_mode")) {
637645
String loreAppendMode = c.getString(currentPath + "lore_append_mode", "OVERRIDE").toUpperCase();
@@ -907,6 +915,18 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
907915
wrapper.setCustomData(c.getInt(rPath + ".modeldata", 0));
908916
}
909917

918+
if (c.contains(rPath + ".model_data_component") && c.isConfigurationSection(rPath + ".model_data_component")) {
919+
final ConfigurationSection modelDataComponent = c.getConfigurationSection(rPath + ".model_data_component");
920+
if (modelDataComponent != null) {
921+
wrapper.setCustomModelDataComponent(
922+
CustomModelDataComponent.builder()
923+
.colors(modelDataComponent.getStringList("colors"))
924+
.flags(modelDataComponent.getStringList("flags"))
925+
.floats(modelDataComponent.getStringList("floats"))
926+
.strings(modelDataComponent.getStringList("strings")));
927+
}
928+
}
929+
910930
if (c.contains(rPath + ".name_contains")) {
911931
wrapper.setNameContains(c.getBoolean(rPath + ".name_contains"));
912932
} else {
@@ -968,7 +988,7 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
968988
plugin.debug(
969989
DebugLevel.HIGHEST,
970990
Level.WARNING,
971-
"Has Permissions requirement at path: " + rPath + " has a minimum higher than the amount of permissions. Using "+permissions.size()+" instead"
991+
"Has Permissions requirement at path: " + rPath + " has a minimum higher than the amount of permissions. Using " + permissions.size() + " instead"
972992
);
973993
minimum = permissions.size();
974994
}
@@ -1252,9 +1272,14 @@ public void debug(String... messages) {
12521272
public File getMenuDirector() {
12531273
return menuDirectory;
12541274
}
1255-
public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final String currentPath,
1256-
final String itemKey, final String menuName,
1257-
final MenuItemOptions.MenuItemOptionsBuilder builder) {
1275+
1276+
public void addEnchantmentsOptionToBuilder(
1277+
final FileConfiguration c,
1278+
final String currentPath,
1279+
final String itemKey,
1280+
final String menuName,
1281+
final MenuItemOptions.MenuItemOptionsBuilder builder
1282+
) {
12581283
if (!c.contains(currentPath + "enchantments")) {
12591284
return;
12601285
}
@@ -1267,9 +1292,7 @@ public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final Stri
12671292
plugin.debug(
12681293
DebugLevel.HIGHEST,
12691294
Level.WARNING,
1270-
"Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey +
1271-
" in GUI " + menuName + "!",
1272-
"Correct format: - '<Enchantment name>;<level>"
1295+
"Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey + " in GUI " + menuName + "!", "Correct format: - '<Enchantment name>;<level>"
12731296
);
12741297
continue;
12751298
}
@@ -1279,9 +1302,7 @@ public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final Stri
12791302
plugin.debug(
12801303
DebugLevel.HIGHEST,
12811304
Level.WARNING,
1282-
"Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey +
1283-
" in GUI " + menuName + "!",
1284-
"Correct format: - '<Enchantment name>;<level>"
1305+
"Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey + " in GUI " + menuName + "!", "Correct format: - '<Enchantment name>;<level>"
12851306
);
12861307
continue;
12871308
}
@@ -1291,8 +1312,7 @@ public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final Stri
12911312
plugin.debug(
12921313
DebugLevel.HIGHEST,
12931314
Level.WARNING,
1294-
"Enchantment '" + parts[0].strip() + "' for item " + itemKey +
1295-
" in menu " + menuName + " is not a valid enchantment name!"
1315+
"Enchantment '" + parts[0].strip() + "' for item " + itemKey + " in menu " + menuName + " is not a valid enchantment name!"
12961316
);
12971317
}
12981318

@@ -1302,17 +1322,21 @@ public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final Stri
13021322
plugin.debug(
13031323
DebugLevel.HIGHEST,
13041324
Level.WARNING,
1305-
"Enchantment level '" + parts[1].strip() + "' is incorrect for item " + itemKey +
1306-
" in menu " + menuName + "!"
1325+
"Enchantment level '" + parts[1].strip() + "' is incorrect for item " + itemKey + " in menu " + menuName + "!"
13071326
);
13081327
}
13091328
parsedEnchantments.put(enchantment, level);
13101329
}
13111330
builder.enchantments(parsedEnchantments);
13121331
}
13131332

1314-
public void addDamageOptionToBuilder(final FileConfiguration c, final String currentPath, final String itemKey,
1315-
final String menuName, final MenuItemOptions.MenuItemOptionsBuilder builder) {
1333+
public void addDamageOptionToBuilder(
1334+
final FileConfiguration c,
1335+
final String currentPath,
1336+
final String itemKey,
1337+
final String menuName,
1338+
final MenuItemOptions.MenuItemOptionsBuilder builder
1339+
) {
13161340
boolean damageOptionIsPresent = false;
13171341
String damageValue = null;
13181342

src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.extendedclip.deluxemenus.menu.options.HeadType;
66
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
77
import com.extendedclip.deluxemenus.menu.options.MenuItemOptions;
8+
import com.extendedclip.deluxemenus.menu.options.CustomModelDataComponent;
89
import com.extendedclip.deluxemenus.nbt.NbtProvider;
910
import com.extendedclip.deluxemenus.utils.DebugLevel;
1011
import com.extendedclip.deluxemenus.utils.ItemUtils;
@@ -39,6 +40,7 @@
3940
import org.bukkit.inventory.meta.trim.TrimPattern;
4041
import org.bukkit.potion.PotionEffect;
4142
import org.jetbrains.annotations.NotNull;
43+
import org.jetbrains.annotations.Nullable;
4244

4345
import java.util.Arrays;
4446
import java.util.ArrayList;
@@ -158,13 +160,9 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
158160

159161
if (meta != null) {
160162
if (this.options.rgb().isPresent()) {
161-
final String rgbString = holder.setPlaceholdersAndArguments(this.options.rgb().get());
162-
final String[] parts = rgbString.split(",");
163-
164-
try {
165-
meta.setColor(Color.fromRGB(Integer.parseInt(parts[0].trim()), Integer.parseInt(parts[1].trim()),
166-
Integer.parseInt(parts[2].trim())));
167-
} catch (Exception ignored) {
163+
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
164+
if (color != null) {
165+
meta.setColor(color);
168166
}
169167
}
170168

@@ -224,14 +222,18 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
224222
return itemStack;
225223
}
226224

227-
if (this.options.customModelData().isPresent() && VersionHelper.IS_CUSTOM_MODEL_DATA) {
225+
if (VersionHelper.IS_CUSTOM_MODEL_DATA && this.options.customModelData().isPresent()) {
228226
try {
229227
final int modelData = Integer.parseInt(holder.setPlaceholdersAndArguments(this.options.customModelData().get()));
230228
itemMeta.setCustomModelData(modelData);
231229
} catch (final Exception ignored) {
232230
}
233231
}
234232

233+
if (VersionHelper.IS_CUSTOM_MODEL_DATA_COMPONENT && this.options.customModelDataComponent().isPresent()) {
234+
itemMeta.setCustomModelDataComponent(parseCustomModelDataComponent(this.options.customModelDataComponent().get(), itemMeta.getCustomModelDataComponent(), holder));
235+
}
236+
235237
if (this.options.displayName().isPresent()) {
236238
final String displayName = holder.setPlaceholdersAndArguments(this.options.displayName().get());
237239
itemMeta.setDisplayName(StringUtils.color(displayName));
@@ -346,37 +348,33 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
346348
}
347349

348350
if (itemMeta instanceof LeatherArmorMeta && this.options.rgb().isPresent()) {
349-
final String rgbString = holder.setPlaceholdersAndArguments(this.options.rgb().get());
350-
final String[] parts = rgbString.split(",");
351351
final LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
352352

353-
try {
354-
leatherArmorMeta.setColor(Color.fromRGB(Integer.parseInt(parts[0].trim()), Integer.parseInt(parts[1].trim()),
355-
Integer.parseInt(parts[2].trim())));
356-
itemStack.setItemMeta(leatherArmorMeta);
357-
} catch (final Exception exception) {
358-
plugin.printStacktrace(
359-
"Invalid rgb colors found for leather armor: " + parts[0].trim() + ", " + parts[1].trim() + ", " +
360-
parts[2].trim(),
361-
exception
353+
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
354+
if (color != null) {
355+
leatherArmorMeta.setColor(color);
356+
} else {
357+
plugin.debug(
358+
DebugLevel.HIGHEST,
359+
Level.WARNING,
360+
"Invalid rgb colors found for leather armor: " + this.options.rgb().get()
362361
);
363362
}
363+
364+
itemStack.setItemMeta(leatherArmorMeta);
364365
} else if (itemMeta instanceof FireworkEffectMeta && this.options.rgb().isPresent()) {
365-
final String rgbString = holder.setPlaceholdersAndArguments(this.options.rgb().get());
366-
final String[] parts = rgbString.split(",");
367366
final FireworkEffectMeta fireworkEffectMeta = (FireworkEffectMeta) itemMeta;
368-
369-
try {
370-
fireworkEffectMeta.setEffect(FireworkEffect.builder().withColor(Color.fromRGB(Integer.parseInt(parts[0].trim()),
371-
Integer.parseInt(parts[1].trim()), Integer.parseInt(parts[2].trim()))).build());
372-
itemStack.setItemMeta(fireworkEffectMeta);
373-
} catch (final Exception exception) {
374-
plugin.printStacktrace(
375-
"Invalid rgb colors found for firework or firework star: " + parts[0].trim() + ", "
376-
+ parts[1].trim() + ", " + parts[2].trim(),
377-
exception
367+
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
368+
if (color != null) {
369+
fireworkEffectMeta.setEffect(FireworkEffect.builder().withColor(color).build());
370+
} else {
371+
plugin.debug(
372+
DebugLevel.HIGHEST,
373+
Level.WARNING,
374+
"Invalid RGB color found for firework or firework star: " + this.options.rgb().get()
378375
);
379376
}
377+
itemStack.setItemMeta(fireworkEffectMeta);
380378
} else if (itemMeta instanceof EnchantmentStorageMeta && !this.options.enchantments().isEmpty()) {
381379
final EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta;
382380
for (final Map.Entry<Enchantment, Integer> entry : this.options.enchantments().entrySet()) {
@@ -547,6 +545,62 @@ protected List<String> getMenuItemLore(@NotNull final MenuHolder holder, @NotNul
547545
.collect(Collectors.toList());
548546
}
549547

548+
private @NotNull org.bukkit.inventory.meta.components.CustomModelDataComponent parseCustomModelDataComponent(
549+
@NotNull final CustomModelDataComponent unparsedComponent,
550+
@NotNull final org.bukkit.inventory.meta.components.CustomModelDataComponent component,
551+
@NotNull final MenuHolder holder
552+
) {
553+
if (!unparsedComponent.colors().isEmpty()) {
554+
final List<Color> colors = unparsedComponent.colors()
555+
.stream()
556+
.map(holder::setPlaceholdersAndArguments)
557+
.map(this::parseRGBColor)
558+
.filter(Objects::nonNull)
559+
.collect(Collectors.toList());
560+
component.setColors(colors);
561+
}
562+
563+
if (!unparsedComponent.flags().isEmpty()) {
564+
final List<Boolean> flags = unparsedComponent.flags()
565+
.stream()
566+
.map(holder::setPlaceholdersAndArguments)
567+
.map(Boolean::parseBoolean)
568+
.collect(Collectors.toList());
569+
component.setFlags(flags);
570+
}
571+
572+
if (!unparsedComponent.floats().isEmpty()) {
573+
final List<Float> floats = unparsedComponent.floats()
574+
.stream()
575+
.map(holder::setPlaceholdersAndArguments)
576+
.map(Float::parseFloat)
577+
.collect(Collectors.toList());
578+
component.setFloats(floats);
579+
}
580+
581+
if (!unparsedComponent.strings().isEmpty()) {
582+
final List<String> strings = unparsedComponent.strings()
583+
.stream()
584+
.map(holder::setPlaceholdersAndArguments)
585+
.collect(Collectors.toList());
586+
component.setStrings(strings);
587+
}
588+
589+
return component;
590+
}
591+
592+
private @Nullable Color parseRGBColor(@NotNull final String input) {
593+
final Color color = StringUtils.parseRGBColor(input);
594+
if (color == null) {
595+
plugin.debug(
596+
DebugLevel.HIGHEST,
597+
Level.WARNING,
598+
"Invalid RGB color found: " + input
599+
);
600+
}
601+
return color;
602+
}
603+
550604
public @NotNull MenuItemOptions options() {
551605
return options;
552606
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.extendedclip.deluxemenus.menu.options;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class CustomModelDataComponent {
9+
private List<String> colors = new ArrayList<>();
10+
private List<String> flags = new ArrayList<>();
11+
private List<String> floats = new ArrayList<>();
12+
private List<String> strings = new ArrayList<>();
13+
14+
private CustomModelDataComponent() {
15+
// Private constructor to force builder usage
16+
}
17+
18+
@NotNull
19+
public CustomModelDataComponent colors(@NotNull final List<@NotNull String> colors) {
20+
this.colors = colors;
21+
return this;
22+
}
23+
24+
@NotNull
25+
public List<@NotNull String> colors() {
26+
return colors;
27+
}
28+
29+
@NotNull
30+
public CustomModelDataComponent flags(@NotNull final List<@NotNull String> flags) {
31+
this.flags = flags;
32+
return this;
33+
}
34+
35+
@NotNull
36+
public List<@NotNull String> flags() {
37+
return flags;
38+
}
39+
40+
@NotNull
41+
public CustomModelDataComponent floats(@NotNull final List<@NotNull String> floats) {
42+
this.floats = floats;
43+
return this;
44+
}
45+
46+
@NotNull
47+
public List<@NotNull String> floats() {
48+
return floats;
49+
}
50+
51+
@NotNull
52+
public CustomModelDataComponent strings(@NotNull final List<@NotNull String> strings) {
53+
this.strings = strings;
54+
return this;
55+
}
56+
57+
@NotNull
58+
public List<@NotNull String> strings() {
59+
return strings;
60+
}
61+
62+
public static @NotNull CustomModelDataComponent builder() {
63+
return new CustomModelDataComponent();
64+
}
65+
}

0 commit comments

Comments
 (0)