mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-12-26 19:18:12 +01:00
Make it easy to clone packets.
This commit is contained in:
parent
f4accbfe2d
commit
300111b850
@ -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();
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user