EntityOwner module

EntityOwner: Minecraft 26.3 cheat module, full source for every implementation (2 clients: OfflineClient, Nicotine).

2 implementationsRENDERRender

Clients with this module

Full module source: 2 implementations

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

OfflineClientRENDERsrc/main/java/com/jellypudding/offlineclient/modules/render/EntityOwner.javaJava · 194 lines · 7.3 KB

Shows who owns the projectiles and pets around you.

SettingsScaleRangeProjectilesPetsOwn
EntityOwner.javaJava · Minecraft 26.2 · 194 lines · 7.3 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.render;
2
3
import com.google.gson.JsonObject;
4
import com.google.gson.JsonParser;
5
import com.jellypudding.offlineclient.OfflineClient;
6
import com.jellypudding.offlineclient.event.Subscribe;
7
import com.jellypudding.offlineclient.event.events.Render2DEvent;
8
import com.jellypudding.offlineclient.module.Category;
9
import com.jellypudding.offlineclient.module.Module;
10
import com.jellypudding.offlineclient.util.EntityUtil;
11
import com.jellypudding.offlineclient.render.WorldToScreen;
12
import com.jellypudding.offlineclient.setting.BoolSetting;
13
import com.jellypudding.offlineclient.setting.NumberSetting;
14
import com.jellypudding.offlineclient.util.RenderUtil;
15
import net.minecraft.client.gui.GuiGraphicsExtractor;
16
import net.minecraft.client.multiplayer.PlayerInfo;
17
import net.minecraft.world.entity.Entity;
18
import net.minecraft.world.entity.EntityReference;
19
import net.minecraft.world.entity.LivingEntity;
20
import net.minecraft.world.entity.OwnableEntity;
21
import net.minecraft.world.entity.projectile.Projectile;
22
import net.minecraft.world.phys.Vec3;
23
24
import java.io.IOException;
25
import java.net.URI;
26
import java.net.http.HttpClient;
27
import java.net.http.HttpRequest;
28
import java.net.http.HttpResponse;
29
import java.time.Duration;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.UUID;
34
import java.util.concurrent.ConcurrentHashMap;
35
36
public final class EntityOwner extends Module {
37
38
    private record Label(String name, int color, Vec3 screen, double distance) {
39
    }
40
41
    private static final String SESSION_SERVER = "https://sessionserver.mojang.com/session/minecraft/profile/";
42
    private static final HttpClient HTTP = HttpClient.newHttpClient();
43
44
    // Names fetched for owners who are not online. An empty name is a lookup in flight.
45
    private final Map<UUID, String> names = new ConcurrentHashMap<>();
46
47
    private final NumberSetting scale = new NumberSetting("Scale",
48
        "Size of the labels.", 1, 0.5, 3, 0.1).min(0.1);
49
    private final NumberSetting range = new NumberSetting("Range",
50
        "Furthest entity to label.", 64, 8, 256, 8, " blocks").min(1);
51
    private final BoolSetting projectiles = new BoolSetting("Projectiles",
52
        "Label pearls and arrows and anything else thrown.", true);
53
    private final BoolSetting pets = new BoolSetting("Pets",
54
        "Label tamed animals.", true);
55
    private final BoolSetting self = new BoolSetting("Own",
56
        "Also label what you threw yourself.", false);
57
58
    public EntityOwner() {
59
        super("EntityOwner", "Shows who owns the projectiles and pets around you.", Category.RENDER);
60
        addSettings(scale, range, projectiles, pets, self);
61
        searchTags("pearl", "owner", "tamed");
62
    }
63
64
    @Subscribe
65
    private void onRender2D(Render2DEvent event) {
66
        if (!inGame() || !WorldToScreen.update()) {
67
            return;
68
        }
69
        float partialTicks = event.getPartialTicks();
70
        Vec3 camera = WorldToScreen.cameraPos();
71
        double maxDistance = range.getValue();
72
        List<Label> labels = new ArrayList<>();
73
74
        for (Entity entity : mc.level.entitiesForRendering()) {
75
            String owner = ownerOf(entity);
76
            if (owner == null) {
77
                continue;
78
            }
79
            Vec3 feet = entity.getPosition(partialTicks);
80
            double distance = camera.distanceTo(feet);
81
            if (distance > maxDistance) {
82
                continue;
83
            }
84
            Vec3 head = feet.add(0, entity.getBbHeight() + 0.5, 0);
85
            Vec3 screen = WorldToScreen.project(head);
86
            if (screen != null) {
87
                labels.add(new Label(owner, colorFor(owner), screen, distance));
88
            }
89
        }
90
91
        // Far labels draw first.
92
        labels.sort((a, b) -> Double.compare(b.distance(), a.distance()));
93
        for (Label label : labels) {
94
            draw(event.getContext(), label);
95
        }
96
    }
97
98
    private String ownerOf(Entity entity) {
99
        if (entity instanceof Projectile projectile) {
100
            if (!projectiles.isOn()) {
101
                return null;
102
            }
103
            Entity owner = projectile.getOwner();
104
            if (owner == null) {
105
                return null;
106
            }
107
            if (owner == mc.player && !self.isOn()) {
108
                return null;
109
            }
110
            return owner.getName().getString();
111
        }
112
        if (entity instanceof OwnableEntity ownable) {
113
            if (!pets.isOn()) {
114
                return null;
115
            }
116
            EntityReference<LivingEntity> reference = ownable.getOwnerReference();
117
            if (reference == null) {
118
                return null;
119
            }
120
            LivingEntity owner = EntityReference.getLivingEntity(reference, mc.level);
121
            if (owner != null) {
122
                if (owner == mc.player && !self.isOn()) {
123
                    return null;
124
                }
125
                return owner.getName().getString();
126
            }
127
            return nameFromTabList(reference.getUUID());
128
        }
129
        return null;
130
    }
131
132
    private String nameFromTabList(UUID uuid) {
133
        if (uuid == null || mc.getConnection() == null) {
134
            return null;
135
        }
136
        if (uuid.equals(mc.player.getUUID()) && !self.isOn()) {
137
            return null;
138
        }
139
        PlayerInfo info = mc.getConnection().getPlayerInfo(uuid);
140
        if (info != null) {
141
            return info.getProfile().name();
142
        }
143
        return lookedUp(uuid);
144
    }
145
146
    // The name the session server knows for an offline owner. Fetched once on
147
    // a worker thread and the short id shows until it arrives.
148
    private String lookedUp(UUID uuid) {
149
        String known = names.get(uuid);
150
        if (known != null) {
151
            return known.isEmpty() ? uuid.toString().substring(0, 8) : known;
152
        }
153
        names.put(uuid, "");
154
        Thread.ofVirtual().name("offlineclient owner lookup").start(() -> {
155
            String name = fetchName(uuid);
156
            if (name != null) {
157
                names.put(uuid, name);
158
            }
159
        });
160
        return uuid.toString().substring(0, 8);
161
    }
162
163
    private static String fetchName(UUID uuid) {
164
        try {
165
            HttpRequest request = HttpRequest.newBuilder(URI.create(SESSION_SERVER
166
                + uuid.toString().replace("-", ""))).timeout(Duration.ofSeconds(10)).GET().build();
167
            HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString());
168
            if (response.statusCode() != 200) {
169
                return null;
170
            }
171
            JsonObject json = JsonParser.parseString(response.body()).getAsJsonObject();
172
            return json.has("name") ? json.get("name").getAsString() : null;
173
        } catch (IOException | RuntimeException e) {
174
            return null;
175
        } catch (InterruptedException e) {
176
            Thread.currentThread().interrupt();
177
            return null;
178
        }
179
    }
180
181
    private static int colorFor(String name) {
182
        if (OfflineClient.INSTANCE.getFriendManager().isFriend(name)) {
183
            return EntityUtil.FRIEND_COLOR;
184
        }
185
        return 0xFFFFD060;
186
    }
187
188
    private void draw(GuiGraphicsExtractor context, Label label) {
189
        float factor = (float) (scale.getValue() * Math.clamp(1 - label.distance() / 80.0, 0.5, 1));
190
        RenderUtil.label(context, mc.font, label.screen().x, label.screen().y, factor,
191
            List.of(label.name()), List.of(label.color()));
192
    }
193
}
194
NicotineRendersrc/client/java/nicotine/mod/mods/render/EntityOwner.javaJava · 81 lines · 2.6 KB

Shows the owner of tamed entities

EntityOwner.javaJava · Minecraft 26.3 · 81 lines · 2.6 KB · complete file, verbatimOriginal ↗
1
package nicotine.mod.mods.render;
2
3
import com.mojang.authlib.services.ProfileResult;
4
import net.minecraft.util.CommonColors;
5
import net.minecraft.world.entity.Entity;
6
import net.minecraft.world.entity.TamableAnimal;
7
import nicotine.events.RenderBeforeEvent;
8
import nicotine.events.RenderEvent;
9
import nicotine.mod.Mod;
10
import nicotine.mod.ModCategory;
11
import nicotine.util.EventBus;
12
import nicotine.util.render.Render3D;
13
14
import java.util.HashMap;
15
import java.util.UUID;
16
17
import static nicotine.util.Common.mc;
18
19
public class EntityOwner extends Mod {
20
21
    private static  HashMap<UUID, String> owners = new HashMap<>();
22
    private static boolean runningFetch = false;
23
24
    public EntityOwner() {
25
        super(ModCategory.Render,"EntityOwner", "Shows the owner of tamed entities");
26
    }
27
28
    private static class RunnableFetchProfile implements Runnable {
29
30
        private final UUID ownerUUID;
31
32
        public RunnableFetchProfile(UUID ownerUUID) {
33
            this.ownerUUID = ownerUUID;
34
        }
35
36
        @Override
37
        public void run() {
38
            ProfileResult profileResult = mc.services().sessionService().fetchProfile(ownerUUID, true);
39
40
            String ownerName = profileResult == null ? null : profileResult.profile().name();
41
42
            owners.put(ownerUUID,  ownerName);
43
            runningFetch = false;
44
        }
45
    }
46
47
    @Override
48
    protected void init() {
49
        EventBus.register(RenderBeforeEvent.class, event -> {
50
            if (!this.enabled)
51
                return true;
52
53
            for (Entity entity : mc.level.entitiesForRendering()) {
54
                if (entity instanceof TamableAnimal tamableAnimal) {
55
                    if (tamableAnimal.getOwnerReference() == null || runningFetch) {
56
                        continue;
57
                    }
58
59
                    UUID ownerUUID = tamableAnimal.getOwnerReference().getUUID();
60
61
                    String ownerName = owners.getOrDefault(ownerUUID, "");
62
63
                    if (ownerName == null) {
64
                        ownerName = ownerUUID.toString();
65
                    } else if (ownerName.isEmpty()) {
66
                        runningFetch = true;
67
                        Thread fetchProfileThread = new Thread(new RunnableFetchProfile(ownerUUID));
68
                        fetchProfileThread.start();
69
                        continue;
70
                    }
71
72
                    String text = String.format("Owned by %s",  ownerName);
73
                    Render3D.drawText(event.submitNodeStorage, event.matrixStack, event.camera, entity.position().add(0, 0.3, 0), text, CommonColors.WHITE, 1.0f);
74
                }
75
            }
76
77
            return true;
78
        });
79
    }
80
}
81