Skip to content

Commit

Permalink
improve fluid to drop performance
Browse files Browse the repository at this point in the history
  • Loading branch information
GlodBlock committed Sep 24, 2024
1 parent 7c7934f commit e07bfb0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.glodblock.github.common.item.fake;

import appeng.api.storage.data.IAEItemStack;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import net.minecraftforge.fluids.FluidStack;

import java.util.Map;
import java.util.function.Function;

public final class DropLookup {

private final static Map<FluidStack, IAEItemStack> FLUID_CACHE_A = new Object2ObjectLinkedOpenHashMap<>();

public static IAEItemStack lookup(FluidStack fluid, Function<FluidStack, IAEItemStack> fallback) {
IAEItemStack cache = FLUID_CACHE_A.get(fluid);
if (cache == null) {
cache = fallback.apply(fluid);
FLUID_CACHE_A.put(fluid, cache);
}
return cache == null ? null : cache.copy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ public FluidStack getStack(@Nullable IAEItemStack stack) {

@Override
public IAEFluidStack getAEStack(ItemStack stack) {
return getAEStack(AEItemStack.fromItemStack(stack));
if (stack.isEmpty()) {
return null;
}
IAEFluidStack fluidStack = AEFluidStack.fromFluidStack(getStack(stack));
if (fluidStack == null) {
return null;
}
fluidStack.setStackSize(stack.getCount());
return fluidStack;
}

@Override
Expand Down Expand Up @@ -92,7 +100,7 @@ public IAEItemStack packAEStack(FluidStack fluid) {
if (fluid == null || fluid.amount <= 0) {
return null;
}
IAEItemStack stack = AEItemStack.fromItemStack(packStack(fluid));
IAEItemStack stack = DropLookup.lookup(fluid, f -> AEItemStack.fromItemStack(packStack(f)));
if (stack == null) {
return null;
}
Expand All @@ -105,7 +113,7 @@ public IAEItemStack packAEStackLong(IAEFluidStack fluid) {
if (fluid == null || fluid.getStackSize() <= 0) {
return null;
}
IAEItemStack stack = AEItemStack.fromItemStack(packStack(fluid.getFluidStack()));
IAEItemStack stack = DropLookup.lookup(fluid.getFluidStack(), f -> AEItemStack.fromItemStack(packStack(f)));
if (stack == null) {
return null;
}
Expand Down

0 comments on commit e07bfb0

Please sign in to comment.