Java Code Examples for net.minecraft.block.Block#getDrops()

The following examples show how to use net.minecraft.block.Block#getDrops() . 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: BlockSnowTest.java    From customstuff4 with GNU General Public License v3.0 7 votes vote down vote up
@Test
public void test_getDrops()
{
    ContentBlockSnow content = new ContentBlockSnow();
    content.id = "test_getDrops";
    content.snowball = new WrappedItemStackConstant(new ItemStack(Items.APPLE, 3));
    content.drop = Attribute.constant(new BlockDrop[] {new BlockDrop(new WrappedItemStackConstant(new ItemStack(Items.STICK)), IntRange.create(2, 2))});

    Block block = content.createBlock();

    NonNullList<ItemStack> drops = NonNullList.create();
    block.getDrops(drops, null, null, block.getDefaultState().withProperty(BlockSnow.LAYERS, 5), 0);
    ItemStack drop1 = drops.get(0);
    ItemStack drop2 = drops.get(1);

    assertEquals(2, drops.size());

    assertSame(Items.APPLE, drop1.getItem());
    assertEquals(18, drop1.getCount());

    assertSame(Items.STICK, drop2.getItem());
    assertEquals(2, drop2.getCount());
}
 
Example 2
Source File: TileEntityTreeFarm.java    From AdvancedMod with GNU General Public License v3.0 7 votes vote down vote up
private void cutTree(){
    if(inventory != null) {
        Block block = worldObj.getBlock(xCoord, yCoord + 2, zCoord);
        int offsetY = yCoord + 2;
        while(block.getMaterial() == Material.wood) {
            List<ItemStack> items = block.getDrops(worldObj, xCoord, offsetY, zCoord, worldObj.getBlockMetadata(xCoord, offsetY, zCoord), 0);
            for(ItemStack item : items) {
                ItemStack remainder = TileEntityHopper.func_145889_a(inventory, item, 0);
                if(remainder != null) {
                    worldObj.spawnEntityInWorld(new EntityItem(worldObj, xCoord + 0.5, yCoord + 2.5, zCoord + 0.5, remainder));
                }
            }
            worldObj.setBlock(xCoord, offsetY, zCoord, Blocks.air);
            offsetY++;
            block = worldObj.getBlock(xCoord, offsetY, zCoord);
        }
    }
}
 
Example 3
Source File: DroneAIDig.java    From PneumaticCraft with GNU General Public License v3.0 7 votes vote down vote up
@Override
protected boolean isValidPosition(ChunkPosition pos){
    Block block = worldCache.getBlock(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
    if(!worldCache.isAirBlock(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ) && !ignoreBlock(block)) {
        int meta = worldCache.getBlockMetadata(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
        List<ItemStack> droppedStacks;
        if(block.canSilkHarvest(drone.getWorld(), drone.getFakePlayer(), pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, meta)) {
            droppedStacks = Arrays.asList(new ItemStack[]{getSilkTouchBlock(block, meta)});
        } else {
            droppedStacks = block.getDrops(drone.getWorld(), pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, meta, 0);
        }
        for(ItemStack droppedStack : droppedStacks) {
            if(widget.isItemValidForFilters(droppedStack, meta)) {
                swapBestItemToFirstSlot(block, pos);
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: DroneAIDig.java    From PneumaticCraft with GNU General Public License v3.0 7 votes vote down vote up
public static boolean isBlockValidForFilter(IBlockAccess worldCache, IDroneBase drone, ChunkPosition pos, ProgWidgetAreaItemBase widget){
    int x = pos.chunkPosX;
    int y = pos.chunkPosY;
    int z = pos.chunkPosZ;
    Block block = worldCache.getBlock(x, y, z);

    if(!block.isAir(worldCache, x, y, z)) {
        int meta = worldCache.getBlockMetadata(x, y, z);
        List<ItemStack> droppedStacks;
        if(block.canSilkHarvest(drone.getWorld(), drone.getFakePlayer(), x, y, z, meta)) {
            droppedStacks = Arrays.asList(new ItemStack[]{getSilkTouchBlock(block, meta)});
        } else {
            droppedStacks = block.getDrops(drone.getWorld(), x, y, z, meta, 0);
        }
        for(ItemStack droppedStack : droppedStacks) {
            if(widget.isItemValidForFilters(droppedStack, meta)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 5
Source File: RenderBlockTileEntity.java    From MiningGadgets with MIT License 6 votes vote down vote up
public void setBlockAllowed() {
    if (!UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.VOID_JUNK)) {
        this.blockAllowed = true;
        return;
    }
    PlayerEntity player = world.getPlayerByUuid(playerUUID);
    if (player == null) return;
    int silk = 0;
    int fortune = 0;

    ItemStack tempTool = new ItemStack(ModItems.MININGGADGET.get());

    // If silk is in the upgrades, apply it without a tier.
    if (UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.SILK)) {
        tempTool.addEnchantment(Enchantments.SILK_TOUCH, 1);
        silk = 1;
    }

    // FORTUNE_1 is eval'd against the basename so this'll support all fortune upgrades
    if (UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.FORTUNE_1)) {
        Optional<Upgrade> upgrade = UpgradeTools.getUpgradeFromList(gadgetUpgrades, Upgrade.FORTUNE_1);
        if (upgrade.isPresent()) {
            fortune = upgrade.get().getTier();
            tempTool.addEnchantment(Enchantments.FORTUNE, fortune);
        }
    }

    List<ItemStack> drops = Block.getDrops(renderBlock, (ServerWorld) world, this.pos, null, player, tempTool);

    this.blockAllowed = blockAllowed(drops, getGadgetFilters(), isGadgetIsWhitelist());
}
 
Example 6
Source File: TileAuraPylon.java    From Gadomancy with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void breakTile() {
    if(!isPartOfMultiblock || worldObj.isRemote) return;

    int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
    Block pylon = worldObj.getBlock(xCoord, yCoord, zCoord);
    if(pylon != null) {
        ArrayList<ItemStack> stacks = pylon.getDrops(worldObj, xCoord, yCoord, zCoord, meta, 0);
        for(ItemStack i : stacks) {
            EntityItem item = new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, i);
            //ItemUtils.applyRandomDropOffset(item, worldObj.rand);
            worldObj.spawnEntityInWorld(item);
        }
    }
    worldObj.removeTileEntity(xCoord, yCoord, zCoord);
    worldObj.setBlockToAir(xCoord, yCoord, zCoord);
    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
 
Example 7
Source File: TileAuraPylonTop.java    From Gadomancy with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void breakTile() {
    int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
    Block pylon = worldObj.getBlock(xCoord, yCoord, zCoord);
    if(pylon != null) {
        ArrayList<ItemStack> stacks = pylon.getDrops(worldObj, xCoord, yCoord, zCoord, meta, 0);
        for(ItemStack i : stacks) {
            EntityItem item = new EntityItem(worldObj, xCoord, yCoord, zCoord, i);
            worldObj.spawnEntityInWorld(item);
        }
    }
    worldObj.removeTileEntity(xCoord, yCoord, zCoord);
    worldObj.setBlockToAir(xCoord, yCoord, zCoord);
    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
 
Example 8
Source File: RenderBlockTileEntity.java    From MiningGadgets with MIT License 5 votes vote down vote up
private void removeBlock() {
    if(world == null || world.isRemote)
        return;

    PlayerEntity player = world.getPlayerByUuid(playerUUID);
    if (player == null)
        return;

    int silk = 0;
    int fortune = 0;

    ItemStack tempTool = new ItemStack(ModItems.MININGGADGET.get());

    // If silk is in the upgrades, apply it without a tier.
    if (UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.SILK)) {
        tempTool.addEnchantment(Enchantments.SILK_TOUCH, 1);
        silk = 1;
    }

    // FORTUNE_1 is eval'd against the basename so this'll support all fortune upgrades
    if (UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.FORTUNE_1)) {
        Optional<Upgrade> upgrade = UpgradeTools.getUpgradeFromList(gadgetUpgrades, Upgrade.FORTUNE_1);
        if (upgrade.isPresent()) {
            fortune = upgrade.get().getTier();
            tempTool.addEnchantment(Enchantments.FORTUNE, fortune);
        }
    }

    List<ItemStack> drops = Block.getDrops(renderBlock, (ServerWorld) world, this.pos, null, player, tempTool);

    if ( blockAllowed ) {
        int exp = renderBlock.getExpDrop(world, pos, fortune, silk);
        boolean magnetMode = (UpgradeTools.containsActiveUpgradeFromList(gadgetUpgrades, Upgrade.MAGNET));
        for (ItemStack drop : drops) {
            if (drop != null) {
                if (magnetMode) {
                    int wasPickedUp = ForgeEventFactory.onItemPickup(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), drop), player);
                    // 1  = someone allowed the event meaning it's handled,
                    // -1 = someone blocked the event and thus we shouldn't drop it nor insert it
                    // 0  = no body captured the event and we should handle it by hand.
                    if( wasPickedUp == 0 ) {
                        if (!player.addItemStackToInventory(drop))
                            Block.spawnAsEntity(world, pos, drop);
                    }
                } else {
                    Block.spawnAsEntity(world, pos, drop);
                }
            }
        }
        if (magnetMode) {
            if (exp > 0)
                player.giveExperiencePoints(exp);
        } else {
            if (exp > 0)
                renderBlock.getBlock().dropXpOnBlockBreak(world, pos, exp);
        }

        renderBlock.spawnAdditionalDrops(world, pos, tempTool); // Fixes silver fish basically...
    }

    world.removeTileEntity(this.pos);
    world.setBlockState(this.pos, Blocks.AIR.getDefaultState());

    // Add to the break stats
    player.addStat(Stats.BLOCK_MINED.get(renderBlock.getBlock()));

    // Handle special cases
    if(SpecialBlockActions.getRegister().containsKey(renderBlock.getBlock()))
        SpecialBlockActions.getRegister().get(renderBlock.getBlock()).accept(world, pos, renderBlock);
}