diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/CommentEntry.java b/common/src/main/java/cn/zbx1425/worldcomment/data/CommentEntry.java index 8779aee..4b08898 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/CommentEntry.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/CommentEntry.java @@ -46,8 +46,8 @@ public CommentEntry(Player initiator, BlockPos placedAt, int messageType, String this.imageUrl = imageUrl; } - public CommentEntry(ResourceLocation level, FriendlyByteBuf src) { - fileOffset = src.readerIndex(); + public CommentEntry(ResourceLocation level, FriendlyByteBuf src, boolean fromFile) { + if (fromFile) fileOffset = src.readerIndex(); id = src.readLong(); timestamp = src.readLong(); this.level = level; @@ -59,7 +59,7 @@ public CommentEntry(ResourceLocation level, FriendlyByteBuf src) { message = src.readUtf(); deleted = src.readBoolean(); imageUrl = src.readUtf(); - src.skipBytes(16 - (src.readerIndex() % 16)); + if (fromFile) src.skipBytes(16 - (src.readerIndex() % 16)); } private static UUID uuidFromByteArray(byte[] bytes) { @@ -67,7 +67,7 @@ private static UUID uuidFromByteArray(byte[] bytes) { return new UUID(bb.getLong(), bb.getLong()); } - public void writeBuffer(FriendlyByteBuf dst) { + public void writeBuffer(FriendlyByteBuf dst, boolean toFile) { dst.writeLong(id); dst.writeLong(timestamp); dst.writeBlockPos(location); @@ -77,12 +77,12 @@ public void writeBuffer(FriendlyByteBuf dst) { dst.writeUtf(message); dst.writeBoolean(deleted); dst.writeUtf(imageUrl); - dst.writeZero(16 - (dst.writerIndex() % 16)); + if (toFile) dst.writeZero(16 - (dst.writerIndex() % 16)); } public void writeFileStream(FileOutputStream oStream) throws IOException { FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(256)); - writeBuffer(buf); + writeBuffer(buf, true); fileOffset = oStream.getChannel().position(); oStream.write(buf.array(), 0, buf.writerIndex()); } diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/CommentTable.java b/common/src/main/java/cn/zbx1425/worldcomment/data/CommentTable.java index 59c15af..faa7d44 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/CommentTable.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/CommentTable.java @@ -40,7 +40,7 @@ public void load() throws IOException { for (Path file : files.toList()) { long region = Long.parseUnsignedLong(file.getFileName().toString().substring(1, 9), 16); byte[] fileContent = Files.readAllBytes(file); - loadRegion(dimension, region, fileContent); + loadRegion(dimension, region, fileContent, true); } } } @@ -50,12 +50,12 @@ public void load() throws IOException { } } - public void loadRegion(ResourceLocation dimension, long region, byte[] data) { + public void loadRegion(ResourceLocation dimension, long region, byte[] data, boolean fromFile) { synchronized (this) { List regionEntries = new ArrayList<>(); FriendlyByteBuf src = new FriendlyByteBuf(Unpooled.wrappedBuffer(data)); while (src.readerIndex() < data.length) { - CommentEntry entry = new CommentEntry(dimension, src); + CommentEntry entry = new CommentEntry(dimension, src, fromFile); regionEntries.add(entry); playerIndex.computeIfAbsent(entry.initiator, ignored -> new ArrayList<>()) .add(entry); diff --git a/common/src/main/java/cn/zbx1425/worldcomment/network/PacketRegionDataS2C.java b/common/src/main/java/cn/zbx1425/worldcomment/network/PacketRegionDataS2C.java index e5f704e..5bf2658 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/network/PacketRegionDataS2C.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/network/PacketRegionDataS2C.java @@ -28,9 +28,8 @@ public static void send(ServerPlayer target, ResourceLocation level, Map> entry : data.entrySet()) { buffer.writeChunkPos(entry.getKey()); buffer.writeInt(entry.getValue().size()); - buffer.writeZero(16 - (buffer.writerIndex() % 16)); for (CommentEntry comment : entry.getValue()) { - comment.writeBuffer(buffer); + comment.writeBuffer(buffer, false); } } ServerPlatform.sendPacketToPlayer(target, IDENTIFIER, buffer); @@ -45,10 +44,9 @@ public static void handle(FriendlyByteBuf buffer) { for (int i = 0; i < regionSize; i++) { ChunkPos region = buffer.readChunkPos(); int commentSize = buffer.readInt(); - buffer.skipBytes(16 - (buffer.readerIndex() % 16)); ArrayList comments = new ArrayList<>(commentSize); for (int j = 0; j < commentSize; j++) { - CommentEntry comment = new CommentEntry(level, buffer); + CommentEntry comment = new CommentEntry(level, buffer, false); comments.add(comment); } regions.put(region.toLong(), comments); diff --git a/common/src/main/java/cn/zbx1425/worldcomment/network/PacketSubmitCommentC2S.java b/common/src/main/java/cn/zbx1425/worldcomment/network/PacketSubmitCommentC2S.java index cf5f672..3370fcb 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/network/PacketSubmitCommentC2S.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/network/PacketSubmitCommentC2S.java @@ -26,14 +26,14 @@ public static class ClientLogics { public static void send(CommentEntry comment) { FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); buffer.writeResourceLocation(comment.level); - comment.writeBuffer(buffer); + comment.writeBuffer(buffer, false); ClientPlatform.sendPacketToServer(IDENTIFIER, buffer); } } public static void handle(MinecraftServer server, ServerPlayer initiator, FriendlyByteBuf buffer) { ResourceLocation level = buffer.readResourceLocation(); - CommentEntry comment = new CommentEntry(level, buffer); + CommentEntry comment = new CommentEntry(level, buffer, false); if (!comment.initiator.equals(Util.NIL_UUID) && !comment.initiator.equals(initiator.getGameProfile().getId())) { return;