PacketWrapper#sendToServer changes

This commit is contained in:
creeper123123321 2018-08-28 14:02:48 -03:00
parent 84aaec6bff
commit 17115460b7
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
4 changed files with 68 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker;
@ -24,7 +25,7 @@ public class BungeeMovementTransmitter extends MovementTransmitterProvider {
PacketWrapper wrapper = new PacketWrapper(0x03, null, userConnection);
wrapper.write(Type.BOOLEAN, userConnection.get(MovementTracker.class).isGround());
try {
wrapper.sendToServer();
wrapper.sendToServer(Protocol1_9TO1_8.class);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -298,7 +298,7 @@ public class PacketWrapper {
*/
public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline);
ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.OUTGOING);
user().sendRawPacket(output, currentThread);
}
}
@ -311,11 +311,13 @@ public class PacketWrapper {
* @return Packet buffer
* @throws Exception if it fails to write
*/
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception {
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
// Apply current pipeline
List<Protocol> protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes());
// Other way if outgoing
Collections.reverse(protocols);
if (direction == Direction.OUTGOING) {
// Other way if outgoing
Collections.reverse(protocols);
}
int index = 0;
for (int i = 0; i < protocols.size(); i++) {
if (protocols.get(i).getClass().equals(packetProtocol)) {
@ -360,7 +362,7 @@ public class PacketWrapper {
*/
public ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception {
if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, true);
ByteBuf output = constructPacket(packetProtocol, true, Direction.OUTGOING);
return user().sendRawPacketFuture(output);
}
return user().getChannel().newFailedFuture(new Exception("Cancelled packet"));
@ -471,15 +473,32 @@ public class PacketWrapper {
*
* @throws Exception If it failed to write
*/
@Deprecated
public void sendToServer() throws Exception {
if (!isCancelled()) {
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
writeToBuffer(output);
user().getChannel().pipeline().context(Via.getManager().getInjector().getDecoderName()).fireChannelRead(output);
user().sendRawPacketToServer(output, true);
}
}
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING);
user().sendRawPacketToServer(output, currentThread);
}
}
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception {
sendToServer(packetProtocol, skipCurrentPipeline, false);
}
public void sendToServer(Class<? extends Protocol> packetProtocol) throws Exception {
sendToServer(packetProtocol, true);
}
@Override
public String toString() {
return "PacketWrapper{" +

View File

@ -6,9 +6,12 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.socket.SocketChannel;
import lombok.Data;
import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil;
import java.util.Map;
import java.util.UUID;
@ -128,7 +131,7 @@ public class UserConnection {
*/
public boolean incrementReceived() {
// handle stats
Long diff = System.currentTimeMillis() - startTime;
long diff = System.currentTimeMillis() - startTime;
if (diff >= 1000) {
packetsPerSecond = intervalPackets;
startTime = System.currentTimeMillis();
@ -147,7 +150,7 @@ public class UserConnection {
// Max PPS Checker
if (conf.getMaxPPS() > 0) {
if (getPacketsPerSecond() >= conf.getMaxPPS()) {
disconnect(conf.getMaxPPSKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + ""));
disconnect(conf.getMaxPPSKickMessage().replace("%pps", Long.toString(getPacketsPerSecond())));
return true; // don't send current packet
}
}
@ -165,7 +168,7 @@ public class UserConnection {
}
if (getWarnings() >= conf.getMaxWarnings()) {
disconnect(conf.getMaxWarningsKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + ""));
disconnect(conf.getMaxWarningsKickMessage().replace("%pps", Long.toString(getPacketsPerSecond())));
return true; // don't send current packet
}
}
@ -195,4 +198,28 @@ public class UserConnection {
}
}
public void sendRawPacketToServer(final ByteBuf packet, boolean currentThread) {
final ByteBuf buf = packet.alloc().buffer();
try {
Type.VAR_INT.write(buf, PacketWrapper.PASSTHROUGH_ID);
} catch (Exception e) {
// Should not happen
Via.getPlatform().getLogger().warning("Type.VAR_INT.write thrown an exception: " + e);
}
buf.writeBytes(packet);
packet.release();
if (currentThread) {
PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline()).fireChannelRead(buf);
} else {
channel.eventLoop().submit(new Runnable() {
@Override
public void run() {
PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline()).fireChannelRead(buf);
}
});
}
}
public void sendRawPacketToServer(ByteBuf packet) { sendRawPacketToServer(packet, false); }
}

View File

@ -100,4 +100,15 @@ public class PipelineUtil {
}
return null;
}
public static ChannelHandlerContext getPreviousContext(String name, ChannelPipeline pipeline) {
String previous = null;
for (String entry : pipeline.toMap().keySet()) {
if (entry.equals(name)) {
return pipeline.context(previous);
}
previous = entry;
}
return null;
}
}