Skip to content

Commit

Permalink
Add show-armor-hud option to emulate armor hud in 1.8->1.9
Browse files Browse the repository at this point in the history
Fixes #152
  • Loading branch information
FlorianMichael committed Jun 26, 2024
1 parent 67d33c6 commit 435ab07
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 16 deletions.
14 changes: 7 additions & 7 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ rootProject.name = "ViaFabric"

include("viafabric-mc189")
include("viafabric-mc1122")
include("viafabric-mc1144")
include("viafabric-mc1152")
include("viafabric-mc1165")
include("viafabric-mc1171")
include("viafabric-mc1182")
include("viafabric-mc1194")
include("viafabric-mc1201")
//include("viafabric-mc1144")
//include("viafabric-mc1152")
//include("viafabric-mc1165")
//include("viafabric-mc1171")
//include("viafabric-mc1182")
//include("viafabric-mc1194")
//include("viafabric-mc1201")
include("viafabric-mc1204")
include("viafabric-mc1206")
include("viafabric-mc121")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class VFConfig extends Config {
public static final String CLIENT_SIDE_FORCE_DISABLE = "client-side-force-disable";
public static final String HIDE_BUTTON = "hide-button";
public static final String IGNORE_REGISTRY_SYNC_ERRORS = "ignore-registry-sync-errors";
public static final String SHOW_ARMOR_HUD = "show-armor-hud";

public VFConfig(File configFile, Logger logger) {
super(configFile, logger);
Expand Down Expand Up @@ -88,4 +89,8 @@ public boolean isForcedDisable(String line) {
public boolean isIgnoreRegistrySyncErrors() {
return getBoolean(IGNORE_REGISTRY_SYNC_ERRORS, false);
}

public boolean isShowArmorHud() {
return getBoolean(SHOW_ARMOR_HUD, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.viaversion.fabric.common.util;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9;
import com.viaversion.viaversion.protocols.v1_8to1_9.packet.ClientboundPackets1_9;

import java.util.UUID;

// Stolen from https://github.com/ViaVersion/ViaFabricPlus/blob/main/src/main/java/de/florianmichael/viafabricplus/fixes/versioned/visual/ArmorHudEmulation1_8.java
public class ArmorHudEmulation {

private static final UUID ARMOR_POINTS_UUID = UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150");

private static double previousArmorPoints = 0;

public static void sendArmorUpdate(final UserConnection userConnection, final int playerId, final int armorPoints) {
// We only want to update the armor points if they actually changed.
if (armorPoints == previousArmorPoints) {
return;
}
previousArmorPoints = armorPoints;

final PacketWrapper updateAttributes = PacketWrapper.create(ClientboundPackets1_9.UPDATE_ATTRIBUTES, userConnection);
updateAttributes.write(Types.VAR_INT, playerId);
updateAttributes.write(Types.INT, 1);
updateAttributes.write(Types.STRING, "generic.armor");
updateAttributes.write(Types.DOUBLE, 0.0D);
updateAttributes.write(Types.VAR_INT, 1);
updateAttributes.write(Types.UUID, ARMOR_POINTS_UUID);
updateAttributes.write(Types.DOUBLE, (double) armorPoints);
updateAttributes.write(Types.BYTE, (byte) 0);
updateAttributes.scheduleSend(Protocol1_8To1_9.class);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/assets/viafabric/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ hide-button: false
client-side-force-disable: ["hypixel.net", "*.hypixel.net", "minemen.club", "*.minemen.club", "icantjoinlmfao.club"]
# Fabric registry synchronization will be disabled when installed server-side and validation errors are ignored when installed on client-side.
# Note: this setting only works on 1.20.4+ ViaFabric versions, and it might cause issues, use with caution.
ignore-registry-sync-errors: false
ignore-registry-sync-errors: false
# Shows the armor hud on 1.8 servers when enabled. Only works on the client-side.
show-armor-hud: true

This comment has been minimized.

Copy link
@Kichura

Kichura Jul 13, 2024

Member

Leaving this as a note: Should be considered a hard-coded feature as i basically do not see the reasoning why we have to make this a boolean unless issues actually occur from it, Also this currently causes crashes on 1.21 instance. (contains nullified value in specific class section)

Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@
*/
package com.viaversion.fabric.mc121;

import com.viaversion.fabric.common.handler.CommonTransformer;
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
import com.viaversion.fabric.common.util.ArmorHudEmulation;
import com.viaversion.fabric.mc121.gui.ViaConfigScreen;
import com.viaversion.fabric.mc121.mixin.debug.client.MixinClientConnectionAccessor;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.protocols.v1_8to1_9.data.ArmorTypes1_8;
import io.netty.channel.ChannelHandler;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ButtonTextures;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import net.minecraft.item.ItemStack;
import net.minecraft.network.ClientConnection;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class ViaFabricClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
registerGui();
registerArmorHud1_8();
}

private void registerGui() {
Expand All @@ -51,4 +64,33 @@ private void registerGui() {
ViaFabric.JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed");
}
}

private void registerArmorHud1_8() {
ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (!ViaFabric.config.isShowArmorHud()) {
return;
}
final UserConnection connection = getUserConnection();
if (connection.getProtocolInfo().protocolVersion().newerThan(ProtocolVersion.v1_8)) {
return;
}
int armor = 0;
for (final ItemStack stack : MinecraftClient.getInstance().player.getInventory().armor) {
armor += ArmorTypes1_8.findByType(Registries.ITEM.getId(stack.getItem()).toString()).getArmorPoints();
}
ArmorHudEmulation.sendArmorUpdate(connection, MinecraftClient.getInstance().player.getId(), armor);
});
}

public static UserConnection getUserConnection() {
ClientConnection connection = MinecraftClient.getInstance().getNetworkHandler().getConnection();
if (connection == null) {
return null;
}
ChannelHandler viaDecoder = ((MixinClientConnectionAccessor) connection).getChannel().pipeline().get(CommonTransformer.HANDLER_DECODER_NAME);
if (viaDecoder instanceof FabricDecodeHandler) {
return ((FabricDecodeHandler) viaDecoder).getInfo();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
*/
package com.viaversion.fabric.mc121.mixin.debug.client;

import com.viaversion.fabric.common.handler.CommonTransformer;
import com.viaversion.fabric.common.handler.FabricDecodeHandler;
import com.viaversion.fabric.mc121.ViaFabricClient;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import io.netty.channel.ChannelHandler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.DebugHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -39,10 +36,8 @@ public class MixinDebugHud {
protected void getLeftText(CallbackInfoReturnable<List<String>> info) {
String line = "[ViaFabric] I: " + Via.getManager().getConnectionManager().getConnections().size() + " (F: "
+ Via.getManager().getConnectionManager().getConnectedClients().size() + ")";
@SuppressWarnings("ConstantConditions") ChannelHandler viaDecoder = ((MixinClientConnectionAccessor) MinecraftClient.getInstance().getNetworkHandler()
.getConnection()).getChannel().pipeline().get(CommonTransformer.HANDLER_DECODER_NAME);
if (viaDecoder instanceof FabricDecodeHandler) {
UserConnection connection = ((FabricDecodeHandler) viaDecoder).getInfo();
UserConnection connection = ViaFabricClient.getUserConnection();
if (connection != null) {
ProtocolInfo protocol = connection.getProtocolInfo();
if (protocol != null) {
ProtocolVersion serverVer = protocol.serverProtocolVersion();
Expand Down

0 comments on commit 435ab07

Please sign in to comment.