ComponentUtils#withoutPreFormatting

This commit is contained in:
NichtStudioCode 2021-08-08 19:54:24 +02:00
parent be90969f22
commit f0ea48506f
2 changed files with 28 additions and 15 deletions

@ -182,18 +182,18 @@ public class ItemBuilder implements ItemProvider {
} }
public ItemBuilder setDisplayName(String displayName) { public ItemBuilder setDisplayName(String displayName) {
this.displayName = ComponentUtils.fromLegacyText(displayName); this.displayName = ComponentUtils.withoutPreFormatting(displayName);
return this; return this;
} }
public ItemBuilder setDisplayName(BaseComponent[] displayName) { public ItemBuilder setDisplayName(BaseComponent... displayName) {
this.displayName = displayName; this.displayName = ComponentUtils.withoutPreFormatting(displayName);
return this; return this;
} }
public ItemBuilder setLegacyLore(@NotNull List<String> lore) { public ItemBuilder setLegacyLore(@NotNull List<String> lore) {
this.lore = lore.stream() this.lore = lore.stream()
.map(ComponentUtils::fromLegacyText) .map(ComponentUtils::withoutPreFormatting)
.collect(Collectors.toList()); .collect(Collectors.toList());
return this; return this;
} }
@ -207,14 +207,14 @@ public class ItemBuilder implements ItemProvider {
if (lore == null) lore = new ArrayList<>(); if (lore == null) lore = new ArrayList<>();
for (String line : lines) for (String line : lines)
lore.add(ComponentUtils.fromLegacyText(line)); lore.add(ComponentUtils.withoutPreFormatting(line));
return this; return this;
} }
public ItemBuilder addLoreLines(@NotNull BaseComponent[]... lines) { public ItemBuilder addLoreLines(@NotNull BaseComponent[]... lines) {
if (lore == null) lore = new ArrayList<>(); if (lore == null) lore = new ArrayList<>();
lore.addAll(Arrays.asList(lines)); lore.addAll(Arrays.stream(lines).map(ComponentUtils::withoutPreFormatting).collect(Collectors.toList()));
return this; return this;
} }

@ -1,20 +1,33 @@
package de.studiocode.invui.util; package de.studiocode.invui.util;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
public class ComponentUtils { public class ComponentUtils {
public static BaseComponent[] fromLegacyText(String text) { private static final BaseComponent FORMATTING_TEMPLATE = new ComponentBuilder("")
return new ComponentBuilder("") .italic(false)
.italic(false) .bold(false)
.bold(false) .strikethrough(false)
.strikethrough(false) .underlined(false)
.underlined(false) .obfuscated(false)
.obfuscated(false) .color(ChatColor.WHITE)
.append(TextComponent.fromLegacyText(text)) .create()[0];
.create();
public static BaseComponent[] withoutPreFormatting(String text) {
return withoutPreFormatting(TextComponent.fromLegacyText(text));
}
public static BaseComponent[] withoutPreFormatting(BaseComponent... components) {
BaseComponent previousComponent = FORMATTING_TEMPLATE;
for (BaseComponent component : components) {
component.copyFormatting(previousComponent, false);
previousComponent = component;
}
return components;
} }
} }