|
| 1 | +/* |
| 2 | + * This file is part of Apollo, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2026 Moonsworth |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package com.lunarclient.apollo.module.heightlimit; |
| 25 | + |
| 26 | +import com.lunarclient.apollo.ApolloManager; |
| 27 | +import com.lunarclient.apollo.common.ApolloComponent; |
| 28 | +import com.lunarclient.apollo.event.player.ApolloRegisterPlayerEvent; |
| 29 | +import com.lunarclient.apollo.heightlimit.v1.OverrideHeightLimitMessage; |
| 30 | +import com.lunarclient.apollo.heightlimit.v1.RemoveHeightLimitMessage; |
| 31 | +import com.lunarclient.apollo.heightlimit.v1.ResetHeightLimitsMessage; |
| 32 | +import com.lunarclient.apollo.option.config.Serializer; |
| 33 | +import com.lunarclient.apollo.player.ApolloPlayer; |
| 34 | +import com.lunarclient.apollo.recipients.Recipients; |
| 35 | +import java.lang.reflect.Type; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.List; |
| 38 | +import lombok.NonNull; |
| 39 | +import net.kyori.adventure.text.Component; |
| 40 | +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; |
| 41 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 42 | +import org.spongepowered.configurate.ConfigurationNode; |
| 43 | +import org.spongepowered.configurate.serialize.SerializationException; |
| 44 | +import org.spongepowered.configurate.serialize.TypeSerializer; |
| 45 | + |
| 46 | +import static com.lunarclient.apollo.util.Ranges.checkStrictlyPositive; |
| 47 | + |
| 48 | +/** |
| 49 | + * Provides the height limit module. |
| 50 | + * |
| 51 | + * @since 1.2.9 |
| 52 | + */ |
| 53 | +public final class HeightLimitModuleImpl extends HeightLimitModule implements Serializer { |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new instance of {@link HeightLimitModuleImpl}. |
| 57 | + * |
| 58 | + * @since 1.2.9 |
| 59 | + */ |
| 60 | + public HeightLimitModuleImpl() { |
| 61 | + super(); |
| 62 | + this.serializer(HeightLimit.class, new HeightLimitSerializer()); |
| 63 | + this.handle(ApolloRegisterPlayerEvent.class, this::onPlayerRegister); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void overrideHeightLimit(@NonNull Recipients recipients, @NonNull HeightLimit heightLimit) { |
| 68 | + OverrideHeightLimitMessage.Builder builder = OverrideHeightLimitMessage.newBuilder() |
| 69 | + .setWorld(heightLimit.getWorld()) |
| 70 | + .setLimit(checkStrictlyPositive(heightLimit.getLimit(), "HeightLimit#limit")); |
| 71 | + |
| 72 | + Component displayName = heightLimit.getDisplayName(); |
| 73 | + if (displayName != null) { |
| 74 | + builder.setDisplayNameAdventureJsonLines(ApolloComponent.toJson(displayName)); |
| 75 | + } |
| 76 | + |
| 77 | + OverrideHeightLimitMessage message = builder.build(); |
| 78 | + ApolloManager.getNetworkManager().sendPacket(recipients, message); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void removeHeightLimit(@NonNull Recipients recipients, @NonNull String world) { |
| 83 | + RemoveHeightLimitMessage message = RemoveHeightLimitMessage.newBuilder() |
| 84 | + .setWorld(world) |
| 85 | + .build(); |
| 86 | + |
| 87 | + ApolloManager.getNetworkManager().sendPacket(recipients, message); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void removeHeightLimit(@NonNull Recipients recipients, @NonNull HeightLimit heightLimit) { |
| 92 | + this.removeHeightLimit(recipients, heightLimit.getWorld()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void resetHeightLimits(@NonNull Recipients recipients) { |
| 97 | + ResetHeightLimitsMessage message = ResetHeightLimitsMessage.getDefaultInstance(); |
| 98 | + ApolloManager.getNetworkManager().sendPacket(recipients, message); |
| 99 | + } |
| 100 | + |
| 101 | + private void onPlayerRegister(ApolloRegisterPlayerEvent event) { |
| 102 | + ApolloPlayer player = event.getPlayer(); |
| 103 | + List<HeightLimit> heightLimits = this.getOptions().get(player, HeightLimitModule.DEFAULT_HEIGHT_LIMITS); |
| 104 | + |
| 105 | + if (heightLimits != null) { |
| 106 | + for (HeightLimit heightLimit : heightLimits) { |
| 107 | + this.overrideHeightLimit(player, heightLimit); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static final class HeightLimitSerializer implements TypeSerializer<HeightLimit> { |
| 113 | + |
| 114 | + @Override |
| 115 | + public HeightLimit deserialize(Type type, ConfigurationNode node) throws SerializationException { |
| 116 | + HeightLimit.HeightLimitBuilder builder = HeightLimit.builder() |
| 117 | + .world(this.virtualNode(node, "world").getString()) |
| 118 | + .limit(this.virtualNode(node, "limit").getInt()); |
| 119 | + |
| 120 | + String displayName = node.node("display-name").getString(); |
| 121 | + if (displayName != null) { |
| 122 | + builder.displayName(ApolloComponent.fromLegacy(displayName)); |
| 123 | + } |
| 124 | + |
| 125 | + return builder.build(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void serialize(Type type, @Nullable HeightLimit heightLimit, ConfigurationNode node) throws SerializationException { |
| 130 | + if (heightLimit == null) { |
| 131 | + node.raw(null); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + node.node("world").set(heightLimit.getWorld()); |
| 136 | + node.node("limit").set(heightLimit.getLimit()); |
| 137 | + |
| 138 | + Component displayName = heightLimit.getDisplayName(); |
| 139 | + if (displayName != null) { |
| 140 | + node.node("display-name").set(LegacyComponentSerializer.legacyAmpersand().serialize(displayName)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private ConfigurationNode virtualNode(ConfigurationNode source, Object... path) throws SerializationException { |
| 145 | + if (!source.hasChild(path)) { |
| 146 | + throw new SerializationException("Required field " + Arrays.toString(path) + " not found!"); |
| 147 | + } |
| 148 | + |
| 149 | + return source.node(path); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments