Java Code Examples for net.minecraftforge.common.util.LazyOptional#cast()

The following examples show how to use net.minecraftforge.common.util.LazyOptional#cast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Gets a capability from the block in <code>to</code> direction from {@link CapabilityCache}'s
 * position. For example, calling this with <code>NORTH</code>, will get a capability from the block
 * IN <code>NORTH</code> direction on ITS <code>SOUTH</code> face.
 *
 * @param capability The capability to get.
 * @param to         The direction to ask.
 * @return A {@link LazyOptional} of the capability, may be empty.
 */
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction to) {
    Objects.requireNonNull(capability, "Null capability.");
    if (world == null || pos == null) {
        return LazyOptional.empty().cast();
    }
    Map<Capability<?>, Object2IntPair<LazyOptional<?>>> sideCache = getCacheForSide(to);
    Object2IntPair<LazyOptional<?>> cache = sideCache.get(capability);
    if (cache == null) {
        cache = new Object2IntPair<>(null, ticks);
        sideCache.put(capability, cache);
        return tryReCache(capability, to, cache).cast();
    }
    LazyOptional<?> lookup = cache.getKey();
    if (lookup == null || !lookup.isPresent()) {
        return tryReCache(capability, to, cache).cast();
    }
    return lookup.cast();
}
 
Example 2
Source File: Capability.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public @Nonnull <R> LazyOptional<R> orEmpty(Capability<R> toCheck, LazyOptional<T> inst) {
	return this == toCheck ? inst.cast() : LazyOptional.empty();
}