Made Entity#generateId public and prevent writing null data to BinaryWriter

This commit is contained in:
Felix Cravic 2020-11-27 13:29:38 +01:00
parent cac3c29e41
commit 1579bee693
2 changed files with 18 additions and 11 deletions

View File

@ -177,7 +177,14 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
return entityById.getOrDefault(id, null); return entityById.getOrDefault(id, null);
} }
private static int generateId() { /**
* Generate and return a new unique entity id.
* <p>
* 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(); return lastEntityId.incrementAndGet();
} }

View File

@ -157,7 +157,7 @@ public class BinaryWriter extends OutputStream {
* *
* @param string the string to write * @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); final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
writeVarInt(bytes.length); writeVarInt(bytes.length);
writeBytes(bytes); writeBytes(bytes);
@ -170,7 +170,7 @@ public class BinaryWriter extends OutputStream {
* *
* @param string the string to write * @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); final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
writeShort((short) bytes.length); writeShort((short) bytes.length);
writeBytes(bytes); writeBytes(bytes);
@ -201,7 +201,7 @@ public class BinaryWriter extends OutputStream {
* *
* @param bytes the byte array to write * @param bytes the byte array to write
*/ */
public void writeBytes(byte[] bytes) { public void writeBytes(@NotNull byte[] bytes) {
buffer.writeBytes(bytes); buffer.writeBytes(bytes);
} }
@ -212,7 +212,7 @@ public class BinaryWriter extends OutputStream {
* *
* @param array the string array to write * @param array the string array to write
*/ */
public void writeStringArray(String[] array) { public void writeStringArray(@NotNull String[] array) {
if (array == null) { if (array == null) {
writeVarInt(0); writeVarInt(0);
return; return;
@ -239,12 +239,12 @@ public class BinaryWriter extends OutputStream {
* *
* @param uuid the {@link UUID} to write * @param uuid the {@link UUID} to write
*/ */
public void writeUuid(UUID uuid) { public void writeUuid(@NotNull UUID uuid) {
writeLong(uuid.getMostSignificantBits()); writeLong(uuid.getMostSignificantBits());
writeLong(uuid.getLeastSignificantBits()); writeLong(uuid.getLeastSignificantBits());
} }
public void writeBlockPosition(BlockPosition blockPosition) { public void writeBlockPosition(@NotNull BlockPosition blockPosition) {
writeBlockPosition(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); writeBlockPosition(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
} }
@ -252,11 +252,11 @@ public class BinaryWriter extends OutputStream {
writeLong(SerializerUtils.positionToLong(x, y, z)); writeLong(SerializerUtils.positionToLong(x, y, z));
} }
public void writeItemStack(ItemStack itemStack) { public void writeItemStack(@NotNull ItemStack itemStack) {
NBTUtils.writeItemStack(this, itemStack); NBTUtils.writeItemStack(this, itemStack);
} }
public void writeNBT(String name, NBT tag) { public void writeNBT(@NotNull String name, @NotNull NBT tag) {
try { try {
nbtWriter.writeNamed(name, tag); nbtWriter.writeNamed(name, tag);
} catch (IOException e) { } catch (IOException e) {
@ -282,7 +282,7 @@ public class BinaryWriter extends OutputStream {
* *
* @param headerWriter the {@link BinaryWriter} to add at the beginning * @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 // Get the buffer of the header
final ByteBuf headerBuf = headerWriter.getBuffer(); final ByteBuf headerBuf = headerWriter.getBuffer();
// Merge both the headerBuf and this buffer // 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 * @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 // Get the buffer of the footer
final ByteBuf footerBuf = footerWriter.getBuffer(); final ByteBuf footerBuf = footerWriter.getBuffer();
// Merge both this buffer and the footerBuf // Merge both this buffer and the footerBuf