Make it easy to clone packets.

This commit is contained in:
Kristian S. Stangeland 2012-12-03 17:51:18 +01:00
parent f4accbfe2d
commit 300111b850
2 changed files with 39 additions and 1 deletions

View File

@ -17,6 +17,8 @@
package com.comphenix.protocol.events; package com.comphenix.protocol.events;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; 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.ChunkPosition;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import com.google.common.io.Closeables;
import net.minecraft.server.Packet; import net.minecraft.server.Packet;
@ -347,6 +350,42 @@ public class PacketContainer implements Serializable {
return id; 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 { private void writeObject(ObjectOutputStream output) throws IOException {
// Default serialization // Default serialization
output.defaultWriteObject(); output.defaultWriteObject();

View File

@ -55,7 +55,6 @@ import com.comphenix.protocol.events.*;
import com.comphenix.protocol.injector.player.PlayerInjectionHandler; import com.comphenix.protocol.injector.player.PlayerInjectionHandler;
import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.wrappers.BukkitConverters;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;