Skip to content

Commit

Permalink
Vastly condense directional cache
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Dec 29, 2023
1 parent fda4df4 commit fb3fc91
Showing 1 changed file with 4 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,14 @@
import net.minecraftforge.common.util.LazyOptional;

public class DirectionalCapabilityCache<C> {
private final Map<Direction, LazyOptional<C>> directional = new HashMap<>();
private LazyOptional<C> nullDirection = LazyOptional.empty();
private final Map<String, LazyOptional<C>> holders = new HashMap<>();

public LazyOptional<C> getOrCache(Direction side, LazyOptional<?> toCache) {
if (toCache.isPresent()) {
if (side == null) {
if (!nullDirection.isPresent()) {
nullDirection = toCache.cast();
}

return nullDirection;
} else {
directional.putIfAbsent(side, toCache.cast());
return directional.get(side);
}
} else {
return LazyOptional.empty();
}
return holders.computeIfAbsent(side == null ? "null" : side.getName(), k -> toCache.cast());
}

public void invalidate() {
directional.forEach((direction, holder) -> holder.invalidate());
directional.clear();
nullDirection.invalidate();
holders.forEach((side, holder) -> holder.invalidate());
holders.clear();
}
}

0 comments on commit fb3fc91

Please sign in to comment.