AirPlace module

AirPlace: Minecraft 26.3 cheat module, full source for every implementation (4 clients: MelaHack (ThunderHack fork), OfflineClient, Combatant, …).

4 implementationsPLAYERWORLD

Full module source: 4 implementations

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

MelaHack (ThunderHack fork)PLAYERsrc/main/java/thunder/hack/features/modules/player/AirPlace.javaJava · 71 lines · 3.3 KB

Allows you to place blocks in the air.

SettingsRangeRenderSwingFill ColorLine ColorLine Width
AirPlace.javaJava · Minecraft 26.2 · 71 lines · 3.3 KB · complete file, verbatimOriginal ↗
1
package thunder.hack.features.modules.player;
2
3
import thunder.hack.core.manager.client.ModuleManager;
4
import thunder.hack.features.modules.Module;
5
import thunder.hack.setting.Setting;
6
import thunder.hack.setting.impl.ColorSetting;
7
import thunder.hack.setting.impl.SettingGroup;
8
import thunder.hack.utility.render.Render3DEngine;
9
import com.mojang.blaze3d.vertex.PoseStack;
10
import java.awt.*;
11
import net.minecraft.network.protocol.game.ServerboundSwingPacket;
12
import net.minecraft.world.InteractionHand;
13
import net.minecraft.world.item.BlockItem;
14
import net.minecraft.world.level.block.Blocks;
15
import net.minecraft.world.phys.AABB;
16
import net.minecraft.world.phys.BlockHitResult;
17
import net.minecraft.world.phys.HitResult;
18
19
public class AirPlace extends Module {
20
    public AirPlace() {
21
        super("AirPlace", Category.PLAYER);
22
    }
23
24
    private final Setting<Float> range = new Setting<>("Range", 5f, 0f, 6f);
25
26
    private final Setting<SettingGroup> renderGroup = new Setting<>("Render", new SettingGroup(false, 0));
27
    private final Setting<Boolean> swing = new Setting<>("Swing", true).addToGroup(renderGroup);
28
    private final Setting<ColorSetting> fillColor = new Setting<>("Fill Color", new ColorSetting(new Color(100, 50, 255, 50))).addToGroup(renderGroup);
29
    private final Setting<ColorSetting> lineColor = new Setting<>("Line Color", new ColorSetting(new Color(100, 50, 255, 150))).addToGroup(renderGroup);
30
    private final Setting<Integer> lineWidth = new Setting<>("Line Width", 2, 1, 5).addToGroup(renderGroup);
31
32
    private BlockHitResult hit;
33
    private int cooldown;
34
35
    @Override
36
    public void onUpdate() {
37
        if (cooldown > 0)
38
            cooldown--;
39
40
        HitResult hitResult = mc.getCameraEntity().pick(range.getValue(), 0, false);
41
42
        if (hitResult instanceof BlockHitResult bhr) hit = bhr;
43
        else return;
44
45
        boolean main = mc.player.getMainHandItem().getItem() instanceof BlockItem;
46
        boolean off = mc.player.getOffhandItem().getItem() instanceof BlockItem;
47
        if (mc.options.keyUse.isDown() && (main || off) && cooldown <= 0) {
48
            mc.gameMode.useItemOn(mc.player, main ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND, hit);
49
            if (swing.getValue()) mc.player.swing(main ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND);
50
            else sendPacket(new ServerboundSwingPacket(main ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND));
51
            cooldown = ModuleManager.fastUse.isEnabled() && (ModuleManager.fastUse.blocks.getValue() || ModuleManager.fastUse.all.getValue()) ? 0 : 4;
52
        }
53
    }
54
55
    @Override
56
    public void onRender3D(PoseStack stack) {
57
        if (hit == null || !mc.level.getBlockState(hit.getBlockPos()).getBlock().equals(Blocks.AIR) || (!(mc.player.getMainHandItem().getItem() instanceof BlockItem) && !(mc.player.getOffhandItem().getItem() instanceof BlockItem)))
58
            return;
59
60
        Render3DEngine.FILLED_QUEUE.add(new Render3DEngine.FillAction(
61
                new AABB(hit.getBlockPos()),
62
                fillColor.getValue().getColorObject()
63
        ));
64
        Render3DEngine.OUTLINE_QUEUE.add(new Render3DEngine.OutlineAction(
65
                new AABB(hit.getBlockPos()),
66
                lineColor.getValue().getColorObject(),
67
                lineWidth.getValue()
68
        ));
69
    }
70
}
71
OfflineClientWORLDsrc/main/java/com/jellypudding/offlineclient/modules/world/AirPlace.javaJava · 161 lines · 5.8 KB

Places blocks in mid air where your crosshair points.

SettingsRangeAny itemGuide
AirPlace.javaJava · Minecraft 26.2 · 161 lines · 5.8 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.world;
2
3
import com.jellypudding.offlineclient.event.Subscribe;
4
import com.jellypudding.offlineclient.event.events.Render3DEvent;
5
import com.jellypudding.offlineclient.event.events.RightClickEvent;
6
import com.jellypudding.offlineclient.event.events.TickEvent;
7
import com.jellypudding.offlineclient.module.Category;
8
import com.jellypudding.offlineclient.module.Module;
9
import com.jellypudding.offlineclient.render.BoxStyle;
10
import com.jellypudding.offlineclient.setting.BoolSetting;
11
import com.jellypudding.offlineclient.setting.NumberSetting;
12
import com.jellypudding.offlineclient.util.BlockUtil;
13
import net.minecraft.core.BlockPos;
14
import net.minecraft.core.Direction;
15
import net.minecraft.world.InteractionHand;
16
import net.minecraft.world.InteractionResult;
17
import net.minecraft.world.item.ArmorStandItem;
18
import net.minecraft.world.item.BlockItem;
19
import net.minecraft.world.item.EndCrystalItem;
20
import net.minecraft.world.item.FireChargeItem;
21
import net.minecraft.world.item.FlintAndSteelItem;
22
import net.minecraft.world.item.FireworkRocketItem;
23
import net.minecraft.world.item.Item;
24
import net.minecraft.world.item.ItemStack;
25
import net.minecraft.world.item.SpawnEggItem;
26
import net.minecraft.world.level.Level;
27
import net.minecraft.world.phys.BlockHitResult;
28
import net.minecraft.world.phys.HitResult;
29
import net.minecraft.world.phys.Vec3;
30
31
public final class AirPlace extends Module {
32
33
    private final NumberSetting range = new NumberSetting("Range",
34
        "How far away blocks can be placed. Your normal block reach grows to match whilst on.",
35
        4.5, 1, 6, 0.1).min(0.5);
36
    private final BoolSetting anyItem = new BoolSetting("Any item",
37
        "Use anything you hold on the empty spot and not just blocks.", false);
38
    private final BoolSetting guide = new BoolSetting("Guide",
39
        "Draws a box on the spot the block will land in.", true);
40
    private final BoxStyle style = new BoxStyle(BoxStyle.Shape.BOTH, 0).under(guide);
41
42
    private BlockPos target;
43
44
    public AirPlace() {
45
        super("AirPlace", "Places blocks in mid air where your crosshair points.", Category.WORLD);
46
        addSettings(range, anyItem, guide);
47
        addSettings(style.settings());
48
        searchTags("air place", "midair", "build");
49
    }
50
51
    // Read by LocalPlayerMixin. Blocks placed in the air can be built onto
52
    // from the same distance.
53
    public double getRange() {
54
        return range.getValue();
55
    }
56
57
    @Override
58
    protected void onDisable() {
59
        target = null;
60
    }
61
62
    @Subscribe
63
    private void onTick(TickEvent event) {
64
        target = null;
65
        if (!inGame() || mc.player.isSpectator() || mc.player.isHandsBusy()) {
66
            return;
67
        }
68
        if (heldPlaceableHand() == null) {
69
            return;
70
        }
71
        target = findSpot();
72
    }
73
74
    @Subscribe
75
    private void onRightClick(RightClickEvent event) {
76
        if (!inGame() || mc.player.isSpectator() || mc.gameMode.isDestroying()) {
77
            return;
78
        }
79
        // Swallowing the click whilst the hands are busy would eat a normal use.
80
        if (mc.player.isHandsBusy()) {
81
            return;
82
        }
83
        InteractionHand hand = heldPlaceableHand();
84
        if (hand == null) {
85
            return;
86
        }
87
        BlockPos spot = findSpot();
88
        if (spot == null) {
89
            return;
90
        }
91
        // The game sets this delay itself on a normal click.
92
        mc.rightClickDelay = 4;
93
        event.cancel();
94
95
        // The click lands on the empty spot itself. The server treats a
96
        // replaceable clicked block as the place to put the new one.
97
        Direction face = BlockUtil.facingSide(spot);
98
        BlockHitResult hit = new BlockHitResult(Vec3.atCenterOf(spot), face, spot, false);
99
        InteractionResult result = mc.gameMode.useItemOn(mc.player, hand, hit);
100
        if (result.consumesAction()) {
101
            mc.player.swing(hand);
102
        }
103
    }
104
105
    @Subscribe
106
    private void onRender3D(Render3DEvent event) {
107
        if (target == null || !guide.isOn()) {
108
            return;
109
        }
110
        style.draw(event.getBatch(), target, false);
111
    }
112
113
    private BlockPos findSpot() {
114
        if (mc.hitResult != null && mc.hitResult.getType() != HitResult.Type.MISS) {
115
            return null;
116
        }
117
        HitResult reach = mc.player.pick(range.getValue(), 0, false);
118
        if (reach.getType() != HitResult.Type.MISS || !(reach instanceof BlockHitResult blockHit)) {
119
            return null;
120
        }
121
        BlockPos pos = blockHit.getBlockPos();
122
        if (!Level.isInSpawnableBounds(pos) || !BlockUtil.isReplaceable(pos)) {
123
            return null;
124
        }
125
        if (BlockUtil.intersectsPlayer(pos)) {
126
            return null;
127
        }
128
        return pos;
129
    }
130
131
    // A tool or food in the main hand keeps its normal use.
132
    private InteractionHand heldPlaceableHand() {
133
        ItemStack main = mc.player.getMainHandItem();
134
        if (placeable(main)) {
135
            return InteractionHand.MAIN_HAND;
136
        }
137
        if (main.isEmpty() && placeable(mc.player.getOffhandItem())) {
138
            return InteractionHand.OFF_HAND;
139
        }
140
        return null;
141
    }
142
143
    // Blocks and the few items that stand in a clicked spot on their own.
144
    // A rocket whilst gliding is a boost and not a placement.
145
    private boolean placeable(ItemStack stack) {
146
        Item item = stack.getItem();
147
        if (item instanceof FireworkRocketItem) {
148
            return !mc.player.isFallFlying();
149
        }
150
        if (anyItem.isOn()) {
151
            return !stack.isEmpty();
152
        }
153
        return item instanceof BlockItem
154
            || item instanceof SpawnEggItem
155
            || item instanceof ArmorStandItem
156
            || item instanceof EndCrystalItem
157
            || item instanceof FlintAndSteelItem
158
            || item instanceof FireChargeItem;
159
    }
160
}
161
CombatantPLAYERsrc/main/java/combatant/client/features/module/modules/player/AirPlace.javaJava · 249 lines · 8.9 KB
Settingsairplace_distanceairplace_fill_colorairplace_line_colorairplace_line_width
AirPlace.javaJava · Minecraft 26.2 · 249 lines · 8.9 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of the Combatant Client distribution.
3
 * Copyright (c) 2026 pivosos2007.
4
 *
5
 * Licensed under the GNU General Public License v3.0.
6
 */
7
8
package combatant.client.features.module.modules.player;
9
10
import net.minecraft.client.Minecraft;
11
import net.minecraft.client.player.LocalPlayer;
12
import net.minecraft.core.BlockPos;
13
import net.minecraft.core.Direction;
14
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
15
import net.minecraft.util.Mth;
16
import net.minecraft.world.InteractionHand;
17
import net.minecraft.world.item.BlockItem;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.level.ClipContext;
20
import net.minecraft.world.phys.AABB;
21
import net.minecraft.world.phys.BlockHitResult;
22
import net.minecraft.world.phys.Vec3;
23
import combatant.client.config.values.BindMode;
24
import combatant.client.config.values.NumberValue;
25
import combatant.client.config.values.RGBAColorValue;
26
import combatant.client.features.module.Module;
27
import combatant.client.features.module.ModuleCategory;
28
import combatant.client.features.module.ModuleInfo;
29
import combatant.client.features.module.WorldPhase;
30
import combatant.client.render.engine.RenderState;
31
import combatant.client.render.engine.renderer.Renderer3D;
32
33
//todo Description
34
@ModuleInfo(
35
        id = "airplace",
36
        displayName = "AirPlace",
37
        category = ModuleCategory.PLAYER
38
)
39
public class AirPlace extends Module {
40
41
    private static final String SETTING_DISTANCE = "distance";
42
    private static final String SETTING_FILL_COLOR = "fill_color";
43
    private static final String SETTING_LINE_COLOR = "line_color";
44
    private static final String SETTING_LINE_WIDTH = "line_width";
45
    private final Minecraft mc = Minecraft.getInstance();
46
    private final NumberValue<Float> distanceValue =
47
            num("airplace_distance", SETTING_DISTANCE, 5f, 3f, 6f);
48
    private final RGBAColorValue fillColor =
49
            color("airplace_fill_color", SETTING_FILL_COLOR, "#326432FF");
50
    private final RGBAColorValue lineColor =
51
            color("airplace_line_color", SETTING_LINE_COLOR, "#966432FF");
52
    private final NumberValue<Integer> lineWidth =
53
            num("airplace_line_width", SETTING_LINE_WIDTH, 2, 1, 5);
54
    private BlockPos lastPos;
55
    private boolean lastRayMiss = false;
56
    private int sequence = 0;
57
    private boolean prevUsePressed = false;
58
59
    {
60
        addAction("increase_distance", "KP_ADD", BindMode.PRESS);
61
        addAction("decrease_distance", "KP_SUBTRACT", BindMode.PRESS);
62
    }
63
64
    private static void addFilledBox(Renderer3D renderer, AABB box, int argb) {
65
        int a = (argb >>> 24) & 0xFF;
66
        int r = (argb >>> 16) & 0xFF;
67
        int g = (argb >>> 8) & 0xFF;
68
        int b = argb & 0xFF;
69
70
        double minX = box.minX;
71
        double minY = box.minY;
72
        double minZ = box.minZ;
73
        double maxX = box.maxX;
74
        double maxY = box.maxY;
75
        double maxZ = box.maxZ;
76
77
        renderer.quad(minX, minY, minZ, maxX, minY, minZ, maxX, minY, maxZ, minX, minY, maxZ, r, g, b, a);
78
        renderer.quad(minX, maxY, minZ, minX, maxY, maxZ, maxX, maxY, maxZ, maxX, maxY, minZ, r, g, b, a);
79
        renderer.quad(minX, minY, maxZ, maxX, minY, maxZ, maxX, maxY, maxZ, minX, maxY, maxZ, r, g, b, a);
80
        renderer.quad(minX, minY, minZ, minX, maxY, minZ, maxX, maxY, minZ, maxX, minY, minZ, r, g, b, a);
81
        renderer.quad(maxX, minY, minZ, maxX, maxY, minZ, maxX, maxY, maxZ, maxX, minY, maxZ, r, g, b, a);
82
        renderer.quad(minX, minY, minZ, minX, minY, maxZ, minX, maxY, maxZ, minX, maxY, minZ, r, g, b, a);
83
    }
84
85
    private static void addOutlineBox(Renderer3D renderer, AABB box, int argb) {
86
        int a = (argb >>> 24) & 0xFF;
87
        int r = (argb >>> 16) & 0xFF;
88
        int g = (argb >>> 8) & 0xFF;
89
        int b = argb & 0xFF;
90
91
        double minX = box.minX;
92
        double minY = box.minY;
93
        double minZ = box.minZ;
94
        double maxX = box.maxX;
95
        double maxY = box.maxY;
96
        double maxZ = box.maxZ;
97
98
        renderer.line(minX, minY, minZ, maxX, minY, minZ, r, g, b, a);
99
        renderer.line(maxX, minY, minZ, maxX, minY, maxZ, r, g, b, a);
100
        renderer.line(maxX, minY, maxZ, minX, minY, maxZ, r, g, b, a);
101
        renderer.line(minX, minY, maxZ, minX, minY, minZ, r, g, b, a);
102
103
        renderer.line(minX, maxY, minZ, maxX, maxY, minZ, r, g, b, a);
104
        renderer.line(maxX, maxY, minZ, maxX, maxY, maxZ, r, g, b, a);
105
        renderer.line(maxX, maxY, maxZ, minX, maxY, maxZ, r, g, b, a);
106
        renderer.line(minX, maxY, maxZ, minX, maxY, minZ, r, g, b, a);
107
108
        renderer.line(minX, minY, minZ, minX, maxY, minZ, r, g, b, a);
109
        renderer.line(maxX, minY, minZ, maxX, maxY, minZ, r, g, b, a);
110
        renderer.line(maxX, minY, maxZ, maxX, maxY, maxZ, r, g, b, a);
111
        renderer.line(minX, minY, maxZ, minX, maxY, maxZ, r, g, b, a);
112
    }
113
114
    @Override
115
    public void onTick() {
116
        if (!isEnabled() || mc == null || mc.player == null) return;
117
118
        LocalPlayer player = mc.player;
119
        boolean usePressed = mc.options.keyUse.isDown();
120
121
        handleDistanceBinds();
122
        handleTick(player, usePressed);
123
124
        prevUsePressed = usePressed;
125
    }
126
127
    private void handleTick(LocalPlayer player, boolean useKeyPressed) {
128
        ItemStack stack = player.getMainHandItem();
129
        if (!(stack.getItem() instanceof BlockItem)) {
130
            lastPos = null;
131
            lastRayMiss = false;
132
            return;
133
        }
134
135
        updateTarget(player, distanceValue.get());
136
137
        if (useKeyPressed && !prevUsePressed) {
138
            if (lastRayMiss && lastPos != null) tryPlaceBlock(player);
139
        }
140
    }
141
142
    @Override
143
    public WorldPhase getWorldPhase() {
144
        return WorldPhase.END_MAIN;
145
    }
146
147
    @Override
148
    public void onRenderWorldEngine(Renderer3D renderer, Renderer3D depthRenderer, float tickDelta) {
149
        if (!isEnabled() || mc.level == null || mc.player == null) return;
150
        boolean main = mc.player.getMainHandItem().getItem() instanceof BlockItem;
151
        boolean off = mc.player.getOffhandItem().getItem() instanceof BlockItem;
152
        if (!main && !off) return;
153
        if (!lastRayMiss || lastPos == null) return;
154
        if (!isPlaceableTarget(lastPos)) return;
155
156
        AABB box = new AABB(lastPos);
157
        int fill = fillColor.getArgb();
158
        int line = lineColor.getArgb();
159
160
        float prevWidth = RenderState.lineWidth;
161
        RenderState.lineWidth = Math.max(1.0f, lineWidth.get());
162
        try {
163
            addFilledBox(renderer, box, fill);
164
            addOutlineBox(renderer, box, line);
165
        } finally {
166
            RenderState.lineWidth = prevWidth;
167
        }
168
    }
169
170
    private void updateTarget(LocalPlayer player, double distance) {
171
        Vec3 eye = player.getEyePosition();
172
        Vec3 end = eye.add(player.getViewVector(1f).normalize().scale(distance));
173
174
        BlockHitResult hit = player.level().clip(new ClipContext(
175
                eye, end,
176
                ClipContext.Block.COLLIDER,
177
                ClipContext.Fluid.NONE,
178
                player
179
        ));
180
181
        BlockPos pos;
182
        if (hit.getType() == BlockHitResult.Type.MISS) {
183
            pos = new BlockPos(
184
                    Mth.floor(end.x),
185
                    Mth.floor(end.y),
186
                    Mth.floor(end.z)
187
            );
188
            lastRayMiss = true;
189
        } else {
190
            pos = hit.getBlockPos().relative(hit.getDirection());
191
            lastRayMiss = false;
192
        }
193
194
        lastPos = isPlaceableTarget(pos) ? pos : null;
195
    }
196
197
    private void tryPlaceBlock(LocalPlayer player) {
198
        if (lastPos == null) return;
199
200
        ItemStack stack = player.getItemInHand(InteractionHand.MAIN_HAND);
201
        if (!(stack.getItem() instanceof BlockItem)) return;
202
203
        if (mc.player.connection == null) return;
204
205
        Vec3 hitVec = new Vec3(
206
                lastPos.getX() + 0.5,
207
                lastPos.getY() + 0.5,
208
                lastPos.getZ() + 0.5
209
        );
210
211
        BlockHitResult hit = new BlockHitResult(
212
                hitVec, Direction.UP, lastPos, false
213
        );
214
215
        mc.player.connection.send(
216
                new ServerboundUseItemOnPacket(InteractionHand.MAIN_HAND, hit, sequence++)
217
        );
218
219
        player.swing(InteractionHand.MAIN_HAND);
220
221
    }
222
223
    private boolean isPlaceableTarget(BlockPos pos) {
224
        if (pos == null || mc.level == null) return false;
225
226
        var state = mc.level.getBlockState(pos);
227
        return state.isAir() || state.canBeReplaced() || !state.getFluidState().isEmpty();
228
    }
229
230
    private void handleDistanceBinds() {
231
        double step = 1.0;
232
        boolean changed = false;
233
234
        if (isActionPressedOnce("increase_distance")) {
235
            distanceValue.set(distanceValue.castToType(Math.min(6.0, distanceValue.get() + step)));
236
            changed = true;
237
        }
238
        if (isActionPressedOnce("decrease_distance")) {
239
            distanceValue.set(distanceValue.castToType(Math.max(3.0, distanceValue.get() - step)));
240
            changed = true;
241
        }
242
243
        if (changed) {
244
            saveConfig();
245
        }
246
    }
247
}
248
249
LiquidBounce (Kotlin)WORLDsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/world/ModuleAirPlace.ktKotlin · 162 lines · 5.9 KB

Allows you to place blocks in mid-air.

SettingsOutlineOnlyColorOutlineColorPlaceInLiquid
ModuleAirPlace.ktKotlin · Minecraft 26.2 · 162 lines · 5.9 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
package net.ccbluex.liquidbounce.features.module.modules.world
20
21
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
22
import net.ccbluex.liquidbounce.event.events.PlayerInteractItemEvent
23
import net.ccbluex.liquidbounce.event.events.WorldRenderEvent
24
import net.ccbluex.liquidbounce.event.handler
25
import net.ccbluex.liquidbounce.features.module.ClientModule
26
import net.ccbluex.liquidbounce.features.module.ModuleCategories
27
import net.ccbluex.liquidbounce.features.module.modules.render.cameraclip.ScrollAdjustValueGroup
28
import net.ccbluex.liquidbounce.render.FULL_BOX
29
import net.ccbluex.liquidbounce.render.drawBox
30
import net.ccbluex.liquidbounce.render.engine.type.Color4b
31
import net.ccbluex.liquidbounce.render.renderEnvironment
32
import net.ccbluex.liquidbounce.render.withPositionRelativeToCamera
33
import net.ccbluex.liquidbounce.utils.block.SwingMode
34
import net.ccbluex.liquidbounce.utils.entity.shouldSwingHand
35
import net.ccbluex.liquidbounce.utils.inventory.ArmorItemSlot
36
import net.ccbluex.liquidbounce.utils.item.isConsumable
37
import net.ccbluex.liquidbounce.utils.item.isGlider
38
import net.ccbluex.liquidbounce.utils.math.toBlockPos
39
import net.minecraft.world.InteractionHand
40
import net.minecraft.world.item.ArmorStandItem
41
import net.minecraft.world.item.BlockItem
42
import net.minecraft.world.item.FireworkRocketItem
43
import net.minecraft.world.item.ItemStack
44
import net.minecraft.world.item.SpawnEggItem
45
import net.minecraft.world.phys.BlockHitResult
46
47
/**
48
 * AirPlace module
49
 *
50
 * Allows you to place blocks in midair.
51
 */
52
object ModuleAirPlace : ClientModule("AirPlace", ModuleCategories.WORLD) {
53
54
    private object Preview : ToggleableValueGroup(this, "Preview", true) {
55
        val outlineOnly by boolean("OutlineOnly", false)
56
        val fillColor by color("Color", Color4b(69, 119, 255, 104))
57
        val outlineColor by color("OutlineColor", Color4b.WHITE)
58
    }
59
60
    private val liquidPlace by boolean("PlaceInLiquid", false)
61
62
    private object CustomRange : ToggleableValueGroup(this, "CustomRange", false) {
63
        private val rangeBounds = 1.0f..4.5f
64
        val range = float("Range", 3.0f, rangeBounds)
65
66
        private val scrollAdjust = ScrollAdjustValueGroup(this, "ScrollAdjust", true, { delta ->
67
            val newValue = range.get() + delta
68
            range.set(newValue.coerceIn(rangeBounds))
69
        })
70
71
        init {
72
            tree(scrollAdjust)
73
        }
74
75
    }
76
77
    init {
78
        tree(Preview)
79
        tree(CustomRange)
80
    }
81
82
    private inline val BlockHitResult.isAirOrFluid: Boolean
83
        get() = world.getBlockState(blockPos).isAir ||
84
            (liquidPlace && !world.getFluidState(blockPos).isEmpty && !ModuleLiquidPlace.running)
85
86
87
    private fun ItemStack.isAirPlaceableAt(hit: BlockHitResult): Boolean {
88
        if (isEmpty || isConsumable) return false
89
        return when (val i = item) {
90
            is BlockItem -> i.block.defaultBlockState().canSurvive(world, hit.blockPos)
91
            is SpawnEggItem, is ArmorStandItem -> true
92
            is FireworkRocketItem -> !ArmorItemSlot.CHEST.itemStack.isGlider
93
            else -> false
94
        }
95
    }
96
97
    private fun canPlayerPlaceAt(hit: BlockHitResult): Boolean {
98
        val main = player.mainHandItem
99
        if (main.isAirPlaceableAt(hit)) return true
100
101
        val off = player.offhandItem
102
        return off.isAirPlaceableAt(hit)
103
    }
104
105
106
    private fun getValidHitResult(): BlockHitResult? {
107
        val hitResult = mc.hitResult as? BlockHitResult ?: return null
108
        if (player.isSpectator) return null
109
        if (!hitResult.isAirOrFluid) return null
110
        if (!canPlayerPlaceAt(hitResult)) return null
111
112
        if (CustomRange.running) {
113
            val distance = CustomRange.range.get().toDouble()
114
            val playerEye = player.eyePosition
115
            val direction = hitResult.location.subtract(playerEye).normalize()
116
            val targetPos = playerEye.add(direction.scale(distance))
117
118
            val newHitResult = BlockHitResult(
119
                targetPos,
120
                hitResult.direction,
121
                targetPos.toBlockPos(),
122
                hitResult.isInside
123
            )
124
125
            if (!newHitResult.isAirOrFluid) return null
126
            if (!canPlayerPlaceAt(newHitResult)) return null
127
128
            return newHitResult
129
        }
130
131
        return hitResult
132
    }
133
134
135
    @Suppress("unused")
136
    private val renderHandler = handler<WorldRenderEvent> { event ->
137
        if (!Preview.running) return@handler
138
        val hitResult = getValidHitResult() ?: return@handler
139
140
        event.renderEnvironment {
141
            withPositionRelativeToCamera(hitResult.blockPos) {
142
                drawBox(
143
                    FULL_BOX,
144
                    if (Preview.outlineOnly) Color4b.TRANSPARENT else Preview.fillColor,
145
                    Preview.outlineColor
146
                )
147
            }
148
        }
149
    }
150
151
    @Suppress("unused")
152
    private val placeHandler = handler<PlayerInteractItemEvent> { event ->
153
        val hitResult = getValidHitResult() ?: return@handler
154
155
        val actionResult = interaction.useItemOn(player, event.hand, hitResult)
156
        if (actionResult.shouldSwingHand()) {
157
            SwingMode.DO_NOT_HIDE.swing(event.hand)
158
        }
159
        event.cancelEvent()
160
    }
161
}
162