Add ComponentWrapper#withoutPreFormatting

This commit is contained in:
NichtStudioCode 2023-03-12 18:42:23 +01:00
parent 20fa465684
commit 9b336db0a8
12 changed files with 102 additions and 41 deletions

@ -5,6 +5,7 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.xenondevs.inventoryaccess.component.i18n.AdventureComponentLocalizer; import xyz.xenondevs.inventoryaccess.component.i18n.AdventureComponentLocalizer;
import xyz.xenondevs.inventoryaccess.component.i18n.Languages; import xyz.xenondevs.inventoryaccess.component.i18n.Languages;
import xyz.xenondevs.inventoryaccess.util.AdventureComponentUtils;
public class AdventureComponentWrapper implements ComponentWrapper { public class AdventureComponentWrapper implements ComponentWrapper {
@ -27,6 +28,11 @@ public class AdventureComponentWrapper implements ComponentWrapper {
return new AdventureComponentWrapper(AdventureComponentLocalizer.getInstance().localize(lang, component)); return new AdventureComponentWrapper(AdventureComponentLocalizer.getInstance().localize(lang, component));
} }
@Override
public @NotNull ComponentWrapper withoutPreFormatting() {
return new AdventureComponentWrapper(AdventureComponentUtils.withoutPreFormatting(component));
}
@Override @Override
public @NotNull AdventureComponentWrapper clone() { public @NotNull AdventureComponentWrapper clone() {
try { try {

@ -3,23 +3,29 @@ package xyz.xenondevs.inventoryaccess.component;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.xenondevs.inventoryaccess.component.i18n.BaseComponentLocalizer; import xyz.xenondevs.inventoryaccess.component.i18n.BungeeComponentLocalizer;
import xyz.xenondevs.inventoryaccess.component.i18n.Languages; import xyz.xenondevs.inventoryaccess.component.i18n.Languages;
import xyz.xenondevs.inventoryaccess.util.BungeeComponentUtils;
public class BaseComponentWrapper implements ComponentWrapper { public class BungeeComponentWrapper implements ComponentWrapper {
private final BaseComponent[] components; private final BaseComponent[] components;
public BaseComponentWrapper(BaseComponent[] components) { public BungeeComponentWrapper(BaseComponent[] components) {
this.components = components; this.components = components;
} }
@Override @Override
public @NotNull ComponentWrapper localized(@NotNull String lang) { public @NotNull BungeeComponentWrapper localized(@NotNull String lang) {
if (!Languages.getInstance().doesServerSideTranslations()) if (!Languages.getInstance().doesServerSideTranslations())
return this; return this;
return new BaseComponentWrapper(BaseComponentLocalizer.getInstance().localize(lang, components)); return new BungeeComponentWrapper(BungeeComponentLocalizer.getInstance().localize(lang, components));
}
@Override
public @NotNull BungeeComponentWrapper withoutPreFormatting() {
return new BungeeComponentWrapper(BungeeComponentUtils.withoutPreFormatting(components));
} }
@Override @Override
@ -28,9 +34,9 @@ public class BaseComponentWrapper implements ComponentWrapper {
} }
@Override @Override
public @NotNull BaseComponentWrapper clone() { public @NotNull BungeeComponentWrapper clone() {
try { try {
var clone = (BaseComponentWrapper) super.clone(); var clone = (BungeeComponentWrapper) super.clone();
for (int i = 0; i < clone.components.length; i++) { for (int i = 0; i < clone.components.length; i++) {
clone.components[i] = clone.components[i].duplicate(); clone.components[i] = clone.components[i].duplicate();
} }

@ -25,6 +25,14 @@ public interface ComponentWrapper extends Cloneable {
*/ */
@NotNull ComponentWrapper localized(@NotNull String lang); @NotNull ComponentWrapper localized(@NotNull String lang);
/**
* Creates a new {@link ComponentWrapper} that forces the default formatting (white color, no decorations),
* unless configured otherwise.
*
* @return A new {@link ComponentWrapper} with the default formatting.
*/
@NotNull ComponentWrapper withoutPreFormatting();
/** /**
* Creates a localized version of the component by replacing all translatable components with text components * Creates a localized version of the component by replacing all translatable components with text components
* of the {@link Player Player's} language. * of the {@link Player Player's} language.

@ -6,14 +6,14 @@ import net.md_5.bungee.api.chat.TranslatableComponent;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BaseComponentLocalizer extends ComponentLocalizer<BaseComponent> { public class BungeeComponentLocalizer extends ComponentLocalizer<BaseComponent> {
private static final BaseComponentLocalizer INSTANCE = new BaseComponentLocalizer(); private static final BungeeComponentLocalizer INSTANCE = new BungeeComponentLocalizer();
private BaseComponentLocalizer() { private BungeeComponentLocalizer() {
} }
public static BaseComponentLocalizer getInstance() { public static BungeeComponentLocalizer getInstance() {
return INSTANCE; return INSTANCE;
} }

@ -3,7 +3,7 @@ package xyz.xenondevs.inventoryaccess.map;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper; import xyz.xenondevs.inventoryaccess.component.BungeeComponentWrapper;
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
public class MapIcon { public class MapIcon {
@ -23,7 +23,7 @@ public class MapIcon {
} }
public MapIcon(@NotNull MapIconType type, int x, int y, int rot, @Nullable BaseComponent[] component) { public MapIcon(@NotNull MapIconType type, int x, int y, int rot, @Nullable BaseComponent[] component) {
this(type, x, y, rot, new BaseComponentWrapper(component)); this(type, x, y, rot, new BungeeComponentWrapper(component));
} }
public MapIcon(MapIconType type, byte x, byte y, byte rot) { public MapIcon(MapIconType type, byte x, byte y, byte rot) {

@ -0,0 +1,23 @@
package xyz.xenondevs.inventoryaccess.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
public class AdventureComponentUtils {
private static final Style FORMATTING_TEMPLATE = Style.style()
.color(NamedTextColor.WHITE)
.decoration(TextDecoration.ITALIC, false)
.decoration(TextDecoration.BOLD, false)
.decoration(TextDecoration.STRIKETHROUGH, false)
.decoration(TextDecoration.UNDERLINED, false)
.decoration(TextDecoration.OBFUSCATED, false)
.build();
public static Component withoutPreFormatting(Component component) {
return component.style(component.style().merge(FORMATTING_TEMPLATE, Style.Merge.Strategy.IF_ABSENT_ON_TARGET));
}
}

@ -1,11 +1,13 @@
package xyz.xenondevs.invui.util; package xyz.xenondevs.inventoryaccess.util;
import net.md_5.bungee.api.ChatColor; 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 { import java.util.Arrays;
public class BungeeComponentUtils {
private static final BaseComponent FORMATTING_TEMPLATE = new ComponentBuilder("") private static final BaseComponent FORMATTING_TEMPLATE = new ComponentBuilder("")
.italic(false) .italic(false)
@ -21,13 +23,19 @@ public class ComponentUtils {
} }
public static BaseComponent[] withoutPreFormatting(BaseComponent... components) { public static BaseComponent[] withoutPreFormatting(BaseComponent... components) {
BaseComponent[] duplicate = duplicate(components);
BaseComponent previousComponent = FORMATTING_TEMPLATE; BaseComponent previousComponent = FORMATTING_TEMPLATE;
for (BaseComponent component : components) { for (BaseComponent component : duplicate) {
component.copyFormatting(previousComponent, false); component.copyFormatting(previousComponent, false);
previousComponent = component; previousComponent = component;
} }
return components; return duplicate;
}
public static BaseComponent[] duplicate(BaseComponent... components) {
return Arrays.stream(components).map(BaseComponent::duplicate).toArray(BaseComponent[]::new);
} }
} }

@ -3,10 +3,9 @@
package xyz.xenondevs.invui.item.builder package xyz.xenondevs.invui.item.builder
import net.md_5.bungee.api.chat.BaseComponent import net.md_5.bungee.api.chat.BaseComponent
import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper import xyz.xenondevs.inventoryaccess.component.BungeeComponentWrapper
import xyz.xenondevs.invui.util.ComponentUtils
/** /**
* Sets the lore of the item stack. * Sets the lore of the item stack.
*/ */
fun <T : AbstractItemBuilder<T>> T.setLore(lore: List<Array<BaseComponent>>): T = setLore(lore.map { BaseComponentWrapper(ComponentUtils.withoutPreFormatting(*it)) }) fun <T : AbstractItemBuilder<T>> T.setLore(lore: List<Array<BaseComponent>>): T = setLore(lore.map { BungeeComponentWrapper(it) })

@ -0,0 +1,10 @@
@file:Suppress("PackageDirectoryMismatch")
package xyz.xenondevs.invui.window
import net.kyori.adventure.text.Component
import xyz.xenondevs.inventoryaccess.component.AdventureComponentWrapper
fun Window.changeTitle(title: Component) {
changeTitle(AdventureComponentWrapper(title))
}

@ -9,7 +9,7 @@ import org.bukkit.event.player.PlayerResourcePackStatusEvent;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.InventoryAccess;
import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper; import xyz.xenondevs.inventoryaccess.component.BungeeComponentWrapper;
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
import xyz.xenondevs.inventoryaccess.util.ReflectionRegistry; import xyz.xenondevs.inventoryaccess.util.ReflectionRegistry;
import xyz.xenondevs.inventoryaccess.util.VersionUtils; import xyz.xenondevs.inventoryaccess.util.VersionUtils;
@ -79,7 +79,7 @@ public class ForceResourcePack implements Listener {
* @throws IOException If the connection was not successful * @throws IOException If the connection was not successful
*/ */
public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt) throws IOException { public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt) throws IOException {
setResourcePack(resourcePackUrl, new BaseComponentWrapper(prompt), true); setResourcePack(resourcePackUrl, new BungeeComponentWrapper(prompt), true);
} }
/** /**
@ -119,7 +119,7 @@ public class ForceResourcePack implements Listener {
* @throws IOException If the connection was not successful * @throws IOException If the connection was not successful
*/ */
public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt, boolean sendToOnlinePlayers) throws IOException { public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt, boolean sendToOnlinePlayers) throws IOException {
setResourcePack(resourcePackUrl, new BaseComponentWrapper(prompt), sendToOnlinePlayers); setResourcePack(resourcePackUrl, new BungeeComponentWrapper(prompt), sendToOnlinePlayers);
} }
public void sendResourcePack(Player player) { public void sendResourcePack(Player player) {

@ -1,6 +1,7 @@
package xyz.xenondevs.invui.item.builder; package xyz.xenondevs.invui.item.builder;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
@ -11,10 +12,9 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.InventoryAccess;
import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper; import xyz.xenondevs.inventoryaccess.component.BungeeComponentWrapper;
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
import xyz.xenondevs.invui.item.ItemProvider; import xyz.xenondevs.invui.item.ItemProvider;
import xyz.xenondevs.invui.util.ComponentUtils;
import xyz.xenondevs.invui.util.Pair; import xyz.xenondevs.invui.util.Pair;
import java.util.ArrayList; import java.util.ArrayList;
@ -207,19 +207,19 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setDisplayName(String displayName) { public @NotNull S setDisplayName(String displayName) {
this.displayName = new BaseComponentWrapper(ComponentUtils.withoutPreFormatting(displayName)); this.displayName = new BungeeComponentWrapper(TextComponent.fromLegacyText(displayName)).withoutPreFormatting();
return (S) this; return (S) this;
} }
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setDisplayName(BaseComponent... displayName) { public @NotNull S setDisplayName(BaseComponent... displayName) {
this.displayName = new BaseComponentWrapper(ComponentUtils.withoutPreFormatting(displayName)); this.displayName = new BungeeComponentWrapper(displayName).withoutPreFormatting();
return (S) this; return (S) this;
} }
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setDisplayName(ComponentWrapper component) { public @NotNull S setDisplayName(ComponentWrapper component) {
this.displayName = component; this.displayName = component.withoutPreFormatting();
return (S) this; return (S) this;
} }
@ -230,14 +230,16 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setLore(List<ComponentWrapper> lore) { public @NotNull S setLore(List<ComponentWrapper> lore) {
this.lore = lore; this.lore = lore.stream()
.map(ComponentWrapper::withoutPreFormatting)
.collect(Collectors.toList());
return (S) this; return (S) this;
} }
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setLegacyLore(@NotNull List<String> lore) { public @NotNull S setLegacyLore(@NotNull List<String> lore) {
this.lore = lore.stream() this.lore = lore.stream()
.map(line -> new BaseComponentWrapper(ComponentUtils.withoutPreFormatting(line))) .map(line -> new BungeeComponentWrapper(TextComponent.fromLegacyText(line)).withoutPreFormatting())
.collect(Collectors.toList()); .collect(Collectors.toList());
return (S) this; return (S) this;
} }
@ -247,7 +249,8 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
if (lore == null) lore = new ArrayList<>(); if (lore == null) lore = new ArrayList<>();
for (String line : lines) for (String line : lines)
lore.add(new BaseComponentWrapper(ComponentUtils.withoutPreFormatting(line))); lore.add(new BungeeComponentWrapper(TextComponent.fromLegacyText(line)).withoutPreFormatting());
return (S) this; return (S) this;
} }
@ -255,11 +258,8 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
public @NotNull S addLoreLines(@NotNull BaseComponent[]... lines) { public @NotNull S addLoreLines(@NotNull BaseComponent[]... lines) {
if (lore == null) lore = new ArrayList<>(); if (lore == null) lore = new ArrayList<>();
lore.addAll( for (BaseComponent[] line : lines)
Arrays.stream(lines) lore.add(new BungeeComponentWrapper(line).withoutPreFormatting());
.map(line -> new BaseComponentWrapper(ComponentUtils.withoutPreFormatting(line)))
.collect(Collectors.toList())
);
return (S) this; return (S) this;
} }
@ -268,7 +268,8 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
public @NotNull S addLoreLines(@NotNull ComponentWrapper... lines) { public @NotNull S addLoreLines(@NotNull ComponentWrapper... lines) {
if (lore == null) lore = new ArrayList<>(); if (lore == null) lore = new ArrayList<>();
lore.addAll(Arrays.asList(lines)); for (ComponentWrapper line : lines)
lore.add(line.withoutPreFormatting());
return (S) this; return (S) this;
} }

@ -18,7 +18,7 @@ import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.InventoryAccess;
import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper; import xyz.xenondevs.inventoryaccess.component.BungeeComponentWrapper;
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
import xyz.xenondevs.inventoryaccess.component.i18n.Languages; import xyz.xenondevs.inventoryaccess.component.i18n.Languages;
import xyz.xenondevs.invui.InvUI; import xyz.xenondevs.invui.InvUI;
@ -285,7 +285,7 @@ public abstract class AbstractWindow implements Window, GuiParent {
@Override @Override
public void changeTitle(@NotNull BaseComponent[] title) { public void changeTitle(@NotNull BaseComponent[] title) {
changeTitle(new BaseComponentWrapper(title)); changeTitle(new BungeeComponentWrapper(title));
} }
@Override @Override
@ -413,13 +413,13 @@ public abstract class AbstractWindow implements Window, GuiParent {
@Override @Override
public @NotNull S setTitle(@NotNull BaseComponent @NotNull [] title) { public @NotNull S setTitle(@NotNull BaseComponent @NotNull [] title) {
this.title = new BaseComponentWrapper(title); this.title = new BungeeComponentWrapper(title);
return (S) this; return (S) this;
} }
@Override @Override
public @NotNull S setTitle(@NotNull String title) { public @NotNull S setTitle(@NotNull String title) {
this.title = new BaseComponentWrapper(TextComponent.fromLegacyText(title)); this.title = new BungeeComponentWrapper(TextComponent.fromLegacyText(title));
return (S) this; return (S) this;
} }