Skip to content

Commit

Permalink
animate pickup all
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 13, 2024
1 parent f9ab468 commit 9d39a64
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cleanroommc.neverenoughanimations;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

Expand All @@ -16,4 +18,35 @@ static IItemLocation of(Slot slot) {
int nea$getSlotNumber();

ItemStack nea$getStack();

IItemLocation CURSOR = new IItemLocation() {

@Override
public int nea$getX() {
int guiX = 0;
if (Minecraft.getMinecraft().currentScreen instanceof GuiContainer container) {
guiX = container.getGuiLeft();
}
return NEA.getMouseX() - 8 - guiX;
}

@Override
public int nea$getY() {
int guiY = 0;
if (Minecraft.getMinecraft().currentScreen instanceof GuiContainer container) {
guiY = container.getGuiTop();
}
return NEA.getMouseY() - 8 - guiY;
}

@Override
public int nea$getSlotNumber() {
return -1;
}

@Override
public ItemStack nea$getStack() {
return Minecraft.getMinecraft().player.inventory.getItemStack();
}
};
}
16 changes: 16 additions & 0 deletions src/main/java/com/cleanroommc/neverenoughanimations/NEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;
Expand All @@ -32,6 +33,7 @@
public class NEA {

public static final Logger LOGGER = LogManager.getLogger(Tags.MODID);
private static int mouseX, mouseY;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
Expand Down Expand Up @@ -60,6 +62,20 @@ public void onGuiOpen(GuiOpenEvent event) {
ItemMoveAnimation.onGuiOpen(event);
}

@SubscribeEvent
public void onGuiDraw(GuiScreenEvent.DrawScreenEvent.Pre event) {
mouseX = event.getMouseX();
mouseY = event.getMouseY();
}

public static int getMouseX() {
return mouseX;
}

public static int getMouseY() {
return mouseY;
}

public static void drawScreenDebug(GuiContainer container, int mouseX, int mouseY) {
if (!FMLLaunchHandler.isDeobfuscatedEnvironment() || container.getClass().getName().contains("modularui")) return;
GlStateManager.disableDepth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,45 @@ public static ItemStack getVirtualStack(GuiContainer container, Slot slot) {

public static ItemStack getVirtualStack(GuiContainer container, IItemLocation slot) {
return container == lastGui && !NEAConfig.isBlacklisted(container) &&
virtualStacks.size() > slot.nea$getSlotNumber() &&
virtualStacksUser.getInt(slot.nea$getSlotNumber()) > 0 ?
virtualStacks.get(slot.nea$getSlotNumber()) : null;
virtualStacks.size() > slot.nea$getSlotNumber() + 1 &&
virtualStacksUser.getInt(slot.nea$getSlotNumber() + 1) > 0 ?
virtualStacks.get(slot.nea$getSlotNumber() + 1) : null;
}

@ApiStatus.Internal
public static void updateVirtualStack(int target, ItemStack stack, int op) {
if (NEAConfig.moveAnimationTime == 0) return;
while (target >= virtualStacks.size()) {
while (target + 1 >= virtualStacks.size()) {
// ensure size and default values
virtualStacks.add(null);
virtualStacksUser.add(0);
}
int users = virtualStacksUser.getInt(target) + op;
int users = virtualStacksUser.getInt(target + 1) + op;
if (users <= 0) {
// no users left -> set to null
stack = null;
users = 0;
}
virtualStacks.set(target, stack);
virtualStacksUser.set(target, users);
virtualStacks.set(target + 1, stack);
virtualStacksUser.set(target + 1, users);
}

public static void queueAnimation(int slot, ItemMovePacket packet) {
List<ItemMovePacket> packets = movingItemsBySource.get(slot + 1);
if (packets == null) {
packets = new ArrayList<>();
movingItemsBySource.put(slot + 1, packets);
}
packets.add(packet);
}

public static void queueAnimation(int slot, List<ItemMovePacket> packet) {
List<ItemMovePacket> packets = movingItemsBySource.get(slot + 1);
if (packets == null) {
packets = new ArrayList<>();
movingItemsBySource.put(slot + 1, packets);
}
packets.addAll(packet);
}

/**
Expand Down Expand Up @@ -166,11 +184,7 @@ public static void handleMove(Slot source, ItemStack oldSource, Pair<List<Slot>,
NEA.LOGGER.error("The original stack had {} items, but {} items where moved!", oldSource.getCount(), oldSource.getCount() - total);
}
if (error || packets.isEmpty()) return;
if (packets.size() == 1) {
movingItemsBySource.put(sourceLoc.nea$getSlotNumber(), Collections.singletonList(packets.get(0)));
} else {
movingItemsBySource.put(sourceLoc.nea$getSlotNumber(), packets);
}
queueAnimation(sourceLoc.nea$getSlotNumber(), packets);
for (var iterator = stagedVirtualStacks.int2ObjectEntrySet().fastIterator(); iterator.hasNext(); ) {
var e = iterator.next();
updateVirtualStack(e.getIntKey(), e.getValue(), 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.cleanroommc.neverenoughanimations.core.mixin;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import com.cleanroommc.neverenoughanimations.animations.ItemMoveAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemMovePacket;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainerCreative;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -14,6 +21,7 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;
Expand Down Expand Up @@ -59,4 +67,34 @@ public void slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPla
cir.setReturnValue(itemstack);
}
}

@Inject(method = "slotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/InventoryPlayer;getItemStack()Lnet/minecraft/item/ItemStack;", ordinal = 13))
public void pickupAllPre(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player,
CallbackInfoReturnable<ItemStack> cir, @Share("cursor") LocalRef<ItemStack> cursor) {
cursor.set(player.inventory.getItemStack().copy());
}

@Redirect(method = "slotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;grow(I)V", ordinal = 2))
public void pickupAllMid(ItemStack instance, int quantity, @Share("packets") LocalRef<Int2ObjectArrayMap<ItemMovePacket>> packets, @Local(ordinal = 1) Slot slot) {
if (NEAConfig.moveAnimationTime == 0) return;
if (packets.get() == null) packets.set(new Int2ObjectArrayMap<>());
IItemLocation source = IItemLocation.of(slot);
ItemStack movingStack = instance.copy();
movingStack.setCount(quantity);
packets.get().put(source.nea$getSlotNumber(), new ItemMovePacket(Minecraft.getSystemTime(), source, IItemLocation.CURSOR, movingStack));
instance.grow(quantity);
}

@Inject(method = "slotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/inventory/Container;detectAndSendChanges()V"))
public void pickupAllPost(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player,
CallbackInfoReturnable<ItemStack> cir, @Share("packets") LocalRef<Int2ObjectArrayMap<ItemMovePacket>> packets,
@Share("cursor") LocalRef<ItemStack> cursor) {
if (packets.get() != null && !packets.get().isEmpty()) {
for (var iterator = packets.get().int2ObjectEntrySet().fastIterator(); iterator.hasNext(); ) {
var e = iterator.next();
ItemMoveAnimation.queueAnimation(e.getIntKey(), e.getValue());
ItemMoveAnimation.updateVirtualStack(-1, cursor.get(), 1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

Expand Down Expand Up @@ -100,4 +101,13 @@ public void endOpeningAnimation(int mouseX, int mouseY, float partialTicks, Call
}
NEA.drawScreenDebug((GuiContainer) (Object) this, mouseX, mouseY);
}

@ModifyArg(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/inventory/GuiContainer;drawItemStack(Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V", ordinal = 0), index = 0)
public ItemStack injectVirtualCursorStack(ItemStack stack) {
if (NEAConfig.moveAnimationTime > 0) {
ItemStack virtual = ItemMoveAnimation.getVirtualStack((GuiContainer) (Object) this, IItemLocation.CURSOR);
return virtual == null ? stack : virtual;
}
return stack;
}
}

0 comments on commit 9d39a64

Please sign in to comment.