Skip to content

Commit

Permalink
Add deleting comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 27, 2023
1 parent 69b29c5 commit c03f71a
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 91 deletions.
15 changes: 9 additions & 6 deletions common/src/main/java/cn/zbx1425/worldcomment/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import cn.zbx1425.worldcomment.data.persist.Database;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import cn.zbx1425.worldcomment.network.PacketRequestCommentUIC2S;
import cn.zbx1425.worldcomment.network.PacketRequestRegionC2S;
import cn.zbx1425.worldcomment.network.PacketSubmitCommentC2S;
import cn.zbx1425.worldcomment.network.PacketCollectionRequestC2S;
import cn.zbx1425.worldcomment.network.PacketEntryActionC2S;
import cn.zbx1425.worldcomment.network.PacketRegionRequestC2S;
import cn.zbx1425.worldcomment.network.PacketEntryCreateC2S;
import cn.zbx1425.worldcomment.util.RegistriesWrapper;
import cn.zbx1425.worldcomment.util.RegistryObject;
import net.minecraft.world.item.CreativeModeTabs;
Expand All @@ -27,11 +28,13 @@ public static void init(RegistriesWrapper registries) {
registries.registerItem("comment_tool", ITEM_COMMENT_TOOL, CreativeModeTabs.TOOLS_AND_UTILITIES);

ServerPlatform.registerNetworkReceiver(
PacketRequestRegionC2S.IDENTIFIER, PacketRequestRegionC2S::handle);
PacketRegionRequestC2S.IDENTIFIER, PacketRegionRequestC2S::handle);
ServerPlatform.registerNetworkReceiver(
PacketRequestCommentUIC2S.IDENTIFIER, PacketRequestCommentUIC2S::handle);
PacketCollectionRequestC2S.IDENTIFIER, PacketCollectionRequestC2S::handle);
ServerPlatform.registerNetworkReceiver(
PacketSubmitCommentC2S.IDENTIFIER, PacketSubmitCommentC2S::handle);
PacketEntryCreateC2S.IDENTIFIER, PacketEntryCreateC2S::handle);
ServerPlatform.registerNetworkReceiver(
PacketEntryActionC2S.IDENTIFIER, PacketEntryActionC2S::handle);

ServerPlatform.registerServerStartingEvent(server -> {
try {
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/cn/zbx1425/worldcomment/MainClient.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cn.zbx1425.worldcomment;

import cn.zbx1425.worldcomment.network.PacketCommentDataUIS2C;
import cn.zbx1425.worldcomment.network.PacketCollectionDataS2C;
import cn.zbx1425.worldcomment.network.PacketRegionDataS2C;
import cn.zbx1425.worldcomment.network.PacketCommentUpdateS2C;
import cn.zbx1425.worldcomment.network.PacketEntryUpdateS2C;

public class MainClient {

public static void init() {
ClientPlatform.registerNetworkReceiver(
PacketRegionDataS2C.IDENTIFIER, PacketRegionDataS2C.ClientLogics::handle);
ClientPlatform.registerNetworkReceiver(
PacketCommentDataUIS2C.IDENTIFIER, PacketCommentDataUIS2C.ClientLogics::handle);
PacketCollectionDataS2C.IDENTIFIER, PacketCollectionDataS2C.ClientLogics::handle);
ClientPlatform.registerNetworkReceiver(
PacketCommentUpdateS2C.IDENTIFIER, PacketCommentUpdateS2C.ClientLogics::handle);
PacketEntryUpdateS2C.IDENTIFIER, PacketEntryUpdateS2C.ClientLogics::handle);
}

}
21 changes: 11 additions & 10 deletions common/src/main/java/cn/zbx1425/worldcomment/data/CommentEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

Expand Down Expand Up @@ -51,15 +52,12 @@ public CommentEntry(Player initiator, boolean isAnonymous, int messageType, Stri
}

public CommentEntry(ResourceLocation level, FriendlyByteBuf src, boolean fromFile) {
if (fromFile) {
fileOffset = src.readerIndex();
src.skipBytes(16);
}
fileOffset = src.readerIndex();

deleted = src.readBoolean();
src.skipBytes(3);
like = src.readInt();
src.skipBytes(4);
src.skipBytes(8);

id = src.readLong();
timestamp = src.readLong();
Expand All @@ -81,14 +79,10 @@ public void setLocation(BlockPos location) {
}

public void writeBuffer(FriendlyByteBuf dst, boolean toFile) {
if (toFile) {
dst.writeBytes("=====(C)=Zbx1425".getBytes(StandardCharsets.UTF_8));
}

dst.writeBoolean(deleted);
dst.writeZero(3);
dst.writeInt(like);
dst.writeZero(4);
dst.writeBytes("====ZBX=".getBytes(StandardCharsets.UTF_8));

dst.writeLong(id);
dst.writeLong(timestamp);
Expand All @@ -110,4 +104,11 @@ public void writeFileStream(FileOutputStream oStream) throws IOException {
oStream.write(buf.array(), 0, buf.writerIndex());
}

public void updateInFile(RandomAccessFile oFile) throws IOException {
oFile.seek(fileOffset);
oFile.writeBoolean(deleted);
oFile.write(new byte[3]);
oFile.writeInt(like);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.zbx1425.worldcomment.data.client;

import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.network.PacketRequestRegionC2S;
import cn.zbx1425.worldcomment.network.PacketRegionRequestC2S;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void tick() {
}
}
if (regionsToRequest.size() > 0) {
PacketRequestRegionC2S.ClientLogics.send(minecraft.level.dimension().location(), regionsToRequest);
PacketRegionRequestC2S.ClientLogics.send(minecraft.level.dimension().location(), regionsToRequest);
}

for (ObjectIterator<Long2LongMap.Entry> it = regionExpiry.long2LongEntrySet().iterator(); it.hasNext(); ) {
Expand Down Expand Up @@ -99,7 +99,11 @@ public void acceptUpdate(CommentEntry comment, boolean update) {
List<CommentEntry> blockData = regionData.get(comment.location);
for (int i = 0; i < blockData.size(); i++) {
if (blockData.get(i).id == comment.id) {
blockData.set(i, comment);
if (comment.deleted) {
blockData.remove(i);
} else {
blockData.set(i, comment);
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.data.persist.Database;
import cn.zbx1425.worldcomment.network.PacketSubmitCommentC2S;
import cn.zbx1425.worldcomment.network.PacketEntryCreateC2S;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void removeJob(long jobId) {
private static void trySendPackage(long jobId) {
SubmitJob job = pendingJobs.get(jobId);
if (job.isReady()) {
PacketSubmitCommentC2S.ClientLogics.send(job.comment);
PacketEntryCreateC2S.ClientLogics.send(job.comment);
if (job.callback != null) job.callback.accept(null);
removeJob(jobId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import cn.zbx1425.worldcomment.data.CommentEntry;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.longs.*;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ChunkPos;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Expand All @@ -31,7 +34,11 @@ public void load() throws IOException {
playerIndex.clear();
timeIndex.clear();
if (db.isHost) {
Files.createDirectories(db.basePath.resolve("region"));
try {
Files.createDirectories(db.basePath.resolve("region"));
} catch (FileAlreadyExistsException ignored) {

}
try (Stream<Path> levelFiles = Files.list(db.basePath.resolve("region"))) {
for (Path levelPath : levelFiles.toList()) {
ResourceLocation dimension = new ResourceLocation(levelPath.getFileName().toString().replace("+", ":"));
Expand All @@ -57,6 +64,7 @@ public void loadRegion(ResourceLocation dimension, long region, byte[] data, boo
FriendlyByteBuf src = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
while (src.readerIndex() < data.length) {
CommentEntry entry = new CommentEntry(dimension, src, fromFile);
if (entry.deleted) continue;
regionEntries.add(entry);
playerIndex.computeIfAbsent(entry.initiator, ignored -> new ArrayList<>())
.add(entry);
Expand Down Expand Up @@ -95,7 +103,18 @@ public List<CommentEntry> queryPlayer(UUID player) {

public List<CommentEntry> queryLatest(int offset, int count) {
synchronized (this) {
return timeIndex.values().stream().skip(offset).limit(count).toList();
List<CommentEntry> result = new ArrayList<>();
for (CommentEntry comment : timeIndex.values()) {
if (comment.deleted) continue;
if (offset > 0) {
offset--;
continue;
}
if (count <= 0) break;
result.add(comment);
count--;
}
return result;
}
}

Expand All @@ -105,7 +124,11 @@ public void insert(CommentEntry newEntry) throws IOException {
if (createLevelFolder) regionIndex.put(newEntry.level, new Long2ObjectOpenHashMap<>());
if (db.isHost) {
if (createLevelFolder) {
Files.createDirectory(getLevelPath(newEntry.level));
try {
Files.createDirectory(getLevelPath(newEntry.level));
} catch (FileAlreadyExistsException ignored) {

}
}
Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), true)) {
Expand All @@ -120,4 +143,25 @@ public void insert(CommentEntry newEntry) throws IOException {
timeIndex.put(newEntry.timestamp, newEntry);
}
}

public void update(CommentEntry newEntry) throws IOException {
synchronized (this) {
List<CommentEntry> regionData = regionIndex.getOrDefault(newEntry.level, Long2ObjectMaps.emptyMap())
.get(newEntry.region.toLong());
if (regionData == null) return;
for (CommentEntry existingEntry : regionData) {
if (existingEntry.id == newEntry.id) {
existingEntry.deleted = newEntry.deleted;
existingEntry.like = newEntry.like;
assert existingEntry.fileOffset > 0;
if (db.isHost) {
Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (RandomAccessFile oStream = new RandomAccessFile(targetFile.toFile(), "rw")) {
existingEntry.updateInFile(oStream);
}
}
}
}
}
}
}
Loading

0 comments on commit c03f71a

Please sign in to comment.