Add immediate packets w/ use packet type

This commit is contained in:
Sotr
2019-03-18 21:35:23 +08:00
parent 3b7adb3d4f
commit c554afae69
9 changed files with 163 additions and 12 deletions

View File

@@ -1,5 +1,12 @@
package io.akarin.server.core;
public enum PacketType { // unused yet
STATUS_OUT_SERVER_INFO,
STATUS_OUT_PONG,
PLAY_OUT_MAP_CHUNK,
PLAY_OUT_SPAWN_POSITION,
PLAY_OUT_CHAT,
UNKNOWN;
}

View File

@@ -3,6 +3,7 @@ package net.minecraft.server;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.akarin.server.core.PacketType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
@@ -221,7 +222,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
}
private final void dispatchOrQueuePacketUnsafe(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
boolean dispatch = packet instanceof PacketStatusOutPong || packet instanceof PacketStatusOutServerInfo || (packet instanceof PacketPlayOutMapChunk && ((PacketPlayOutMapChunk) packet).isReady());
boolean dispatch = packet instanceof PacketStatusOutPong || packet instanceof PacketStatusOutServerInfo || (packet.getType() == PacketType.PLAY_OUT_MAP_CHUNK && ((PacketPlayOutMapChunk) packet).isReady());
if (dispatch) {
this.dispatchPacket(packet, genericfuturelistener);
} else {
@@ -232,9 +233,10 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
private final void sendPacketQueueUnsafe() {
while (!this.packetQueue.isEmpty()) {
QueuedPacket queuedPacket = this.packetQueue.poll(packet ->
packet != null &&
(!(packet.getPacket() instanceof PacketPlayOutMapChunk) ||
(((PacketPlayOutMapChunk) packet.getPacket()).isReady())), null);
packet != null && (
!(packet.getPacket().getType() == PacketType.PLAY_OUT_MAP_CHUNK) ||
(((PacketPlayOutMapChunk) packet.getPacket()).isReady())
), null);
if (queuedPacket != null)
this.dispatchPacket(queuedPacket.getPacket(), queuedPacket.getGenericFutureListener());
@@ -333,7 +335,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
NetworkManager.QueuedPacket networkmanager_queuedpacket = (NetworkManager.QueuedPacket) this.getPacketQueue().peek(); // poll -> peek
if (networkmanager_queuedpacket != null) { // Fix NPE (Spigot bug caused by handleDisconnection())
if (networkmanager_queuedpacket.getPacket() instanceof PacketPlayOutMapChunk && !((PacketPlayOutMapChunk) networkmanager_queuedpacket.getPacket()).isReady()) { // Check if the peeked packet is a chunk packet which is not ready
if (networkmanager_queuedpacket.getPacket().getType() == PacketType.PLAY_OUT_MAP_CHUNK && !((PacketPlayOutMapChunk) networkmanager_queuedpacket.getPacket()).isReady()) { // Check if the peeked packet is a chunk packet which is not ready
return false; // Return false if the peeked packet is a chunk packet which is not ready
} else {
this.getPacketQueue().poll(); // poll here

View File

@@ -25,5 +25,9 @@ public interface Packet<T extends PacketListener> {
public default io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.UNKNOWN;
}
public default boolean canDispatchImmediately() {
return false;
}
// Akarin end
}

View File

@@ -57,4 +57,15 @@ public class PacketPlayOutChat implements Packet<PacketListenerPlayOut> {
public boolean a() {
return true;
}
// Akarin start
@Override
public io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.PLAY_OUT_CHAT;
}
@Override
public boolean canDispatchImmediately() {
return true;
}
// Akarin end
}

View File

@@ -204,4 +204,15 @@ public class PacketPlayOutMapChunk implements Packet<PacketListenerPlayOut> {
public boolean f() {
return this.f;
}
// Akarin start
@Override
public io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.PLAY_OUT_MAP_CHUNK;
}
@Override
public boolean canDispatchImmediately() {
return this.ready;
}
// Akarin end
}

View File

@@ -0,0 +1,37 @@
package net.minecraft.server;
import java.io.IOException;
public class PacketPlayOutSpawnPosition implements Packet<PacketListenerPlayOut> {
public BlockPosition position;
public PacketPlayOutSpawnPosition() {}
public PacketPlayOutSpawnPosition(BlockPosition blockposition) {
this.position = blockposition;
}
public void a(PacketDataSerializer packetdataserializer) throws IOException {
this.position = packetdataserializer.e();
}
public void b(PacketDataSerializer packetdataserializer) throws IOException {
packetdataserializer.a(this.position);
}
public void a(PacketListenerPlayOut packetlistenerplayout) {
packetlistenerplayout.a(this);
}
// Akarin start
@Override
public io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.PLAY_OUT_SPAWN_POSITION;
}
@Override
public boolean canDispatchImmediately() {
return true;
}
// Akarin end
}

View File

@@ -0,0 +1,37 @@
package net.minecraft.server;
import java.io.IOException;
public class PacketStatusOutPong implements Packet<PacketStatusOutListener> {
private long a;
public PacketStatusOutPong() {}
public PacketStatusOutPong(long i) {
this.a = i;
}
public void a(PacketDataSerializer packetdataserializer) throws IOException {
this.a = packetdataserializer.readLong();
}
public void b(PacketDataSerializer packetdataserializer) throws IOException {
packetdataserializer.writeLong(this.a);
}
public void a(PacketStatusOutListener packetstatusoutlistener) {
packetstatusoutlistener.a(this);
}
// Akarin start
@Override
public io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.STATUS_OUT_PONG;
}
@Override
public boolean canDispatchImmediately() {
return true;
}
// Akarin end
}

View File

@@ -0,0 +1,40 @@
package net.minecraft.server;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
public class PacketStatusOutServerInfo implements Packet<PacketStatusOutListener> {
private static final Gson a = (new GsonBuilder()).registerTypeAdapter(ServerPing.ServerData.class, new ServerPing.ServerData.Serializer()).registerTypeAdapter(ServerPing.ServerPingPlayerSample.class, new ServerPing.ServerPingPlayerSample.Serializer()).registerTypeAdapter(ServerPing.class, new ServerPing.Serializer()).registerTypeHierarchyAdapter(IChatBaseComponent.class, new IChatBaseComponent.ChatSerializer()).registerTypeHierarchyAdapter(ChatModifier.class, new ChatModifier.ChatModifierSerializer()).registerTypeAdapterFactory(new ChatTypeAdapterFactory()).create();
private ServerPing b;
public PacketStatusOutServerInfo() {}
public PacketStatusOutServerInfo(ServerPing serverping) {
this.b = serverping;
}
public void a(PacketDataSerializer packetdataserializer) throws IOException {
this.b = (ServerPing) ChatDeserializer.a(PacketStatusOutServerInfo.a, packetdataserializer.e(32767), ServerPing.class);
}
public void b(PacketDataSerializer packetdataserializer) throws IOException {
packetdataserializer.a(PacketStatusOutServerInfo.a.toJson(this.b));
}
public void a(PacketStatusOutListener packetstatusoutlistener) {
packetstatusoutlistener.a(this);
}
// Akarin start
@Override
public io.akarin.server.core.PacketType getType() {
return io.akarin.server.core.PacketType.STATUS_OUT_SERVER_INFO;
}
@Override
public boolean canDispatchImmediately() {
return true;
}
// Akarin end
}

View File

@@ -7,6 +7,8 @@ import com.google.common.util.concurrent.Futures;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.suggestion.Suggestions;
import io.akarin.server.core.PacketType;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
@@ -1501,10 +1503,10 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
if (this.processedDisconnect)
return;
if (packet0 instanceof PacketPlayOutSpawnPosition) {
if (packet0.getType() == PacketType.PLAY_OUT_SPAWN_POSITION) {
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet0;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
} else if (packet1 instanceof PacketPlayOutSpawnPosition) {
} else if (packet1.getType() == PacketType.PLAY_OUT_SPAWN_POSITION) {
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet1;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
}
@@ -1526,13 +1528,13 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
if (this.processedDisconnect)
return;
if (packet0 instanceof PacketPlayOutSpawnPosition) {
if (packet0 .getType() == PacketType.PLAY_OUT_SPAWN_POSITION) {
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet0;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
} else if (packet1 instanceof PacketPlayOutSpawnPosition) {
} else if (packet1.getType() == PacketType.PLAY_OUT_SPAWN_POSITION) {
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet1;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
} else if (packet2 instanceof PacketPlayOutSpawnPosition) {
} else if (packet2.getType() == PacketType.PLAY_OUT_SPAWN_POSITION) {
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet2;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
}
@@ -1569,7 +1571,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
// Akarin end
public final void a(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) { // Akarin - add final
if (packet instanceof PacketPlayOutChat) {
if (packet.getType() == PacketType.PLAY_OUT_CHAT) { // Akarin
PacketPlayOutChat packetplayoutchat = (PacketPlayOutChat) packet;
EntityHuman.EnumChatVisibility entityhuman_enumchatvisibility = this.player.getChatFlags();
@@ -1585,7 +1587,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
// CraftBukkit start
if (/*packet == null ||*/ this.processedDisconnect) { // Spigot // Akarin
return;
} else if (packet instanceof PacketPlayOutSpawnPosition) {
} else if (packet.getType() == PacketType.PLAY_OUT_SPAWN_POSITION) { // Akarin
PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet;
this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
}