Skip to content

Commit

Permalink
Use ProtocolRunnable in PlayerPacketsTickTask
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 29, 2025
1 parent 9740910 commit c6dc075
Showing 1 changed file with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,48 @@
import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.Protocol1_21_2To1_21;
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.PlayerStorage;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.protocol.ProtocolRunnable;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPackets1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.ClientVehicleStorage;
import io.netty.channel.Channel;
import java.util.logging.Level;

public final class PlayerPacketsTickTask implements Runnable {
public final class PlayerPacketsTickTask extends ProtocolRunnable {

public PlayerPacketsTickTask() {
super(Protocol1_21_2To1_21.class);
}

@Override
public void run() {
for (final UserConnection user : Via.getManager().getConnectionManager().getConnections()) {
final ProtocolInfo protocolInfo = user.getProtocolInfo();
if (protocolInfo.getClientState() != State.PLAY || protocolInfo.getServerState() != State.PLAY || !protocolInfo.getPipeline().contains(Protocol1_21_2To1_21.class)) {
continue;
}
public void run(final UserConnection connection) {
final ProtocolInfo protocolInfo = connection.getProtocolInfo();
if (protocolInfo.getClientState() != State.PLAY || protocolInfo.getServerState() != State.PLAY) {
return;
}

final Channel channel = user.getChannel();
channel.eventLoop().submit(() -> {
if (!channel.isActive() || protocolInfo.getClientState() != State.PLAY || protocolInfo.getServerState() != State.PLAY) {
return;
}
try {
if (!user.has(ClientVehicleStorage.class)) {
final PlayerStorage playerStorage = user.get(PlayerStorage.class);
playerStorage.tick(user);
}
} catch (final Throwable t) {
ViaBackwards.getPlatform().getLogger().log(Level.SEVERE, "Error while sending player input packet.", t);
}
try {
final PacketWrapper clientTickEndPacket = PacketWrapper.create(ServerboundPackets1_21_2.CLIENT_TICK_END, user);
clientTickEndPacket.sendToServer(Protocol1_21_2To1_21.class);
} catch (final Throwable t) {
ViaBackwards.getPlatform().getLogger().log(Level.SEVERE, "Error while sending client tick end packet.", t);
final Channel channel = connection.getChannel();
channel.eventLoop().submit(() -> {
if (!channel.isActive() || protocolInfo.getClientState() != State.PLAY || protocolInfo.getServerState() != State.PLAY) {
return;
}
try {
if (!connection.has(ClientVehicleStorage.class)) {
final PlayerStorage playerStorage = connection.get(PlayerStorage.class);
playerStorage.tick(connection);
}
});
}
} catch (final Throwable t) {
ViaBackwards.getPlatform().getLogger().log(Level.SEVERE, "Error while sending player input packet.", t);
}
try {
final PacketWrapper clientTickEndPacket = PacketWrapper.create(ServerboundPackets1_21_2.CLIENT_TICK_END, connection);
clientTickEndPacket.sendToServer(Protocol1_21_2To1_21.class);
} catch (final Throwable t) {
ViaBackwards.getPlatform().getLogger().log(Level.SEVERE, "Error while sending client tick end packet.", t);
}
});
}
}

0 comments on commit c6dc075

Please sign in to comment.