BreakIndicators module

BreakIndicators: Minecraft 26.3 cheat module, full source for every implementation (1 client: OfflineClient).

1 implementationRENDER

Clients with this module

Full module source: 1 implementation

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

OfflineClientRENDERsrc/main/java/com/jellypudding/offlineclient/modules/render/BreakIndicators.javaJava · 261 lines · 10.0 KB

Shows blocks other players are mining.

SettingsThrough wallsColour by progressStart line colourStart fill colourEnd line colourEnd fill colourColourGrowNamesScaleSelf
BreakIndicators.javaJava · Minecraft 26.2 · 261 lines · 10.0 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.render;
2
3
import com.jellypudding.offlineclient.event.Subscribe;
4
import com.jellypudding.offlineclient.event.events.PacketReceiveEvent;
5
import com.jellypudding.offlineclient.event.events.Render2DEvent;
6
import com.jellypudding.offlineclient.event.events.Render3DEvent;
7
import com.jellypudding.offlineclient.event.events.TickEvent;
8
import com.jellypudding.offlineclient.mixin.MultiPlayerGameModeAccessor;
9
import com.jellypudding.offlineclient.module.Category;
10
import com.jellypudding.offlineclient.module.Module;
11
import com.jellypudding.offlineclient.render.BoxStyle;
12
import com.jellypudding.offlineclient.render.DrawBatch;
13
import com.jellypudding.offlineclient.render.WorldToScreen;
14
import com.jellypudding.offlineclient.setting.BoolSetting;
15
import com.jellypudding.offlineclient.setting.ColorSetting;
16
import com.jellypudding.offlineclient.setting.NumberSetting;
17
import com.jellypudding.offlineclient.util.ColorUtil;
18
import com.jellypudding.offlineclient.util.RenderUtil;
19
import net.minecraft.client.gui.GuiGraphicsExtractor;
20
import net.minecraft.core.BlockPos;
21
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
22
import net.minecraft.world.entity.player.Player;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.phys.AABB;
25
import net.minecraft.world.phys.Vec3;
26
import net.minecraft.world.phys.shapes.VoxelShape;
27
28
import java.lang.ref.WeakReference;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.Queue;
33
import java.util.concurrent.ConcurrentLinkedQueue;
34
35
// The server sends a destruction stage from zero to nine for every block being broken.
36
public final class BreakIndicators extends Module {
37
38
    private record Stage(int breaker, BlockPos pos, float progress) {
39
    }
40
41
    private static final class Indicator {
42
43
        private final BlockPos pos;
44
        private final String name;
45
        private float progress;
46
        private long updated;
47
48
        private Indicator(BlockPos pos, String name, float progress, long updated) {
49
            this.pos = pos;
50
            this.name = name;
51
            this.progress = progress;
52
            this.updated = updated;
53
        }
54
    }
55
56
    private static final int STAGES = 10;
57
    // The server stops sending updates when a player walks off mid break.
58
    private static final long TIMEOUT_MS = 2000;
59
60
    private final BoxStyle style = BoxStyle.shapeOnly(BoxStyle.Shape.BOTH);
61
    private final BoolSetting throughWalls = new BoolSetting("Through walls",
62
        "Show boxes behind blocks.", true);
63
    private final BoolSetting progressColor = new BoolSetting("Colour by progress",
64
        "Blend from the start colours to the end colours as the block gives way.", true);
65
    private final ColorSetting startLine = new ColorSetting("Start line colour",
66
        "Edge colour of a block that has just been hit.", 120, 0.9f, 0.99f, false)
67
        .under(progressColor);
68
    private final ColorSetting startFill = new ColorSetting("Start fill colour",
69
        "Face colour of a block that has just been hit.", 120, 0.9f, 0.99f, false)
70
        .under(progressColor);
71
    private final ColorSetting endLine = new ColorSetting("End line colour",
72
        "Edge colour of a block about to break.", 0, 0.9f, 1f, false)
73
        .under(progressColor);
74
    private final ColorSetting endFill = new ColorSetting("End fill colour",
75
        "Face colour of a block about to break.", 0, 0.9f, 1f, false)
76
        .under(progressColor);
77
    private final ColorSetting color = new ColorSetting("Colour",
78
        "Box colour when progress colouring is off.", 20, false)
79
        .unless(progressColor);
80
    private final BoolSetting grow = new BoolSetting("Grow",
81
        "Start the box small and grow it to full size.", true);
82
    private final BoolSetting names = new BoolSetting("Names",
83
        "Show who is mining above the box.", true);
84
    private final NumberSetting scale = new NumberSetting("Scale",
85
        "Size of the name text.", 1, 0.5, 3, 0.1).min(0.1);
86
    private final BoolSetting self = new BoolSetting("Self",
87
        "Also show blocks you are mining.", false);
88
89
    // Filled on the netty thread and drained on the game thread.
90
    private final Queue<Stage> pending = new ConcurrentLinkedQueue<>();
91
    // The server keys break progress by breaker id.
92
    private final Map<Integer, Indicator> indicators = new HashMap<>();
93
94
    private WeakReference<Level> world;
95
96
    public BreakIndicators() {
97
        super("BreakIndicators", "Shows blocks other players are mining.", Category.RENDER);
98
        addSettings(style.settings());
99
        addSettings(throughWalls, progressColor, startLine, startFill, endLine, endFill, color,
100
            grow, names, scale, self);
101
        searchTags("mining", "break progress", "city");
102
    }
103
104
    @Override
105
    public String getSuffix() {
106
        return count(indicators.size());
107
    }
108
109
    @Override
110
    protected void onEnable() {
111
        clear();
112
    }
113
114
    @Override
115
    protected void onDisable() {
116
        clear();
117
    }
118
119
    private void clear() {
120
        pending.clear();
121
        indicators.clear();
122
    }
123
124
    // Fired on the netty thread.
125
    @Subscribe
126
    private void onPacketReceive(PacketReceiveEvent event) {
127
        if (event.getPacket() instanceof ClientboundBlockDestructionPacket packet) {
128
            pending.add(new Stage(packet.getId(), packet.getPos(), stageFraction(packet.getProgress())));
129
        }
130
    }
131
132
    // Anything outside zero to nine means the job is over.
133
    private static float stageFraction(int stage) {
134
        return stage < 0 || stage >= STAGES ? -1 : (stage + 1) / (float) STAGES;
135
    }
136
137
    @Subscribe
138
    private void onTick(TickEvent event) {
139
        if (world == null || world.get() != mc.level) {
140
            // Block positions and entity ids from the old world mean nothing here.
141
            world = new WeakReference<>(mc.level);
142
            clear();
143
            return;
144
        }
145
        long now = System.currentTimeMillis();
146
        Stage stage;
147
        while ((stage = pending.poll()) != null) {
148
            apply(stage, now);
149
        }
150
        if (self.isOn()) {
151
            applyOwnProgress(now);
152
        }
153
        indicators.values().removeIf(indicator -> now - indicator.updated > TIMEOUT_MS);
154
    }
155
156
    // The server never sends your own break progress back. The client keeps a
157
    // smooth fraction of its own which beats the ten coarse stages.
158
    private void applyOwnProgress(long now) {
159
        if (!inGame() || mc.gameMode == null) {
160
            return;
161
        }
162
        MultiPlayerGameModeAccessor mode = (MultiPlayerGameModeAccessor) mc.gameMode;
163
        BlockPos pos = mode.offlineclient$destroyBlockPos();
164
        if (!mc.gameMode.isDestroying() || pos == null) {
165
            return;
166
        }
167
        apply(new Stage(mc.player.getId(), pos, Math.clamp(mode.offlineclient$destroyProgress(), 0, 1)), now);
168
    }
169
170
    private void apply(Stage stage, long now) {
171
        if (!inGame()) {
172
            return;
173
        }
174
        if (!self.isOn() && stage.breaker() == mc.player.getId()) {
175
            return;
176
        }
177
        if (stage.progress() < 0) {
178
            indicators.remove(stage.breaker());
179
            return;
180
        }
181
        Indicator indicator = indicators.get(stage.breaker());
182
        if (indicator == null || !indicator.pos.equals(stage.pos())) {
183
            indicators.put(stage.breaker(), new Indicator(stage.pos(),
184
                nameOf(stage.breaker()), stage.progress(), now));
185
            return;
186
        }
187
        indicator.progress = stage.progress();
188
        indicator.updated = now;
189
    }
190
191
    private String nameOf(int entityId) {
192
        if (mc.level.getEntity(entityId) instanceof Player player) {
193
            return player.getGameProfile().name();
194
        }
195
        return null;
196
    }
197
198
    @Subscribe
199
    private void onRender3D(Render3DEvent event) {
200
        if (indicators.isEmpty()) {
201
            return;
202
        }
203
        DrawBatch batch = event.getBatch();
204
        boolean through = throughWalls.isOn();
205
        for (Indicator indicator : indicators.values()) {
206
            style.draw(batch, boxOf(indicator), lineColor(indicator), fillColor(indicator), through);
207
        }
208
    }
209
210
    // The block's own outline. A slab or a fence gets a box its size.
211
    private AABB boxOf(Indicator indicator) {
212
        VoxelShape shape = mc.level.getBlockState(indicator.pos).getShape(mc.level, indicator.pos);
213
        AABB full = shape.isEmpty() ? DrawBatch.blockBox(indicator.pos)
214
            : shape.bounds().move(indicator.pos).deflate(DrawBatch.BLOCK_INSET);
215
        if (!grow.isOn()) {
216
            return full;
217
        }
218
        double shrink = (1 - indicator.progress) * 0.5;
219
        return full.deflate(shrink * full.getXsize(), shrink * full.getYsize(), shrink * full.getZsize());
220
    }
221
222
    private int lineColor(Indicator indicator) {
223
        if (!progressColor.isOn()) {
224
            return color.getColor();
225
        }
226
        return ColorUtil.lerp(startLine.getColor(), endLine.getColor(), indicator.progress);
227
    }
228
229
    private int fillColor(Indicator indicator) {
230
        if (!progressColor.isOn()) {
231
            return color.getColor();
232
        }
233
        return ColorUtil.lerp(startFill.getColor(), endFill.getColor(), indicator.progress);
234
    }
235
236
    @Subscribe
237
    private void onRender2D(Render2DEvent event) {
238
        if (!names.isOn() || indicators.isEmpty() || !inGame() || !WorldToScreen.update()) {
239
            return;
240
        }
241
        for (Indicator indicator : indicators.values()) {
242
            if (indicator.name == null) {
243
                continue;
244
            }
245
            AABB box = boxOf(indicator);
246
            Vec3 centre = box.getCenter();
247
            Vec3 top = new Vec3(centre.x, box.maxY + 0.3, centre.z);
248
            Vec3 screen = WorldToScreen.project(top);
249
            if (screen != null) {
250
                drawName(event.getContext(), indicator, screen);
251
            }
252
        }
253
    }
254
255
    private void drawName(GuiGraphicsExtractor context, Indicator indicator, Vec3 screen) {
256
        String text = indicator.name + " " + Math.round(indicator.progress * 100) + "%";
257
        RenderUtil.label(context, mc.font, screen.x, screen.y, scale.getFloat(),
258
            List.of(text), List.of(lineColor(indicator)));
259
    }
260
}
261