Skip to content

Commit

Permalink
fix: invalid packets in PacketEvents on 1.13+ servers with 1.12.2- cl…
Browse files Browse the repository at this point in the history
…ients and 1.12.2- servers with 1.13+ clients
  • Loading branch information
vytskalt committed Aug 22, 2024
1 parent 0e579e6 commit a883942
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
allprojects {
version = "2.1.12"
version = "2.1.13-SNAPSHOT"
group = "net.megavex"
description = "Powerful packet-level scoreboard library for Paper/Spigot servers"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.PacketEventsAPI;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.megavex.scoreboardlibrary.implementation.commons.LineRenderingStrategy;
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.team.TeamsPacketAdapter;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.objective.ObjectivePacketAdapter;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.packetevents.team.TeamsPacketAdapterImpl;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.team.TeamsPacketAdapter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
public class PacketAdapterProviderImpl implements PacketAdapterProvider, PacketSender<PacketWrapper<?>> {
public class PacketAdapterProviderImpl implements PacketAdapterProvider {
private final PacketEventsAPI<?> packetEvents;
private final PacketEventsSender packetSender;

public PacketAdapterProviderImpl() {
this.packetEvents = PacketEvents.getAPI();
if (this.packetEvents == null) {
throw new IllegalStateException("PacketEvents exists in classpath but isn't loaded");
}
this.packetSender = new PacketEventsSender(this.packetEvents);
}

@Override
public @NotNull TeamsPacketAdapter createTeamPacketAdapter(@NotNull String teamName) {
return new TeamsPacketAdapterImpl(this, teamName);
return new TeamsPacketAdapterImpl(packetSender, teamName);
}

@Override
public @NotNull ObjectivePacketAdapter createObjectiveAdapter(@NotNull String objectiveName) {
return new ObjectivePacketAdapterImpl(this, packetEvents, objectiveName);
return new ObjectivePacketAdapterImpl(packetSender, packetEvents, objectiveName);
}

@Override
public @NotNull LineRenderingStrategy lineRenderingStrategy(@NotNull Player player) {
ClientVersion ver = packetEvents.getPlayerManager().getClientVersion(player);
return ver.isNewerThanOrEquals(ClientVersion.V_1_13) ? LineRenderingStrategy.MODERN : LineRenderingStrategy.LEGACY;
}

public void sendPacket(@NotNull Player player, @NotNull PacketWrapper<?> packet) {
packetEvents.getPlayerManager().sendPacket(player, packet);
}
ServerVersion serverVer = packetEvents.getServerManager().getVersion();
if (serverVer.isOlderThan(ServerVersion.V_1_13)) {
return LineRenderingStrategy.LEGACY;
}

public void sendPacket(@NotNull Iterable<Player> players, @NotNull PacketWrapper<?> packet) {
for (Player player : players) {
sendPacket(player, packet);
if (Bukkit.getServer().getPluginManager().isPluginEnabled("ProtocolSupport")) {
return LineRenderingStrategy.MODERN;
}

ClientVersion clientVer = packetEvents.getPlayerManager().getClientVersion(player);
return clientVer.isNewerThanOrEquals(ClientVersion.V_1_13) ? LineRenderingStrategy.MODERN : LineRenderingStrategy.LEGACY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.megavex.scoreboardlibrary.implementation.packetAdapter.packetevents;

import com.github.retrooper.packetevents.PacketEventsAPI;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.netty.channel.ChannelHelper;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class PacketEventsSender implements PacketSender<PacketWrapper<?>> {
private final PacketEventsAPI<?> packetEvents;

public PacketEventsSender(@NotNull PacketEventsAPI<?> packetEvents) {
this.packetEvents = packetEvents;
}

@SuppressWarnings("UnstableApiUsage, deprecation")
@Override
public void sendPacket(Player player, PacketWrapper<?> packet) {
ServerVersion serverVer = packetEvents.getServerManager().getVersion();
ClientVersion clientVer = packetEvents.getPlayerManager().getClientVersion(player);
if (serverVer.isOlderThan(ServerVersion.V_1_13) || clientVer.isNewerThanOrEquals(ClientVersion.V_1_13)) {
packetEvents.getPlayerManager().sendPacket(player, packet);
return;
}

// This is a hack to send packets directly skipping plugins such as ViaVersion and ViaRewind
// Based on https://discord.com/channels/721686193061888071/755472595096174873/1193239879560212520
// (PacketEvents Discord)

User user = packetEvents.getPlayerManager().getUser(player);
if (user == null) {
return;
}

Object channel = user.getChannel();
if (!ChannelHelper.isOpen(channel)) {
return;
}

String viaEncoder = "via-encoder";
if (ChannelHelper.getPipelineHandler(channel, viaEncoder) == null) {
packetEvents.getPlayerManager().sendPacket(player, packet);
return;
}

packet.buffer = ChannelHelper.pooledByteBuf(channel);

int id = Objects.requireNonNull(packet.getPacketTypeData().getPacketType()).getId(user.getClientVersion());
ByteBufHelper.writeVarInt(packet.buffer, id);
packet.setServerVersion(clientVer.toServerVersion());
packet.write();
ChannelHelper.writeAndFlushInContext(channel, viaEncoder, packet.buffer);
}
}

0 comments on commit a883942

Please sign in to comment.