net.minecraftforge.common.util.LazyOptional Java Examples

The following examples show how to use net.minecraftforge.common.util.LazyOptional. 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: CapabilityDispatcher.java    From patchwork-api with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction side) {
	for (ICapabilityProvider provider : providers) {
		LazyOptional<T> ret = provider.getCapability(cap, side);

		//noinspection ConstantConditions
		if (ret == null) {
			throw new RuntimeException(
					String.format(
							"Provider %s.getCapability() returned null; return LazyOptional.empty() instead!",
							provider.getClass().getTypeName()
					)
			);
		}

		if (ret.isPresent()) {
			return ret;
		}
	}

	return LazyOptional.empty();
}
 
Example #2
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
private LazyOptional<?> tryReCache(Capability<?> capability, Direction to, Object2IntPair<LazyOptional<?>> cache) {
    boolean isFirst = cache.getKey() == null;
    if (isFirst || !cache.getKey().isPresent()) {
        if (isFirst || cache.getValue() + waitTicks <= ticks) {
            LazyOptional<?> lookup = requestCapability(capability, to);
            if (lookup.isPresent()) {
                cache.setKey(lookup);
                cache.setValue(ticks);
                lookup.addListener(l -> {//TODO, probably not needed? we check every lookup anyway..
                    //When the LazyOptional notifies us that its gone,
                    //set the cache to empty, and mark ticks.
                    cache.setKey(LazyOptional.empty());
                    cache.setValue(ticks);
                });
            } else {
                cache.setKey(LazyOptional.empty());
                cache.setValue(ticks);
            }
        }
    }
    return cache.getKey();
}
 
Example #3
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 #4
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Notifies {@link CapabilityCache} of a {@link Block#onNeighborChange} event.<br/>
 * Marks all empty capabilities provided by <code>from</code> block, to be re-cached
 * next query.
 *
 * @param from The from position.
 */
public void onNeighborChanged(BlockPos from) {
    if (world == null || pos == null) {
        return;
    }
    BlockPos offset = from.subtract(pos);
    int diff = MathHelper.absSum(offset);
    int side = MathHelper.toSide(offset);
    if (side < 0 || diff != 1) {
        return;
    }
    Direction sideChanged = Direction.BY_INDEX[side];

    Iterables.concat(selfCache.entrySet(), getCacheForSide(sideChanged).entrySet()).forEach(entry -> {
        Object2IntPair<LazyOptional<?>> pair = entry.getValue();
        if (pair.getKey() != null && !pair.getKey().isPresent()) {
            pair.setKey(null);
            pair.setValue(ticks);
        }
    });
}
 
Example #5
Source File: TileEnderTank.java    From EnderStorage with MIT License 5 votes vote down vote up
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    if (!removed && cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
        return fluidHandler.cast();
    }
    return super.getCapability(cap, side);
}
 
Example #6
Source File: DryingRackTileEntity.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing)
{
    if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
        return itemsProvider.cast();
    return super.getCapability(capability, facing);
}
 
Example #7
Source File: ItemBreakingTracker.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SubscribeEvent
public void attachCapabilities(AttachCapabilitiesEvent<Entity> e)
{
    if (!ConfigManager.SERVER.enableScraping.get())
        return;
    final Entity entity = e.getObject();

    if (!(entity instanceof ServerPlayerEntity))
        return;

    if (entity.world == null || entity.world.isRemote)
        return;

    e.addCapability(PROP_KEY, new ICapabilityProvider()
    {
        ItemBreakingTracker cap = new ItemBreakingTracker();
        LazyOptional<ItemBreakingTracker> cap_provider = LazyOptional.of(() -> cap);

        {
            cap.init(entity, entity.world);
        }

        @SuppressWarnings("unchecked")
        @Override
        public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing)
        {
            if (capability == TRACKER)
                return cap_provider.cast();
            return LazyOptional.empty();
        }
    });
}
 
Example #8
Source File: SawmillTileEntity.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing)
{
    if (capability == ITEMS_CAP)
    {
        if (facing == Direction.UP) return top_provider.cast();
        if (facing == Direction.DOWN) return bottom_provider.cast();
        if (facing != null) return sides_provider.cast();
        return combined_provider.cast();
    }

    return super.getCapability(capability, facing);
}
 
Example #9
Source File: ChoppingBlockTileEntity.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@Nonnull
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side)
{
    if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
        return slotInventoryGetter.cast();
    return super.getCapability(cap, side);
}
 
Example #10
Source File: ChoppingBlockRenderer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void render(ChoppingBlockTileEntity te, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffer, int p_225616_5_, int p_225616_6_)
{
    BlockState state = te.getWorld().getBlockState(te.getPos());
    if (!(state.getBlock() instanceof ChoppingBlock))
        return;

    //if (destroyStage < 0)
    {
        matrixStack.push();

        ItemRenderer itemRenderer = mc.getItemRenderer();

        LazyOptional<IItemHandler> linv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
        linv.ifPresent((inv) -> {
            ItemStack stack = inv.getStackInSlot(0);
            if (stack.getCount() > 0)
            {
                matrixStack.push();
                matrixStack.translate(0.5, 0.5, 0.5);

                matrixStack.translate(0, -4.5 / 16.0f, 0);
                matrixStack.scale(2, 2, 2);

                IBakedModel ibakedmodel = itemRenderer.getItemModelWithOverrides(stack, te.getWorld(), (LivingEntity) null);
                itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.GROUND, true, matrixStack, buffer, p_225616_5_, p_225616_6_, ibakedmodel);
                /*int breakStage = te.getBreakStage();
                if (breakStage >= 0)
                {
                    renderItem(stack, ItemCameraTransforms.TransformType.GROUND, breakStage);
                }*/

                matrixStack.pop();
            }
        });

        matrixStack.pop();
    }
}
 
Example #11
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
private LazyOptional<?> requestCapability(Capability<?> capability, Direction to) {
    TileEntity tile = world.getTileEntity(pos.offset(to));
    Direction inverse = to == null ? null : to.getOpposite();
    if (tile != null) {
        return tile.getCapability(capability, inverse);
    }
    return LazyOptional.empty();
}
 
Example #12
Source File: SimpleCapProvider.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Nonnull
@Override
public <R> LazyOptional<R> getCapability(@Nonnull Capability<R> cap, @Nullable Direction side) {
    if (capability == cap) {
        return instanceOpt.cast();
    }
    return null;
}
 
Example #13
Source File: TileEnderTank.java    From EnderStorage with MIT License 5 votes vote down vote up
@Override
public void onFrequencySet() {
    if (world == null) {
        return;
    }
    if (!world.isRemote) {
        liquid_state.setFrequency(frequency);
    }
    fluidHandler.invalidate();
    fluidHandler = LazyOptional.of(this::getStorage);
}
 
Example #14
Source File: TileEnderChest.java    From EnderStorage with MIT License 5 votes vote down vote up
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    if (!removed && cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
        return itemHandler.cast();
    }
    return super.getCapability(cap, side);
}
 
Example #15
Source File: ModificationTableTileEntity.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
        return handler.cast();
    }
    return super.getCapability(cap, side);
}
 
Example #16
Source File: ModificationTable.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
    if (newState.getBlock() != this) {
        TileEntity tileEntity = worldIn.getTileEntity(pos);
        if (tileEntity != null) {
            LazyOptional<IItemHandler> cap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
            cap.ifPresent(handler -> {
                for(int i = 0; i < handler.getSlots(); ++i) {
                    InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), handler.getStackInSlot(i));
                }
            });
        }
        super.onReplaced(state, worldIn, pos, newState, isMoving);
    }
}
 
Example #17
Source File: CapabilityProvider.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
@Nonnull
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
	final CapabilityDispatcher disp = getCapabilities();
	return !valid || disp == null ? LazyOptional.empty() : disp.getCapability(cap, side);
}
 
Example #18
Source File: SimpleCapProvider.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
public SimpleCapProvider(Capability<T> capability, T instance) {
    this.capability = capability;
    this.instance = instance;
    instanceOpt = LazyOptional.of(() -> this.instance);
}
 
Example #19
Source File: TileEnderChest.java    From EnderStorage with MIT License 4 votes vote down vote up
@Override
public void onFrequencySet() {
    itemHandler.invalidate();
    itemHandler = LazyOptional.of(() -> new InvWrapper(getStorage()));
}
 
Example #20
Source File: CapabilityEnergyProvider.java    From MiningGadgets with MIT License 4 votes vote down vote up
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    return cap == CapabilityEnergy.ENERGY ? capability.cast() : LazyOptional.empty();
}
 
Example #21
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Map<Capability<?>, Object2IntPair<LazyOptional<?>>> getCacheForSide(Direction side) {
    if (side == null) {
        return selfCache;
    }
    return sideCache.computeIfAbsent(side, s -> new HashMap<>());
}
 
Example #22
Source File: CapabilityProviderHolder.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Nonnull
default <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap) {
	return getCapabilityProvider().getCapability(cap);
}
 
Example #23
Source File: CapabilityProviderHolder.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Nonnull
default <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
	return getCapabilityProvider().getCapability(cap, side);
}
 
Example #24
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();
}
 
Example #25
Source File: ItemBreakingTracker.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static LazyOptional<ItemBreakingTracker> get(PlayerEntity p)
{
    return p.getCapability(Handler.TRACKER, null);
}
 
Example #26
Source File: ICapabilityProvider.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Nonnull
default <T> LazyOptional<T> getCapability(@Nonnull final Capability<T> cap) {
	return getCapability(cap, null);
}
 
Example #27
Source File: ICapabilityProvider.java    From patchwork-api with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Retrieves the {@link LazyOptional optional} handler for the capability requested on the specific side.
 * The return value <strong>CAN</strong> be the same for multiple faces.
 *
 * <p>Modders are encouraged to cache this value, using the listener capabilities of the optional to
 * be notified if the requested capability get lost.
 *
 * @param capability The {@link Capability capability} to check
 * @param direction  The {@link Direction direction} to check from,
 *                   <strong>CAN BE NULL</strong>. Null is defined to represent 'internal' or 'self'
 * @return The requested a {@link LazyOptional optional} holding the requested capability.
 */
@Nonnull
<T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction direction);