From 326bd4b1d5b0c30640cb6d51f42fc8eed3787593 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Sat, 3 Aug 2024 02:11:39 +0200 Subject: [PATCH] Migrate hotbar --- .../exordium/access/VanillaBufferAccess.java | 5 - .../exordium/components/BufferManager.java | 3 +- .../components/vanilla/HotbarComponent.java | 120 ++++++++++++++ .../tr7zw/exordium/mixin/GuiHotbarMixin.java | 155 ++---------------- 4 files changed, 133 insertions(+), 150 deletions(-) create mode 100644 src/main/java/dev/tr7zw/exordium/components/vanilla/HotbarComponent.java diff --git a/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java b/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java index 0d906dc..152014b 100644 --- a/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java +++ b/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java @@ -15,11 +15,6 @@ interface ScoreBoardOverlayAccess extends VanillaBufferAccess { LegacyBuffer getScoreBoardOverlayBuffer(); } - interface HotbarOverlayAccess extends VanillaBufferAccess { - - LegacyBuffer getHotbarOverlayBuffer(); - } - interface BossHealthOverlayAccess extends VanillaBufferAccess { LegacyBuffer getHotbarOverlayBuffer(); } diff --git a/src/main/java/dev/tr7zw/exordium/components/BufferManager.java b/src/main/java/dev/tr7zw/exordium/components/BufferManager.java index 02442cc..6addc1d 100644 --- a/src/main/java/dev/tr7zw/exordium/components/BufferManager.java +++ b/src/main/java/dev/tr7zw/exordium/components/BufferManager.java @@ -9,6 +9,7 @@ import dev.tr7zw.exordium.components.vanilla.CrosshairComponent; import dev.tr7zw.exordium.components.vanilla.DebugOverlayComponent; import dev.tr7zw.exordium.components.vanilla.ExperienceComponent; +import dev.tr7zw.exordium.components.vanilla.HotbarComponent; import dev.tr7zw.exordium.components.vanilla.VignetteComponent; import dev.tr7zw.exordium.versionless.config.Config; import lombok.NoArgsConstructor; @@ -31,8 +32,8 @@ public void initialize() { // vanillaBuffers.put(new ResourceLocation("scoreboard"), // gui -> ((ScoreBoardOverlayAccess) gui).getScoreBoardOverlayBuffer()); -// vanillaBuffers.put(new ResourceLocation("hotbar"), gui -> ((HotbarOverlayAccess) gui).getHotbarOverlayBuffer()); + registerBuffer(HotbarComponent.getId(), new HotbarComponent(), () -> inst.config.hotbarSettings); registerBuffer(ChatComponent.getId(), new ChatComponent(), () -> inst.config.chatSettings); // registerCustomHandler(new ResourceLocation("player_list"), data -> { diff --git a/src/main/java/dev/tr7zw/exordium/components/vanilla/HotbarComponent.java b/src/main/java/dev/tr7zw/exordium/components/vanilla/HotbarComponent.java new file mode 100644 index 0000000..45f51e0 --- /dev/null +++ b/src/main/java/dev/tr7zw/exordium/components/vanilla/HotbarComponent.java @@ -0,0 +1,120 @@ +package dev.tr7zw.exordium.components.vanilla; + +import dev.tr7zw.exordium.components.BufferComponent; +import dev.tr7zw.util.NMSHelper; +import lombok.Getter; +import net.minecraft.client.AttackIndicatorStatus; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.model.BakedModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; + +public class HotbarComponent implements BufferComponent { + + private static final Minecraft minecraft = Minecraft.getInstance(); + @Getter + private static final ResourceLocation id = NMSHelper.getResourceLocation("minecraft", "hotbar"); + + private float lastAttackState = 0; + private final BakedModel[] hotbarModels = new BakedModel[10]; + private final int[] itemPopAnimation = new int[10]; + private final int[] itemAmount = new int[10]; + private final int[] itemDurability = new int[10]; + private int selectedSlot = 0; + private boolean hasEnchantedItem = false; + private boolean cooldownActive = false; + + @Override + public void captureState(Void context) { + hasEnchantedItem = false; + cooldownActive = false; + lastAttackState = minecraft.player.getAttackStrengthScale(0.0F); + Player player = getCameraPlayer(); + if (player == null) + return; + for (int m = 0; m < 9; m++) { + store((player.getInventory()).items.get(m), m, player); + } + store(player.getOffhandItem(), 9, player); + selectedSlot = player.getInventory().selected; + } + + @Override + public boolean hasChanged(int tickCount, Void context) { + if (minecraft.options.attackIndicator().get() == AttackIndicatorStatus.HOTBAR) { + float g = minecraft.player.getAttackStrengthScale(0.0F); + if (g != lastAttackState) { + return true; + } + } + if (hasEnchantedItem || cooldownActive) { + return true; + } + Player player = getCameraPlayer(); + if (player == null) + return true; + if (selectedSlot != player.getInventory().selected) { + return true; + } + for (int m = 0; m < 9; m++) { + ItemStack item = (player.getInventory()).items.get(m); + if (hasChanged(item, m, player)) { + return true; + } + } + if (hasChanged(player.getOffhandItem(), 9, player)) { + return true; + } + + return false; + } + + private void store(ItemStack item, int id, Player player) { + if (item != null && !item.isEmpty()) { + hotbarModels[id] = Minecraft.getInstance().getItemRenderer().getModel(item, player.level(), player, 0); + itemPopAnimation[id] = item.getPopTime(); + itemAmount[id] = item.getCount(); + itemDurability[id] = item.getDamageValue(); + if (item.isEnchanted()) { + this.hasEnchantedItem = true; + } + if (player.getCooldowns().isOnCooldown(item.getItem())) { + this.cooldownActive = true; + } + } else { + hotbarModels[id] = null; + itemPopAnimation[id] = 0; + itemAmount[id] = 0; + itemDurability[id] = -1; + } + } + + private boolean hasChanged(ItemStack item, int id, Player player) { + if (item != null && !item.isEmpty()) { + if (itemAmount[id] != item.getCount()) { + return true; + } + if (itemPopAnimation[id] != item.getPopTime()) { + return true; + } + if (itemDurability[id] != item.getDamageValue()) { + return true; + } + if (Minecraft.getInstance().getItemRenderer().getModel(item, player.level(), player, + 0) != hotbarModels[id]) { + return true; + } + } else if (hotbarModels[id] != null) { + return true; + } + return false; + } + + private Player getCameraPlayer() { + Entity var2 = minecraft.getCameraEntity(); + return var2 instanceof Player player ? player : null; + } + +} diff --git a/src/main/java/dev/tr7zw/exordium/mixin/GuiHotbarMixin.java b/src/main/java/dev/tr7zw/exordium/mixin/GuiHotbarMixin.java index 13fd995..280d721 100644 --- a/src/main/java/dev/tr7zw/exordium/mixin/GuiHotbarMixin.java +++ b/src/main/java/dev/tr7zw/exordium/mixin/GuiHotbarMixin.java @@ -1,175 +1,42 @@ package dev.tr7zw.exordium.mixin; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import dev.tr7zw.exordium.ExordiumModBase; -import dev.tr7zw.exordium.access.VanillaBufferAccess.HotbarOverlayAccess; -import dev.tr7zw.exordium.render.BufferedComponent; -import dev.tr7zw.exordium.render.LegacyBuffer; -import net.minecraft.client.AttackIndicatorStatus; -import net.minecraft.client.Minecraft; +import dev.tr7zw.exordium.components.BufferInstance; +import dev.tr7zw.exordium.components.vanilla.HotbarComponent; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.spectator.SpectatorGui; -import net.minecraft.client.resources.model.BakedModel; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; @Mixin(Gui.class) -public class GuiHotbarMixin implements HotbarOverlayAccess { - - @Unique - private boolean outdated = false; - @Unique - private float lastAttackState = 0; - @Unique - private final BakedModel[] hotbarModels = new BakedModel[10]; - @Unique - private final int[] itemPopAnimation = new int[10]; - @Unique - private final int[] itemAmount = new int[10]; - @Unique - private final int[] itemDurability = new int[10]; - @Unique - private int selectedSlot = 0; - @Unique - private boolean hasEnchantedItem = false; - @Unique - private boolean cooldownActive = false; - - @Unique - private final LegacyBuffer hotbarBufferedComponent = new LegacyBuffer( - () -> ExordiumModBase.instance.config.hotbarSettings) { - - @Override - public boolean shouldRenderNextCappedFrame() { - return outdated; - } - - @Override - public void captureState() { - hasEnchantedItem = false; - cooldownActive = false; - lastAttackState = Minecraft.getInstance().player.getAttackStrengthScale(0.0F); - Player player = getCameraPlayer(); - if (player == null) - return; - for (int m = 0; m < 9; m++) { - store((player.getInventory()).items.get(m), m, player); - } - store(player.getOffhandItem(), 9, player); - selectedSlot = player.getInventory().selected; - } - }; - - @Unique - private void store(ItemStack item, int id, Player player) { - if (item != null && !item.isEmpty()) { - hotbarModels[id] = Minecraft.getInstance().getItemRenderer().getModel(item, player.level(), player, 0); - itemPopAnimation[id] = item.getPopTime(); - itemAmount[id] = item.getCount(); - itemDurability[id] = item.getDamageValue(); - if (item.isEnchanted()) { - this.hasEnchantedItem = true; - } - if (player.getCooldowns().isOnCooldown(item.getItem())) { - this.cooldownActive = true; - } - } else { - hotbarModels[id] = null; - itemPopAnimation[id] = 0; - itemAmount[id] = 0; - itemDurability[id] = -1; - } - } - - @Unique - private boolean hasChanged(ItemStack item, int id, Player player) { - if (item != null && !item.isEmpty()) { - if (itemAmount[id] != item.getCount()) { - return true; - } - if (itemPopAnimation[id] != item.getPopTime()) { - return true; - } - if (itemDurability[id] != item.getDamageValue()) { - return true; - } - if (Minecraft.getInstance().getItemRenderer().getModel(item, player.level(), player, - 0) != hotbarModels[id]) { - return true; - } - } else if (hotbarModels[id] != null) { - return true; - } - return false; - } - - @Unique - public boolean hasChanged() { - if (Minecraft.getInstance().options.attackIndicator().get() == AttackIndicatorStatus.HOTBAR) { - float g = Minecraft.getInstance().player.getAttackStrengthScale(0.0F); - if (g != lastAttackState) { - return true; - } - } - if (hasEnchantedItem || cooldownActive) { - return true; - } - Player player = getCameraPlayer(); - if (player == null) - return true; - if (selectedSlot != player.getInventory().selected) { - return true; - } - for (int m = 0; m < 9; m++) { - ItemStack item = (player.getInventory()).items.get(m); - if (hasChanged(item, m, player)) { - return true; - } - } - if (hasChanged(player.getOffhandItem(), 9, player)) { - return true; - } - - return false; - } +public class GuiHotbarMixin { @WrapOperation(method = "renderHotbarAndDecorations", at = { @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/Gui;renderItemHotbar(Lnet/minecraft/client/gui/GuiGraphics;F)V"), }) private void renderHotbarWrapper(Gui gui, GuiGraphics guiGraphics, float f, final Operation operation) { - outdated = hasChanged(); - if (!hotbarBufferedComponent.render()) { + BufferInstance buffer = ExordiumModBase.instance.getBufferManager() + .getBufferInstance(HotbarComponent.getId(), Void.class); + if (!buffer.renderBuffer(0, null)) { operation.call(gui, guiGraphics, f); } - hotbarBufferedComponent.renderEnd(); + buffer.postRender(null); } @WrapOperation(method = "renderHotbarAndDecorations", at = { @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/spectator/SpectatorGui;renderHotbar(Lnet/minecraft/client/gui/GuiGraphics;)V"), }) private void renderHotbarWrapperSpectator(SpectatorGui gui, GuiGraphics guiGraphics, final Operation operation) { - outdated = hasChanged(); - if (!hotbarBufferedComponent.render()) { + BufferInstance buffer = ExordiumModBase.instance.getBufferManager() + .getBufferInstance(HotbarComponent.getId(), Void.class); + if (!buffer.renderBuffer(0, null)) { operation.call(gui, guiGraphics); } - hotbarBufferedComponent.renderEnd(); - } - - @Shadow - private Player getCameraPlayer() { - return null; - } - - @Override - public LegacyBuffer getHotbarOverlayBuffer() { - return hotbarBufferedComponent; + buffer.postRender(null); } }