|
5 | 5 | import com.extendedclip.deluxemenus.menu.options.HeadType; |
6 | 6 | import com.extendedclip.deluxemenus.menu.options.LoreAppendMode; |
7 | 7 | import com.extendedclip.deluxemenus.menu.options.MenuItemOptions; |
| 8 | +import com.extendedclip.deluxemenus.menu.options.CustomModelDataComponent; |
8 | 9 | import com.extendedclip.deluxemenus.nbt.NbtProvider; |
9 | 10 | import com.extendedclip.deluxemenus.utils.DebugLevel; |
10 | 11 | import com.extendedclip.deluxemenus.utils.ItemUtils; |
|
39 | 40 | import org.bukkit.inventory.meta.trim.TrimPattern; |
40 | 41 | import org.bukkit.potion.PotionEffect; |
41 | 42 | import org.jetbrains.annotations.NotNull; |
| 43 | +import org.jetbrains.annotations.Nullable; |
42 | 44 |
|
43 | 45 | import java.util.Arrays; |
44 | 46 | import java.util.ArrayList; |
@@ -158,13 +160,9 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { |
158 | 160 |
|
159 | 161 | if (meta != null) { |
160 | 162 | 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); |
168 | 166 | } |
169 | 167 | } |
170 | 168 |
|
@@ -224,14 +222,18 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { |
224 | 222 | return itemStack; |
225 | 223 | } |
226 | 224 |
|
227 | | - if (this.options.customModelData().isPresent() && VersionHelper.IS_CUSTOM_MODEL_DATA) { |
| 225 | + if (VersionHelper.IS_CUSTOM_MODEL_DATA && this.options.customModelData().isPresent()) { |
228 | 226 | try { |
229 | 227 | final int modelData = Integer.parseInt(holder.setPlaceholdersAndArguments(this.options.customModelData().get())); |
230 | 228 | itemMeta.setCustomModelData(modelData); |
231 | 229 | } catch (final Exception ignored) { |
232 | 230 | } |
233 | 231 | } |
234 | 232 |
|
| 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 | + |
235 | 237 | if (this.options.displayName().isPresent()) { |
236 | 238 | final String displayName = holder.setPlaceholdersAndArguments(this.options.displayName().get()); |
237 | 239 | itemMeta.setDisplayName(StringUtils.color(displayName)); |
@@ -346,37 +348,33 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) { |
346 | 348 | } |
347 | 349 |
|
348 | 350 | if (itemMeta instanceof LeatherArmorMeta && this.options.rgb().isPresent()) { |
349 | | - final String rgbString = holder.setPlaceholdersAndArguments(this.options.rgb().get()); |
350 | | - final String[] parts = rgbString.split(","); |
351 | 351 | final LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta; |
352 | 352 |
|
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() |
362 | 361 | ); |
363 | 362 | } |
| 363 | + |
| 364 | + itemStack.setItemMeta(leatherArmorMeta); |
364 | 365 | } 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(","); |
367 | 366 | 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() |
378 | 375 | ); |
379 | 376 | } |
| 377 | + itemStack.setItemMeta(fireworkEffectMeta); |
380 | 378 | } else if (itemMeta instanceof EnchantmentStorageMeta && !this.options.enchantments().isEmpty()) { |
381 | 379 | final EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta; |
382 | 380 | for (final Map.Entry<Enchantment, Integer> entry : this.options.enchantments().entrySet()) { |
@@ -547,6 +545,62 @@ protected List<String> getMenuItemLore(@NotNull final MenuHolder holder, @NotNul |
547 | 545 | .collect(Collectors.toList()); |
548 | 546 | } |
549 | 547 |
|
| 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 | + |
550 | 604 | public @NotNull MenuItemOptions options() { |
551 | 605 | return options; |
552 | 606 | } |
|
0 commit comments