Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] backport a lot of stuff #38

Draft
wants to merge 1 commit into
base: 1.19
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/net/foxes4life/foxclient/MainClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.foxes4life.foxclient.configuration.FoxClientSetting;
import net.foxes4life.foxclient.networking.Networking;
import net.foxes4life.foxclient.rpc.DiscordInstance;
import net.foxes4life.foxclient.rpc.DiscordMinecraftClient;
import net.foxes4life.foxclient.rpc.PresenceUpdater;
Expand All @@ -17,15 +16,18 @@
import org.lwjgl.glfw.GLFW;




public class MainClient implements ClientModInitializer {
private static final KeyBinding toggleHud = new KeyBinding("key.foxclient.toggle_hud", GLFW.GLFW_KEY_F6, "category.foxclient.main");
private static final KeyBinding toggleInfoHud = new KeyBinding("key.foxclient.toggle-info-hud", GLFW.GLFW_KEY_F6, "category.foxclient.main");
private static final KeyBinding clientConfig = new KeyBinding("key.foxclient.configKey", GLFW.GLFW_KEY_RIGHT_CONTROL, "category.foxclient.main");
public static long deltaTime = 0;

@Override
public void onInitializeClient() {
ZoomUtils.initZoom();
FreelookUtils.init();
KeyBindingHelper.registerKeyBinding(toggleHud);
KeyBindingHelper.registerKeyBinding(toggleInfoHud);
KeyBindingHelper.registerKeyBinding(clientConfig);

ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
Expand All @@ -36,15 +38,15 @@ public void onInitializeClient() {
});

ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleHud.wasPressed()) {
while (toggleInfoHud.wasPressed()) {
Main.config.set(FoxClientSetting.HudEnabled, !Main.config.get(FoxClientSetting.HudEnabled, Boolean.class));
Main.config.save();
}
});

ClientTickEvents.START_CLIENT_TICK.register(client -> {
while (clientConfig.wasPressed()) {
MinecraftClient.getInstance().setScreen(new FoxClientSettingsScreen());
MinecraftClient.getInstance().setScreen(new FoxClientSettingsScreen(false));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public void initializeDefaults() {

setDefault(FoxClientSetting.ArmorHudEnabled, false);
setDefault(FoxClientSetting.ArmorHudDisplayPercentage, false);
setDefault(FoxClientSetting.BlockHudEnabled, true);
setDefault(FoxClientSetting.BlockHudAnimations, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ public enum FoxClientSetting {
HudBiome,

ArmorHudEnabled,
ArmorHudDisplayPercentage
ArmorHudDisplayPercentage,

BlockHudEnabled,
BlockHudAnimations
}
54 changes: 24 additions & 30 deletions src/main/java/net/foxes4life/foxclient/gui/ArmorHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import net.foxes4life.foxclient.util.draw.Anchor;
import net.foxes4life.foxclient.util.draw.AnchoredBounds;
import net.foxes4life.foxclient.util.draw.Bounds;
import net.foxes4life.foxclient.util.rendering.ItemRender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

public class ArmorHud extends DrawableHelper {
public class ArmorHud {
private final MinecraftClient client;

private final TextRenderer fontRenderer;
Expand All @@ -27,7 +29,7 @@ public ArmorHud(MinecraftClient client) {
Main.LOGGER.info(client.getWindow().getScaledWidth() + " " + client.getWindow().getScaledHeight());
}

public void render(MatrixStack matrices) {
public void render(DrawContext context, ItemRender itemRender,) {
int width = client.getWindow().getScaledWidth();
int height = client.getWindow().getScaledHeight();

Expand All @@ -36,46 +38,38 @@ public void render(MatrixStack matrices) {
ClientPlayerEntity player = client.player;
if (player == null) return;

ItemRenderer itemRenderer = client.getItemRenderer();

renderItem(matrices, player, itemRenderer, EquipmentSlot.MAINHAND, bounds);
renderItem(matrices, player, itemRenderer, EquipmentSlot.OFFHAND, bounds);
renderItem(matrices, player, itemRenderer, EquipmentSlot.HEAD, bounds);
renderItem(matrices, player, itemRenderer, EquipmentSlot.CHEST, bounds);
renderItem(matrices, player, itemRenderer, EquipmentSlot.LEGS, bounds);
renderItem(matrices, player, itemRenderer, EquipmentSlot.FEET, bounds);
renderItem(context, itemRender, player, EquipmentSlot.MAINHAND, bounds);
renderItem(context, itemRender, player, EquipmentSlot.OFFHAND, bounds);
renderItem(context, itemRender, player, EquipmentSlot.HEAD, bounds);
renderItem(context, itemRender, player, EquipmentSlot.CHEST, bounds);
renderItem(context, itemRender, player, EquipmentSlot.LEGS, bounds);
renderItem(context, itemRender, player, EquipmentSlot.FEET, bounds);
}

private void renderItem(MatrixStack matrices, ClientPlayerEntity player, ItemRenderer itemRenderer, EquipmentSlot slot, Bounds bounds) {
private void renderItem(DrawContext context, ItemRender itemRender, ClientPlayerEntity player, EquipmentSlot slot, Bounds bounds) {
int x = bounds.x;
int y = bounds.y;
boolean right = slot == EquipmentSlot.OFFHAND || slot == EquipmentSlot.LEGS || slot == EquipmentSlot.FEET;

switch (slot) {
case MAINHAND:
break;
case OFFHAND:
x += bounds.width - 16;
break;
case HEAD:
y += 20;
break;
case CHEST:
y += 40;
break;
case LEGS:
case MAINHAND -> {
}
case OFFHAND -> x += bounds.width - 16;
case HEAD -> y += 20;
case CHEST -> y += 40;
case LEGS -> {
x += bounds.width - 16;
y += 20;
break;
case FEET:
}
case FEET -> {
x += bounds.width - 16;
y += 40;
break;
}
}

ItemStack stack = player.getEquippedStack(slot);
if (!stack.isEmpty()) {
itemRenderer.renderGuiItemIcon(matrices, stack, x, y);
itemRender.render(x, y, stack);

String text = "";
int color = 0xFFFFFF;
Expand Down Expand Up @@ -116,7 +110,7 @@ private void renderItem(MatrixStack matrices, ClientPlayerEntity player, ItemRen
int countWidth = fontRenderer.getWidth(text);

int x2 = x + (right ? 20 : -4 - countWidth);
fontRenderer.draw(matrices, text, x2, y + 3, color);
context.drawText(fontRenderer, text, x2, y + 3, color, true);
}
}
}
116 changes: 116 additions & 0 deletions src/main/java/net/foxes4life/foxclient/gui/BlockHUD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package net.foxes4life.foxclient.gui;

import net.foxes4life.foxclient.Main;
import net.foxes4life.foxclient.MainClient;
import net.foxes4life.foxclient.configuration.FoxClientSetting;
import net.foxes4life.foxclient.util.TextUtils;
import net.foxes4life.foxclient.util.draw.Anchor;
import net.foxes4life.foxclient.util.draw.AnchoredBounds;
import net.foxes4life.foxclient.util.draw.DrawUtils;
import net.foxes4life.foxclient.util.rendering.ItemRender;
import net.minecraft.block.AirBlock;
import net.minecraft.block.Block;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;

import java.awt.*;

public class BlockHud {
private final MinecraftClient client;
private Block lastBlock;

private float y = 0;
private float width = 0;

public BlockHud(MinecraftClient client) {
this.client = client;
}

public void render(DrawContext context, ItemRender itemRender) {
Block block = getBlock();
boolean show = false;

if (block != null && !(block instanceof AirBlock)) {
lastBlock = block;
show = true;
} else {
block = lastBlock;
}

// if the block is still null, we don't want to render anything
if (block == null) return;

boolean showAnimations = Main.config.get(FoxClientSetting.BlockHudAnimations, Boolean.class);

float delta = MainClient.deltaTime;
// context.drawText(client.textRenderer, "Draw delta: " + (int)delta + "ms", 10, 10, 0xFFFFFF, false);

final int padding = 2;
final int border = 3;
final int height = 16;

final int minY = -height - padding * 2 - border - 2;
int goalY = show ? 0 : minY;

// transition y position
if (showAnimations) {
y = (float) MathHelper.lerp(Math.exp(-0.01 * delta), goalY, y);
} else {
y = goalY;
}

// don't even bother if its off screen
if (y == minY) return;

MutableText text = TextUtils.translatable(block.getTranslationKey());
int w = client.textRenderer.getWidth(text);

// transition the width
if (showAnimations) {
width = (float) MathHelper.lerp(Math.exp(-0.01 * delta), w, width);
} else {
width = w;
}

AnchoredBounds bounds = new AnchoredBounds(0, (int)y, (int)width + 20 + padding * 2, height + padding * 2, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight(), Anchor.TopCenter, Anchor.TopCenter);
Color backgroundColor = new Color(0, 0, 0, .5f);
int backgroundColorInt = backgroundColor.getRGB();

DrawUtils.drawRect(context, bounds, backgroundColorInt);
DrawUtils.drawRect(context, bounds.x - border, bounds.y, border, bounds.height, backgroundColorInt);
DrawUtils.drawRect(context, bounds.x + bounds.width, bounds.y, border, bounds.height, backgroundColorInt);
DrawUtils.drawRect(context, bounds.x, bounds.y + bounds.height, bounds.width, border, backgroundColorInt);

context.drawText(client.textRenderer, text, bounds.x + 20 + padding, bounds.y + 5 + padding, 0xFFFFFF, false);
itemRender.render(bounds.x + padding, bounds.y + padding, new ItemStack(block));
}

private Block getBlock() {
ClientWorld world = client.world;
Entity camera = client.getCameraEntity();
ClientPlayerInteractionManager interactionManager = client.interactionManager;

if (world == null || camera == null || interactionManager == null) return null;

float maxRange = interactionManager.getReachDistance();
float tickDelta = client.getTickDelta();

Vec3d viewVector = camera.getRotationVec(tickDelta);
Vec3d startVector = camera.getCameraPosVec(tickDelta);
Vec3d endVector = startVector.add(viewVector.x * maxRange, viewVector.y * maxRange, viewVector.z * maxRange);

RaycastContext raycastContext = new RaycastContext(startVector, endVector, RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.SOURCE_ONLY, camera);
BlockPos blockPos = world.raycast(raycastContext).getBlockPos();

return world.getBlockState(blockPos).getBlock();
}
}
Loading