Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 181
src/client/java/com/kodari/vaulthud/client/VaultHudClient.java
package com.kodari.vaulthud.client;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.rendertype.RenderTypes;
import net.minecraft.client.renderer.state.level.BlockOutlineRenderState;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.VaultBlock;
import net.minecraft.world.level.block.entity.vault.VaultBlockEntity;
import net.minecraft.world.level.block.entity.vault.VaultState;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.lwjgl.glfw.GLFW;
public final class VaultHudClient implements ClientModInitializer {
private static KeyMapping toggleKey;
private static boolean enabled = true;
private static BlockPos targetVaultPos;
private static Component displayedItemName;
@Override
public void onInitializeClient() {
toggleKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.vaulthud.toggle",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_V,
KeyMapping.Category.MISC
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleKey.consumeClick()) {
enabled = !enabled;
}
refreshTarget(client);
});
LevelRenderEvents.BEFORE_BLOCK_OUTLINE.register((context, outline) -> {
if (!enabled || targetVaultPos == null || !targetVaultPos.equals(outline.pos())) {
return true;
}
Vec3 cameraPos = context.levelState().cameraRenderState.pos;
PoseStack poseStack = context.poseStack();
poseStack.pushPose();
poseStack.translate(
targetVaultPos.getX() - cameraPos.x(),
targetVaultPos.getY() - cameraPos.y(),
targetVaultPos.getZ() - cameraPos.z()
);
context.submitNodeCollector().submitShapeOutline(
poseStack,
outline.shape(),
RenderTypes.lines(),
0xFF43E8FF,
2.5F,
true
);
poseStack.popPose();
return false;
});
HudElementRegistry.addLast(
net.minecraft.resources.Identifier.fromNamespaceAndPath("vaulthud", "vault_item"),
(graphics, deltaTracker) -> {
if (enabled && displayedItemName != null) {
graphics.text(Minecraft.getInstance().font, displayedItemName, 8, 8, 0xFFFFFFFF, true);
}
}
);
}
private static void refreshTarget(Minecraft client) {
targetVaultPos = null;
displayedItemName = null;
if (!enabled || client.level == null || client.player == null || !(client.hitResult instanceof BlockHitResult hit)) {
return;
}
BlockPos pos = hit.getBlockPos();
BlockState state = client.level.getBlockState(pos);
if (state.getBlock() != Blocks.VAULT) {
return;
}
double reach = client.player.blockInteractionRange();
if (client.player.getEyePosition().distanceToSqr(hit.getLocation()) > reach * reach) {
return;
}
targetVaultPos = pos;
if (state.getValue(VaultBlock.STATE) == VaultState.ACTIVE
&& client.level.getBlockEntity(pos) instanceof VaultBlockEntity vault
&& vault.getSharedData().hasDisplayItem()) {
displayedItemName = vault.getSharedData().getDisplayItem().getHoverName();
}
}
}