Search in sources :

Example 1 with HarvestingProperty

use of org.spongepowered.api.data.property.item.HarvestingProperty in project SpongeCommon by SpongePowered.

the class HarvestingPropertyStore method getFor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Optional<HarvestingProperty> getFor(ItemStack itemStack) {
    final Item item = itemStack.getItem();
    if (item instanceof ItemTool && !(item instanceof ItemPickaxe)) {
        final ImmutableSet<BlockType> blocks = ImmutableSet.copyOf((Set) ((ItemTool) item).effectiveBlocks);
        return Optional.of(new HarvestingProperty(blocks));
    }
    final Collection<BlockType> blockTypes = SpongeImpl.getRegistry().getAllOf(BlockType.class);
    final ImmutableSet.Builder<BlockType> builder = ImmutableSet.builder();
    blockTypes.stream().filter(blockType -> item.canHarvestBlock((IBlockState) blockType.getDefaultState())).forEach(builder::add);
    final ImmutableSet<BlockType> blocks = builder.build();
    if (blocks.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(new HarvestingProperty(blocks));
}
Also used : SpongeImpl(org.spongepowered.common.SpongeImpl) ImmutableSet(com.google.common.collect.ImmutableSet) ItemTool(net.minecraft.item.ItemTool) Item(net.minecraft.item.Item) ItemPickaxe(net.minecraft.item.ItemPickaxe) Collection(java.util.Collection) Set(java.util.Set) IBlockState(net.minecraft.block.state.IBlockState) ItemStack(net.minecraft.item.ItemStack) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) AbstractItemStackPropertyStore(org.spongepowered.common.data.property.store.common.AbstractItemStackPropertyStore) HarvestingProperty(org.spongepowered.api.data.property.item.HarvestingProperty) Item(net.minecraft.item.Item) ItemTool(net.minecraft.item.ItemTool) ImmutableSet(com.google.common.collect.ImmutableSet) BlockType(org.spongepowered.api.block.BlockType) ItemPickaxe(net.minecraft.item.ItemPickaxe) HarvestingProperty(org.spongepowered.api.data.property.item.HarvestingProperty)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Set (java.util.Set)1 IBlockState (net.minecraft.block.state.IBlockState)1 Item (net.minecraft.item.Item)1 ItemPickaxe (net.minecraft.item.ItemPickaxe)1 ItemStack (net.minecraft.item.ItemStack)1 ItemTool (net.minecraft.item.ItemTool)1 BlockType (org.spongepowered.api.block.BlockType)1 HarvestingProperty (org.spongepowered.api.data.property.item.HarvestingProperty)1 SpongeImpl (org.spongepowered.common.SpongeImpl)1 AbstractItemStackPropertyStore (org.spongepowered.common.data.property.store.common.AbstractItemStackPropertyStore)1