Search in sources :

Example 1 with CriticalMiningEvent

use of cavern.api.event.CriticalMiningEvent in project Cavern2 by kegare.

the class CaveEventHooks method onHarvestDrops.

@SubscribeEvent
public void onHarvestDrops(HarvestDropsEvent event) {
    if (!GeneralConfig.criticalMining || event.isSilkTouching()) {
        return;
    }
    World world = event.getWorld();
    if (world.isRemote) {
        return;
    }
    EntityPlayer player = event.getHarvester();
    if (player == null || player instanceof FakePlayer || !CavernAPI.dimension.isInCaveDimensions(player)) {
        return;
    }
    IBlockState state = event.getState();
    if (MinerStats.getPointAmount(state) <= 0) {
        return;
    }
    if (state.getMaterial() != Material.ROCK) {
        return;
    }
    MinerRank rank = MinerRank.get(MinerStats.get(player).getRank());
    float f = rank.getBoost();
    if (f <= 1.0F) {
        return;
    }
    f = (f - 1.0F) * 0.3F;
    ItemStack held = player.getHeldItemMainhand();
    String tool = state.getBlock().getHarvestTool(state);
    if (held.isEmpty() || tool == null) {
        return;
    }
    int toolLevel = held.getItem().getHarvestLevel(held, tool, player, state);
    if (toolLevel <= 0) {
        return;
    }
    f *= 1.0F + toolLevel * 0.1F;
    List<ItemStack> originalDrops = event.getDrops();
    List<ItemStack> drops = Lists.newArrayList();
    for (ItemStack stack : originalDrops) {
        if (!stack.isEmpty() && !(stack.getItem() instanceof ItemBlock) && RANDOM.nextFloat() <= f) {
            drops.add(stack.copy());
        }
    }
    if (!drops.isEmpty()) {
        CriticalMiningEvent criticalEvent = new CriticalMiningEvent(world, event.getPos(), state, player, event.getFortuneLevel(), originalDrops, drops);
        if (MinecraftForge.EVENT_BUS.post(criticalEvent)) {
            return;
        }
        player.sendStatusMessage(new TextComponentTranslation("cavern.message.mining.critical"), true);
        originalDrops.addAll(criticalEvent.getBonusDrops());
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IBlockState(net.minecraft.block.state.IBlockState) CriticalMiningEvent(cavern.api.event.CriticalMiningEvent) World(net.minecraft.world.World) ItemBlock(net.minecraft.item.ItemBlock) FakePlayer(net.minecraftforge.common.util.FakePlayer) MinerRank(cavern.stats.MinerRank) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

CriticalMiningEvent (cavern.api.event.CriticalMiningEvent)1 MinerRank (cavern.stats.MinerRank)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemBlock (net.minecraft.item.ItemBlock)1 ItemStack (net.minecraft.item.ItemStack)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 World (net.minecraft.world.World)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1