Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve StreamSerializer ItemStack methods #875

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions src/main/java/com/comphenix/protocol/utility/StreamSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,60 +283,53 @@ public String deserializeString(@Nonnull DataInputStream input, int maximumLengt
* @throws IOException If the operation fails due to reflection problems.
*/
public String serializeItemStack(ItemStack stack) throws IOException {
Object nmsItem = MinecraftReflection.getMinecraftItemStack(stack);
byte[] bytes = null;

if (MinecraftReflection.isUsingNetty()) {
ByteBuf buf = Unpooled.buffer();
Object serializer = MinecraftReflection.getPacketDataSerializer(buf);

if (WRITE_ITEM_METHOD == null) {
WRITE_ITEM_METHOD = Accessors.getMethodAccessor(
FuzzyReflection.fromClass(MinecraftReflection.getPacketDataSerializerClass(), true).
getMethodByParameters("writeStack", // a()
MinecraftReflection.getItemStackClass()));
}
return Base64Coder.encodeLines(serializeItemStackToByteArray(stack));
}

WRITE_ITEM_METHOD.invoke(serializer, nmsItem);
/**
* Deserialize an item stack from a base-64 encoded string.
* @param input - base-64 encoded string.
* @return A deserialized item stack, or NULL if the serialized ItemStack was also NULL.
* @throws IOException If the operation failed due to reflection or corrupt data.
*/
public ItemStack deserializeItemStack(String input) throws IOException {
Validate.notNull(input, "input cannot be null!");

bytes = buf.array();
} else {
if (WRITE_ITEM_METHOD == null) {
WRITE_ITEM_METHOD = Accessors.getMethodAccessor(
FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).getMethod(
FuzzyMethodContract.newBuilder().
parameterCount(2).
parameterDerivedOf(MinecraftReflection.getItemStackClass(), 0).
parameterDerivedOf(DataOutput.class, 1).
build())
);
}
return deserializeItemStackFromByteArray(Base64Coder.decodeLines(input));
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutput = new DataOutputStream(outputStream);
/**
* Serialize an item stack as byte array.
* <p>
* Note: An ItemStack can be written to the serialized text even if it's NULL.
*
* @param stack - the item stack to serialize, or NULL to represent air/nothing.
* @return A binary representation of the given item stack.
* @throws IOException If the operation fails due to reflection problems.
*/
public byte[] serializeItemStackToByteArray(ItemStack stack) throws IOException {

WRITE_ITEM_METHOD.invoke(null, nmsItem, dataOutput);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(outputStream);

bytes = outputStream.toByteArray();
}
serializeItemStack(output, stack);

return Base64Coder.encodeLines(bytes);
return outputStream.toByteArray();
}

/**
* Deserialize an item stack from a base-64 encoded string.
* @param input - base-64 encoded string.
* Deserialize an item stack from a byte array.
* @param input - serialized item.
* @return A deserialized item stack, or NULL if the serialized ItemStack was also NULL.
* @throws IOException If the operation failed due to reflection or corrupt data.
*/
public ItemStack deserializeItemStack(String input) throws IOException {
public ItemStack deserializeItemStackFromByteArray(byte[] input) throws IOException {
Validate.notNull(input, "input cannot be null!");

Object nmsItem = null;
byte[] bytes = Base64Coder.decodeLines(input);
Object nmsItem;

if (MinecraftReflection.isUsingNetty()) {
ByteBuf buf = Unpooled.copiedBuffer(bytes);
ByteBuf buf = Unpooled.copiedBuffer(input);
Object serializer = MinecraftReflection.getPacketDataSerializer(buf);

if (READ_ITEM_METHOD == null) {
Expand All @@ -358,7 +351,7 @@ public ItemStack deserializeItemStack(String input) throws IOException {
);
}

ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ByteArrayInputStream byteStream = new ByteArrayInputStream(input);
DataInputStream inputStream = new DataInputStream(byteStream);

nmsItem = READ_ITEM_METHOD.invoke(null, inputStream);
Expand All @@ -380,7 +373,7 @@ public ItemStack deserializeItemStack(String input) throws IOException {
* @throws IOException If the operation fails due to reflection problems.
*/
public void serializeItemStack(DataOutputStream output, ItemStack stack) throws IOException {
Validate.notNull("output cannot be null!");
Validate.notNull(output, "output cannot be null!");

// Get the NMS version of the ItemStack
Object nmsItem = MinecraftReflection.getMinecraftItemStack(stack);
Expand Down Expand Up @@ -429,7 +422,7 @@ public void serializeItemStack(DataOutputStream output, ItemStack stack) throws
@Deprecated
public ItemStack deserializeItemStack(DataInputStream input) throws IOException {
Validate.notNull(input, "input cannot be null!");
Object nmsItem = null;
Object nmsItem;

if (MinecraftReflection.isUsingNetty()) {
if (READ_ITEM_METHOD == null) {
Expand Down