3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

PacketWrapper allow returning of ChannelFuture with send, and fix blocked protocols disconnect.

Also remove a TODO.
Dieser Commit ist enthalten in:
Myles 2016-07-02 22:19:43 +01:00
Ursprung 84d006c71d
Commit b50d0075ca
4 geänderte Dateien mit 45 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -3,6 +3,7 @@ package us.myles.ViaVersion.api;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
@ -281,7 +282,7 @@ public class PacketWrapper {
* @param skipCurrentPipeline - Skip the current pipeline * @param skipCurrentPipeline - Skip the current pipeline
* @throws Exception if it fails to write * @throws Exception if it fails to write
*/ */
public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception { public ChannelFuture send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception {
if (!isCancelled()) { if (!isCancelled()) {
// Apply current pipeline // Apply current pipeline
List<Protocol> protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes()); List<Protocol> protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes());
@ -303,8 +304,9 @@ public class PacketWrapper {
// Send // Send
ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer();
writeToBuffer(output); writeToBuffer(output);
user().sendRawPacket(output); return user().sendRawPacketFuture(output);
} }
return user().getChannel().newFailedFuture(new Exception("Cancelled packet"));
} }
/** /**
@ -319,6 +321,20 @@ public class PacketWrapper {
send(packetProtocol, true); send(packetProtocol, true);
} }
/**
* Send this packet to the associated user.
* Be careful not to send packets twice.
* (Sends it after current)
* Also returns the packets ChannelFuture
*
* @param packetProtocol - The protocol version of the packet.
* @return The packets ChannelFuture
* @throws Exception if it fails to write
*/
public ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception {
return send(packetProtocol, true);
}
/** /**
* Send this packet to the associated user. * Send this packet to the associated user.
* Be careful not to send packets twice. * Be careful not to send packets twice.

Datei anzeigen

@ -1,6 +1,7 @@
package us.myles.ViaVersion.api.data; package us.myles.ViaVersion.api.data;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import lombok.Data; import lombok.Data;
@ -85,6 +86,17 @@ public class UserConnection {
} }
} }
/**
* Send a raw packet to the player with returning the future
*
* @param packet The raw packet to send
* @return ChannelFuture of the packet being sent
*/
public ChannelFuture sendRawPacketFuture(final ByteBuf packet) {
final ChannelHandler handler = channel.pipeline().get("encoder");
return channel.pipeline().context(handler).writeAndFlush(packet);
}
/** /**
* Send a raw packet to the player (netty thread) * Send a raw packet to the player (netty thread)
* *

Datei anzeigen

@ -1,5 +1,8 @@
package us.myles.ViaVersion.protocols.base; package us.myles.ViaVersion.protocols.base;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -166,18 +169,24 @@ public class BaseProtocol extends Protocol {
public void registerMap() { public void registerMap() {
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(final PacketWrapper wrapper) throws Exception {
int protocol = wrapper.user().get(ProtocolInfo.class).getProtocolVersion(); int protocol = wrapper.user().get(ProtocolInfo.class).getProtocolVersion();
if (ViaVersion.getConfig().getBlockedProtocols().contains(protocol)) { if (ViaVersion.getConfig().getBlockedProtocols().contains(protocol)) {
if (!wrapper.user().getChannel().isOpen()) return; if (!wrapper.user().getChannel().isOpen()) return;
PacketWrapper disconnectPacket = new PacketWrapper(0x00, null, wrapper.user()); // Disconnect Packet PacketWrapper disconnectPacket = new PacketWrapper(0x00, null, wrapper.user()); // Disconnect Packet
Protocol1_9TO1_8.FIX_JSON.write(disconnectPacket, ChatColor.translateAlternateColorCodes('&', ViaVersion.getConfig().getBlockedDisconnectMsg())); Protocol1_9TO1_8.FIX_JSON.write(disconnectPacket, ChatColor.translateAlternateColorCodes('&', ViaVersion.getConfig().getBlockedDisconnectMsg()));
disconnectPacket.send(BaseProtocol.class); wrapper.cancel(); // cancel current
wrapper.cancel(); // Send and close
wrapper.user().getChannel().closeFuture(); ChannelFuture future = disconnectPacket.sendFuture(BaseProtocol.class);
} future.addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
wrapper.user().getChannel().close();
}
});
}
} }
}); });
} }

Datei anzeigen

@ -38,7 +38,7 @@ public class FakeTileEntity {
register(Arrays.asList(Material.REDSTONE_COMPARATOR, Material.REDSTONE_COMPARATOR_OFF, Material.REDSTONE_COMPARATOR_ON), "Comparator"); register(Arrays.asList(Material.REDSTONE_COMPARATOR, Material.REDSTONE_COMPARATOR_OFF, Material.REDSTONE_COMPARATOR_ON), "Comparator");
register(Material.FLOWER_POT, "FlowerPot"); register(Material.FLOWER_POT, "FlowerPot");
register(Arrays.asList(Material.STANDING_BANNER, Material.WALL_BANNER, Material.BANNER), "Banner"); register(Arrays.asList(Material.STANDING_BANNER, Material.WALL_BANNER, Material.BANNER), "Banner");
register(209, "EndGateway"); // todo test register(209, "EndGateway");
register(Material.COMMAND.getId(), "Control"); register(Material.COMMAND.getId(), "Control");
} }