From 300111b8503d3eb253923247e938eef02b2c0212 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 3 Dec 2012 17:51:18 +0100 Subject: [PATCH] Make it easy to clone packets. --- .../protocol/events/PacketContainer.java | 39 +++++++++++++++++++ .../injector/PacketFilterManager.java | 1 - 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java index 4150acba..fc52b2bd 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -17,6 +17,8 @@ package com.comphenix.protocol.events; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -41,6 +43,7 @@ import com.comphenix.protocol.wrappers.BukkitConverters; import com.comphenix.protocol.wrappers.ChunkPosition; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; +import com.google.common.io.Closeables; import net.minecraft.server.Packet; @@ -347,6 +350,42 @@ public class PacketContainer implements Serializable { return id; } + /** + * Create a deep copy of the current packet. + * @return A deep copy of the current packet. + */ + public PacketContainer deepClone() { + ObjectOutputStream output = null; + ObjectInputStream input = null; + + try { + // Use a small buffer of 32 bytes initially. + ByteArrayOutputStream bufferOut = new ByteArrayOutputStream(); + output = new ObjectOutputStream(bufferOut); + output.writeObject(this); + + ByteArrayInputStream bufferIn = new ByteArrayInputStream(bufferOut.toByteArray()); + input = new ObjectInputStream(bufferIn); + return (PacketContainer) input.readObject(); + + } catch (IOException e) { + throw new IllegalStateException("Unexpected error occured during object cloning.", e); + } catch (ClassNotFoundException e) { + // Cannot happen + throw new IllegalStateException("Unexpected failure with serialization.", e); + } finally { + try { + if (output != null) + output.close(); + if (input != null) + input.close(); + + } catch (IOException e) { + // STOP IT + } + } + } + private void writeObject(ObjectOutputStream output) throws IOException { // Default serialization output.defaultWriteObject(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index 663b4c09..27f7c7c0 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -55,7 +55,6 @@ import com.comphenix.protocol.events.*; import com.comphenix.protocol.injector.player.PlayerInjectionHandler; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FuzzyReflection; -import com.comphenix.protocol.wrappers.BukkitConverters; import com.google.common.base.Objects; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet;