From f4a0b53a6d5ef8d6e624f668c67fc0cadc80f31e Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Wed, 22 Aug 2018 16:25:55 -0300 Subject: [PATCH] Better ViaApi implementation --- .../viarift/handler/VRInHandler.java | 10 +++++- .../viarift/platform/VRUserConnection.java | 6 ++-- .../viarift/platform/VRViaAPI.java | 33 ++++++++++++++----- .../provider/VRMovementTransmitter.java | 3 -- .../viarift/util/PipelineUtil.java | 2 +- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/github/creeper123123321/viarift/handler/VRInHandler.java b/src/main/java/com/github/creeper123123321/viarift/handler/VRInHandler.java index 81878a7..e6e6cb6 100644 --- a/src/main/java/com/github/creeper123123321/viarift/handler/VRInHandler.java +++ b/src/main/java/com/github/creeper123123321/viarift/handler/VRInHandler.java @@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.CancelException; @@ -66,10 +67,17 @@ public class VRInHandler extends ByteToMessageDecoder { buf.release(); } - @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (PipelineUtil.containsCause(cause, CancelException.class)) return; super.exceptionCaught(ctx, cause); } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + ProtocolInfo info = user.get(ProtocolInfo.class); + if (info.getUuid() != null) { + Via.getManager().removePortedClient(info.getUuid()); + } + } } diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRUserConnection.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRUserConnection.java index e93692a..5f90867 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRUserConnection.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRUserConnection.java @@ -29,14 +29,14 @@ public class VRUserConnection extends UserConnection { final Channel channel = this.getChannel(); if (currentThread) { try { - PipelineUtil.getContextBefore("decoder", channel.pipeline()).fireChannelRead(copy); + PipelineUtil.getPreviousContext("decoder", channel.pipeline()).fireChannelRead(copy); } catch (Exception e) { e.printStackTrace(); } } else { channel.eventLoop().submit(() -> { try { - PipelineUtil.getContextBefore("decoder", channel.pipeline()).fireChannelRead(copy); + PipelineUtil.getPreviousContext("decoder", channel.pipeline()).fireChannelRead(copy); } catch (Exception e) { e.printStackTrace(); } @@ -56,7 +56,7 @@ public class VRUserConnection extends UserConnection { packet.release(); final Channel channel = this.getChannel(); try { - PipelineUtil.getContextBefore("decoder", channel.pipeline()).fireChannelRead(copy); + PipelineUtil.getPreviousContext("decoder", channel.pipeline()).fireChannelRead(copy); return channel.newSucceededFuture(); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java index 6553d86..02a74dd 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java @@ -6,24 +6,35 @@ import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.boss.BossBar; import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.SortedSet; +import java.util.TreeSet; import java.util.UUID; -public class VRViaAPI implements ViaAPI { +public class VRViaAPI implements ViaAPI { @Override - public int getPlayerVersion(Object o) { - throw new UnsupportedOperationException(); + public int getPlayerVersion(Void o) { + throw new UnsupportedOperationException("WHAT??? A INSTANCE OF VOID???"); } @Override public int getPlayerVersion(UUID uuid) { - throw new UnsupportedOperationException(); + if (!isPorted(uuid)) { + try { + return Via.getManager().getInjector().getServerProtocolVersion(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return Via.getManager().getPortedPlayers().get(uuid).get(ProtocolInfo.class).getProtocolVersion(); } @Override public boolean isPorted(UUID uuid) { - throw new UnsupportedOperationException(); + return Via.getManager().getPortedPlayers().containsKey(uuid); } @Override @@ -32,13 +43,14 @@ public class VRViaAPI implements ViaAPI { } @Override - public void sendRawPacket(Object o, ByteBuf byteBuf) throws IllegalArgumentException { - throw new UnsupportedOperationException(); + public void sendRawPacket(Void o, ByteBuf byteBuf) throws IllegalArgumentException { + throw new UnsupportedOperationException("WHAT??? A INSTANCE OF VOID???"); } @Override public void sendRawPacket(UUID uuid, ByteBuf byteBuf) throws IllegalArgumentException { - throw new UnsupportedOperationException(); + UserConnection ci = Via.getManager().getPortedPlayers().get(uuid); + ci.sendRawPacket(byteBuf); } @Override @@ -53,6 +65,9 @@ public class VRViaAPI implements ViaAPI { @Override public SortedSet getSupportedVersions() { - throw new UnsupportedOperationException(); + SortedSet outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions()); + outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols()); + + return outputSet; } } diff --git a/src/main/java/com/github/creeper123123321/viarift/provider/VRMovementTransmitter.java b/src/main/java/com/github/creeper123123321/viarift/provider/VRMovementTransmitter.java index 26e9512..aa4115a 100644 --- a/src/main/java/com/github/creeper123123321/viarift/provider/VRMovementTransmitter.java +++ b/src/main/java/com/github/creeper123123321/viarift/provider/VRMovementTransmitter.java @@ -31,11 +31,8 @@ public class VRMovementTransmitter extends MovementTransmitterProvider { ByteBuf buf = userConnection.getChannel().alloc().buffer(); try { - //Type.VAR_INT.write(buf, PacketWrapper.PASSTHROUGH_ID); packet.writeToBuffer(buf); - //PipelineUtil.getContextAfter("encoder", userConnection.getChannel().pipeline()).writeAndFlush(buf); userConnection.getChannel().pipeline().context("encoder").writeAndFlush(buf); - //PipelineUtil.getContextBefore("encoder", userConnection.getChannel().pipeline()).writeAndFlush(buf); userConnection.get(MovementTracker.class).incrementIdlePacket(); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/com/github/creeper123123321/viarift/util/PipelineUtil.java b/src/main/java/com/github/creeper123123321/viarift/util/PipelineUtil.java index 2dca112..f8e3bbb 100644 --- a/src/main/java/com/github/creeper123123321/viarift/util/PipelineUtil.java +++ b/src/main/java/com/github/creeper123123321/viarift/util/PipelineUtil.java @@ -4,7 +4,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; public class PipelineUtil { - public static ChannelHandlerContext getContextBefore(String name, ChannelPipeline pipe) { + public static ChannelHandlerContext getPreviousContext(String name, ChannelPipeline pipe) { String previous = null; for (String current : pipe.names()) { if (name.equals(current))