private void explodeBlock(BlockPos pos) { IBlockState state = world.getBlockState(pos); Block block = state.getBlock(); if(!world.isRemote && block.canDropFromExplosion(explosion)) { List<ItemStack> drops = block.getDrops(world, pos, state, 0); ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, 0, 1f, false, null); droppedItems.addAll(drops); } if(world instanceof WorldServer) { ((WorldServer) world).spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, true, pos.getX(), pos.getY(), pos.getZ(), 2, 0, 0, 0, 0d); ((WorldServer) world).spawnParticle(EnumParticleTypes.SMOKE_LARGE, true, pos.getX(), pos.getY(), pos.getZ(), 1, 0, 0, 0, 0d); } block.onBlockExploded(world, pos, explosion); } }
NonNullList<ItemStack> drops = NonNullList.create(); state.getBlock().getDrops(drops, world, pos, state, fortune); chance = ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, fortune, chance, false, player);
NonNullList<ItemStack> items = NonNullList.create(); block.getDrops(items, world, collidePos, world.getBlockState(collidePos), 0); float chance = net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(items, world, collidePos, state, 0, 1.0f, false, null);
public enum Region { PLAINS(EnumPlantType.Plains, Blocks.GRASS, Blocks.DIRT), DESERT(EnumPlantType.Desert, Blocks.SAND); private final EnumPlantType plantType; private final Block[] soilBlocks; Region(EnumPlantType plantType, Block... soilBlocks) { this.plantType = plantType; this.soilBlocks = soilBlocks; } public EnumPlantType getPlantType() { return plantType; } public Block[] getSoilBlocks() { return soilBlocks; } }
public static void collectDrops(World world, BlockPos pos, IBlockState state, EntityPlayer player, List<ItemStack> drops) { Block block = state.getBlock(); List<ItemStack> blockDrops = new ArrayList<>(); if (block.canSilkHarvest(world, pos, state, player)) { try { Method method = ReflectionHelper.findMethod(Block.class, null, new String[] { "getSilkTouchDrop", "func_180643_i" } , IBlockState.class); ItemStack stack = (ItemStack) method.invoke(block, state); if (stack != null) { blockDrops.add(stack); ForgeEventFactory.fireBlockHarvesting(blockDrops, world, pos, state, 0, 1F, true, player); drops.addAll(blockDrops); //Add all the drops to our list } } catch (IllegalAccessException | InvocationTargetException e) {/**/} } else { blockDrops = block.getDrops(world, pos, state, 0); ForgeEventFactory.fireBlockHarvesting(blockDrops, world, pos, state, 0, 1F, false, player); drops.addAll(blockDrops); //Add all the drops to our list } }
chance = net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(items, worldIn, pos, state, fortune, chance, false, harvesters.get());
private static void harvestNetherWart(IBlockState blockState, EntityPlayer player, World world, BlockPos blockPos) { final BlockNetherWart netherWart = (BlockNetherWart) blockState.getBlock(); final ItemStack stack = player.getHeldItemMainhand(); final int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack); final List<ItemStack> drops = netherWart.getDrops(world, blockPos, blockState, fortune); // This removes exactly one seed from drops in order to make this more fair compared to vanilla // as one seed stays planted. final Item seedItem = netherWart.getItemDropped(blockState, world.rand, fortune); if (seedItem != null) { for (Iterator<ItemStack> iterator = drops.iterator(); iterator.hasNext(); ) { final ItemStack drop = iterator.next(); // Remove a seed, then break. if (!(drop.getItem() == seedItem)) { iterator.remove(); break; } } } ForgeEventFactory.fireBlockHarvesting(drops, world, blockPos, blockState, fortune, 1f, false, player); world.setBlockState(blockPos, blockState.withProperty(BlockNetherWart.AGE, 0)); for (ItemStack drop : drops) { dropItem(drop, world, blockPos); } }
private static void harvestCrops(IBlockState blockState, EntityPlayer player, World world, BlockPos blockPos) { final BlockCrops crops = (BlockCrops) blockState.getBlock(); final ItemStack stack = player.getHeldItemMainhand(); final int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack); final List<ItemStack> drops = crops.getDrops(world, blockPos, blockState, fortune); // This removes exactly one seed from drops in order to make this more fair compared to vanilla // as one seed stays planted. final Item seedItem = crops.getItemDropped(blockState, world.rand, fortune); if(seedItem != null) for(Iterator<ItemStack> iterator = drops.iterator(); iterator.hasNext();) { final ItemStack drop = iterator.next(); // Remove a seed, then break. if(!(drop.getItem() == seedItem) || crops instanceof BlockCarrot || crops instanceof BlockPotato) { iterator.remove(); break; } } ForgeEventFactory.fireBlockHarvesting(drops, world, blockPos, blockState, fortune, 1f, false, player); // Reset growth level world.setBlockState(blockPos, crops.withAge(0)); for(ItemStack drop : drops) { dropItem(drop, world, blockPos); } }
private static void harvestFruit(IBlockState blockState, EntityPlayer player, World world, BlockPos blockPos) { final PamCropGrowable blockPamFruit = (PamCropGrowable) blockState.getBlock(); final ItemStack stack = player.getHeldItemMainhand(); final int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack); final List<ItemStack> drops = blockPamFruit.getDrops(world, blockPos, blockState, fortune); // This removes exactly one fruit from drops in order to make this more fair compared to "vanilla" // as one fruit stays planted. if(drops.size() > 1) drops.remove(drops.size() - 1); ForgeEventFactory.fireBlockHarvesting(drops, world, blockPos, blockState, fortune, 1f, false, player); if(BlockPamFruit.fruitRemoval) { world.setBlockToAir(blockPos); } else { world.setBlockState(blockPos, blockState.withProperty(blockPamFruit.getAgeProperty(), 0), 3); } for(ItemStack drop : drops) { dropItem(drop, world, blockPos); } }
protected void handleNormalDrops(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, @Nonnull ItemStack stack) { harvesters.set(player); final int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack); boolean addNormalDrops = true; if (te instanceof ICustomHarvestDrops) { final ICustomHarvestDrops dropper = (ICustomHarvestDrops)te; final List<ItemStack> drops = Lists.newArrayList(); dropper.addHarvestDrops(player, drops, state, fortune, false); ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, fortune, 1.0f, false, player); for (ItemStack drop : drops) spawnAsEntity(world, pos, drop); addNormalDrops = !dropper.suppressBlockHarvestDrops(); } if (addNormalDrops) dropBlockAsItem(world, pos, state, fortune); harvesters.set(null); }
@Override public void harvestBlock(@Nonnull final World worldIn, @Nonnull EntityPlayer player, @Nonnull final BlockPos pos, @Nonnull IBlockState state, @Nullable TileEntity te, @Nonnull ItemStack stack) { if (BlockConfig.paintedGlowstoneRequireSilkTouch.get() && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) <= 0) { super.harvestBlock(worldIn, player, pos, state, te, stack); return; } // need special code so we can get the paint source from the te supressed(player); player.addExhaustion(0.025F); NNList<ItemStack> items = new NNList<ItemStack>(createPaintedDrop(te)); ForgeEventFactory.fireBlockHarvesting(items, worldIn, pos, state, 0, 1.0f, true, player); items.apply(new NNList.Callback<ItemStack>() { @Override public void apply(@Nonnull ItemStack itemStack) { spawnAsEntity(worldIn, pos, itemStack); } }); }
public static float fireFakeHarvestEventsForDropChance(NonNullList<ItemStack> drops, World world, BlockPos pos) { if (world instanceof WorldServer) { FakePlayer fake = FakePlayerFactory.getMinecraft((WorldServer) world); IBlockState state = world.getBlockState(pos); BreakEvent event = new BreakEvent(world, pos, state, fake); if (!MinecraftForge.EVENT_BUS.post(event)) { return ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, 0, 1, false, fake); } } return 0F; }
protected void handleSilkTouchDrops(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te) { List<ItemStack> items = Lists.newArrayList(); boolean addNormalDrops = true; if (te instanceof ICustomHarvestDrops) { final ICustomHarvestDrops dropper = (ICustomHarvestDrops)te; dropper.addHarvestDrops(player, items, state, 0, true); addNormalDrops = !dropper.suppressBlockHarvestDrops(); } if (addNormalDrops) { final ItemStack drop = new ItemStack(Item.getItemFromBlock(this), 1, damageDropped(state)); items.add(drop); } ForgeEventFactory.fireBlockHarvesting(items, world, pos, state, 0, 1.0f, true, player); for (ItemStack stack : items) spawnAsEntity(world, pos, stack); }
@Override protected NonNullList<ItemStack> harvestBlock(World world, BlockPos pos) { NonNullList<ItemStack> harvested = NonNullList.create(); harvested.add(new ItemStack(Blocks.CHORUS_FLOWER)); float chance = ForgeEventFactory.fireBlockHarvesting(harvested, world, pos, BLOCK_STATE, 0, 1.0F, false, null); harvested.removeIf(next -> world.rand.nextFloat() > chance); PacketFXSignal packet = new PacketFXSignal(PacketFXSignal.VisualFXType.BLOCK_BREAK, PacketFXSignal.SoundFXType.BLOCK_BREAK, pos, BLOCK_STATE); NetworkUtil.sendNetworkPacket(packet, pos, world); world.setBlockToAir(pos); return harvested; } }
Block block = blockState.getBlock(); List<ItemStack> harvested = block.getDrops(world, pos, blockState, 0); //TODO - remove once harvestcraft (and maybe others) stop using deprecated getDrops float chance = ForgeEventFactory.fireBlockHarvesting(harvested, world, pos, blockState, 0, 1.0F, false, null);
@Override public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) { player.addStat(StatList.getBlockStats(this)); player.addExhaustion(0.005F); if (EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) > 0 && te instanceof TileEntityMetadata) { List<ItemStack> items = new ArrayList<>(); TileEntityMetadata tile = (TileEntityMetadata) te; int damage = getDroppedMeta(state, tile.getTileMetadata()); ItemStack itemstack = TileEntityMetadata.getItemStack(this, damage); if (!itemstack.isEmpty()) { items.add(itemstack); } ForgeEventFactory.fireBlockHarvesting(items, worldIn, pos, state, 0, 1.0f, true, player); for (ItemStack item : items) { spawnAsEntity(worldIn, pos, item); } } }
@Override public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) { player.addStat(StatList.getBlockStats(this)); player.addExhaustion(0.005F); if (EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) > 0 && te instanceof TileEntityMetadata) { java.util.List<ItemStack> items = new java.util.ArrayList<>(); TileEntityMetadata tile = (TileEntityMetadata) te; int damage = getDroppedMeta(state, tile.getTileMetadata()); ItemStack itemstack = TileEntityMetadata.getItemStack(this, damage); if (!itemstack.isEmpty()) { items.add(itemstack); } ForgeEventFactory.fireBlockHarvesting(items, worldIn, pos, state, 0, 1.0f, true, player); for (ItemStack item : items) { spawnAsEntity(worldIn, pos, item); } } }
private void doHarvest(@Nonnull final IFarmer farm, @Nonnull final World world, @Nonnull IBlockState blockState, @Nonnull final BlockPos pos, int fortune, @Nonnull final HarvestResult result) { FakePlayer joe = farm.startUsingItem(FarmingTool.AXE); NNList<ItemStack> drops = new NNList<>(); blockState.getBlock().getDrops(drops, world, pos, blockState, fortune); final float chance = ForgeEventFactory.fireBlockHarvesting(drops, world, pos, blockState, fortune, 1f, false, joe); // flowers drop here by spawning their drops into the world (joe's world captures those) blockState.getBlock().harvestBlock(joe.world, joe, pos, blockState, null, joe.getHeldItemMainhand()); NNList.wrap(drops).apply(new Callback<ItemStack>() { @Override public void apply(@Nonnull ItemStack drop) { if (farm.getWorld().rand.nextFloat() <= chance) { result.getDrops().add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, drop.copy())); } } }); NNList.wrap(farm.endUsingItem(FarmingTool.AXE)).apply(new Callback<ItemStack>() { @Override public void apply(@Nonnull ItemStack drop) { result.getDrops().add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, drop.copy())); } }); farm.registerAction(FarmingAction.HARVEST, FarmingTool.AXE, blockState, pos); world.setBlockToAir(pos); result.getHarvestedBlocks().add(pos); }
private boolean doMultiHarvest(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos bc) { IBlockState bs = world.getBlockState(bc); Block block = bs.getBlock(); bs = bs.getActualState(world, bc); ItemStack held = player.getHeldItemMainhand(); NNList<ItemStack> drops = new NNList<>(); block.getDrops(drops, world, bc, bs, 0); float chance = ForgeEventFactory.fireBlockHarvesting(drops, world, bc, bs, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, held), 1, EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, held) != 0, player); world.setBlockToAir(bc); boolean usedPower = false; for (ItemStack stack : drops) { if (world.rand.nextFloat() <= chance) { world.spawnEntity(new EntityItem(world, bc.getX() + 0.5, bc.getY() + 0.5, bc.getZ() + 0.5, stack.copy())); EnergyUpgradeManager.extractEnergy(player.getHeldItemMainhand(), this, DarkSteelConfig.crookEnergyPerDamageMulti, false); usedPower = true; } } return usedPower; }
private boolean doMultiHarvest(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos bc) { IBlockState bs = world.getBlockState(bc); Block block = bs.getBlock(); bs = bs.getActualState(world, bc); ItemStack held = player.getHeldItemMainhand(); NNList<ItemStack> drops = new NNList<>(); block.getDrops(drops, world, bc, bs, 0); float chance = ForgeEventFactory.fireBlockHarvesting(drops, world, bc, bs, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, held), 1, EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, held) != 0, player); world.setBlockToAir(bc); boolean usedPower = false; for (ItemStack stack : drops) { if (world.rand.nextFloat() <= chance) { world.spawnEntity(new EntityItem(world, bc.getX() + 0.5, bc.getY() + 0.5, bc.getZ() + 0.5, stack.copy())); if (FarmersRegistry.isLog(block)) { // other wise leaves EnergyUpgradeManager.extractEnergy(player.getHeldItemMainhand(), this, DarkSteelConfig.axePowerUsePerDamagePointMultiHarvest, false); usedPower = true; } } } return usedPower; }