mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
163 lines
6.0 KiB
Diff
163 lines
6.0 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Fri, 7 Jul 2023 16:50:06 +0800
|
|
Subject: [PATCH] Bladeren Protocol
|
|
|
|
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..4d9285d69724e8600fd7a3f9a6c31ecd579de6ad
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java
|
|
@@ -0,0 +1,150 @@
|
|
+package org.leavesmc.leaves.protocol.bladeren;
|
|
+
|
|
+import net.minecraft.nbt.CompoundTag;
|
|
+import net.minecraft.network.FriendlyByteBuf;
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+import org.jetbrains.annotations.Contract;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.leavesmc.leaves.LeavesConfig;
|
|
+import org.leavesmc.leaves.LeavesLogger;
|
|
+import org.leavesmc.leaves.protocol.core.LeavesCustomPayload;
|
|
+import org.leavesmc.leaves.protocol.core.LeavesProtocol;
|
|
+import org.leavesmc.leaves.protocol.core.ProtocolHandler;
|
|
+import org.leavesmc.leaves.protocol.core.ProtocolUtils;
|
|
+
|
|
+import java.util.HashMap;
|
|
+import java.util.Map;
|
|
+import java.util.function.BiConsumer;
|
|
+
|
|
+@LeavesProtocol(namespace = "bladeren")
|
|
+public class BladerenProtocol {
|
|
+
|
|
+ public static final String PROTOCOL_ID = "bladeren";
|
|
+ public static final String PROTOCOL_VERSION = "1.0.0";
|
|
+
|
|
+ private static final ResourceLocation HELLO_ID = id("hello");
|
|
+ private static final ResourceLocation FEATURE_MODIFY_ID = id("feature_modify");
|
|
+
|
|
+ private static final Map<String, BiConsumer<ServerPlayer, CompoundTag>> registeredFeatures = new HashMap<>();
|
|
+
|
|
+ @Contract("_ -> new")
|
|
+ public static @NotNull ResourceLocation id(String path) {
|
|
+ return new ResourceLocation(PROTOCOL_ID, path);
|
|
+ }
|
|
+
|
|
+ @ProtocolHandler.PayloadReceiver(payload = BladerenHelloPayload.class, payloadId = "hello")
|
|
+ private static void handleHello(@NotNull ServerPlayer player, @NotNull BladerenHelloPayload payload) {
|
|
+ if (LeavesConfig.bladerenLeavesProtocol) {
|
|
+ String clientVersion = payload.version;
|
|
+ CompoundTag tag = payload.nbt;
|
|
+
|
|
+ LeavesLogger.LOGGER.info("Player " + player.getScoreboardName() + " joined with bladeren " + clientVersion);
|
|
+
|
|
+ if (tag != null) {
|
|
+ CompoundTag featureNbt = tag.getCompound("Features");
|
|
+ for (String name : featureNbt.getAllKeys()) {
|
|
+ if (registeredFeatures.containsKey(name)) {
|
|
+ registeredFeatures.get(name).accept(player, featureNbt.getCompound(name));
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @ProtocolHandler.PayloadReceiver(payload = BladerenFeatureModifyPayload.class, payloadId = "feature_modify")
|
|
+ private static void handleModify(@NotNull ServerPlayer player, @NotNull BladerenFeatureModifyPayload payload) {
|
|
+ if (LeavesConfig.bladerenLeavesProtocol) {
|
|
+ String name = payload.name;
|
|
+ CompoundTag tag = payload.nbt;
|
|
+
|
|
+ if (registeredFeatures.containsKey(name)) {
|
|
+ registeredFeatures.get(name).accept(player, tag);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @ProtocolHandler.PlayerJoin
|
|
+ public static void onPlayerJoin(@NotNull ServerPlayer player) {
|
|
+ if (LeavesConfig.bladerenLeavesProtocol) {
|
|
+ CompoundTag tag = new CompoundTag();
|
|
+ LeavesFeatureSet.writeNBT(tag);
|
|
+ ProtocolUtils.sendPayloadPacket(player, new BladerenHelloPayload(PROTOCOL_VERSION, tag));
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void registerFeature(String name, BiConsumer<ServerPlayer, CompoundTag> consumer) {
|
|
+ registeredFeatures.put(name, consumer);
|
|
+ }
|
|
+
|
|
+ public static class LeavesFeatureSet {
|
|
+
|
|
+ private static final Map<String, LeavesFeature> features = new HashMap<>();
|
|
+
|
|
+ public static void writeNBT(@NotNull CompoundTag tag) {
|
|
+ CompoundTag featureNbt = new CompoundTag();
|
|
+ features.values().forEach(feature -> feature.writeNBT(featureNbt));
|
|
+ tag.put("Features", featureNbt);
|
|
+ }
|
|
+
|
|
+ public static void register(LeavesFeature feature) {
|
|
+ features.put(feature.name, feature);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record LeavesFeature(String name, String value) {
|
|
+
|
|
+ @NotNull
|
|
+ @Contract("_, _ -> new")
|
|
+ public static LeavesFeature of(String name, boolean value) {
|
|
+ return new LeavesFeature(name, Boolean.toString(value));
|
|
+ }
|
|
+
|
|
+ public void writeNBT(@NotNull CompoundTag rules) {
|
|
+ CompoundTag rule = new CompoundTag();
|
|
+ rule.putString("Feature", name);
|
|
+ rule.putString("Value", value);
|
|
+ rules.put(name, rule);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record BladerenFeatureModifyPayload(String name, CompoundTag nbt) implements LeavesCustomPayload<BladerenFeatureModifyPayload> {
|
|
+
|
|
+ @New
|
|
+ public BladerenFeatureModifyPayload(ResourceLocation location, FriendlyByteBuf buf) {
|
|
+ this(buf.readUtf(), buf.readNbt());
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void write(@NotNull FriendlyByteBuf buf) {
|
|
+ buf.writeUtf(name);
|
|
+ buf.writeNbt(nbt);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ @NotNull
|
|
+ public ResourceLocation id() {
|
|
+ return FEATURE_MODIFY_ID;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record BladerenHelloPayload(String version, CompoundTag nbt) implements LeavesCustomPayload<BladerenHelloPayload> {
|
|
+
|
|
+ @New
|
|
+ public BladerenHelloPayload(ResourceLocation location, @NotNull FriendlyByteBuf buf) {
|
|
+ this(buf.readUtf(64), buf.readNbt());
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void write(@NotNull FriendlyByteBuf buf) {
|
|
+ buf.writeUtf(version);
|
|
+ buf.writeNbt(nbt);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ @NotNull
|
|
+ public ResourceLocation id() {
|
|
+ return HELLO_ID;
|
|
+ }
|
|
+ }
|
|
+}
|