9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00
Files
LeavesMC/patches/server/0058-Leaves-carpet-support.patch
2024-08-14 17:07:19 +08:00

133 lines
4.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: violetc <58360096+s-yh-china@users.noreply.github.com>
Date: Tue, 27 Jun 2023 01:54:43 +0800
Subject: [PATCH] Leaves carpet support
diff --git a/src/main/java/org/leavesmc/leaves/protocol/CarpetServerProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/CarpetServerProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..3b6801435790fd892ece3ba7d89499eec29626ff
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/CarpetServerProtocol.java
@@ -0,0 +1,120 @@
+package org.leavesmc.leaves.protocol;
+
+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.Locale;
+import java.util.Map;
+
+@LeavesProtocol(namespace = "carpet")
+public class CarpetServerProtocol {
+
+ public static final String PROTOCOL_ID = "carpet";
+ public static final String VERSION = ProtocolUtils.buildProtocolVersion(PROTOCOL_ID);
+
+ private static final ResourceLocation HELLO_ID = CarpetServerProtocol.id("hello");
+
+ private static final String HI = "69";
+ private static final String HELLO = "420";
+
+ @Contract("_ -> new")
+ public static @NotNull ResourceLocation id(String path) {
+ return new ResourceLocation(PROTOCOL_ID, path);
+ }
+
+ @ProtocolHandler.PlayerJoin
+ public static void onPlayerJoin(ServerPlayer player) {
+ if (LeavesConfig.leavesCarpetSupport) {
+ CompoundTag data = new CompoundTag();
+ data.putString(HI, VERSION);
+ ProtocolUtils.sendPayloadPacket(player, new CarpetPayload(data));
+ }
+ }
+
+ @ProtocolHandler.PayloadReceiver(payload = CarpetPayload.class, payloadId = "hello")
+ private static void handleHello(@NotNull ServerPlayer player, @NotNull CarpetServerProtocol.CarpetPayload payload) {
+ if (LeavesConfig.leavesCarpetSupport) {
+ if (payload.nbt.contains(HELLO)) {
+ LeavesLogger.LOGGER.info("Player " + player.getScoreboardName() + " joined with carpet " + payload.nbt.getString(HELLO));
+ CompoundTag data = new CompoundTag();
+ CarpetRules.write(data);
+ ProtocolUtils.sendPayloadPacket(player, new CarpetPayload(data));
+ }
+ }
+ }
+
+ public static class CarpetRules {
+
+ private static final Map<String, CarpetRule> rules = new HashMap<>();
+
+ public static void write(@NotNull CompoundTag tag) {
+ CompoundTag rulesNbt = new CompoundTag();
+ rules.values().forEach(rule -> rule.writeNBT(rulesNbt));
+
+ tag.put("Rules", rulesNbt);
+ }
+
+ public static void register(CarpetRule rule) {
+ rules.put(rule.name, rule);
+ }
+ }
+
+ public record CarpetRule(String identifier, String name, String value) {
+
+ @NotNull
+ @Contract("_, _, _ -> new")
+ public static CarpetRule of(String identifier, String name, Enum<?> value) {
+ return new CarpetRule(identifier, name, value.name().toLowerCase(Locale.ROOT));
+ }
+
+ @NotNull
+ @Contract("_, _, _ -> new")
+ public static CarpetRule of(String identifier, String name, boolean value) {
+ return new CarpetRule(identifier, name, Boolean.toString(value));
+ }
+
+ public void writeNBT(@NotNull CompoundTag rules) {
+ CompoundTag rule = new CompoundTag();
+ String key = name;
+
+ while (rules.contains(key)) {
+ key = key + "2";
+ }
+
+ rule.putString("Value", value);
+ rule.putString("Manager", identifier);
+ rule.putString("Rule", name);
+ rules.put(key, rule);
+ }
+ }
+
+ public record CarpetPayload(CompoundTag nbt) implements LeavesCustomPayload<CarpetPayload> {
+
+ @New
+ public CarpetPayload(ResourceLocation location, FriendlyByteBuf buf) {
+ this(buf.readNbt());
+ }
+
+ @Override
+ public void write(FriendlyByteBuf buf) {
+ buf.writeNbt(nbt);
+ }
+
+ @Override
+ @NotNull
+ public ResourceLocation id() {
+ return HELLO_ID;
+ }
+ }
+}