Skip to content

Commit

Permalink
throw animation
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 15, 2024
1 parent 3c2c48c commit dadf5da
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,42 @@ static IItemLocation of(Slot slot) {
return Minecraft.getMinecraft().player.inventory.getItemStack();
}
};

class Impl implements IItemLocation {

private final int x, y;
private final int slotNumber;
private final ItemStack stack;

public Impl(int x, int y, ItemStack stack) {
this(x, y, -2, stack);
}

public Impl(int x, int y, int slotNumber, ItemStack stack) {
this.x = x;
this.y = y;
this.slotNumber = slotNumber;
this.stack = stack;
}

@Override
public int nea$getX() {
return x;
}

@Override
public int nea$getY() {
return y;
}

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

@Override
public ItemStack nea$getStack() {
return stack;
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/cleanroommc/neverenoughanimations/NEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cleanroommc.neverenoughanimations.animations.ItemHoverAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemMoveAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemPickupThrowAnimation;
import com.cleanroommc.neverenoughanimations.animations.OpeningAnimation;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void onGuiOpen(GuiOpenEvent event) {
if (OpeningAnimation.onGuiOpen(event)) return;
ItemHoverAnimation.onGuiOpen(event);
ItemMoveAnimation.onGuiOpen(event);
ItemPickupThrowAnimation.onGuiOpen(event);
}

@SubscribeEvent(priority = EventPriority.HIGHEST, receiveCanceled = true)
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/cleanroommc/neverenoughanimations/NEAConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ public class NEAConfig {
@Config.Comment("If the gray slot overlay (minecraft feature) should be rendered at all on hover. Default: false")
public static boolean itemHoverOverlay = false;

@Config.Name("Move animation time")
@Config.Name("Item move animation time")
@Config.SlidingOption
@Config.RangeInt(min = 0, max = 1000)
@Config.Comment("How many millieseconds it takes until an item has moved to its target (activated on shift click). 0 to disable.")
public static int moveAnimationTime = 200;
@Config.Name("Move animation easing curve")
@Config.Name("Item move animation easing curve")
public static Interpolation moveAnimationCurve = Interpolation.SINE_OUT;

@Config.Name("Item (dis)appear animation time")
@Config.SlidingOption
@Config.RangeInt(min = 0, max = 1000)
@Config.Comment("How many millieseconds it takes until an item has moved to its target (activated on shift click). 0 to disable.")
public static int appearAnimationTime = 150;
@Config.Name("Item (dis)appear animation easing curve")
public static Interpolation appearAnimationCurve = Interpolation.SINE_OUT;

@Config.Name("Hotbar animation time")
@Config.SlidingOption
@Config.RangeInt(min = 0, max = 1000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.GuiOpenEvent;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ItemPickupThrowAnimation {

private static final Object2LongOpenHashMap<IItemLocation> animated = new Object2LongOpenHashMap<>();
private static final List<IItemLocation> removalAnimation = new ArrayList<>();
private static GuiContainer lastGui;

@ApiStatus.Internal
public static void onGuiOpen(GuiOpenEvent event) {
if (NEAConfig.hoverAnimationTime > 0) {
if (!(event.getGui() instanceof GuiContainer)) {
if (lastGui != null) {
lastGui = null;
animated.clear();
}
return;
}
if (!NEAConfig.isBlacklisted(event.getGui())) {
lastGui = (GuiContainer) event.getGui();
animated.clear();
}
}
}

public static void animate(Slot slot) {
if (lastGui == null) return;
animate(IItemLocation.of(slot));
}

public static void animate(IItemLocation slot) {
if (lastGui == null) return;
animated.put(slot, Minecraft.getSystemTime());
if (slot.nea$getSlotNumber() < 0) removalAnimation.add(slot);
}

public static void animate(int x, int y, ItemStack stack, boolean absolutePos) {
if (lastGui == null) return;
if (absolutePos) {
x -= lastGui.getGuiLeft();
y -= lastGui.getGuiTop();
}
animate(new IItemLocation.Impl(x, y, stack));
}

public static float getValue(GuiContainer container, Slot slot) {
return getValue(container, IItemLocation.of(slot));
}

public static float getValue(GuiContainer container, IItemLocation slot) {
if (lastGui != container || !animated.containsKey(slot)) return 1f;
long time = animated.getLong(slot);
float val = (Minecraft.getSystemTime() - time) / (float) NEAConfig.appearAnimationTime;
if (val >= 1f) {
animated.removeLong(slot);
return 1f;
}
return NEAConfig.appearAnimationCurve.interpolate(0f, 1f, val);
}

public static void drawIndependentAnimations(GuiContainer container, RenderItem itemRender, FontRenderer fontRenderer) {
for (Iterator<IItemLocation> iterator = removalAnimation.iterator(); iterator.hasNext(); ) {
IItemLocation slot = iterator.next();
int x = slot.nea$getX();
int y = slot.nea$getY();
float value = 1f - getValue(container, slot);
if (value <= 0f) {
iterator.remove();
continue;
}
GlStateManager.translate(x, y, 0);
if (value <= 1f) {
GlStateManager.pushMatrix();
GlStateManager.translate(8, 8, 0);
GlStateManager.scale(value, value, 1);
GlStateManager.translate(-8, -8, 0);
} else if (!animated.containsKey(slot)) {
iterator.remove();
GlStateManager.translate(-x, -y, 0);
continue;
}
GlStateManager.translate(0, 0, 32f);
FontRenderer font = slot.nea$getStack().getItem().getFontRenderer(slot.nea$getStack());
if (font == null) font = fontRenderer;
itemRender.renderItemAndEffectIntoGUI(Minecraft.getMinecraft().player, slot.nea$getStack(), 0, 0);
itemRender.renderItemOverlayIntoGUI(font, slot.nea$getStack(), 0, 0, null);
if (value <= 1f) {
GlStateManager.popMatrix();
}
GlStateManager.translate(-x, -y, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.cleanroommc.neverenoughanimations.core.mixin;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import com.cleanroommc.neverenoughanimations.animations.ItemMoveAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemMovePacket;
import com.cleanroommc.neverenoughanimations.animations.ItemPickupThrowAnimation;
import com.cleanroommc.neverenoughanimations.animations.SwapHolder;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.Share;
Expand All @@ -22,6 +24,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.CallbackInfoReturnable;

Expand Down Expand Up @@ -112,4 +115,22 @@ public void pickupAllPost(int slotId, int dragType, ClickType clickTypeIn, Entit
}
}
}

@Inject(method = "slotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;dropItem(Lnet/minecraft/item/ItemStack;Z)Lnet/minecraft/entity/item/EntityItem;", ordinal = 3))
public void throwItem(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player,
CallbackInfoReturnable<ItemStack> cir, @Local(ordinal = 1) LocalRef<ItemStack> throwing) {
IItemLocation slot = IItemLocation.of(this.inventorySlots.get(slotId));
if (slot.nea$getStack().isEmpty()) {
// only animate when shift is held (throw hole stack) or only one item is left
ItemPickupThrowAnimation.animateRemove(slot.nea$getX(), slot.nea$getY(), throwing.get(), false);
}
}

@ModifyArg(method = "slotClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;dropItem(Lnet/minecraft/item/ItemStack;Z)Lnet/minecraft/entity/item/EntityItem;"))
public ItemStack animateThrow(ItemStack itemStackIn, @Local(ordinal = 0) int slot) {
if (slot == -999) {
ItemPickupThrowAnimation.animateRemove(NEA.getMouseX() - 8, NEA.getMouseY() - 8, itemStackIn.copy(), true);
}
return itemStackIn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cleanroommc.neverenoughanimations.NEAConfig;
import com.cleanroommc.neverenoughanimations.animations.ItemHoverAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemMoveAnimation;
import com.cleanroommc.neverenoughanimations.animations.ItemPickupThrowAnimation;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import net.minecraft.client.gui.GuiScreen;
Expand Down Expand Up @@ -63,6 +64,7 @@ public void dontDrawOverlay(GuiContainer instance, int i1, int i2, int i3, int i
public void drawMovingItems(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) {
zLevel = 200;
itemRender.zLevel = 200;
ItemPickupThrowAnimation.drawIndependentAnimations((GuiContainer) (Object) this, itemRender, fontRenderer);
ItemMoveAnimation.drawAnimations(itemRender, fontRenderer);
itemRender.zLevel = 0;
zLevel = 0;
Expand Down

0 comments on commit dadf5da

Please sign in to comment.