Skip to content

Commit

Permalink
Migrate hotbar
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Aug 3, 2024
1 parent e58d161 commit 326bd4b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ interface ScoreBoardOverlayAccess extends VanillaBufferAccess {
LegacyBuffer getScoreBoardOverlayBuffer();
}

interface HotbarOverlayAccess extends VanillaBufferAccess {

LegacyBuffer getHotbarOverlayBuffer();
}

interface BossHealthOverlayAccess extends VanillaBufferAccess {
LegacyBuffer getHotbarOverlayBuffer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> {

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;
}

}
155 changes: 11 additions & 144 deletions src/main/java/dev/tr7zw/exordium/mixin/GuiHotbarMixin.java
Original file line number Diff line number Diff line change
@@ -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<Void> operation) {
outdated = hasChanged();
if (!hotbarBufferedComponent.render()) {
BufferInstance<Void> 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<Void> operation) {
outdated = hasChanged();
if (!hotbarBufferedComponent.render()) {
BufferInstance<Void> 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);
}

}

0 comments on commit 326bd4b

Please sign in to comment.