diff --git a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index 030f59098..fb98b284b 100644 --- a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -3,18 +3,24 @@ package us.myles.ViaVersion.api; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import lombok.Data; import lombok.Getter; import lombok.Setter; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.TypeConverter; import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.handlers.ViaDecodeHandler; +import us.myles.ViaVersion.packets.Direction; +import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.util.PipelineUtil; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -250,10 +256,44 @@ public class PacketWrapper { * Be careful not to send packets twice. * (Sends it after current) * + * @param packetProtocol - The protocol version of the packet. * @throws Exception if it fails to write */ + public void send(Class packetProtocol) throws Exception { + if (!isCancelled()) { + // Apply current pipeline + List protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes()); + // 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)) { + index = i + 1; + break; + } + } + // Apply other protocols + apply(Direction.OUTGOING, user().get(ProtocolInfo.class).getState(), index, protocols); + // Send + ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); + writeToBuffer(output); + user().sendRawPacket(output); + } + } + + /** + * Send this packet to the associated user. + * Be careful not to send packets twice. + * (Sends it after current) + *
+ * This method is no longer used, it's favoured to use send(Protocol) as it will handle the pipeline properly. + * + * @throws Exception if it fails to write + */ + @Deprecated public void send() throws Exception { if (!isCancelled()) { + // Send ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); user().sendRawPacket(output); @@ -284,6 +324,26 @@ public class PacketWrapper { return wrapper; } + /** + * Applies a pipeline from an index to the wrapper + * + * @param direction The direction + * @param state The state + * @param index The index to start from + * @param pipeline The pipeline + * @return The current packetwrapper + * @throws Exception + */ + public PacketWrapper apply(Direction direction, State state, int index, List pipeline) throws Exception { + for (int i = index; i < pipeline.size(); i++) { // Copy to prevent from removal. + pipeline.get(i).transform(direction, state, this); + // Reset the reader for the packetWrapper (So it can be recycled across packets) + resetReader(); + } + + return this; + } + /** * Cancel this packet from sending */ diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index 04ab4b306..267e0f7e3 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -69,11 +69,9 @@ public class ProtocolPipeline extends Protocol { if (direction == Direction.OUTGOING) Collections.reverse(protocols); - for (Protocol protocol : protocols) { // Copy to prevent from removal. - protocol.transform(direction, state, packetWrapper); - // Reset the reader for the packetWrapper (So it can be recycled across packets) - packetWrapper.resetReader(); - } + // Apply protocols + packetWrapper.apply(direction, state, 0, protocols); + super.transform(direction, state, packetWrapper); if (ViaVersion.getInstance().isDebug()) { diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/BlockEntity.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/BlockEntity.java index 916d7f0dc..988364720 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/BlockEntity.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/BlockEntity.java @@ -75,7 +75,7 @@ public class BlockEntity { wrapper.write(Type.POSITION, pos); wrapper.write(Type.UNSIGNED_BYTE, id); wrapper.write(Type.NBT, tag); - wrapper.send(); + wrapper.send(Protocol1_9_1_2TO1_9_3_4.class); } private static void updateSign(Position pos, String[] lines, UserConnection connection) throws Exception { @@ -83,6 +83,6 @@ public class BlockEntity { wrapper.write(Type.POSITION, pos); for (String s : lines) wrapper.write(Type.STRING, s); - wrapper.send(); + wrapper.send(Protocol1_9_1_2TO1_9_3_4.class); } } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java index cccabf7fb..4f9496d34 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/DeathListener.java @@ -55,7 +55,7 @@ public class DeathListener implements Listener { wrapper.write(Type.VAR_INT, p.getEntityId()); wrapper.write(Type.INT, p.getEntityId()); Protocol1_9TO1_8.FIX_JSON.write(wrapper, msg); - wrapper.send(); + wrapper.send(Protocol1_9TO1_8.class); } catch (Exception e) { e.printStackTrace(); wrapper.clearInputBuffer(); diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 0fd0b03eb..f9e417810 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -57,7 +57,7 @@ public class EntityPackets { passengerPacket.write(Type.VAR_INT_ARRAY, new Integer[]{passenger}); tracker.getVehicleMap().put(passenger, vehicle); } - passengerPacket.send(); // Send the packet + passengerPacket.send(Protocol1_9TO1_8.class); // Send the packet } return null; } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java index 21b064c97..cc52e186a 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java @@ -44,7 +44,7 @@ public class InventoryPackets { wrapper.write(Type.SHORT, property); wrapper.write(Type.SHORT, enchantID); } - }).send(); + }).send(Protocol1_9TO1_8.class); wrapper.set(Type.SHORT, 0, (short) (property + 3)); wrapper.set(Type.SHORT, 1, level); @@ -230,7 +230,7 @@ public class InventoryPackets { wrapper.write(Type.SHORT, slot); wrapper.write(Type.SHORT, (short) -1); } - }).send(); + }).send(Protocol1_9TO1_8.class); // Finally reset to simulate throwing item wrapper.set(Type.SHORT, 0, (short) -999); // Set slot to -999 } @@ -285,7 +285,7 @@ public class InventoryPackets { wrapper.write(Type.SHORT, slot); wrapper.write(Type.SHORT, (short) -1); } - }).send(); + }).send(Protocol1_9TO1_8.class); // Finally reset to simulate throwing item wrapper.set(Type.BYTE, 0, (byte) 0); // Set button to 0 wrapper.set(Type.BYTE, 1, (byte) 0); // Set mode to 0 diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index c9bb171a0..c3f6eb3eb 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -107,12 +107,12 @@ public class SpawnPackets { Item item = new Item((short) Material.POTION.getId(), (byte) 1, (short) data, null); ItemRewriter.toClient(item); // Rewrite so that it gets the right nbt // TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS - Metadata potion = new Metadata(wrapper.user().get(ProtocolInfo.class).getPipeline().contains(Protocol1_10To1_9_3_4.class) ? 6 : 5, NewType.Slot.getTypeID(), Type.ITEM, item); + Metadata potion = new Metadata(5, NewType.Slot.getTypeID(), Type.ITEM, item); meta.add(potion); wrapper.write(Protocol1_9TO1_8.METADATA_LIST, meta); } }); - metaPacket.send(); + metaPacket.send(Protocol1_9TO1_8.class); } } }); @@ -305,7 +305,7 @@ public class SpawnPackets { packet.write(Type.VAR_INT, 0); packet.write(Type.ITEM, new Item(item, (byte) 1, (short) 0, null)); try { - packet.send(); + packet.send(Protocol1_9TO1_8.class); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java index 564daa11a..63d4efc27 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java @@ -77,7 +77,7 @@ public class EntityTracker extends StoredObject { wrapper.write(Type.VAR_INT, 1); // slot wrapper.write(Type.ITEM, item); try { - wrapper.send(); + wrapper.send(Protocol1_9TO1_8.class); } catch (Exception e) { e.printStackTrace(); } @@ -258,7 +258,7 @@ public class EntityTracker extends StoredObject { } teamExists = b; try { - wrapper.send(); + wrapper.send(Protocol1_9TO1_8.class); } catch (Exception e) { e.printStackTrace(); } @@ -281,7 +281,7 @@ public class EntityTracker extends StoredObject { handleMetadata(entityID, metadataBuffer.get(entityID)); if (metadataBuffer.get(entityID).size() > 0) { try { - wrapper.send(); + wrapper.send(Protocol1_9TO1_8.class); } catch (Exception e) { e.printStackTrace(); }