NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockAnimalGenerator.java
2018-11-29 00:47:21 +01:00

68 lines
2.7 KiB
Java

package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityAnimalGenerator;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.INpc;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class BlockAnimalGenerator extends BlockContainerImpl {
public BlockAnimalGenerator() {
super(Material.ROCK, "animal_generator", TileEntityAnimalGenerator.class, "animal_generator");
this.setSoundType(SoundType.WOOD);
this.setHardness(2F);
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onEntityDeath(LivingDeathEvent event) {
EntityLivingBase entity = event.getEntityLiving();
if (entity.world.isRemote || !(entity instanceof IAnimals) || entity instanceof IMob || entity instanceof INpc)
return;
BlockPos pos = entity.getPosition();
Helper.getTileEntitiesInArea(entity.world, pos, 5, tile -> {
if (!(tile instanceof TileEntityAnimalGenerator))
return false;
TileEntityAnimalGenerator gen = (TileEntityAnimalGenerator) tile;
entity.getEntityData().setBoolean(NaturesAura.MOD_ID + ":no_drops", true);
if (gen.isBusy())
return false;
boolean child = entity.isChild();
int time = child ? 60 : 120;
int amount = child ? 45 : 70;
gen.setGenerationValues(time, amount);
BlockPos genPos = gen.getPos();
PacketHandler.sendToAllAround(entity.world, pos, 32, new PacketParticles(
(float) entity.posX, (float) entity.posY, (float) entity.posZ, 17,
child ? 1 : 0,
(int) (entity.getEyeHeight() * 10F),
genPos.getX(), genPos.getY(), genPos.getZ()));
return true;
});
}
@SubscribeEvent
public void onEntityDrops(LivingDropsEvent event) {
EntityLivingBase entity = event.getEntityLiving();
if (entity.world.isRemote)
return;
if (entity.getEntityData().getBoolean(NaturesAura.MOD_ID + ":no_drops")) {
event.setCanceled(true);
}
}
}