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

@ -1,20 +1,33 @@
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.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
public class ComponentUtils {
public static BaseComponent[] fromLegacyText(String text) {
return new ComponentBuilder("")
.italic(false)
.bold(false)
.strikethrough(false)
.underlined(false)
.obfuscated(false)
.append(TextComponent.fromLegacyText(text))
.create();
private static final BaseComponent FORMATTING_TEMPLATE = new ComponentBuilder("")
.italic(false)
.bold(false)
.strikethrough(false)
.underlined(false)
.obfuscated(false)
.color(ChatColor.WHITE)
.create()[0];
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;
}
}