Skip to content

Commit

Permalink
[RGB] Remove now redundant check for modern/legacy colors (partially)
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Jan 2, 2025
1 parent 2bdfaa1 commit af0151f
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PaperComponentConverter extends ComponentConverter {

@Override
@NotNull
public Component convert(@NotNull TabComponent component, boolean modern) {
public Component convert(@NotNull TabComponent component) {
switch (component) {
case SimpleComponent simpleComponent -> {
return Component.literal(simpleComponent.getText());
Expand All @@ -33,16 +33,12 @@ public Component convert(@NotNull TabComponent component, boolean modern) {
ChatModifier modifier = component1.getModifier();
TextColor color = null;
if (modifier.getColor() != null) {
if (modern) {
color = TextColor.fromRgb(modifier.getColor().getRgb());
} else {
color = TextColor.fromRgb(modifier.getColor().getLegacyColor().getRgb());
}
color = TextColor.fromRgb(modifier.getColor().getRgb());
}
nmsComponent.setStyle(newStyle(color, modifier.isBold(), modifier.isItalic(), modifier.isUnderlined(),
modifier.isStrikethrough(), modifier.isObfuscated(), modifier.getFont()));
for (StructuredComponent extra : component1.getExtra()) {
nmsComponent.append(convert(extra, modern));
nmsComponent.append(convert(extra));
}
return nmsComponent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class PaperHeaderFooter extends HeaderFooter {

@Override
public void set(@NotNull BukkitTabPlayer player, @NotNull TabComponent header, @NotNull TabComponent footer) {
player.getPlayer().sendPlayerListHeaderAndFooter(header.toAdventure(player.getVersion()), footer.toAdventure(player.getVersion()));
player.getPlayer().sendPlayerListHeaderAndFooter(header.toAdventure(), footer.toAdventure());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public abstract class ComponentConverter {
*
* @param component
* Component to convert
* @param modern
* Whether client supports RGB or not
* @return Converted component
*/
@NotNull
public abstract Object convert(@NotNull TabComponent component, boolean modern);
public abstract Object convert(@NotNull TabComponent component);

/**
* Attempts to load component converter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Class for converting TAB component into NMS components (1.7+).
Expand All @@ -29,7 +29,7 @@ public class ReflectionComponentConverter extends ComponentConverter {
private final FunctionWithException<String, Object> newTextComponent;
private final FunctionWithException<String, Object> newTranslatableComponent;
private final FunctionWithException<String, Object> newKeybindComponent;
private final BiFunction<ChatModifier, Boolean, Object> convertModifier;
private final Function<ChatModifier, Object> convertModifier;

private final Class<?> ChatModifier = BukkitReflection.getClass("network.chat.Style", "network.chat.ChatModifier", "ChatModifier");
private final Class<Enum> EnumChatFormat = (Class<Enum>) BukkitReflection.getClass("ChatFormatting", "EnumChatFormat");
Expand Down Expand Up @@ -103,22 +103,22 @@ public ReflectionComponentConverter() throws ReflectiveOperationException {
} else {
newChatModifier = ChatModifier.getConstructor();
ChatModifier_setColor = ReflectionUtils.getOnlyMethod(ChatModifier, ChatModifier, EnumChatFormat);
convertModifier = (modifier, protocolVersion) -> createModifierLegacy(modifier);
convertModifier = this::createModifierLegacy;
}
}

@Override
@SneakyThrows
@NotNull
public Object convert(@NotNull TabComponent component, boolean modern) {
public Object convert(@NotNull TabComponent component) {
if (component instanceof SimpleComponent) {
return newTextComponent.apply(((SimpleComponent) component).getText());
} else if (component instanceof StructuredComponent) {
StructuredComponent component1 = (StructuredComponent) component;
Object nmsComponent = newTextComponent.apply(component1.getText());
Component_modifier.set(nmsComponent, convertModifier.apply(component1.getModifier(), modern));
Component_modifier.set(nmsComponent, convertModifier.apply(component1.getModifier()));
for (StructuredComponent extra : component1.getExtra()) {
ChatBaseComponent_addSibling.invoke(nmsComponent, convert(extra, modern));
ChatBaseComponent_addSibling.invoke(nmsComponent, convert(extra));
}
return nmsComponent;
} else {
Expand Down Expand Up @@ -158,17 +158,9 @@ private Object fromAdventure(@NotNull Component component) {
}

@SneakyThrows
private Object createModifierModern(@NotNull ChatModifier modifier, boolean modern) {
Object color = null;
if (modifier.getColor() != null) {
if (modern) {
color = ChatHexColor_fromRGB.invoke(null, modifier.getColor().getRgb());
} else {
color = ChatHexColor_fromRGB.invoke(null, modifier.getColor().getLegacyColor().getRgb());
}
}
private Object createModifierModern(@NotNull ChatModifier modifier) {
return newStyleModern(
color,
modifier.getColor() == null ? null : ChatHexColor_fromRGB.invoke(null, modifier.getColor().getRgb()),
modifier.isBold(),
modifier.isItalic(),
modifier.isUnderlined(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public File getDataFolder() {
@NotNull
public Object convertComponent(@NotNull TabComponent component, boolean modern) {
if (ComponentConverter.INSTANCE != null) {
return ComponentConverter.INSTANCE.convert(component, modern);
return ComponentConverter.INSTANCE.convert(component);
} else {
return component;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ public PaperScoreboard(@NonNull BukkitTabPlayer player) {
public org.bukkit.scoreboard.Objective newObjective(@NonNull String objectiveName, @NonNull String criteria,
@NonNull TabComponent title, @NonNull HealthDisplay display) {
return scoreboard.registerNewObjective(objectiveName, criteria,
title.toAdventure(player.getVersion()), RenderType.values()[display.ordinal()]);
title.toAdventure(), RenderType.values()[display.ordinal()]);
}

@Override
public void setDisplayName(@NonNull org.bukkit.scoreboard.Objective objective, @NonNull TabComponent displayName) {
objective.displayName(displayName.toAdventure(player.getVersion()));
objective.displayName(displayName.toAdventure());
}

@Override
public void setPrefix(@NonNull org.bukkit.scoreboard.Team team, @NonNull TabComponent prefix) {
team.prefix(prefix.toAdventure(player.getVersion()));
team.prefix(prefix.toAdventure());
}

@Override
public void setSuffix(@NonNull org.bukkit.scoreboard.Team team, @NonNull TabComponent suffix) {
team.suffix(suffix.toAdventure(player.getVersion()));
team.suffix(suffix.toAdventure());
}

@Override
@SneakyThrows
public void setScoreDisplayName(@NotNull org.bukkit.scoreboard.Score s, @Nullable TabComponent displayName) {
if (numberFormatAPI) {
Component component = displayName == null ? null : displayName.toAdventure(player.getVersion());
Component component = displayName == null ? null : displayName.toAdventure();
Method m = s.getClass().getMethod("customName", Component.class);
m.setAccessible(true); // Why is this needed? The method is public!
m.invoke(s, component);
Expand Down Expand Up @@ -103,6 +103,6 @@ public void setObjectiveNumberFormat(@NotNull org.bukkit.scoreboard.Objective o,
private Object numberFormat(@Nullable TabComponent numberFormat) {
if (numberFormat == null) return null;
return Class.forName("io.papermc.paper.scoreboard.numbers.NumberFormat").getMethod("fixed", ComponentLike.class)
.invoke(null, numberFormat.toAdventure(player.getVersion()));
.invoke(null, numberFormat.toAdventure());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ public static void addSibling(@NotNull Component parent, @NotNull Component chil
*
* @param modifier
* Modifier to convert
* @param modern
* Whether RGB should be supported or not
* @return Converted style
*/
@NotNull
public static Style convertModifier(@NotNull ChatModifier modifier, boolean modern) {
if (serverVersion.getMinorVersion() >= 16) return loaderLatest.convertModifier(modifier, modern);
return loader1_14_4.convertModifier(modifier, modern);
public static Style convertModifier(@NotNull ChatModifier modifier) {
if (serverVersion.getMinorVersion() >= 16) return loaderLatest.convertModifier(modifier);
return loader1_14_4.convertModifier(modifier);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Component convertComponent(@NotNull TabComponent component, boolean moder
if (component instanceof StructuredComponent component1) {
Component nmsComponent = FabricMultiVersion.newTextComponent(component1.getText());

FabricMultiVersion.setStyle(nmsComponent, FabricMultiVersion.convertModifier(component1.getModifier(), modern));
FabricMultiVersion.setStyle(nmsComponent, FabricMultiVersion.convertModifier(component1.getModifier()));
for (StructuredComponent extra : component1.getExtra()) {
FabricMultiVersion.addSibling(nmsComponent, convertComponent(extra, modern));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ default Component newTextComponent(@NotNull String text) {
*
* @param modifier
* Modifier to convert
* @param modern
* Whether RGB should be supported or not
* @return Converted style
*/
@NotNull
default Style convertModifier(@NotNull ChatModifier modifier, boolean modern) {
default Style convertModifier(@NotNull ChatModifier modifier) {
throw new UnsupportedOperationException("Not implemented.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,9 @@ public Component newTextComponent(@NotNull String text) {

@Override
@NotNull
public Style convertModifier(@NotNull ChatModifier modifier, boolean modern) {
TextColor color = null;
if (modifier.getColor() != null) {
if (modern) {
color = TextColor.fromRgb(modifier.getColor().getRgb());
} else {
color = TextColor.fromRgb(modifier.getColor().getLegacyColor().getRgb());
}
}
public Style convertModifier(@NotNull ChatModifier modifier) {
return Style.EMPTY
.withColor(color)
.withColor(modifier.getColor() == null ? null : TextColor.fromRgb(modifier.getColor().getRgb()))
.withBold(modifier.isBold())
.withItalic(modifier.isItalic())
.withUnderlined(modifier.isUnderlined())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Component newTextComponent(@NotNull String text) {

@Override
@NotNull
public Style convertModifier(@NotNull ChatModifier modifier, boolean modern) {
public Style convertModifier(@NotNull ChatModifier modifier) {
Style style = new Style();
if (modifier.getColor() != null) {
style.setColor(ChatFormatting.valueOf(modifier.getColor().getLegacyColor().name()));
Expand Down
37 changes: 12 additions & 25 deletions shared/src/main/java/me/neznamy/tab/shared/chat/TabComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -30,13 +30,9 @@ public abstract class TabComponent {
@Nullable
private Object convertedLegacy;

/** Adventure component from this component for 1.16+ players */
/** Adventure component from this component */
@Nullable
private Component adventureModern;

/** Adventure component from this component for 1.15- players */
@Nullable
private Component adventureLegacy;
private Component adventureComponent;

@Nullable
private Object fixedFormat;
Expand All @@ -46,8 +42,9 @@ public abstract class TabComponent {
private Object textHolder;

/**
* Last color of this component. Used to determine team color based on last color of prefix.
* Saves as TextColor instead of EnumChatFormat to have things ready if Mojang adds RGB support to team color.
* Last color of this component.
* Used to determine team color based on the last color of prefix.
* Saved as TextColor instead of EnumChatFormat to have things ready if Mojang adds RGB support to team color.
*/
@Nullable
private TextColor lastColor;
Expand All @@ -74,21 +71,13 @@ public <T> T convert(@NotNull ProtocolVersion clientVersion) {
}

/**
* Converts this component to adventure component.
*
* @param clientVersion
* Client version
* Converts this component to an Adventure component.
* @return Converted component
*/
@NotNull
public Component toAdventure(@NotNull ProtocolVersion clientVersion) {
if (clientVersion.supportsRGB()) {
if (adventureModern == null) adventureModern = AdventureHook.toAdventureComponent(this, true);
return adventureModern;
} else {
if (adventureLegacy == null) adventureLegacy = AdventureHook.toAdventureComponent(this, false);
return adventureLegacy;
}
public Component toAdventure() {
if (adventureComponent == null) adventureComponent = AdventureHook.toAdventureComponent(this);
return adventureComponent;
}

/**
Expand All @@ -115,16 +104,14 @@ public <F, C> F toFixedFormat(@NotNull FunctionWithException<C, F> createFunctio
*
* @param convertFunction
* Function for converting adventure Component to TextHolder
* @param version
* Player version
* @return Converted TextHolder
* @param <T>
* TextHolder type
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> T toTextHolder(@NotNull BiFunction<TabComponent, ProtocolVersion, T> convertFunction, @NotNull ProtocolVersion version) {
if (textHolder == null) textHolder = convertFunction.apply(this, version);
public <T> T toTextHolder(@NotNull Function<TabComponent, T> convertFunction) {
if (textHolder == null) textHolder = convertFunction.apply(this);
return (T) textHolder;
}

Expand Down
32 changes: 6 additions & 26 deletions shared/src/main/java/me/neznamy/tab/shared/hook/AdventureHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

/**
* Class for Adventure component conversion.
Expand Down Expand Up @@ -43,20 +44,18 @@ private static EnumSet<TextDecoration>[] loadDecorations() {
*
* @param component
* Component to convert
* @param modern
* Whether client supports RGB or not
* @return Adventure component from this component.
*/
@NotNull
public static Component toAdventureComponent(@NotNull TabComponent component, boolean modern) {
public static Component toAdventureComponent(@NotNull TabComponent component) {
if (component instanceof AdventureComponent) return ((AdventureComponent) component).getComponent();
if (component instanceof SimpleComponent) return Component.text(((SimpleComponent) component).getText());
StructuredComponent iComponent = (StructuredComponent) component;
ChatModifier modifier = iComponent.getModifier();

Component adventureComponent = Component.text(
iComponent.getText(),
convertColor(modifier.getColor(), modern),
modifier.getColor() == null ? null : TextColor.color(modifier.getColor().getRgb()),
decorations[modifier.getMagicCodeBitMask()]
);

Expand All @@ -66,29 +65,10 @@ public static Component toAdventureComponent(@NotNull TabComponent component, bo
if (!iComponent.getExtra().isEmpty()) {
List<Component> list = new ArrayList<>();
for (StructuredComponent extra : iComponent.getExtra()) {
list.add(toAdventureComponent(extra, modern));
list.add(toAdventureComponent(extra));
}
adventureComponent = adventureComponent.children(list);
}
return adventureComponent;
}

/**
* Converts TAB color into adventure color.
*
* @param color
* Color to convert
* @param rgbSupport
* Whether RGB is supported or not
* @return Converted color
*/
@Nullable
private static TextColor convertColor(@Nullable me.neznamy.tab.shared.chat.TextColor color, boolean rgbSupport) {
if (color == null) return null;
if (rgbSupport) {
return TextColor.color(color.getRgb());
} else {
return TextColor.color(color.getLegacyColor().getRgb());
}
}
}
Loading

0 comments on commit af0151f

Please sign in to comment.