Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProtocolLib packet adapter #11

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
[ci skip] cleanup
vytskalt committed Mar 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 97b1abc6315151d57718dd9eae9e80895e171036
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.megavex.scoreboardlibrary.implementation.packetAdapter.protocollib;

import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.NotNull;

// NOTE: ProtocolLib already has these utilities (AdventureComponentConverter)
// but they cannot be used as adventure is potentially relocated
public final class ComponentConversions {
private static final GsonComponentSerializer SERIALIZER;

static {
if (MinecraftVersion.NETHER_UPDATE.atOrAbove()) {
SERIALIZER = GsonComponentSerializer.gson();
} else {
SERIALIZER = GsonComponentSerializer.colorDownsamplingGson();
}
}

public static @NotNull WrappedChatComponent wrapAdventure(@NotNull Component component) {
return WrappedChatComponent.fromJson(SERIALIZER.serialize(component));
}
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.megavex.scoreboardlibrary.api.objective.ObjectiveDisplaySlot;
@@ -21,7 +20,6 @@

import java.util.Collection;

import static net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson;
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;

public class ObjectivePacketAdapterImpl implements ObjectivePacketAdapter {
@@ -42,7 +40,7 @@ public void display(@NotNull Collection<Player> players, @NotNull ObjectiveDispl
packet.getStrings().write(0, objectiveName);

int displaySlotIdx = ObjectiveConstants.displaySlotIndex(slot);
if (MinecraftVersion.getCurrentVersion().isAtLeast(new MinecraftVersion(1, 20, 2))) {
if (new MinecraftVersion(1, 20, 2).atOrAbove()) {
packet.getDisplaySlots().write(0, DISPLAY_SLOTS[displaySlotIdx]);
} else {
packet.getIntegers().write(0, displaySlotIdx);
@@ -74,9 +72,8 @@ public void sendProperties(@NotNull Collection<Player> players, @NotNull Propert
packet.getStrings().write(0, objectiveName);

Component translatedValue = GlobalTranslator.render(value, locale);
if (MinecraftVersion.getCurrentVersion().isAtLeast(MinecraftVersion.AQUATIC_UPDATE)) {
WrappedChatComponent wrappedValue = WrappedChatComponent.fromJson(gson().serialize(translatedValue));
packet.getChatComponents().write(0, wrappedValue);
if (MinecraftVersion.AQUATIC_UPDATE.atOrAbove()) {
packet.getChatComponents().write(0, ComponentConversions.wrapAdventure(translatedValue));
} else {
String legacyValue = LegacyFormatUtil.limitLegacyText(
legacySection().serialize(translatedValue),
@@ -119,7 +116,7 @@ public void sendScore(@NotNull Collection<Player> players, @NotNull String entry
@Override
public void removeScore(@NotNull Collection<Player> players, @NotNull String entry) {
PacketContainer packet;
if (MinecraftVersion.getCurrentVersion().isAtLeast(new MinecraftVersion(1, 20, 3))) {
if (new MinecraftVersion(1, 20, 3).atOrAbove()) {
packet = pm.createPacket(PacketType.Play.Server.RESET_SCORE);
packet.getStrings().write(0, entry)
.write(1, objectiveName);
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.EnumWrappers;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketAdapterProvider;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.objective.ObjectivePacketAdapter;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.protocollib.team.TeamsPacketAdapterImpl;
@@ -20,9 +19,7 @@ public class PacketAdapterProviderImpl implements PacketAdapterProvider {

public PacketAdapterProviderImpl() {
this.pm = Objects.requireNonNull(ProtocolLibrary.getProtocolManager());
this.isLegacyVersion = !MinecraftVersion.getCurrentVersion().isAtLeast(MinecraftVersion.AQUATIC_UPDATE);
System.out.println("[scoreboard-library] Render type class = " + EnumWrappers.getRenderTypeClass());
System.out.println("[scoreboard-library] Display slot class = " + EnumWrappers.getDisplaySlotClass());
this.isLegacyVersion = !MinecraftVersion.AQUATIC_UPDATE.atOrAbove();
}

@Override
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedTeamParameters;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.megavex.scoreboardlibrary.implementation.commons.LegacyFormatUtil;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.ImmutableTeamProperties;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketSender;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PropertiesPacketType;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.protocollib.ComponentConversions;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.team.EntriesPacketType;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.team.TeamConstants;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.team.TeamDisplayPacketAdapter;
@@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.Optional;

import static net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson;
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;
import static net.megavex.scoreboardlibrary.implementation.commons.LegacyFormatUtil.limitLegacyText;

@@ -58,12 +57,12 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
PacketSender<PacketContainer> sender = pm::sendServerPacket;
char legacyChar = LegacyFormatUtil.getChar(properties.playerColor());
EnumWrappers.ChatFormatting color = EnumWrappers.ChatFormatting.fromBukkit(ChatColor.getByChar(legacyChar));
boolean usesParameters = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
boolean usesComponents = MinecraftVersion.AQUATIC_UPDATE.atOrAbove();
boolean v1_17OrAbove = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
boolean v1_13OrAbove = MinecraftVersion.AQUATIC_UPDATE.atOrAbove();

LocalePacketUtil.sendLocalePackets(sender, players, locale -> {
PacketContainer packet = pm.createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);
int modeIdx = usesComponents ? 0 : 1;
int modeIdx = v1_13OrAbove ? 0 : 1;
packet.getIntegers().write(modeIdx, TeamConstants.mode(packetType));
packet.getStrings().write(0, teamName);
packet.getSpecificModifier(Collection.class).write(0, properties.entries());
@@ -72,11 +71,11 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
Component prefix = GlobalTranslator.render(properties.prefix(), locale);
Component suffix = GlobalTranslator.render(properties.suffix(), locale);

if (usesParameters) {
if (v1_17OrAbove) {
WrappedTeamParameters params = WrappedTeamParameters.newBuilder()
.displayName(wrapComponent(displayName))
.prefix(wrapComponent(prefix))
.suffix(wrapComponent(suffix))
.displayName(ComponentConversions.wrapAdventure(displayName))
.prefix(ComponentConversions.wrapAdventure(prefix))
.suffix(ComponentConversions.wrapAdventure(suffix))
.nametagVisibility(properties.nameTagVisibility().key())
.collisionRule(properties.collisionRule().key())
.color(color)
@@ -85,17 +84,17 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
packet.getOptionalTeamParameters().write(0, Optional.of(params));
} else {
packet.getIntegers().write(modeIdx + 1, properties.packOptions());
if (usesComponents) {
if (v1_13OrAbove) {
packet.getChatFormattings().write(0, color);
} else {
packet.getIntegers().write(0, color.ordinal());
}

if (usesComponents) {
if (v1_13OrAbove) {
packet.getChatComponents()
.write(0, wrapComponent(displayName))
.write(1, wrapComponent(prefix))
.write(2, wrapComponent(suffix));
.write(0, ComponentConversions.wrapAdventure(displayName))
.write(1, ComponentConversions.wrapAdventure(prefix))
.write(2, ComponentConversions.wrapAdventure(suffix));

packet.getStrings()
.write(1, properties.nameTagVisibility().key())
@@ -114,10 +113,6 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
});
}

private @NotNull WrappedChatComponent wrapComponent(@NotNull Component component) {
return WrappedChatComponent.fromJson(gson().serialize(component));
}

private @NotNull String toLegacy(@NotNull Component component) {
return limitLegacyText(legacySection().serialize(component), TeamConstants.LEGACY_CHAR_LIMIT);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.megavex.scoreboardlibrary.implementation.packetAdapter.v1_8_R3;

import net.megavex.scoreboardlibrary.implementation.packetAdapter.objective.ObjectivePacketAdapter;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketAdapterProvider;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketSender;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.objective.ObjectivePacketAdapter;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.team.TeamsPacketAdapter;
import net.minecraft.server.v1_8_R3.Packet;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;