From 1579bee6939b83580ee9424be7e68ad5380d1ab3 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 27 Nov 2020 13:29:38 +0100 Subject: [PATCH] Made Entity#generateId public and prevent writing null data to BinaryWriter --- .../net/minestom/server/entity/Entity.java | 9 ++++++++- .../server/utils/binary/BinaryWriter.java | 20 +++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 91dc20453..c9938222b 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -177,7 +177,14 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P return entityById.getOrDefault(id, null); } - private static int generateId() { + /** + * Generate and return a new unique entity id. + *

+ * Useful if you want to spawn entities using packet but don't risk to have duplicated id. + * + * @return a newly generated entity id + */ + public static int generateId() { return lastEntityId.incrementAndGet(); } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java index 2ccf212dd..e414e9617 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java @@ -157,7 +157,7 @@ public class BinaryWriter extends OutputStream { * * @param string the string to write */ - public void writeSizedString(String string) { + public void writeSizedString(@NotNull String string) { final byte[] bytes = string.getBytes(StandardCharsets.UTF_8); writeVarInt(bytes.length); writeBytes(bytes); @@ -170,7 +170,7 @@ public class BinaryWriter extends OutputStream { * * @param string the string to write */ - public void writeShortSizedString(String string) { + public void writeShortSizedString(@NotNull String string) { final byte[] bytes = string.getBytes(StandardCharsets.UTF_8); writeShort((short) bytes.length); writeBytes(bytes); @@ -201,7 +201,7 @@ public class BinaryWriter extends OutputStream { * * @param bytes the byte array to write */ - public void writeBytes(byte[] bytes) { + public void writeBytes(@NotNull byte[] bytes) { buffer.writeBytes(bytes); } @@ -212,7 +212,7 @@ public class BinaryWriter extends OutputStream { * * @param array the string array to write */ - public void writeStringArray(String[] array) { + public void writeStringArray(@NotNull String[] array) { if (array == null) { writeVarInt(0); return; @@ -239,12 +239,12 @@ public class BinaryWriter extends OutputStream { * * @param uuid the {@link UUID} to write */ - public void writeUuid(UUID uuid) { + public void writeUuid(@NotNull UUID uuid) { writeLong(uuid.getMostSignificantBits()); writeLong(uuid.getLeastSignificantBits()); } - public void writeBlockPosition(BlockPosition blockPosition) { + public void writeBlockPosition(@NotNull BlockPosition blockPosition) { writeBlockPosition(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); } @@ -252,11 +252,11 @@ public class BinaryWriter extends OutputStream { writeLong(SerializerUtils.positionToLong(x, y, z)); } - public void writeItemStack(ItemStack itemStack) { + public void writeItemStack(@NotNull ItemStack itemStack) { NBTUtils.writeItemStack(this, itemStack); } - public void writeNBT(String name, NBT tag) { + public void writeNBT(@NotNull String name, @NotNull NBT tag) { try { nbtWriter.writeNamed(name, tag); } catch (IOException e) { @@ -282,7 +282,7 @@ public class BinaryWriter extends OutputStream { * * @param headerWriter the {@link BinaryWriter} to add at the beginning */ - public void writeAtStart(BinaryWriter headerWriter) { + public void writeAtStart(@NotNull BinaryWriter headerWriter) { // Get the buffer of the header final ByteBuf headerBuf = headerWriter.getBuffer(); // Merge both the headerBuf and this buffer @@ -296,7 +296,7 @@ public class BinaryWriter extends OutputStream { * * @param footerWriter the {@link BinaryWriter} to add at the end */ - public void writeAtEnd(BinaryWriter footerWriter) { + public void writeAtEnd(@NotNull BinaryWriter footerWriter) { // Get the buffer of the footer final ByteBuf footerBuf = footerWriter.getBuffer(); // Merge both this buffer and the footerBuf