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());
}
}
Aggregations