add Nametags module
Custom nametags for players and thrown enderpearls projected onto the screen: name (friend colored), distance, totem pop counter fed by the totem entity event, armor and held item icons, and gapple/turtle/strength effect icons from the synced effect particles. Vanilla player nametags are suppressed while the module is on; pearl owners are cached to pearl_owners.json like any other config.
This commit is contained in:
@@ -0,0 +1,414 @@
|
||||
package cc.fros.features.modules.render;
|
||||
|
||||
import cc.fros.Fros;
|
||||
import cc.fros.event.impl.network.PacketEvent;
|
||||
import cc.fros.event.impl.render.Render2DEvent;
|
||||
import cc.fros.event.system.Subscribe;
|
||||
import cc.fros.features.modules.Module;
|
||||
import cc.fros.features.settings.Setting;
|
||||
import cc.fros.util.DamageSyncTracker;
|
||||
import cc.fros.util.render.FrosFont;
|
||||
import cc.fros.util.render.MatrixCapture;
|
||||
import cc.fros.util.traits.Jsonable;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.component.DataComponents;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.entity.projectile.throwableitemprojectile.ThrownEnderpearl;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.alchemy.Potion;
|
||||
import net.minecraft.world.item.alchemy.PotionContents;
|
||||
import net.minecraft.world.item.alchemy.Potions;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class NametagsModule extends Module {
|
||||
|
||||
private final Setting<Page> page = mode("Page", Page.Info);
|
||||
|
||||
private final Setting<Boolean> showPearls = bool("ShowPearls", true);
|
||||
private final Setting<Boolean> showItems = bool("ShowItems", true);
|
||||
private final Setting<Boolean> showArmor = bool("ShowArmor", true);
|
||||
private final Setting<Boolean> showDist = bool("ShowDist", true);
|
||||
private final Setting<Boolean> showPops = bool("ShowPops", true);
|
||||
private final Setting<Boolean> showResistance = bool("ShowResistance", true);
|
||||
private final Setting<Boolean> showStrength = bool("ShowStrength", true);
|
||||
|
||||
private final Setting<Color> nameColor = color("NameColor", 255, 255, 255, 255);
|
||||
private final Setting<Color> friendColor = color("FriendColor", 0, 255, 100, 255);
|
||||
private final Setting<Color> bgColor = color("BgColor", 0, 0, 0, 128);
|
||||
private final Setting<Color> distColor = color("DistColor", 170, 170, 170, 255);
|
||||
private final Setting<Color> popColor = color("PopColor", 255, 80, 80, 255);
|
||||
|
||||
private final Setting<Float> gap = num("Gap", 1.0f, 0.1f, 15.0f);
|
||||
private final Setting<Float> armorGap = num("ArmorGap", 2.0f, 0.0f, 20.0f);
|
||||
private final Setting<Float> scale = num("Scale", 1.0f, 0.1f, 3.0f);
|
||||
private final Setting<Float> minScale = num("MinScale", 0.3f, 0.1f, 1.0f);
|
||||
private final Setting<Boolean> itemsOnArmor = bool("ItemsOnArmor", false);
|
||||
|
||||
private static final byte TOTEM_POP_EVENT_ID = 35;
|
||||
|
||||
private record PearlEntry(String ownerName, long lastSeenMs) {
|
||||
}
|
||||
|
||||
private static final long FIVE_DAYS_MS = 5L * 24 * 60 * 60 * 1000;
|
||||
|
||||
private final Map<UUID, PearlEntry> pearlOwnerCache = new HashMap<>();
|
||||
private final Map<UUID, Integer> totemPops = new ConcurrentHashMap<>();
|
||||
|
||||
private final Jsonable pearlCacheJson = new Jsonable() {
|
||||
@Override
|
||||
public JsonElement toJson() {
|
||||
JsonObject root = new JsonObject();
|
||||
long now = System.currentTimeMillis();
|
||||
for (Map.Entry<UUID, PearlEntry> e : pearlOwnerCache.entrySet()) {
|
||||
if (now - e.getValue().lastSeenMs() <= FIVE_DAYS_MS) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("owner", e.getValue().ownerName());
|
||||
obj.addProperty("lastSeen", e.getValue().lastSeenMs());
|
||||
root.add(e.getKey().toString(), obj);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJson(JsonElement element) {
|
||||
if (element == null || element.isJsonNull()) return;
|
||||
long now = System.currentTimeMillis();
|
||||
for (Map.Entry<String, JsonElement> e : element.getAsJsonObject().entrySet()) {
|
||||
try {
|
||||
JsonObject obj = e.getValue().getAsJsonObject();
|
||||
String owner = obj.get("owner").getAsString();
|
||||
long lastSeen = obj.get("lastSeen").getAsLong();
|
||||
if (now - lastSeen <= FIVE_DAYS_MS) {
|
||||
pearlOwnerCache.put(UUID.fromString(e.getKey()), new PearlEntry(owner, lastSeen));
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return "pearl_owners.json";
|
||||
}
|
||||
};
|
||||
|
||||
static final EquipmentSlot[] ARMOR_SLOTS = {
|
||||
EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET
|
||||
};
|
||||
|
||||
private static final ItemStack TURTLE_MASTER_ICON = makePotionIcon(Potions.TURTLE_MASTER);
|
||||
private static final ItemStack GAPPLE_ICON = new ItemStack(Items.ENCHANTED_GOLDEN_APPLE);
|
||||
private static final ItemStack STRENGTH_ICON = makePotionIcon(Potions.STRENGTH);
|
||||
|
||||
private static ItemStack makePotionIcon(Holder<Potion> potion) {
|
||||
ItemStack stack = new ItemStack(Items.SPLASH_POTION);
|
||||
stack.set(DataComponents.POTION_CONTENTS, new PotionContents(potion));
|
||||
return stack;
|
||||
}
|
||||
|
||||
public NametagsModule() {
|
||||
super("Nametags", "Renders custom nametags above players", Category.RENDER);
|
||||
refreshVisibility();
|
||||
Fros.configManager.addConfig(pearlCacheJson);
|
||||
}
|
||||
|
||||
private void refreshVisibility() {
|
||||
showPearls.setVisibility(v -> on(Page.Info));
|
||||
showItems.setVisibility(v -> on(Page.Info));
|
||||
showArmor.setVisibility(v -> on(Page.Info));
|
||||
showDist.setVisibility(v -> on(Page.Info));
|
||||
showPops.setVisibility(v -> on(Page.Info));
|
||||
showResistance.setVisibility(v -> on(Page.Info));
|
||||
showStrength.setVisibility(v -> on(Page.Info));
|
||||
|
||||
nameColor.setVisibility(v -> on(Page.Colors));
|
||||
friendColor.setVisibility(v -> on(Page.Colors));
|
||||
bgColor.setVisibility(v -> on(Page.Colors));
|
||||
distColor.setVisibility(v -> on(Page.Colors));
|
||||
popColor.setVisibility(v -> on(Page.Colors));
|
||||
|
||||
gap.setVisibility(v -> on(Page.Render));
|
||||
armorGap.setVisibility(v -> on(Page.Render));
|
||||
scale.setVisibility(v -> on(Page.Render));
|
||||
minScale.setVisibility(v -> on(Page.Render));
|
||||
itemsOnArmor.setVisibility(v -> on(Page.Render));
|
||||
}
|
||||
|
||||
private boolean on(Page p) {
|
||||
return page.getValue() == p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
totemPops.clear();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPacketReceive(PacketEvent.Receive event) {
|
||||
if (!(event.getPacket() instanceof ClientboundEntityEventPacket packet)) return;
|
||||
if (packet.getEventId() != TOTEM_POP_EVENT_ID) return;
|
||||
if (mc.level == null) return;
|
||||
// runs on the network thread; the pop map is concurrent and the lookup is read-only
|
||||
Entity entity = packet.getEntity(mc.level);
|
||||
if (entity instanceof Player player && player != mc.player) {
|
||||
totemPops.merge(player.getUUID(), 1, Integer::sum);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRender2D(Render2DEvent event) {
|
||||
if (nullCheck()) return;
|
||||
if (MatrixCapture.projection == null) return;
|
||||
|
||||
GuiGraphics graphics = event.getContext();
|
||||
float delta = event.getDelta();
|
||||
|
||||
record RenderJob(double distSq, Runnable draw) {
|
||||
}
|
||||
|
||||
List<RenderJob> jobs = new ArrayList<>();
|
||||
Set<UUID> seen = new HashSet<>();
|
||||
|
||||
for (Player player : mc.level.players()) {
|
||||
if (player == mc.player) continue;
|
||||
if (!player.isAlive()) continue;
|
||||
|
||||
seen.add(player.getUUID());
|
||||
|
||||
double px = player.xo + (player.getX() - player.xo) * delta;
|
||||
double py = player.yo + (player.getY() - player.yo) * delta + player.getBbHeight()
|
||||
+ gap.getValue() * 0.5;
|
||||
double pz = player.zo + (player.getZ() - player.zo) * delta;
|
||||
|
||||
double dist = mc.player.position().distanceTo(player.position());
|
||||
boolean isFriend = Fros.friendManager.isFriend(player);
|
||||
int nameArgb = (isFriend ? friendColor.getValue() : nameColor.getValue()).getRGB();
|
||||
String secondaryStr = showDist.getValue() ? " " + (int) dist + "m" : "";
|
||||
int pops = totemPops.getOrDefault(player.getUUID(), 0);
|
||||
|
||||
Map<EquipmentSlot, ItemStack> armor = new EnumMap<>(EquipmentSlot.class);
|
||||
for (EquipmentSlot slot : ARMOR_SLOTS) {
|
||||
armor.put(slot, player.getItemBySlot(slot));
|
||||
}
|
||||
ItemStack mainHand = player.getMainHandItem();
|
||||
ItemStack offHand = player.getOffhandItem();
|
||||
String name = player.getGameProfile().name();
|
||||
// Lead icons render left-to-right before the name; order here is their left-to-right order,
|
||||
// so Strength sits to the right of any resistance potion icon.
|
||||
List<ItemStack> leadIcons = new ArrayList<>(2);
|
||||
if (showResistance.getValue()) {
|
||||
if (DamageSyncTracker.isTurtleMaster(player)) leadIcons.add(TURTLE_MASTER_ICON);
|
||||
else if (DamageSyncTracker.hasResistance(player)) leadIcons.add(GAPPLE_ICON);
|
||||
}
|
||||
if (showStrength.getValue() && DamageSyncTracker.hasStrength(player)) leadIcons.add(STRENGTH_ICON);
|
||||
|
||||
jobs.add(new RenderJob(dist * dist, () ->
|
||||
renderNametag(graphics, px, py, pz, dist,
|
||||
name, nameArgb, secondaryStr, pops,
|
||||
armor, mainHand, offHand, leadIcons)));
|
||||
}
|
||||
|
||||
// pops of players who died or left are forgotten
|
||||
totemPops.keySet().removeIf(uuid -> !seen.contains(uuid));
|
||||
|
||||
if (showPearls.getValue()) {
|
||||
long now = System.currentTimeMillis();
|
||||
pearlOwnerCache.entrySet().removeIf(e -> now - e.getValue().lastSeenMs() > FIVE_DAYS_MS);
|
||||
|
||||
for (Entity e : mc.level.entitiesForRendering()) {
|
||||
if (!(e instanceof ThrownEnderpearl pearl)) continue;
|
||||
if (pearl.tickCount <= 2) continue;
|
||||
|
||||
UUID uuid = pearl.getUUID();
|
||||
|
||||
if (pearl.getOwner() instanceof Player thrower) {
|
||||
pearlOwnerCache.put(uuid, new PearlEntry(thrower.getGameProfile().name(), now));
|
||||
} else {
|
||||
PearlEntry existing = pearlOwnerCache.get(uuid);
|
||||
if (existing != null) {
|
||||
pearlOwnerCache.put(uuid, new PearlEntry(existing.ownerName(), now));
|
||||
}
|
||||
}
|
||||
|
||||
PearlEntry entry = pearlOwnerCache.get(uuid);
|
||||
if (entry == null) continue;
|
||||
|
||||
double px = pearl.xo + (pearl.getX() - pearl.xo) * delta;
|
||||
double py = pearl.yo + (pearl.getY() - pearl.yo) * delta + pearl.getBbHeight()
|
||||
+ gap.getValue() * 0.5;
|
||||
double pz = pearl.zo + (pearl.getZ() - pearl.zo) * delta;
|
||||
|
||||
double dist = mc.player.position().distanceTo(pearl.position());
|
||||
|
||||
String ownerName = entry.ownerName();
|
||||
boolean isFriend = Fros.friendManager.isFriend(ownerName);
|
||||
int nameArgb = (isFriend ? friendColor.getValue() : nameColor.getValue()).getRGB();
|
||||
|
||||
jobs.add(new RenderJob(dist * dist, () ->
|
||||
renderPearlTag(graphics, px, py, pz, dist, ownerName, nameArgb)));
|
||||
}
|
||||
}
|
||||
|
||||
// far tags first so near ones draw on top
|
||||
jobs.sort(Comparator.comparingDouble(RenderJob::distSq).reversed());
|
||||
for (RenderJob job : jobs) job.draw.run();
|
||||
}
|
||||
|
||||
private void renderPearlTag(GuiGraphics graphics,
|
||||
double wx, double wy, double wz,
|
||||
double dist,
|
||||
String name, int nameArgb) {
|
||||
float[] screen = MatrixCapture.worldToScreen(wx, wy, wz);
|
||||
if (screen == null) return;
|
||||
|
||||
float anchorX = screen[0];
|
||||
float anchorY = screen[1];
|
||||
float s = (float) Math.max(minScale.getValue(), scale.getValue() * 8.0 / (dist + 8.0));
|
||||
|
||||
int nameW = FrosFont.width(name);
|
||||
int halfW = nameW / 2;
|
||||
int textH = FrosFont.lineHeight();
|
||||
int textTopY = -textH;
|
||||
|
||||
graphics.pose().pushMatrix();
|
||||
graphics.pose().translate(anchorX, anchorY);
|
||||
graphics.pose().scale(s, s);
|
||||
|
||||
graphics.fill(-halfW - 2, textTopY - 1, halfW + 2, 1, bgColor.getValue().getRGB());
|
||||
FrosFont.draw(graphics, name, -halfW, textTopY, nameArgb);
|
||||
|
||||
graphics.pose().popMatrix();
|
||||
}
|
||||
|
||||
private void renderNametag(GuiGraphics graphics,
|
||||
double wx, double wy, double wz,
|
||||
double dist,
|
||||
String name, int nameArgb,
|
||||
String secondaryStr,
|
||||
int totemPops,
|
||||
Map<EquipmentSlot, ItemStack> armor,
|
||||
ItemStack mainHand, ItemStack offHand,
|
||||
List<ItemStack> leadIcons) {
|
||||
float[] screen = MatrixCapture.worldToScreen(wx, wy, wz);
|
||||
if (screen == null) return;
|
||||
|
||||
float anchorX = screen[0];
|
||||
float anchorY = screen[1];
|
||||
|
||||
float s = (float) Math.max(minScale.getValue(), scale.getValue() * 8.0 / (dist + 8.0));
|
||||
|
||||
int nameW = FrosFont.width(name);
|
||||
int secondaryW = FrosFont.width(secondaryStr);
|
||||
|
||||
String popsStr = (showPops.getValue() && totemPops > 0) ? " -" + totemPops : "";
|
||||
int popsW = FrosFont.width(popsStr);
|
||||
|
||||
int textH = FrosFont.lineHeight();
|
||||
int textTopY = -textH;
|
||||
|
||||
int iconCount = (leadIcons == null) ? 0 : leadIcons.size();
|
||||
int iconSize = iconCount > 0 ? textH : 0;
|
||||
int iconGap = 1;
|
||||
int leadW = iconCount * (iconSize + iconGap);
|
||||
|
||||
int totalW = leadW + nameW + secondaryW + popsW;
|
||||
int halfW = totalW / 2;
|
||||
int nameX = -halfW + leadW;
|
||||
|
||||
graphics.pose().pushMatrix();
|
||||
graphics.pose().translate(anchorX, anchorY);
|
||||
graphics.pose().scale(s, s);
|
||||
|
||||
graphics.fill(-halfW - 2, textTopY - 1, halfW + 2, 1, bgColor.getValue().getRGB());
|
||||
|
||||
if (iconCount > 0) {
|
||||
float itemScale = iconSize / 16.0f;
|
||||
int iconY = textTopY + (textH - iconSize) / 2;
|
||||
for (int k = 0; k < iconCount; k++) {
|
||||
graphics.pose().pushMatrix();
|
||||
graphics.pose().translate(-halfW + k * (iconSize + iconGap), iconY);
|
||||
graphics.pose().scale(itemScale, itemScale);
|
||||
graphics.renderItem(leadIcons.get(k), 0, 0);
|
||||
graphics.pose().popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
FrosFont.draw(graphics, name, nameX, textTopY, nameArgb);
|
||||
if (!secondaryStr.isEmpty()) {
|
||||
FrosFont.draw(graphics, secondaryStr, nameX + nameW, textTopY, distColor.getValue().getRGB());
|
||||
}
|
||||
if (!popsStr.isEmpty()) {
|
||||
FrosFont.draw(graphics, popsStr, nameX + nameW + secondaryW, textTopY, popColor.getValue().getRGB());
|
||||
}
|
||||
|
||||
if (showArmor.getValue()) {
|
||||
boolean hasArmor = false;
|
||||
for (EquipmentSlot slot : ARMOR_SLOTS) {
|
||||
if (!armor.get(slot).isEmpty()) {
|
||||
hasArmor = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasArmor) {
|
||||
int slotSize = 16;
|
||||
int armorTopY = textTopY - 1 - armorGap.getValue().intValue() - slotSize;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
ItemStack armorStack = armor.get(ARMOR_SLOTS[i]);
|
||||
int slotX = -(slotSize * 4) / 2 + i * slotSize;
|
||||
graphics.pose().pushMatrix();
|
||||
graphics.pose().translate(slotX, armorTopY);
|
||||
graphics.renderItem(armorStack, 0, 0);
|
||||
graphics.renderItemDecorations(mc.font, armorStack, 0, 0);
|
||||
graphics.pose().popMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showItems.getValue()) {
|
||||
int slotSize = 16;
|
||||
int itemY, offX, mainX;
|
||||
if (itemsOnArmor.getValue()) {
|
||||
itemY = textTopY - 1 - armorGap.getValue().intValue() - slotSize;
|
||||
offX = -(slotSize * 4) / 2 - slotSize;
|
||||
mainX = (slotSize * 4) / 2;
|
||||
} else {
|
||||
itemY = textTopY + textH / 2 - slotSize / 2;
|
||||
offX = -halfW - 2 - slotSize;
|
||||
mainX = halfW + 2;
|
||||
}
|
||||
if (!offHand.isEmpty()) {
|
||||
graphics.renderItem(offHand, offX, itemY);
|
||||
graphics.renderItemDecorations(mc.font, offHand, offX, itemY);
|
||||
}
|
||||
if (!mainHand.isEmpty()) {
|
||||
graphics.renderItem(mainHand, mainX, itemY);
|
||||
graphics.renderItemDecorations(mc.font, mainHand, mainX, itemY);
|
||||
}
|
||||
}
|
||||
|
||||
graphics.pose().popMatrix();
|
||||
}
|
||||
|
||||
public enum Page {
|
||||
Info, Colors, Render
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import cc.fros.features.modules.player.NoFallModule;
|
||||
import cc.fros.features.modules.player.VelocityModule;
|
||||
import cc.fros.features.modules.render.BlockHighlightModule;
|
||||
import cc.fros.features.modules.render.FullbrightModule;
|
||||
import cc.fros.features.modules.render.NametagsModule;
|
||||
import cc.fros.util.traits.Jsonable;
|
||||
import cc.fros.util.traits.Util;
|
||||
import org.slf4j.Logger;
|
||||
@@ -75,6 +76,7 @@ public class ModuleManager implements Jsonable, Util {
|
||||
register(new VelocityModule());
|
||||
register(new BlockHighlightModule());
|
||||
register(new FullbrightModule());
|
||||
register(new NametagsModule());
|
||||
register(new NoFallModule());
|
||||
register(new KeyPearlModule());
|
||||
register(new KillAuraModule());
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package cc.fros.mixin.render;
|
||||
|
||||
import cc.fros.Fros;
|
||||
import cc.fros.features.modules.render.NametagsModule;
|
||||
import cc.fros.util.models.Angles;
|
||||
import net.minecraft.client.renderer.SubmitNodeCollector;
|
||||
import net.minecraft.client.renderer.entity.player.AvatarRenderer;
|
||||
import net.minecraft.client.renderer.entity.state.AvatarRenderState;
|
||||
import net.minecraft.client.renderer.state.CameraRenderState;
|
||||
import net.minecraft.world.entity.Avatar;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
@@ -23,4 +27,13 @@ public class MixinAvatarRenderer {
|
||||
state.yRot = 0;
|
||||
state.bodyRot = lerped.yRot();
|
||||
}
|
||||
|
||||
@Inject(method = "submitNameTag(Lnet/minecraft/client/renderer/entity/state/AvatarRenderState;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/SubmitNodeCollector;Lnet/minecraft/client/renderer/state/CameraRenderState;)V",
|
||||
at = @At("HEAD"), cancellable = true)
|
||||
private void cancelVanillaNameTag(AvatarRenderState state, PoseStack poseStack, SubmitNodeCollector submitNodeCollector, CameraRenderState cameraRenderState, CallbackInfo ci) {
|
||||
NametagsModule nametags = Fros.moduleManager.getModuleByClass(NametagsModule.class);
|
||||
if (nametags != null && nametags.isEnabled()) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user