Skip to content

Commit

Permalink
Remember that NonNullList is a thing that exists
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Dec 29, 2023
1 parent fb3fc91 commit 070cac4
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package gripe._90.aecapfix.misc;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraftforge.common.util.LazyOptional;

public class DirectionalCapabilityCache<C> {
private final Map<String, LazyOptional<C>> holders = new HashMap<>();
private final List<LazyOptional<C>> holders = NonNullList.withSize(7, LazyOptional.empty());

public LazyOptional<C> getOrCache(Direction side, LazyOptional<?> toCache) {
return holders.computeIfAbsent(side == null ? "null" : side.getName(), k -> toCache.cast());
var index = side != null ? side.get3DDataValue() : 6;

if (!holders.get(index).isPresent() && toCache.isPresent()) {
holders.set(index, toCache.cast());
}

return holders.get(index);
}

public void invalidate() {
holders.forEach((side, holder) -> holder.invalidate());
holders.forEach(LazyOptional::invalidate);
holders.clear();
}
}

0 comments on commit 070cac4

Please sign in to comment.