feat: add teammate markers

This commit is contained in:
Octol1ttle 2024-05-06 15:12:23 +05:00
parent 2e288d0738
commit 0c4c307c28
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
16 changed files with 337 additions and 34 deletions

View file

@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.4-SNAPSHOT" apply false
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
}
architectury {

View file

@ -9,10 +9,9 @@ import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.knockdowns.common.api.IKnockableDown;
import ru.octol1ttle.knockdowns.common.events.KnockdownsClientEvents;
import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork;
import ru.octol1ttle.knockdowns.common.network.packets.RequestStartRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.StopRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.reviving.RequestStartRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.reviving.StopRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.registries.KnockdownsSoundEvents;
import ru.octol1ttle.knockdowns.common.registries.KnockedDownSoundInstance;
@ -21,7 +20,6 @@ public class KnockdownsClient {
public static Entity reviving;
public static void init() {
KnockdownsClientEvents.registerCallbacks();
}
public static void playKnockedDownSound(Vec3d pos) {

View file

@ -0,0 +1,5 @@
package ru.octol1ttle.knockdowns.common.api;
import net.minecraft.util.math.Vec3d;
public record RemotePlayer(Vec3d eyePosition, boolean knockedDown) { }

View file

@ -1,44 +1,130 @@
package ru.octol1ttle.knockdowns.common.events;
import dev.architectury.event.events.client.ClientGuiEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import net.minecraft.SharedConstants;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.PlayerSkinDrawer;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import ru.octol1ttle.knockdowns.common.KnockdownsClient;
import ru.octol1ttle.knockdowns.common.api.IKnockableDown;
import ru.octol1ttle.knockdowns.common.api.RemotePlayer;
import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork;
import ru.octol1ttle.knockdowns.common.network.packets.position.RequestRemotePlayerC2SPacket;
import ru.octol1ttle.knockdowns.common.util.RendererUtilsCopy;
import ru.octol1ttle.knockdowns.common.util.ScreenSpaceTransformResult;
public class KnockdownsClientEvents {
public static void registerCallbacks() {
registerOnHudRender();
public static void onHudRender(DrawContext context) {
renderReviveText(context);
renderPlayerIcons(context);
}
private static void registerOnHudRender() {
ClientGuiEvent.RENDER_HUD.register((drawContext, tickDelta) -> {
IKnockableDown reviving = (IKnockableDown) KnockdownsClient.reviving;
MinecraftClient client = MinecraftClient.getInstance();
if (reviving == null) {
reviving = (IKnockableDown) client.player;
if (reviving == null || reviving.get_ReviverCount() == 0) {
return;
private static void renderReviveText(DrawContext context) {
IKnockableDown reviving = (IKnockableDown) KnockdownsClient.reviving;
MinecraftClient client = MinecraftClient.getInstance();
if (reviving == null) {
reviving = (IKnockableDown) client.player;
if (reviving == null || reviving.get_ReviverCount() == 0) {
return;
}
}
TextRenderer renderer = client.textRenderer;
String timerText = String.format("%.1f", reviving.get_ReviveTimer() / (float) SharedConstants.TICKS_PER_SECOND);
int timerX = (context.getScaledWindowWidth() - renderer.getWidth(timerText)) / 2;
int reviverCount = reviving.get_ReviverCount();
Integer color = reviverCount > 1 ? Formatting.GREEN.getColorValue() : Formatting.WHITE.getColorValue();
String reviverCountText = "x" + reviverCount;
int reviveCountX = (context.getScaledWindowWidth() - renderer.getWidth(reviverCountText)) / 2;
if (color != null) {
context.drawTextWithShadow(renderer, timerText, timerX, context.getScaledWindowHeight() / 2 + 5, color);
context.drawTextWithShadow(renderer, reviverCountText, reviveCountX, context.getScaledWindowHeight() / 2 + 14, color);
}
}
public static final Map<UUID, Optional<RemotePlayer>> remotePlayers = new HashMap<>();
private static final Identifier KNOCKED_ICON_ID = new Identifier("knockdowns", "textures/knocked_icon.png");
@SuppressWarnings("DataFlowIssue")
private static void renderPlayerIcons(DrawContext context) {
MinecraftClient client = MinecraftClient.getInstance();
Collection<PlayerListEntry> entries = client.player.networkHandler.getListedPlayerListEntries();
for (PlayerListEntry entry : entries) {
UUID id = entry.getProfile().getId();
PlayerEntity player = client.world.getPlayerByUuid(id);
if (client.player.equals(player)) {
continue;
}
Vec3d eyePosition;
boolean knockedDown;
if (player != null) {
remotePlayers.remove(id);
eyePosition = player.getEyePos();
knockedDown = ((IKnockableDown) player).is_KnockedDown();
} else {
Optional<RemotePlayer> remote = remotePlayers.get(id);
if (remote != null) {
if (remote.isEmpty()) {
continue;
}
eyePosition = remote.get().eyePosition();
knockedDown = remote.get().knockedDown();
} else {
remotePlayers.put(id, Optional.empty());
KnockdownsNetwork.sendToServer(new RequestRemotePlayerC2SPacket(id));
continue;
}
}
TextRenderer renderer = client.textRenderer;
ScreenSpaceTransformResult result = RendererUtilsCopy.worldSpaceToScreenSpace(eyePosition);
int size = 16;
String timerText = String.format("%.1f", reviving.get_ReviveTimer() / (float) SharedConstants.TICKS_PER_SECOND);
int timerX = (drawContext.getScaledWindowWidth() - renderer.getWidth(timerText)) / 2;
int width = context.getScaledWindowWidth();
int x = MathHelper.clamp(MathHelper.floor(result.vec().x - size * 0.5), size + 5, width - size - 5);
int reviverCount = reviving.get_ReviverCount();
Integer color = reviverCount > 1 ? Formatting.GREEN.getColorValue() : Formatting.WHITE.getColorValue();
int height = context.getScaledWindowHeight();
int y = MathHelper.clamp(MathHelper.floor(result.vec().y - size * 0.5), size + 5, height - size - 5);
String reviverCountText = "x" + reviverCount;
int reviveCountX = (drawContext.getScaledWindowWidth() - renderer.getWidth(reviverCountText)) / 2;
if (color != null) {
drawContext.drawTextWithShadow(renderer, timerText, timerX, drawContext.getScaledWindowHeight() / 2 + 5, color);
drawContext.drawTextWithShadow(renderer, reviverCountText, reviveCountX, drawContext.getScaledWindowHeight() / 2 + 14, color);
if (result.type() != ScreenSpaceTransformResult.ScreenSpaceTransformType.ON_SCREEN
|| client.player.getEyePos().distanceTo(eyePosition) > 64.0
|| client.world
.raycast(new RaycastContext(
client.player.getEyePos(),
eyePosition,
RaycastContext.ShapeType.VISUAL,
RaycastContext.FluidHandling.SOURCE_ONLY,
client.player
)).getType() == HitResult.Type.BLOCK)
{
PlayerSkinDrawer.draw(context, entry.getSkinTexture(), x, y, size, true, false);
}
});
if (knockedDown) {
context.drawTexture(KNOCKED_ICON_ID, x, y, size, size, 0, 0, 18, 18, 18, 18);
}
if (client.player.age % 20 == 0 && remotePlayers.containsKey(id)) {
KnockdownsNetwork.sendToServer(new RequestRemotePlayerC2SPacket(id));
}
}
}
}

View file

@ -0,0 +1,21 @@
package ru.octol1ttle.knockdowns.common.mixin.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.octol1ttle.knockdowns.common.util.RendererUtilsCopy;
@Mixin(GameRenderer.class)
public abstract class GameRendererMixin {
@Inject(at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/GameRenderer;renderHand:Z", opcode = Opcodes.GETFIELD, ordinal = 0), method = "renderWorld")
void renderer_postWorldRender(float tickDelta, long limitTime, MatrixStack matrix, CallbackInfo ci) {
RendererUtilsCopy.lastProjMat.set(RenderSystem.getProjectionMatrix());
RendererUtilsCopy.lastModMat.set(RenderSystem.getModelViewMatrix());
RendererUtilsCopy.lastWorldSpaceMatrix.set(matrix.peek().getPositionMatrix());
}
}

View file

@ -0,0 +1,17 @@
package ru.octol1ttle.knockdowns.common.mixin.client;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.octol1ttle.knockdowns.common.events.KnockdownsClientEvents;
@Mixin(InGameHud.class)
public abstract class InGameHudMixin {
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;getCurrentGameMode()Lnet/minecraft/world/GameMode;", ordinal = 0))
public void render(DrawContext drawContext, float tickDelta, CallbackInfo callbackInfo) {
KnockdownsClientEvents.onHudRender(drawContext);
}
}

View file

@ -9,15 +9,21 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import ru.octol1ttle.knockdowns.common.KnockdownsCommon;
import ru.octol1ttle.knockdowns.common.network.packets.PlayKnockedDownSoundS2CPacket;
import ru.octol1ttle.knockdowns.common.network.packets.RequestStartRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.StopRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.position.RemotePlayerS2CPacket;
import ru.octol1ttle.knockdowns.common.network.packets.position.RequestRemotePlayerC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.reviving.RequestStartRevivingC2SPacket;
import ru.octol1ttle.knockdowns.common.network.packets.reviving.StopRevivingC2SPacket;
public class KnockdownsNetwork {
private static final NetworkChannel CHANNEL = NetworkChannel.create(new Identifier(KnockdownsCommon.MOD_ID, "main"));
public static void registerPackets() {
CHANNEL.register(PlayKnockedDownSoundS2CPacket.class, PlayKnockedDownSoundS2CPacket::encode, PlayKnockedDownSoundS2CPacket::new, PlayKnockedDownSoundS2CPacket::apply);
CHANNEL.register(RequestStartRevivingC2SPacket.class, RequestStartRevivingC2SPacket::encode, RequestStartRevivingC2SPacket::new, RequestStartRevivingC2SPacket::apply);
CHANNEL.register(StopRevivingC2SPacket.class, StopRevivingC2SPacket::encode, StopRevivingC2SPacket::new, StopRevivingC2SPacket::apply);
CHANNEL.register(RequestRemotePlayerC2SPacket.class, RequestRemotePlayerC2SPacket::encode, RequestRemotePlayerC2SPacket::new, RequestRemotePlayerC2SPacket::apply);
CHANNEL.register(RemotePlayerS2CPacket.class, RemotePlayerS2CPacket::encode, RemotePlayerS2CPacket::new, RemotePlayerS2CPacket::apply);
}
public static <T> void sendToServer(T message) {

View file

@ -0,0 +1,42 @@
package ru.octol1ttle.knockdowns.common.network.packets.position;
import dev.architectury.networking.NetworkManager;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.Vec3d;
import ru.octol1ttle.knockdowns.common.api.RemotePlayer;
import ru.octol1ttle.knockdowns.common.events.KnockdownsClientEvents;
import ru.octol1ttle.knockdowns.common.network.packets.KnockdownsPacket;
public class RemotePlayerS2CPacket extends KnockdownsPacket {
private final UUID targetUuid;
private final Vec3d eyePosition;
private final boolean knockedDown;
public RemotePlayerS2CPacket(PacketByteBuf buf) {
this(buf.readUuid(), new Vec3d(buf.readDouble(), buf.readDouble(), buf.readDouble()), buf.readBoolean());
}
public RemotePlayerS2CPacket(UUID targetUuid, Vec3d eyePosition, boolean knockedDown) {
this.targetUuid = targetUuid;
this.eyePosition = eyePosition;
this.knockedDown = knockedDown;
}
@Override
public void encode(PacketByteBuf buf) {
buf.writeUuid(this.targetUuid);
buf.writeDouble(this.eyePosition.x);
buf.writeDouble(this.eyePosition.y);
buf.writeDouble(this.eyePosition.z);
buf.writeBoolean(this.knockedDown);
}
@Override
public void apply(Supplier<NetworkManager.PacketContext> contextSupplier) {
NetworkManager.PacketContext context = contextSupplier.get();
context.queue(() -> KnockdownsClientEvents.remotePlayers.put(targetUuid, Optional.of(new RemotePlayer(this.eyePosition, this.knockedDown))));
}
}

View file

@ -0,0 +1,39 @@
package ru.octol1ttle.knockdowns.common.network.packets.position;
import dev.architectury.networking.NetworkManager;
import java.util.UUID;
import java.util.function.Supplier;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import ru.octol1ttle.knockdowns.common.api.IKnockableDown;
import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork;
import ru.octol1ttle.knockdowns.common.network.packets.KnockdownsPacket;
public class RequestRemotePlayerC2SPacket extends KnockdownsPacket {
private final UUID targetUuid;
public RequestRemotePlayerC2SPacket(PacketByteBuf buf) {
this(buf.readUuid());
}
public RequestRemotePlayerC2SPacket(UUID targetUuid) {
this.targetUuid = targetUuid;
}
@Override
public void encode(PacketByteBuf buf) {
buf.writeUuid(this.targetUuid);
}
@Override
public void apply(Supplier<NetworkManager.PacketContext> contextSupplier) {
NetworkManager.PacketContext context = contextSupplier.get();
context.queue(() -> {
PlayerEntity sender = context.getPlayer();
PlayerEntity target = sender.getEntityWorld().getPlayerByUuid(targetUuid);
if (target != null) {
KnockdownsNetwork.sendToPlayer(context.getPlayer(), new RemotePlayerS2CPacket(targetUuid, target.getEyePos(), ((IKnockableDown)target).is_KnockedDown()));
}
});
}
}

View file

@ -1,4 +1,4 @@
package ru.octol1ttle.knockdowns.common.network.packets;
package ru.octol1ttle.knockdowns.common.network.packets.reviving;
import dev.architectury.networking.NetworkManager;
import java.util.UUID;
@ -6,6 +6,7 @@ import java.util.function.Supplier;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import ru.octol1ttle.knockdowns.common.api.IKnockableDown;
import ru.octol1ttle.knockdowns.common.network.packets.KnockdownsPacket;
public class RequestStartRevivingC2SPacket extends KnockdownsPacket {
private final UUID targetUuid;

View file

@ -1,4 +1,4 @@
package ru.octol1ttle.knockdowns.common.network.packets;
package ru.octol1ttle.knockdowns.common.network.packets.reviving;
import dev.architectury.networking.NetworkManager;
import java.util.UUID;
@ -6,6 +6,7 @@ import java.util.function.Supplier;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import ru.octol1ttle.knockdowns.common.api.IKnockableDown;
import ru.octol1ttle.knockdowns.common.network.packets.KnockdownsPacket;
public class StopRevivingC2SPacket extends KnockdownsPacket {
private final UUID targetUuid;

View file

@ -0,0 +1,75 @@
package ru.octol1ttle.knockdowns.common.util;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;
import org.joml.Vector2d;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.opengl.GL11;
/**
* @author 0x3C50
* <p>Utils for rendering in minecraft</p>
*/
public class RendererUtilsCopy {
@ApiStatus.Internal
public static final Matrix4f lastProjMat = new Matrix4f();
@ApiStatus.Internal
public static final Matrix4f lastModMat = new Matrix4f();
@ApiStatus.Internal
public static final Matrix4f lastWorldSpaceMatrix = new Matrix4f();
private static final MinecraftClient client = MinecraftClient.getInstance();
@Contract(value = "_ -> new", pure = true)
public static ScreenSpaceTransformResult worldSpaceToScreenSpace(@NotNull Vec3d pos) {
Camera camera = client.getEntityRenderDispatcher().camera;
double deltaX = pos.x - camera.getPos().x;
double deltaY = pos.y - camera.getPos().y;
double deltaZ = pos.z - camera.getPos().z;
Vector4f transformedCoordinates = new Vector4f((float) deltaX, (float) deltaY, (float) deltaZ, 1.f).mul(
lastWorldSpaceMatrix);
Matrix4f matrixProj = new Matrix4f(lastProjMat);
Vector4f clip = matrixProj.transform(new Vector4f(transformedCoordinates));
if (clip.z > 0 && Math.abs(clip.x) < Math.abs(clip.w) && Math.abs(clip.y) < Math.abs(clip.w)) {
int[] viewport = new int[4];
GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
Matrix4f matrixModel = new Matrix4f(lastModMat);
Vector3f target = new Vector3f();
matrixProj
.mul(matrixModel)
.project(transformedCoordinates.x(), transformedCoordinates.y(), transformedCoordinates.z(), viewport, target);
return new ScreenSpaceTransformResult(
new Vector2d(
target.x / client.getWindow().getScaleFactor(),
(client.getWindow().getHeight() - target.y) / client.getWindow().getScaleFactor()
),
ScreenSpaceTransformResult.ScreenSpaceTransformType.ON_SCREEN
);
}
clip.normalize();
double angle = Math.atan2(clip.y, clip.x);
double width = client.getWindow().getScaledWidth();
double height = client.getWindow().getScaledHeight();
double x = MathHelper.clamp(Math.cos(angle) * width + width / 2, 0.0f, width);
double y = height - (MathHelper.clamp(Math.sin(angle) * height + height / 2, 0.0f, height));
return new ScreenSpaceTransformResult(
new Vector2d(x, y),
ScreenSpaceTransformResult.ScreenSpaceTransformType.STUCK_TO_EDGES
);
}
}

View file

@ -0,0 +1,10 @@
package ru.octol1ttle.knockdowns.common.util;
import org.joml.Vector2d;
public record ScreenSpaceTransformResult(Vector2d vec, ScreenSpaceTransformType type) {
public enum ScreenSpaceTransformType {
ON_SCREEN,
STUCK_TO_EDGES
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

View file

@ -4,7 +4,9 @@
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"client": [
"client.ClientPlayerEntityMixin"
"client.ClientPlayerEntityMixin",
"client.GameRendererMixin",
"client.InGameHudMixin"
],
"mixins": [
"LivingEntityMixin",

View file

@ -2,7 +2,7 @@ pluginManagement {
repositories {
maven { url "https://maven.fabricmc.net/" }
maven { url "https://maven.architectury.dev/" }
maven { url "https://maven.minecraftforge.net/" }
maven { url "https://maven.neoforged.net/releases" }
gradlePluginPortal()
}
}