From f16dd54a7fe2b0cacf7ac61086b1ed3d344fa6f2 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 14 Dec 2023 23:00:04 +0000 Subject: [PATCH] feat: Add support for Minecraft 1.20.3/1.20.4 (#126) * docs: Minor comment tweak * Prepare 1.20.3 support pending Velocity * 1.20.3/1.20.4 & java 17 (#128) * Improved PacketAdapter system + added support for 1.20.3/1.20.4 * Changed java version to 17, updated velocity dependencies, improved packet adapters & added support for 1.20.3/4. * Fixed compile error with adventure * deps: Bump `netty-codec-http` to 4.1.103 * ci: Upgrade dependabot config * ci: Update CI & Docs with new requirements * refactor: Rename `LUCK_PERMS_META` -> `LUCKPERMS_META` * docs: Document `%luckperms_meta_(key)%` --------- Co-authored-by: AlexDev_ <56083016+alexdev03@users.noreply.github.com> --- .github/dependabot.yml | 17 +++- .github/workflows/{java_ci.yml => ci.yml} | 35 ++++--- .github/workflows/pr_tests.yml | 15 +-- .github/workflows/release.yml | 35 ++++--- .github/workflows/update_docs.yml | 9 +- README.md | 4 +- build.gradle | 16 +++- docs/Placeholders.md | 1 + docs/Setup.md | 2 +- gradle.properties | 2 +- .../velocitab/api/VelocitabAPI.java | 1 - .../velocitab/config/Placeholder.java | 6 +- .../velocitab/packet/Protocol340Adapter.java | 94 ------------------- .../velocitab/packet/Protocol404Adapter.java | 23 ++--- .../velocitab/packet/Protocol48Adapter.java | 24 ++--- .../velocitab/packet/Protocol735Adapter.java | 41 +------- .../velocitab/packet/Protocol765Adapter.java | 46 +++++++++ .../velocitab/packet/ScoreboardManager.java | 15 ++- .../velocitab/packet/TeamsPacketAdapter.java | 6 +- .../velocitab/packet/UpdateTeamsPacket.java | 2 +- .../velocitab/player/TabPlayer.java | 2 +- 21 files changed, 170 insertions(+), 226 deletions(-) rename .github/workflows/{java_ci.yml => ci.yml} (77%) delete mode 100644 src/main/java/net/william278/velocitab/packet/Protocol340Adapter.java create mode 100644 src/main/java/net/william278/velocitab/packet/Protocol765Adapter.java diff --git a/.github/dependabot.yml b/.github/dependabot.yml index a0a14d38..db2b51cc 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -2,7 +2,18 @@ version: 2 updates: - - package-ecosystem: "gradle" # See documentation for possible values - directory: "/" # Location of package manifests + # CI workflow action updates + - package-ecosystem: "github-actions" + directory: "/" schedule: - interval: "daily" \ No newline at end of file + interval: "weekly" + commit-message: + prefix: "ci" + + # Gradle package updates + - package-ecosystem: "gradle" + directory: "/" + schedule: + interval: "weekly" + commit-message: + prefix: "deps" \ No newline at end of file diff --git a/.github/workflows/java_ci.yml b/.github/workflows/ci.yml similarity index 77% rename from .github/workflows/java_ci.yml rename to .github/workflows/ci.yml index 19d22341..b3db5f6a 100644 --- a/.github/workflows/java_ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ -# Builds, tests the project with Gradle -name: Java CI +# Builds, tests the project with Gradle and publishes to Modrinth & Hangar +name: CI Tests & Publish on: push: @@ -11,32 +11,34 @@ on: permissions: contents: read + checks: write jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 16 - uses: actions/setup-java@v3 + - name: 'Checkout for CI 🛎️' + uses: actions/checkout@v4 + - name: 'Set up JDK 17 📦' + uses: actions/setup-java@v4 with: - java-version: '16' + java-version: '17' distribution: 'temurin' - - name: Build with Gradle + - name: 'Build with Gradle 🏗️' uses: gradle/gradle-build-action@v2 with: - arguments: build test publish + arguments: build publish env: SNAPSHOTS_MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} SNAPSHOTS_MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} - - name: Query Version + - name: 'Fetch Version Name 📝' run: | echo "::set-output name=VERSION_NAME::$(${{github.workspace}}/gradlew properties --no-daemon --console=plain -q | grep "^version:" | awk '{printf $2}')" id: fetch-version - name: Get Version run: | echo "version_name=${{steps.fetch-version.outputs.VERSION_NAME}}" >> $GITHUB_ENV - - name: Upload to Modrinth & Hangar + - name: 'Publish to Modrinth & Hangar 🧽' uses: WiIIiam278/mc-publish@hangar with: modrinth-id: Q10irTG0 @@ -47,7 +49,7 @@ jobs: hangar-token: ${{ secrets.HANGAR_API_KEY }} hangar-version-type: Alpha hangar-game-versions: | - 3.2 + 3.3 files: target/Velocitab-*.jar name: Velocitab v${{ env.version_name }} version: ${{ env.version_name }} @@ -100,9 +102,12 @@ jobs: 1.20 1.20.1 1.20.2 - java: 16 - - name: Upload GitHub Artifact - uses: actions/upload-artifact@v2 + 1.20.3 + 1.20.4 + java: 17 + - name: 'Upload GitHub Artifact 📦' + uses: actions/upload-artifact@v3 + if: success() || failure() with: - name: Velocitab Plugin + name: Velocitab path: target/Velocitab-*.jar \ No newline at end of file diff --git a/.github/workflows/pr_tests.yml b/.github/workflows/pr_tests.yml index 84ddcbc8..afb52f31 100644 --- a/.github/workflows/pr_tests.yml +++ b/.github/workflows/pr_tests.yml @@ -1,4 +1,3 @@ -# Carry out tests on pull requests name: PR Tests on: @@ -7,18 +6,20 @@ on: permissions: contents: read + checks: write jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 16 - uses: actions/setup-java@v3 + - name: 'Checkout for CI 🛎' + uses: actions/checkout@v4 + - name: 'Set up JDK 17 📦' + uses: actions/setup-java@v4 with: - java-version: '16' + java-version: '17' distribution: 'temurin' - - name: Test Pull Request - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + - name: 'Build with Gradle 🏗️' + uses: gradle/gradle-build-action@v2 with: arguments: build \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 004a5c4b..be97e434 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,32 +7,27 @@ on: permissions: contents: read + checks: write jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 16 - uses: actions/setup-java@v3 + - name: 'Checkout for CI 🛎️' + uses: actions/checkout@v4 + - name: 'Set up JDK 17 📦' + uses: actions/setup-java@v4 with: - java-version: '16' + java-version: '17' distribution: 'temurin' - - name: Build with Gradle + - name: 'Build with Gradle 🏗️' uses: gradle/gradle-build-action@v2 with: - arguments: build test publish + arguments: build publish env: RELEASES_MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} RELEASES_MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} - - name: Query Version - run: | - echo "::set-output name=VERSION_NAME::$(${{github.workspace}}/gradlew properties --no-daemon --console=plain -q | grep "^version:" | awk '{printf $2}')" - id: fetch-version - - name: Get Version - run: | - echo "version_name=${{steps.fetch-version.outputs.VERSION_NAME}}" >> $GITHUB_ENV - - name: Upload to Modrinth & Hangar + - name: 'Publish to Modrinth & Hangar 🚰' uses: WiIIiam278/mc-publish@hangar with: modrinth-id: Q10irTG0 @@ -43,7 +38,7 @@ jobs: hangar-token: ${{ secrets.HANGAR_API_KEY }} hangar-version-type: Release hangar-game-versions: | - 3.2 + 3.3 files: target/Velocitab-*.jar name: Velocitab v${{ github.event.release.tag_name }} version: ${{ github.event.release.tag_name }} @@ -96,9 +91,11 @@ jobs: 1.20 1.20.1 1.20.2 - java: 16 - - name: Upload GitHub Artifact - uses: actions/upload-artifact@v2 + 1.20.3 + 1.20.4 + java: 17 + - name: 'Upload GitHub Artifacts 📦' + uses: actions/upload-artifact@v3 with: - name: Velocitab Plugin + name: Velocitab path: target/Velocitab-*.jar \ No newline at end of file diff --git a/.github/workflows/update_docs.yml b/.github/workflows/update_docs.yml index fd188eb0..ca4b5fd4 100644 --- a/.github/workflows/update_docs.yml +++ b/.github/workflows/update_docs.yml @@ -1,4 +1,3 @@ -# Update the GitHub Wiki documentation when a push is made to docs/ name: Update Docs on: @@ -17,10 +16,10 @@ jobs: deploy-wiki: runs-on: ubuntu-latest steps: - - name: 'Checkout Code' - uses: actions/checkout@v3 - - name: 'Push Changes to Wiki' - uses: Andrew-Chen-Wang/github-wiki-action@v3 + - name: 'Checkout for CI 🛎️' + uses: actions/checkout@v4 + - name: 'Push Docs to Github Wiki 📄️' + uses: Andrew-Chen-Wang/github-wiki-action@v4 env: WIKI_DIR: 'docs/' GH_TOKEN: ${{ github.token }} diff --git a/README.md b/README.md index f677b6c7..d401eea9 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@

Velocitab - - + + diff --git a/build.gradle b/build.gradle index 73d11059..124c1508 100644 --- a/build.gradle +++ b/build.gradle @@ -26,17 +26,25 @@ repositories { maven { url = 'https://repo.minebench.de/' } maven { url = 'https://maven.elytrium.net/repo/' } maven { url = 'https://mvn.exceptionflug.de/repository/exceptionflug-public/' } + maven { url = 'https://repo.william278.net/velocity/' } } dependencies { - compileOnly 'com.velocitypowered:velocity-api:3.2.0-SNAPSHOT' - compileOnly 'com.velocitypowered:velocity-proxy:3.2.0-SNAPSHOT' + compileOnly ('com.velocitypowered:velocity-api:3.3.0-SNAPSHOT') { + exclude group: 'net.kyori' + } + compileOnly 'com.velocitypowered:velocity-proxy:3.3.0-SNAPSHOT' compileOnly 'io.netty:netty-codec-http:4.1.103.Final' compileOnly 'org.projectlombok:lombok:1.18.30' compileOnly 'net.luckperms:api:5.4' compileOnly 'io.github.miniplaceholders:miniplaceholders-api:2.0.0' compileOnly 'net.william278:PAPIProxyBridge:1.4.2' compileOnly 'it.unimi.dsi:fastutil:8.5.12' + compileOnly 'net.kyori:adventure-nbt:4.14.0' + compileOnly 'net.kyori:adventure-api:4.14.0' + compileOnly 'net.kyori:adventure-text-minimessage:4.14.0' + compileOnly 'net.kyori:adventure-text-serializer-legacy:4.14.0' + compileOnly 'net.kyori:adventure-text-serializer-gson:4.14.0' implementation 'org.apache.commons:commons-text:1.11.0' @@ -72,6 +80,9 @@ javadoc.options.encoding = 'UTF-8' javadoc.options.addStringOption('Xdoclint:none', '-quiet') java { + def javaVersion = JavaVersion.toVersion(javaVersion) + sourceCompatibility = javaVersion + targetCompatibility = javaVersion withSourcesJar() withJavadocJar() } @@ -89,7 +100,6 @@ shadowJar { relocate 'org.bstats', 'net.william278.velocitab.libraries.bstats' dependencies { - //noinspection GroovyAssignabilityCheck exclude dependency(':slf4j-api') } diff --git a/docs/Placeholders.md b/docs/Placeholders.md index 1d023138..69b9747b 100644 --- a/docs/Placeholders.md +++ b/docs/Placeholders.md @@ -18,6 +18,7 @@ Placeholders can be included in the header, footer and player name format of the | `%role%` | The player's primary LuckPerms group name | `admin` | | `%role_display_name%` | The player's primary LuckPerms group display name | `Admin` | | `%role_weight%` | Comparable-formatted primary LuckPerms group weight. | `100` | +| `%luckperms_meta_(key)%` | Formats a meta key from the user's LuckPerms group | (varies) | | `%server_group%` | The name of the server group the player is on | `default` | | `%server_group_index%` | Indexed order of the server group in the list | `0` | | `%debug_team_name%` | (Debug) Player's team name, used for [[Sorting]] | `1alphaWilliam278` | diff --git a/docs/Setup.md b/docs/Setup.md index 409603c6..3bb49196 100644 --- a/docs/Setup.md +++ b/docs/Setup.md @@ -1,7 +1,7 @@ This page will walk you through installing Velocitab on a Velocity proxy server. ## Requirements -* A Velocity proxy server (running Velocity 3.2.0 or newer) +* A Velocity proxy server (running Velocity 3.3.0 or newer) * Backend Minecraft servers. The following Minecraft server versions are fully supported: - Minecraft 1.8—1.8.9 - Minecraft 1.12.2—latest diff --git a/gradle.properties b/gradle.properties index d0d5e93c..32b10a66 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -javaVersion=16 +javaVersion=17 org.gradle.jvmargs='-Dfile.encoding=UTF-8' org.gradle.daemon=true diff --git a/src/main/java/net/william278/velocitab/api/VelocitabAPI.java b/src/main/java/net/william278/velocitab/api/VelocitabAPI.java index df35f05f..feabc703 100644 --- a/src/main/java/net/william278/velocitab/api/VelocitabAPI.java +++ b/src/main/java/net/william278/velocitab/api/VelocitabAPI.java @@ -17,7 +17,6 @@ * limitations under the License. */ - package net.william278.velocitab.api; import com.velocitypowered.api.proxy.Player; diff --git a/src/main/java/net/william278/velocitab/config/Placeholder.java b/src/main/java/net/william278/velocitab/config/Placeholder.java index f22c31cf..b9f8734e 100644 --- a/src/main/java/net/william278/velocitab/config/Placeholder.java +++ b/src/main/java/net/william278/velocitab/config/Placeholder.java @@ -57,9 +57,9 @@ public enum Placeholder { SERVER_GROUP((plugin, player) -> player.getServerGroup(plugin)), SERVER_GROUP_INDEX((plugin, player) -> Integer.toString(player.getServerGroupPosition(plugin))), DEBUG_TEAM_NAME((plugin, player) -> plugin.getFormatter().escape(player.getLastTeamName().orElse(""))), - LUCK_PERMS_META_((param, plugin, player) -> plugin.getLuckPermsHook().map(hook -> hook.getMeta(player.getPlayer(), param)) - .orElse("")), - ; + LUCKPERMS_META_((param, plugin, player) -> plugin.getLuckPermsHook() + .map(hook -> hook.getMeta(player.getPlayer(), param)) + .orElse("")); /** * Function to replace placeholders with a real value diff --git a/src/main/java/net/william278/velocitab/packet/Protocol340Adapter.java b/src/main/java/net/william278/velocitab/packet/Protocol340Adapter.java deleted file mode 100644 index 9f962304..00000000 --- a/src/main/java/net/william278/velocitab/packet/Protocol340Adapter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * This file is part of Velocitab, licensed under the Apache License 2.0. - * - * Copyright (c) William278 - * Copyright (c) contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package net.william278.velocitab.packet; - -import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.proxy.protocol.ProtocolUtils; -import io.netty.buffer.ByteBuf; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.william278.velocitab.Velocitab; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -/** - * Adapter for handling the UpdateTeamsPacket for Minecraft 1.12.2 - */ -@SuppressWarnings("DuplicatedCode") -public class Protocol340Adapter extends TeamsPacketAdapter { - - private final LegacyComponentSerializer serializer; - - public Protocol340Adapter(@NotNull Velocitab plugin) { - super(plugin, Set.of(ProtocolVersion.MINECRAFT_1_12_2)); - serializer = LegacyComponentSerializer.legacySection(); - } - - @Override - public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) { - ProtocolUtils.writeString(byteBuf, packet.teamName().substring(0, Math.min(packet.teamName().length(), 16))); - UpdateTeamsPacket.UpdateMode mode = packet.mode(); - byteBuf.writeByte(mode.id()); - if (mode == UpdateTeamsPacket.UpdateMode.REMOVE_TEAM) { - return; - } - if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.UPDATE_INFO) { - final String displayName = getChatString(packet.displayName()); - final String prefix = getChatString(packet.prefix()); - final String suffix = getChatString(packet.suffix()); - - ProtocolUtils.writeString(byteBuf, shrinkString(displayName)); - ProtocolUtils.writeString(byteBuf, shrinkString(prefix)); - ProtocolUtils.writeString(byteBuf, shrinkString(suffix)); - byteBuf.writeByte(UpdateTeamsPacket.FriendlyFlag.toBitMask(packet.friendlyFlags())); - ProtocolUtils.writeString(byteBuf, packet.nametagVisibility().id()); - ProtocolUtils.writeString(byteBuf, packet.collisionRule().id()); - byteBuf.writeByte(packet.color()); - } - if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.ADD_PLAYERS || mode == UpdateTeamsPacket.UpdateMode.REMOVE_PLAYERS) { - List entities = packet.entities(); - ProtocolUtils.writeVarInt(byteBuf, entities != null ? entities.size() : 0); - for (String entity : entities != null ? entities : new ArrayList()) { - ProtocolUtils.writeString(byteBuf, entity); - } - } - } - - /** - * Returns a shortened version of the given string, with a maximum length of 16 characters. - * This is used to ensure that the team name, display name, prefix and suffix are not too long for the client. - * @param string the string to be shortened - * @return the shortened string - */ - @NotNull - private String shrinkString(@NotNull String string) { - return string.substring(0, Math.min(string.length(), 16)); - } - - @NotNull - @Override - protected String getChatString(@NotNull Component component) { - return serializer.serialize(component); - } -} diff --git a/src/main/java/net/william278/velocitab/packet/Protocol404Adapter.java b/src/main/java/net/william278/velocitab/packet/Protocol404Adapter.java index 450c500a..8bb3bb0d 100644 --- a/src/main/java/net/william278/velocitab/packet/Protocol404Adapter.java +++ b/src/main/java/net/william278/velocitab/packet/Protocol404Adapter.java @@ -17,7 +17,6 @@ * limitations under the License. */ - package net.william278.velocitab.packet; import com.velocitypowered.api.network.ProtocolVersion; @@ -33,14 +32,13 @@ import java.util.Set; /** - * Adapter for handling the UpdateTeamsPacket for Minecraft 1.13.2 - 1.15.2 + * Adapter for handling the UpdateTeamsPacket for Minecraft 1.13.2-1.15.2 */ @SuppressWarnings("DuplicatedCode") public class Protocol404Adapter extends TeamsPacketAdapter { private final GsonComponentSerializer serializer; - public Protocol404Adapter(@NotNull Velocitab plugin) { super(plugin, Set.of(ProtocolVersion.MINECRAFT_1_13_2, ProtocolVersion.MINECRAFT_1_14, @@ -55,8 +53,13 @@ public Protocol404Adapter(@NotNull Velocitab plugin) { serializer = GsonComponentSerializer.colorDownsamplingGson(); } + public Protocol404Adapter(@NotNull Velocitab plugin, Set protocolVersions) { + super(plugin, protocolVersions); + serializer = null; + } + @Override - public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) { + public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet, @NotNull ProtocolVersion protocolVersion) { ProtocolUtils.writeString(byteBuf, packet.teamName()); UpdateTeamsPacket.UpdateMode mode = packet.mode(); byteBuf.writeByte(mode.id()); @@ -64,13 +67,13 @@ public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) return; } if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.UPDATE_INFO) { - ProtocolUtils.writeString(byteBuf, getChatString(packet.displayName())); + writeComponent(byteBuf, packet.displayName()); byteBuf.writeByte(UpdateTeamsPacket.FriendlyFlag.toBitMask(packet.friendlyFlags())); ProtocolUtils.writeString(byteBuf, packet.nametagVisibility().id()); ProtocolUtils.writeString(byteBuf, packet.collisionRule().id()); byteBuf.writeByte(packet.color()); - ProtocolUtils.writeString(byteBuf, getChatString(packet.prefix())); - ProtocolUtils.writeString(byteBuf, getChatString(packet.suffix())); + writeComponent(byteBuf, packet.prefix()); + writeComponent(byteBuf, packet.suffix()); } if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.ADD_PLAYERS || mode == UpdateTeamsPacket.UpdateMode.REMOVE_PLAYERS) { List entities = packet.entities(); @@ -81,10 +84,8 @@ public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) } } - @NotNull - @Override - protected String getChatString(@NotNull Component component) { - return serializer.serialize(component); + protected void writeComponent(ByteBuf buf, Component component) { + ProtocolUtils.writeString(buf, serializer.serialize(component)); } } diff --git a/src/main/java/net/william278/velocitab/packet/Protocol48Adapter.java b/src/main/java/net/william278/velocitab/packet/Protocol48Adapter.java index d287f09d..961a6167 100644 --- a/src/main/java/net/william278/velocitab/packet/Protocol48Adapter.java +++ b/src/main/java/net/william278/velocitab/packet/Protocol48Adapter.java @@ -17,7 +17,6 @@ * limitations under the License. */ - package net.william278.velocitab.packet; import com.velocitypowered.api.network.ProtocolVersion; @@ -41,12 +40,12 @@ public class Protocol48Adapter extends TeamsPacketAdapter { private final LegacyComponentSerializer serializer; public Protocol48Adapter(@NotNull Velocitab plugin) { - super(plugin, Set.of(ProtocolVersion.MINECRAFT_1_8)); + super(plugin, Set.of(ProtocolVersion.MINECRAFT_1_8, ProtocolVersion.MINECRAFT_1_12_2)); serializer = LegacyComponentSerializer.legacySection(); } @Override - public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) { + public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet, @NotNull ProtocolVersion protocolVersion) { ProtocolUtils.writeString(byteBuf, shrinkString(packet.teamName())); UpdateTeamsPacket.UpdateMode mode = packet.mode(); byteBuf.writeByte(mode.id()); @@ -54,15 +53,14 @@ public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) return; } if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.UPDATE_INFO) { - final String displayName = getChatString(packet.displayName()); - final String prefix = getChatString(packet.prefix()); - final String suffix = getChatString(packet.suffix()); - - ProtocolUtils.writeString(byteBuf, shrinkString(displayName)); - ProtocolUtils.writeString(byteBuf, shrinkString(prefix)); - ProtocolUtils.writeString(byteBuf, shrinkString(suffix)); + writeComponent(byteBuf, packet.displayName()); + writeComponent(byteBuf, packet.prefix()); + writeComponent(byteBuf, packet.suffix()); byteBuf.writeByte(UpdateTeamsPacket.FriendlyFlag.toBitMask(packet.friendlyFlags())); ProtocolUtils.writeString(byteBuf, packet.nametagVisibility().id()); + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { + ProtocolUtils.writeString(byteBuf, packet.collisionRule().id()); + } byteBuf.writeByte(packet.color()); } if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.ADD_PLAYERS || mode == UpdateTeamsPacket.UpdateMode.REMOVE_PLAYERS) { @@ -85,9 +83,7 @@ private String shrinkString(@NotNull String string) { return string.substring(0, Math.min(string.length(), 16)); } - @NotNull - @Override - protected String getChatString(@NotNull Component component) { - return serializer.serialize(component); + protected void writeComponent(ByteBuf buf, Component component) { + ProtocolUtils.writeString(buf, shrinkString(serializer.serialize(component))); } } diff --git a/src/main/java/net/william278/velocitab/packet/Protocol735Adapter.java b/src/main/java/net/william278/velocitab/packet/Protocol735Adapter.java index d2777594..e8ba7a1b 100644 --- a/src/main/java/net/william278/velocitab/packet/Protocol735Adapter.java +++ b/src/main/java/net/william278/velocitab/packet/Protocol735Adapter.java @@ -17,7 +17,6 @@ * limitations under the License. */ - package net.william278.velocitab.packet; import com.velocitypowered.api.network.ProtocolVersion; @@ -28,15 +27,12 @@ import net.william278.velocitab.Velocitab; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.List; import java.util.Set; /** - * Adapter for handling the UpdateTeamsPacket for Minecraft 1.16 - 1.20.2 + * Adapter for handling the UpdateTeamsPacket for Minecraft 1.16-1.20.2 */ -@SuppressWarnings("DuplicatedCode") -public class Protocol735Adapter extends TeamsPacketAdapter { +public class Protocol735Adapter extends Protocol404Adapter { private final GsonComponentSerializer serializer; @@ -62,35 +58,8 @@ public Protocol735Adapter(@NotNull Velocitab plugin) { } @Override - public void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet) { - ProtocolUtils.writeString(byteBuf, packet.teamName()); - UpdateTeamsPacket.UpdateMode mode = packet.mode(); - byteBuf.writeByte(mode.id()); - if (mode == UpdateTeamsPacket.UpdateMode.REMOVE_TEAM) { - return; - } - if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.UPDATE_INFO) { - ProtocolUtils.writeString(byteBuf, getChatString(packet.displayName())); - byteBuf.writeByte(UpdateTeamsPacket.FriendlyFlag.toBitMask(packet.friendlyFlags())); - ProtocolUtils.writeString(byteBuf, packet.nametagVisibility().id()); - ProtocolUtils.writeString(byteBuf, packet.collisionRule().id()); - byteBuf.writeByte(packet.color()); - ProtocolUtils.writeString(byteBuf, getChatString(packet.prefix())); - ProtocolUtils.writeString(byteBuf, getChatString(packet.suffix())); - } - if (mode == UpdateTeamsPacket.UpdateMode.CREATE_TEAM || mode == UpdateTeamsPacket.UpdateMode.ADD_PLAYERS || mode == UpdateTeamsPacket.UpdateMode.REMOVE_PLAYERS) { - List entities = packet.entities(); - ProtocolUtils.writeVarInt(byteBuf, entities != null ? entities.size() : 0); - for (String entity : entities != null ? entities : new ArrayList()) { - ProtocolUtils.writeString(byteBuf, entity); - } - } - } - - @NotNull - @Override - protected String getChatString(@NotNull Component component) { - return serializer.serialize(component); + protected void writeComponent(ByteBuf buf, Component component) { + ProtocolUtils.writeString(buf, serializer.serialize(component)); } -} +} \ No newline at end of file diff --git a/src/main/java/net/william278/velocitab/packet/Protocol765Adapter.java b/src/main/java/net/william278/velocitab/packet/Protocol765Adapter.java new file mode 100644 index 00000000..0e643066 --- /dev/null +++ b/src/main/java/net/william278/velocitab/packet/Protocol765Adapter.java @@ -0,0 +1,46 @@ +/* + * This file is part of Velocitab, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.velocitab.packet; + +import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; +import io.netty.buffer.ByteBuf; +import net.kyori.adventure.text.Component; +import net.william278.velocitab.Velocitab; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; + +/** + * Adapter for handling the UpdateTeamsPacket for Minecraft 1.20.3-1.20.4 + */ +public class Protocol765Adapter extends Protocol404Adapter { + + public Protocol765Adapter(@NotNull Velocitab plugin) { + super(plugin, Set.of( + ProtocolVersion.MINECRAFT_1_20_3 + )); + } + + protected void writeComponent(ByteBuf buf, Component component) { + new ComponentHolder(ProtocolVersion.MINECRAFT_1_20_3, component).write(buf); + } + +} diff --git a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java index de970bc0..79d7fa9a 100644 --- a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java +++ b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java @@ -53,10 +53,14 @@ public ScoreboardManager(@NotNull Velocitab velocitab) { } private void registerVersions() { - versions.add(new Protocol735Adapter(plugin)); - versions.add(new Protocol404Adapter(plugin)); - versions.add(new Protocol340Adapter(plugin)); - versions.add(new Protocol48Adapter(plugin)); + try { + versions.add(new Protocol765Adapter(plugin)); + versions.add(new Protocol735Adapter(plugin)); + versions.add(new Protocol404Adapter(plugin)); + versions.add(new Protocol48Adapter(plugin)); + } catch (NoSuchFieldError e) { + throw new IllegalStateException("Failed to register scoreboard packet adapters. Try to update velocity to latest build", e); + } } @NotNull @@ -282,7 +286,8 @@ public void registerPacket() { .mapping(0x58, MINECRAFT_1_19_1, true) .mapping(0x56, MINECRAFT_1_19_3, true) .mapping(0x5A, MINECRAFT_1_19_4, true) - .mapping(0x5C, MINECRAFT_1_20_2, true); + .mapping(0x5C, MINECRAFT_1_20_2, true) + .mapping(0x5E, MINECRAFT_1_20_3, true); packetRegistration.register(); } catch (Throwable e) { plugin.log(Level.ERROR, "Failed to register UpdateTeamsPacket", e); diff --git a/src/main/java/net/william278/velocitab/packet/TeamsPacketAdapter.java b/src/main/java/net/william278/velocitab/packet/TeamsPacketAdapter.java index 802a530f..9808224a 100644 --- a/src/main/java/net/william278/velocitab/packet/TeamsPacketAdapter.java +++ b/src/main/java/net/william278/velocitab/packet/TeamsPacketAdapter.java @@ -17,7 +17,6 @@ * limitations under the License. */ - package net.william278.velocitab.packet; import com.velocitypowered.api.network.ProtocolVersion; @@ -37,9 +36,8 @@ public abstract class TeamsPacketAdapter { private final Velocitab plugin; private final Set protocolVersions; - public abstract void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet); + public abstract void encode(@NotNull ByteBuf byteBuf, @NotNull UpdateTeamsPacket packet, @NotNull ProtocolVersion protocolVersion); - @NotNull - protected abstract String getChatString(@NotNull Component component); + protected abstract void writeComponent(ByteBuf buf, Component component); } diff --git a/src/main/java/net/william278/velocitab/packet/UpdateTeamsPacket.java b/src/main/java/net/william278/velocitab/packet/UpdateTeamsPacket.java index d258fbeb..135670be 100644 --- a/src/main/java/net/william278/velocitab/packet/UpdateTeamsPacket.java +++ b/src/main/java/net/william278/velocitab/packet/UpdateTeamsPacket.java @@ -202,7 +202,7 @@ public void encode(ByteBuf byteBuf, ProtocolUtils.Direction direction, ProtocolV if (optionalManager.isEmpty()) { return; } - optionalManager.get().getPacketAdapter(protocolVersion).encode(byteBuf, this); + optionalManager.get().getPacketAdapter(protocolVersion).encode(byteBuf, this, protocolVersion); } @Override diff --git a/src/main/java/net/william278/velocitab/player/TabPlayer.java b/src/main/java/net/william278/velocitab/player/TabPlayer.java index 3ff24ff1..7414b716 100644 --- a/src/main/java/net/william278/velocitab/player/TabPlayer.java +++ b/src/main/java/net/william278/velocitab/player/TabPlayer.java @@ -196,7 +196,7 @@ public boolean equals(Object obj) { } /** - * Represents a nametag to be displayed above a player, with prefix & suffix + * Represents a nametag to be displayed above a player, with prefix and suffix */ @Getter @AllArgsConstructor