Skip to content

Commit

Permalink
Add ClientDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 23, 2023
1 parent cce8cd6 commit 2921255
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 25 deletions.
5 changes: 5 additions & 0 deletions common/src/main/java/cn/zbx1425/worldcomment/MainClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cn.zbx1425.worldcomment;

import cn.zbx1425.worldcomment.data.client.ClientDatabase;
import cn.zbx1425.worldcomment.network.PacketRegionDataS2C;

public class MainClient {

public static void init() {
ClientPlatform.registerNetworkReceiver(PacketRegionDataS2C.IDENTIFIER, PacketRegionDataS2C.ClientLogics::handle);

ClientPlatform.registerTickEvent(ignored -> {
ClientDatabase.INSTANCE.tick();
});
}

}
38 changes: 23 additions & 15 deletions common/src/main/java/cn/zbx1425/worldcomment/data/CommentTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMap;
import it.unimi.dsi.fastutil.longs.*;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ChunkPos;
Expand All @@ -31,17 +28,20 @@ public CommentTable(Database db) {
}

public void load() throws IOException {
regionIndex.clear();
playerIndex.clear();
timeIndex.clear();
if (db.isHost) {
Files.createDirectories(db.basePath.resolve("regions"));
for (Level level : db.server.getAllLevels()) {
ResourceLocation dimension = level.dimension().location();
Path levelPath = getLevelPath(dimension);
Files.createDirectory(levelPath);
try (Stream<Path> files = Files.list(levelPath)) {
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);
try (Stream<Path> levelFiles = Files.list(db.basePath.resolve("regions"))) {
for (Path levelPath : levelFiles.toList()) {
ResourceLocation dimension = new ResourceLocation(levelPath.getFileName().toString().replace("+", ":"));
try (Stream<Path> files = Files.list(levelPath)) {
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);
}
}
}
}
Expand Down Expand Up @@ -79,13 +79,16 @@ private Path getLevelRegionPath(ResourceLocation dimension, ChunkPos region) {

public List<CommentEntry> queryRegion(ResourceLocation level, ChunkPos region) {
synchronized (this) {
return regionIndex.get(level).get(region.toLong());
return regionIndex
.getOrDefault(level, Long2ObjectMaps.emptyMap())
.getOrDefault(region.toLong(), List.of());
}
}

public List<CommentEntry> queryPlayer(UUID player) {
synchronized (this) {
return playerIndex.get(player);
return playerIndex
.getOrDefault(player, List.of());
}
}

Expand All @@ -97,7 +100,12 @@ public List<CommentEntry> queryLatest(int offset, int count) {

public void insert(CommentEntry newEntry) throws IOException {
synchronized (this) {
boolean createLevelFolder = !regionIndex.containsKey(newEntry.level);
if (createLevelFolder) regionIndex.put(newEntry.level, new Long2ObjectOpenHashMap<>());
if (db.isHost) {
if (createLevelFolder) {
Files.createDirectory(getLevelPath(newEntry.level));
}
Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), true)) {
newEntry.writeFileStream(oStream);
Expand Down

This file was deleted.

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

import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.network.PacketRequestRegionC2S;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ChunkPos;

import java.util.ArrayList;
import java.util.List;

public class ClientDatabase {

public ResourceLocation level;
public Long2ObjectMap<List<CommentEntry>> regions = new Long2ObjectOpenHashMap<>();
public Long2LongMap regionExpiry = new Long2LongOpenHashMap();

public static ClientDatabase INSTANCE = new ClientDatabase();

public static final long REGION_TTL = 60000;

private long lastTickTime = 0;

public void tick() {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft.player == null || minecraft.level == null) return;
long currentTime = System.currentTimeMillis();
if (currentTime - lastTickTime < 1000) return;
lastTickTime = currentTime;

BlockPos playerPos = minecraft.player.blockPosition();
int cx = playerPos.getX() >> CommentEntry.REGION_SHIFT;
int cz = playerPos.getZ() >> CommentEntry.REGION_SHIFT;

List<ChunkPos> regionsToRequest = new ArrayList<>();
for (int x = cx - 1; x <= cx + 1; x++) {
for (int z = cz - 1; z <= cz + 1; z++) {
long chunkLong = ChunkPos.asLong(x, z);
if (!regionExpiry.containsKey(chunkLong) || (regionExpiry.get(chunkLong) > currentTime)) {
regionExpiry.put(chunkLong, currentTime + REGION_TTL);
regionsToRequest.add(new ChunkPos(x, z));
}
}
}
PacketRequestRegionC2S.ClientLogics.send(minecraft.level.dimension().location(), regionsToRequest);

for (ObjectIterator<Long2LongMap.Entry> it = regionExpiry.long2LongEntrySet().iterator(); it.hasNext(); ) {
Long2LongMap.Entry entry = it.next();
if (entry.getLongValue() > currentTime) {
regions.remove(entry.getLongKey());
it.remove();
}
}
}

public void clear() {
regions.clear();
regionExpiry.clear();
}

public void acceptRegions(ResourceLocation level, Long2ObjectMap<List<CommentEntry>> regions) {
if (!level.equals(this.level)) clear();
this.level = level;
long currentTime = System.currentTimeMillis();
for (Long2ObjectMap.Entry<List<CommentEntry>> entry : regions.long2ObjectEntrySet()) {
regions.put(entry.getLongKey(), entry.getValue());
regionExpiry.put(entry.getLongKey(), currentTime + REGION_TTL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.ServerPlatform;
import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.data.client.ClientDatabase;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.ChunkPos;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -37,14 +41,19 @@ public static class ClientLogics {
public static void handle(FriendlyByteBuf buffer) {
ResourceLocation level = buffer.readResourceLocation();
int regionSize = buffer.readInt();
Long2ObjectMap<List<CommentEntry>> regions = new Long2ObjectOpenHashMap<>();
for (int i = 0; i < regionSize; i++) {
ChunkPos region = buffer.readChunkPos();
int commentSize = buffer.readInt();
buffer.skipBytes(16 - (buffer.readerIndex() % 16));
ArrayList<CommentEntry> comments = new ArrayList<>(commentSize);
for (int j = 0; j < commentSize; j++) {
CommentEntry comment = new CommentEntry(level, buffer);
comments.add(comment);
}
regions.put(region.toLong(), comments);
}
ClientDatabase.INSTANCE.acceptRegions(level, regions);
}
}
}

0 comments on commit 2921255

Please sign in to comment.