|
| 1 | +package derp.immersivehotbar; |
| 2 | + |
| 3 | +import derp.immersivehotbar.config.ImmersiveHotbarConfigHandler; |
| 4 | +import derp.immersivehotbar.util.TooltipAnimationState; |
| 5 | +import net.fabricmc.api.ClientModInitializer; |
| 6 | +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; |
| 7 | +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; |
| 8 | +import net.fabricmc.fabric.api.event.client.player.ClientPlayerBlockBreakEvents; |
| 9 | +import net.fabricmc.loader.api.FabricLoader; |
| 10 | +import net.minecraft.item.*; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | +import static derp.immersivehotbar.config.ImmersiveHotbarConfig.*; |
| 15 | +import static derp.immersivehotbar.util.ItemChecker.isTool; |
| 16 | +import static derp.immersivehotbar.util.SlotAnimationState.*; |
| 17 | + |
| 18 | +public class ImmersiveHotbarClient implements ClientModInitializer { |
| 19 | + private boolean wasUsingItem = false; |
| 20 | + private ItemStack lastUsedItem = ItemStack.EMPTY; |
| 21 | + private boolean wasCrossbowChargedMainhand = false; |
| 22 | + private boolean wasCrossbowChargedOffhand = false; |
| 23 | + public static final boolean IS_DOUBLEHOTBAR_LOADED = FabricLoader.getInstance().isModLoaded("double_hotbar"); |
| 24 | + |
| 25 | + @Override |
| 26 | + public void onInitializeClient() { |
| 27 | + ImmersiveHotbarConfigHandler.load(); |
| 28 | + ClientPlayConnectionEvents.DISCONNECT.register((client, world) -> Arrays.fill(lastSlotStacks, ItemStack.EMPTY)); |
| 29 | + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> TooltipAnimationState.reset()); |
| 30 | + |
| 31 | + ClientTickEvents.END_CLIENT_TICK.register(client -> { |
| 32 | + if (client.player == null) return; |
| 33 | + |
| 34 | + boolean isUsing = client.player.isUsingItem(); |
| 35 | + |
| 36 | + // track bow usage |
| 37 | + if (isUsing) { |
| 38 | + lastUsedItem = client.player.getActiveItem(); |
| 39 | + } else if (wasUsingItem && !lastUsedItem.isEmpty()) { |
| 40 | + Item item = lastUsedItem.getItem(); |
| 41 | + |
| 42 | + if (weaponAnimates && (item instanceof BowItem || item instanceof CrossbowItem)) { |
| 43 | + int slot = client.player.getMainHandStack() == lastUsedItem ? client.player.getInventory().getSelectedSlot() : 9; |
| 44 | + triggerShrink(slot); |
| 45 | + } |
| 46 | + lastUsedItem = ItemStack.EMPTY; |
| 47 | + } |
| 48 | + |
| 49 | + wasUsingItem = isUsing; |
| 50 | + |
| 51 | + // crossbow mainhand |
| 52 | + ItemStack mainHandStack = client.player.getMainHandStack(); |
| 53 | + if (mainHandStack.getItem() instanceof CrossbowItem) { |
| 54 | + boolean isCharged = CrossbowItem.isCharged(mainHandStack); |
| 55 | + if (wasCrossbowChargedMainhand && !isCharged && weaponAnimates) { |
| 56 | + int slot = client.player.getInventory().getSelectedSlot(); |
| 57 | + triggerShrink(slot); |
| 58 | + } |
| 59 | + wasCrossbowChargedMainhand = isCharged; |
| 60 | + } else { |
| 61 | + wasCrossbowChargedMainhand = false; |
| 62 | + } |
| 63 | + |
| 64 | + // crossbow offhand |
| 65 | + ItemStack offHandStack = client.player.getOffHandStack(); |
| 66 | + if (offHandStack.getItem() instanceof CrossbowItem) { |
| 67 | + boolean isCharged = CrossbowItem.isCharged(offHandStack); |
| 68 | + if (wasCrossbowChargedOffhand && !isCharged && weaponAnimates) { |
| 69 | + triggerShrink(9); |
| 70 | + } |
| 71 | + wasCrossbowChargedOffhand = isCharged; |
| 72 | + } else { |
| 73 | + wasCrossbowChargedOffhand = false; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + // tool break animation |
| 78 | + ClientPlayerBlockBreakEvents.AFTER.register((world, player, pos, state) -> { |
| 79 | + ItemStack stack = player.getMainHandStack(); |
| 80 | + if (isTool(stack) && toolAnimates) { |
| 81 | + int slot = player.getInventory().getSelectedSlot(); |
| 82 | + triggerShrink(slot); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + private void triggerShrink(int slot) { |
| 88 | + if (slot >= 0 && slot < 9) { |
| 89 | + wasUsed[slot] = true; |
| 90 | + slotScales[slot] = nonSelectedItemSize - (shouldItemGrowWhenSelected ? 0.03f : 0.2f); |
| 91 | + } else if (slot == 9) { |
| 92 | + wasUsed[slot] = true; |
| 93 | + slotScales[slot] = nonSelectedItemSize - 0.2f; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +} |
0 commit comments