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/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 794dac1e308fc73eae3568daae7f87bb3fe2b7ca..f671a22f032a0f0a72045c62c9e6cadf24cdd4cb 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -14,6 +14,8 @@ import top.leavesmc.leaves.bot.agent.Actions; import top.leavesmc.leaves.profile.LeavesMinecraftSessionService; import top.leavesmc.leaves.protocol.syncmatica.SyncmaticaProtocol; import top.leavesmc.leaves.util.MathUtils; +import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule; +import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules; import java.io.File; import java.lang.reflect.InvocationTargetException; @@ -611,6 +613,8 @@ public final class LeavesConfig { } public static void registerCarpetRules() { + CarpetRules.register(CarpetRule.of("carpet", "creativeNoClip", creativeNoClip)); + CarpetRules.register(CarpetRule.of("pca", "avoidAnvilTooExpensive", avoidAnvilTooExpensive)); } public static boolean creativeNoClip = false; diff --git a/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java new file mode 100644 index 0000000000000000000000000000000000000000..207bbbdf462d12f39273eee1b1552477be35346f --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java @@ -0,0 +1,106 @@ +package top.leavesmc.leaves.protocol; + +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.LeavesLogger; +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; + +@LeavesProtocol(namespace = "carpet") +public class CarpetServerProtocol { + + public static final String PROTOCOL_ID = "carpet"; + public static final String VERSION = "leaves-carpet-1.0.0"; + + 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 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, boolean value) { + return new CarpetRule(identifier, name, Boolean.toString(value)); + } + + public void writeNBT(@NotNull CompoundTag rules) { + CompoundTag rule = new CompoundTag(); + rule.putString("Value", value); + rule.putString("Manager", identifier); + rule.putString("Rule", name); + rules.put(name, rule); + } + } + + public record CarpetPayload(CompoundTag nbt) implements CustomPacketPayload { + + 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; + } + } +}