Various changes

This commit is contained in:
Camotoy 2023-03-12 23:51:36 -04:00
parent 8252330c7d
commit 60547d54fd
10 changed files with 107 additions and 37 deletions

View file

@ -86,7 +86,7 @@ public class ClientListener extends SessionAdapter {
} else if (packet instanceof ClientboundGameProfilePacket) { } else if (packet instanceof ClientboundGameProfilePacket) {
protocol.setState(ProtocolState.GAME); protocol.setState(ProtocolState.GAME);
} else if (packet instanceof ClientboundLoginDisconnectPacket) { } else if (packet instanceof ClientboundLoginDisconnectPacket) {
session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason().toString()); session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason());
} else if (packet instanceof ClientboundLoginCompressionPacket) { } else if (packet instanceof ClientboundLoginCompressionPacket) {
session.setCompressionThreshold(((ClientboundLoginCompressionPacket) packet).getThreshold(), false); session.setCompressionThreshold(((ClientboundLoginCompressionPacket) packet).getThreshold(), false);
} }
@ -112,7 +112,7 @@ public class ClientListener extends SessionAdapter {
if (packet instanceof ClientboundKeepAlivePacket && session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) { if (packet instanceof ClientboundKeepAlivePacket && session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
session.send(new ServerboundKeepAlivePacket(((ClientboundKeepAlivePacket) packet).getPingId())); session.send(new ServerboundKeepAlivePacket(((ClientboundKeepAlivePacket) packet).getPingId()));
} else if (packet instanceof ClientboundDisconnectPacket) { } else if (packet instanceof ClientboundDisconnectPacket) {
session.disconnect(((ClientboundDisconnectPacket) packet).getReason().toString()); session.disconnect(((ClientboundDisconnectPacket) packet).getReason());
} }
} }
} }

View file

@ -0,0 +1,20 @@
package com.github.steveice10.mc.protocol.packet.ingame.clientbound;
import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper;
import com.github.steveice10.mc.protocol.codec.MinecraftPacket;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.IOException;
import java.util.List;
@Data
@AllArgsConstructor
public class ClientboundBundlePacket implements MinecraftPacket {
private final List<MinecraftPacket> packets;
@Override
public void serialize(ByteBuf buf, MinecraftCodecHelper helper) throws IOException {
}
}

View file

@ -4,8 +4,12 @@ import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper;
import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.codec.MinecraftPacket;
import com.nukkitx.math.vector.Vector3d; import com.nukkitx.math.vector.Vector3d;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@Data
@AllArgsConstructor
public class ClientboundDamageEventPacket implements MinecraftPacket { public class ClientboundDamageEventPacket implements MinecraftPacket {
private final int entityId; private final int entityId;
private final int sourceTypeId; private final int sourceTypeId;

View file

@ -4,10 +4,14 @@ import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper;
import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.codec.MinecraftPacket;
import com.github.steveice10.mc.protocol.data.game.chunk.ChunkBiomeData; import com.github.steveice10.mc.protocol.data.game.chunk.ChunkBiomeData;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data
@AllArgsConstructor
public class ClientboundChunksBiomesPacket implements MinecraftPacket { public class ClientboundChunksBiomesPacket implements MinecraftPacket {
private final List<ChunkBiomeData> chunkBiomeData; private final List<ChunkBiomeData> chunkBiomeData;

View file

@ -6,6 +6,8 @@ import com.github.steveice10.packetlib.event.session.SessionEvent;
import com.github.steveice10.packetlib.event.session.SessionListener; import com.github.steveice10.packetlib.event.session.SessionListener;
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.packet.PacketProtocol; import com.github.steveice10.packetlib.packet.PacketProtocol;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.List; import java.util.List;
@ -246,7 +248,7 @@ public interface Session {
* *
* @param reason Reason for disconnecting. * @param reason Reason for disconnecting.
*/ */
public void disconnect(String reason); void disconnect(@Nullable String reason);
/** /**
* Disconnects the session. * Disconnects the session.
@ -254,5 +256,20 @@ public interface Session {
* @param reason Reason for disconnecting. * @param reason Reason for disconnecting.
* @param cause Throwable responsible for disconnecting. * @param cause Throwable responsible for disconnecting.
*/ */
public void disconnect(String reason, Throwable cause); void disconnect(@Nullable String reason, Throwable cause);
/**
* Disconnects the session.
*
* @param reason Reason for disconnecting.
*/
void disconnect(@Nullable Component reason);
/**
* Disconnects the session.
*
* @param reason Reason for disconnecting.
* @param cause Throwable responsible for disconnecting.
*/
void disconnect(@Nullable Component reason, Throwable cause);
} }

View file

@ -1,24 +1,15 @@
package com.github.steveice10.packetlib.event.session; package com.github.steveice10.packetlib.event.session;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import net.kyori.adventure.text.Component;
/** /**
* Called when the session is disconnected. * Called when the session is disconnected.
*/ */
public class DisconnectedEvent implements SessionEvent { public class DisconnectedEvent implements SessionEvent {
private Session session; private final Session session;
private String reason; private final Component reason;
private Throwable cause; private final Throwable cause;
/**
* Creates a new DisconnectedEvent instance.
*
* @param session Session being disconnected.
* @param reason Reason for the session to disconnect.
*/
public DisconnectedEvent(Session session, String reason) {
this(session, reason, null);
}
/** /**
* Creates a new DisconnectedEvent instance. * Creates a new DisconnectedEvent instance.
@ -27,7 +18,7 @@ public class DisconnectedEvent implements SessionEvent {
* @param reason Reason for the session to disconnect. * @param reason Reason for the session to disconnect.
* @param cause Throwable that caused the disconnect. * @param cause Throwable that caused the disconnect.
*/ */
public DisconnectedEvent(Session session, String reason, Throwable cause) { public DisconnectedEvent(Session session, Component reason, Throwable cause) {
this.session = session; this.session = session;
this.reason = reason; this.reason = reason;
this.cause = cause; this.cause = cause;
@ -47,7 +38,7 @@ public class DisconnectedEvent implements SessionEvent {
* *
* @return The event's reason. * @return The event's reason.
*/ */
public String getReason() { public Component getReason() {
return this.reason; return this.reason;
} }

View file

@ -1,24 +1,15 @@
package com.github.steveice10.packetlib.event.session; package com.github.steveice10.packetlib.event.session;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import net.kyori.adventure.text.Component;
/** /**
* Called when the session is about to disconnect. * Called when the session is about to disconnect.
*/ */
public class DisconnectingEvent implements SessionEvent { public class DisconnectingEvent implements SessionEvent {
private Session session; private final Session session;
private String reason; private final Component reason;
private Throwable cause; private final Throwable cause;
/**
* Creates a new DisconnectingEvent instance.
*
* @param session Session being disconnected.
* @param reason Reason for the session to disconnect.
*/
public DisconnectingEvent(Session session, String reason) {
this(session, reason, null);
}
/** /**
* Creates a new DisconnectingEvent instance. * Creates a new DisconnectingEvent instance.
@ -27,7 +18,7 @@ public class DisconnectingEvent implements SessionEvent {
* @param reason Reason for the session to disconnect. * @param reason Reason for the session to disconnect.
* @param cause Throwable that caused the disconnect. * @param cause Throwable that caused the disconnect.
*/ */
public DisconnectingEvent(Session session, String reason, Throwable cause) { public DisconnectingEvent(Session session, Component reason, Throwable cause) {
this.session = session; this.session = session;
this.reason = reason; this.reason = reason;
this.cause = cause; this.cause = cause;
@ -47,7 +38,7 @@ public class DisconnectingEvent implements SessionEvent {
* *
* @return The event's reason. * @return The event's reason.
*/ */
public String getReason() { public Component getReason() {
return this.reason; return this.reason;
} }

View file

@ -0,0 +1,30 @@
package com.github.steveice10.packetlib.tcp;
import com.github.steveice10.mc.protocol.codec.MinecraftPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundBundlePacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundDelimiterPacket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import java.util.ArrayList;
import java.util.List;
public class TcpBundlerUnpacker extends MessageToMessageDecoder<MinecraftPacket> {
private List<MinecraftPacket> currentPackets;
@Override
protected void decode(ChannelHandlerContext ctx, MinecraftPacket packet, List<Object> out) throws Exception {
if (currentPackets != null) {
if (packet.getClass() == ClientboundDelimiterPacket.class) {
out.add(new ClientboundBundlePacket(currentPackets));
currentPackets = null;
} else {
currentPackets.add(packet);
}
} else {
if (packet.getClass() == ClientboundDelimiterPacket.class) {
currentPackets = new ArrayList<>(2);
}
}
}
}

View file

@ -315,5 +315,7 @@ public class TcpClientSession extends TcpSession {
DATAGRAM_CHANNEL_CLASS = NioDatagramChannel.class; DATAGRAM_CHANNEL_CLASS = NioDatagramChannel.class;
break; break;
} }
Runtime.getRuntime().addShutdownHook(new Thread(() -> EVENT_LOOP_GROUP.shutdownGracefully()));
} }
} }

View file

@ -15,6 +15,7 @@ import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutException; import io.netty.handler.timeout.WriteTimeoutException;
import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler;
import net.kyori.adventure.text.Component;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.net.ConnectException; import java.net.ConnectException;
@ -258,11 +259,21 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
@Override @Override
public void disconnect(String reason) { public void disconnect(String reason) {
this.disconnect(Component.text(reason));
}
@Override
public void disconnect(String reason, Throwable cause) {
this.disconnect(Component.text(reason), cause);
}
@Override
public void disconnect(Component reason) {
this.disconnect(reason, null); this.disconnect(reason, null);
} }
@Override @Override
public void disconnect(final String reason, final Throwable cause) { public void disconnect(final Component reason, final Throwable cause) {
if (this.disconnected) { if (this.disconnected) {
return; return;
} }
@ -273,9 +284,9 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
this.callEvent(new DisconnectingEvent(this, reason, cause)); this.callEvent(new DisconnectingEvent(this, reason, cause));
this.channel.flush().close().addListener((ChannelFutureListener) future -> this.channel.flush().close().addListener((ChannelFutureListener) future ->
callEvent(new DisconnectedEvent(TcpSession.this, callEvent(new DisconnectedEvent(TcpSession.this,
reason != null ? reason : "Connection closed.", cause))); reason != null ? reason : Component.text("Connection closed."), cause)));
} else { } else {
this.callEvent(new DisconnectedEvent(this, reason != null ? reason : "Connection closed.", cause)); this.callEvent(new DisconnectedEvent(this, reason != null ? reason : Component.text("Connection closed."), cause));
} }
} }