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/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 117ab409df92a5d96568becaf58ea3a6a24c19a6..855b8b466ae6acc1d5f0367b7b8e3d64e149a7e0 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -16,6 +16,8 @@ import top.leavesmc.leaves.profile.LeavesMinecraftSessionService; import top.leavesmc.leaves.util.MathUtils; import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule; import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules; +import top.leavesmc.leaves.protocol.bladeren.BladerenProtocol.LeavesFeatureSet; +import top.leavesmc.leaves.protocol.bladeren.BladerenProtocol.LeavesFeature; import java.io.File; import java.lang.reflect.InvocationTargetException; @@ -75,6 +77,7 @@ public final class LeavesConfig { LeavesConfig.load(config); registerCarpetRules(); + registerLeavesFeatures(); commands = new HashMap<>(); commands.put("leaves", new LeavesCommand("leaves")); @@ -851,6 +854,10 @@ public final class LeavesConfig { } public static void registerLeavesFeatures() { + LeavesFeatureSet.register(LeavesFeature.of("lava_riptide", lavaRiptide)); + LeavesFeatureSet.register(LeavesFeature.of("mspt_sync", msptSyncProtocol)); + LeavesFeatureSet.register(LeavesFeature.of("loot_world_random", lootWorldRandom)); + LeavesFeatureSet.register(LeavesFeature.of("use_vanilla_random", useVanillaRandom)); } public static boolean hopperCounter = false; diff --git a/src/main/java/top/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java new file mode 100644 index 0000000000000000000000000000000000000000..a91011c7a2c56646053bb9d158ff0c265c658c00 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/bladeren/BladerenProtocol.java @@ -0,0 +1,145 @@ +package top.leavesmc.leaves.protocol.bladeren; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import top.leavesmc.leaves.LeavesConfig; +import top.leavesmc.leaves.protocol.core.LeavesProtocol; +import top.leavesmc.leaves.protocol.core.ProtocolHandler; +import top.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> 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; + + 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 consumer) { + registeredFeatures.put(name, consumer); + } + + public static class LeavesFeatureSet { + + private static final Map 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 CustomPacketPayload { + + 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 CustomPacketPayload { + + 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; + } + } +}