diff --git a/settings.gradle.kts b/settings.gradle.kts index f8478917..38fb6c85 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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") diff --git a/src/main/java/com/viaversion/fabric/common/config/VFConfig.java b/src/main/java/com/viaversion/fabric/common/config/VFConfig.java index 684a8f45..9343122c 100644 --- a/src/main/java/com/viaversion/fabric/common/config/VFConfig.java +++ b/src/main/java/com/viaversion/fabric/common/config/VFConfig.java @@ -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); @@ -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); + } } diff --git a/src/main/java/com/viaversion/fabric/common/util/ArmorHudEmulation.java b/src/main/java/com/viaversion/fabric/common/util/ArmorHudEmulation.java new file mode 100644 index 00000000..7911a158 --- /dev/null +++ b/src/main/java/com/viaversion/fabric/common/util/ArmorHudEmulation.java @@ -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); + } +} diff --git a/src/main/resources/assets/viafabric/config.yml b/src/main/resources/assets/viafabric/config.yml index d187b8fb..f8e8997c 100644 --- a/src/main/resources/assets/viafabric/config.yml +++ b/src/main/resources/assets/viafabric/config.yml @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/ViaFabricClient.java b/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/ViaFabricClient.java index 83bd42f4..c8deb58e 100644 --- a/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/ViaFabricClient.java +++ b/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/ViaFabricClient.java @@ -17,8 +17,17 @@ */ 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; @@ -26,6 +35,9 @@ 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; @@ -33,6 +45,7 @@ public class ViaFabricClient implements ClientModInitializer { @Override public void onInitializeClient() { registerGui(); + registerArmorHud1_8(); } private void registerGui() { @@ -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; + } } diff --git a/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/mixin/debug/client/MixinDebugHud.java b/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/mixin/debug/client/MixinDebugHud.java index b68dbe81..07303a68 100644 --- a/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc121/src/main/java/com/viaversion/fabric/mc121/mixin/debug/client/MixinDebugHud.java @@ -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; @@ -39,10 +36,8 @@ public class MixinDebugHud { protected void getLeftText(CallbackInfoReturnable> 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();