Skip to content

Commit

Permalink
Minecraft.getSystemTime() -> System.nanoTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 20, 2024
1 parent 76eb06f commit 6c3e483
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/cleanroommc/neverenoughanimations/NEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ public static boolean isJeiLoaded() {
public static boolean isHeiLoaded() {
return heiLoaded;
}

public static long time() {
return System.nanoTime() / 1_000_000L;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
Expand All @@ -22,7 +23,7 @@ public static void animate(int oldIndex, int newIndex) {
reset();
return;
}
HotbarAnimation.startTime = Minecraft.getSystemTime();
HotbarAnimation.startTime = NEA.time();
}

public static boolean isAnimationInProgress() {
Expand All @@ -44,7 +45,7 @@ public static int getX(ScaledResolution sr) {
reset();
return getX(sr, def);
}
float val = (Minecraft.getSystemTime() - HotbarAnimation.startTime) / (float) NEAConfig.hotbarAnimationTime;
float val = (NEA.time() - HotbarAnimation.startTime) / (float) NEAConfig.hotbarAnimationTime;
if (val >= 1f) {
// animation ended
reset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
import net.minecraftforge.client.event.GuiOpenEvent;
Expand Down Expand Up @@ -37,7 +37,7 @@ public static void onGuiOpen(GuiOpenEvent event) {
}

private static void startAnimation(Slot slot, boolean grow) {
hoveredSlots.put(slot, Minecraft.getSystemTime() * (grow ? 1 : -1));
hoveredSlots.put(slot, NEA.time() * (grow ? 1 : -1));
}

public static boolean isAnimating(Slot slot) {
Expand All @@ -64,12 +64,10 @@ public static void onGuiTick() {
}

public static float getRenderScale(GuiContainer gui, Slot slot) {
if (lastHoveredGui != gui ||
!isAnimating(slot) ||
NEAConfig.isBlacklisted(gui)) return 1f;
if (lastHoveredGui != gui || !isAnimating(slot) || NEAConfig.isBlacklisted(gui)) return 1f;
float min = 1f, max = 1.25f;
float slotTime = hoveredSlots.getLong(slot);
float val = (Minecraft.getSystemTime() - Math.abs(slotTime)) / (float) NEAConfig.hoverAnimationTime;
float val = (NEA.time() - Math.abs(slotTime)) / (float) NEAConfig.hoverAnimationTime;
if (slotTime < 0) {
// negative time means slot is no longer hovered
val = 1f - val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jetbrains.annotations.ApiStatus;

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

Expand Down Expand Up @@ -62,10 +61,9 @@ 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() + 1 &&
virtualStacksUser.getInt(slot.nea$getSlotNumber() + 1) > 0 ?
virtualStacks.get(slot.nea$getSlotNumber() + 1) : null;
return container == lastGui && !NEAConfig.isBlacklisted(
container) && virtualStacks.size() > slot.nea$getSlotNumber() + 1 && virtualStacksUser.getInt(
slot.nea$getSlotNumber() + 1) > 0 ? virtualStacks.get(slot.nea$getSlotNumber() + 1) : null;
}

@ApiStatus.Internal
Expand Down Expand Up @@ -109,9 +107,10 @@ public static void queueAnimation(int slot, List<ItemMovePacket> packet) {
*/
@ApiStatus.Internal
public static Pair<List<Slot>, List<ItemStack>> getCandidates(Slot in, List<Slot> allSlots) {
if (NEAConfig.moveAnimationTime == 0 ||
NEAConfig.isBlacklisted(Minecraft.getMinecraft().currentScreen) ||
Minecraft.getSystemTime() - lastAnimation <= 10) return null;
if (NEAConfig.moveAnimationTime == 0 || NEAConfig.isBlacklisted(
Minecraft.getMinecraft().currentScreen) || NEA.time() - lastAnimation <= 10) {
return null;
}
List<Slot> slots = new ArrayList<>(allSlots.size());
List<ItemStack> stacks = new ArrayList<>(allSlots.size());
ItemStack item = IItemLocation.of(in).nea$getStack();
Expand Down Expand Up @@ -143,7 +142,7 @@ public static void handleMove(Slot source, ItemStack oldSource, Pair<List<Slot>,
List<ItemMovePacket> packets = new ArrayList<>();
Int2ObjectArrayMap<ItemStack> stagedVirtualStacks = new Int2ObjectArrayMap<>();
boolean error = false;
long time = Minecraft.getSystemTime();
long time = NEA.time();
for (int i = 0; i < slots.size(); i++) {
IItemLocation slot = IItemLocation.of(slots.get(i));
if (slot == sourceLoc) continue;
Expand Down Expand Up @@ -181,15 +180,16 @@ public static void handleMove(Slot source, ItemStack oldSource, Pair<List<Slot>,
if (total <= 0) break;
}
if (total < 0) {
NEA.LOGGER.error("The original stack had {} items, but {} items where moved!", oldSource.getCount(), oldSource.getCount() - total);
NEA.LOGGER.error("The original stack had {} items, but {} items where moved!", oldSource.getCount(),
oldSource.getCount() - total);
}
if (error || packets.isEmpty()) return;
queueAnimation(sourceLoc.nea$getSlotNumber(), packets);
for (var iterator = stagedVirtualStacks.int2ObjectEntrySet().fastIterator(); iterator.hasNext(); ) {
var e = iterator.next();
updateVirtualStack(e.getIntKey(), e.getValue(), 1);
}
lastAnimation = Minecraft.getSystemTime();
lastAnimation = NEA.time();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;

public class ItemMovePacket {
Expand Down Expand Up @@ -41,7 +41,7 @@ public IItemLocation getTarget() {
}

public float value() {
return Math.min(1f, (Minecraft.getSystemTime() - this.time) / (float) NEAConfig.moveAnimationTime);
return Math.min(1f, (NEA.time() - this.time) / (float) NEAConfig.moveAnimationTime);
}

public int getDrawX(float value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -47,7 +48,7 @@ public static void animate(Slot slot) {

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

Expand All @@ -67,7 +68,7 @@ public static float getValue(GuiContainer container, Slot 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;
float val = (NEA.time() - time) / (float) NEAConfig.appearAnimationTime;
if (val >= 1f) {
animated.removeLong(slot);
return 1f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import com.cleanroommc.neverenoughanimations.util.Interpolations;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -31,7 +32,7 @@ public static void animate(GuiContainer container, boolean open) {
if (NEAConfig.openingAnimationTime == 0 || NEAConfig.isBlacklisted(container)) return;
animatedGui = container;
lastGui = container;
startTime = Minecraft.getSystemTime() * (open ? 1 : -1);
startTime = NEA.time() * (open ? 1 : -1);
}

public static float getScale(GuiContainer container) {
Expand All @@ -42,7 +43,7 @@ public static float getScale(GuiContainer container) {
public static float getValue(GuiContainer container) {
if (shouldCloseLast) return 0.001f;
if (animatedGui != container) return 1f;
float val = (Minecraft.getSystemTime() - Math.abs(startTime)) / (float) NEAConfig.openingAnimationTime;
float val = (NEA.time() - Math.abs(startTime)) / (float) NEAConfig.openingAnimationTime;
if (startTime < 0) {
val = 1f - val;
if (val <= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cleanroommc.neverenoughanimations.animations;

import com.cleanroommc.neverenoughanimations.IItemLocation;
import com.cleanroommc.neverenoughanimations.NEA;
import com.cleanroommc.neverenoughanimations.NEAConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void performSwap() {
}
IItemLocation hotbar = IItemLocation.of(this.hotbarSlot);
IItemLocation hovering = IItemLocation.of(this.targetSlot);
long time = Minecraft.getSystemTime();
long time = NEA.time();
if (this.targetStack.isEmpty()) {
if (!hovering.nea$getStack().isEmpty()) {
ItemMoveAnimation.queueAnimation(hotbar.nea$getSlotNumber(),
Expand Down Expand Up @@ -97,10 +98,7 @@ public Slot getTargetSlot() {
public static Slot findHotbarSlot(List<Slot> slots, int index) {
for (Slot slot : slots) {
if (slot.getSlotIndex() != index) continue;
if (slot.inventory instanceof InventoryPlayer ||
(slot instanceof SlotItemHandler slotItemHandler &&
(slotItemHandler.getItemHandler() instanceof PlayerMainInvWrapper ||
slotItemHandler.getItemHandler() instanceof PlayerInvWrapper))) {
if (slot.inventory instanceof InventoryPlayer || (slot instanceof SlotItemHandler slotItemHandler && (slotItemHandler.getItemHandler() instanceof PlayerMainInvWrapper || slotItemHandler.getItemHandler() instanceof PlayerInvWrapper))) {
return slot;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void pickupAllMid(ItemStack instance, int quantity, @Share("packets") Loc
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));
packets.get().put(source.nea$getSlotNumber(), new ItemMovePacket(NEA.time(), source, IItemLocation.CURSOR, movingStack));
instance.grow(quantity);
}

Expand Down

0 comments on commit 6c3e483

Please sign in to comment.