-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package cn.zbx1425.worldcomment; | ||
|
||
import cn.zbx1425.worldcomment.network.PacketRegionDataS2C; | ||
|
||
public class MainClient { | ||
|
||
public static void init() { | ||
|
||
ClientPlatform.registerNetworkReceiver(PacketRegionDataS2C.IDENTIFIER, PacketRegionDataS2C.ClientLogics::handle); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
common/src/main/java/cn/zbx1425/worldcomment/network/PacketRegionDataS2C.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,50 @@ | ||
package cn.zbx1425.worldcomment.network; | ||
|
||
import cn.zbx1425.worldcomment.ClientPlatform; | ||
import cn.zbx1425.worldcomment.Main; | ||
import cn.zbx1425.worldcomment.ServerPlatform; | ||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import io.netty.buffer.Unpooled; | ||
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.List; | ||
import java.util.Map; | ||
|
||
public class PacketRegionDataS2C { | ||
|
||
public static final ResourceLocation IDENTIFIER = new ResourceLocation(Main.MOD_ID, "region_data"); | ||
|
||
public static void send(ServerPlayer target, ResourceLocation level, Map<ChunkPos, List<CommentEntry>> data) { | ||
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); | ||
buffer.writeResourceLocation(level); | ||
buffer.writeInt(data.size()); | ||
for (Map.Entry<ChunkPos, List<CommentEntry>> 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); | ||
} | ||
} | ||
ServerPlatform.sendPacketToPlayer(target, IDENTIFIER, buffer); | ||
} | ||
|
||
public static class ClientLogics { | ||
|
||
public static void handle(FriendlyByteBuf buffer) { | ||
ResourceLocation level = buffer.readResourceLocation(); | ||
int regionSize = buffer.readInt(); | ||
for (int i = 0; i < regionSize; i++) { | ||
ChunkPos region = buffer.readChunkPos(); | ||
int commentSize = buffer.readInt(); | ||
buffer.skipBytes(16 - (buffer.readerIndex() % 16)); | ||
for (int j = 0; j < commentSize; j++) { | ||
CommentEntry comment = new CommentEntry(level, buffer); | ||
} | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
common/src/main/java/cn/zbx1425/worldcomment/network/PacketRequestRegionC2S.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,45 @@ | ||
package cn.zbx1425.worldcomment.network; | ||
|
||
import cn.zbx1425.worldcomment.ClientPlatform; | ||
import cn.zbx1425.worldcomment.Main; | ||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import io.netty.buffer.Unpooled; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.level.ChunkPos; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PacketRequestRegionC2S { | ||
|
||
public static final ResourceLocation IDENTIFIER = new ResourceLocation(Main.MOD_ID, "request_region"); | ||
|
||
public static class ClientLogics { | ||
|
||
public static void send(ResourceLocation level, List<ChunkPos> requests) { | ||
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); | ||
buffer.writeResourceLocation(level); | ||
buffer.writeInt(requests.size()); | ||
for (ChunkPos request : requests) { | ||
buffer.writeChunkPos(request); | ||
} | ||
ClientPlatform.sendPacketToServer(IDENTIFIER, buffer); | ||
} | ||
} | ||
|
||
public static void handle(MinecraftServer server, ServerPlayer initiator, FriendlyByteBuf buffer) { | ||
ResourceLocation level = buffer.readResourceLocation(); | ||
Map<ChunkPos, List<CommentEntry>> results = new Object2ObjectArrayMap<>(); | ||
int size = buffer.readInt(); | ||
for (int i = 0; i < size; i++) { | ||
ChunkPos request = buffer.readChunkPos(); | ||
List<CommentEntry> regionResults = Main.DATABASE.comments.queryRegion(level, request); | ||
results.put(request, regionResults); | ||
} | ||
PacketRegionDataS2C.send(initiator, level, results); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
common/src/main/java/cn/zbx1425/worldcomment/network/PacketSubmitCommentC2S.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,47 @@ | ||
package cn.zbx1425.worldcomment.network; | ||
|
||
import cn.zbx1425.worldcomment.ClientPlatform; | ||
import cn.zbx1425.worldcomment.Main; | ||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import io.netty.buffer.Unpooled; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; | ||
import net.minecraft.Util; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.level.ChunkPos; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class PacketSubmitCommentC2S { | ||
|
||
public static final ResourceLocation IDENTIFIER = new ResourceLocation(Main.MOD_ID, "submit_comment"); | ||
|
||
public static class ClientLogics { | ||
|
||
public static void send(CommentEntry comment) { | ||
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); | ||
buffer.writeResourceLocation(comment.level); | ||
comment.writeBuffer(buffer); | ||
ClientPlatform.sendPacketToServer(IDENTIFIER, buffer); | ||
} | ||
} | ||
|
||
public static void handle(MinecraftServer server, ServerPlayer initiator, FriendlyByteBuf buffer) { | ||
ResourceLocation level = buffer.readResourceLocation(); | ||
CommentEntry comment = new CommentEntry(level, buffer); | ||
if (!comment.initiator.equals(Util.NIL_UUID) | ||
&& !comment.initiator.equals(initiator.getGameProfile().getId())) { | ||
return; | ||
} | ||
try { | ||
Main.DATABASE.comments.insert(comment); | ||
} catch (IOException e) { | ||
Main.LOGGER.error("Failed to create comment", e); | ||
} | ||
} | ||
} |