NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockAnimalGenerator.java

120 lines
5.1 KiB
Java
Raw Normal View History

2018-11-29 00:47:21 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityAnimalGenerator;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
2020-01-23 16:05:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2018-11-29 00:47:21 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.INPC;
2019-11-04 19:08:49 +01:00
import net.minecraft.entity.LivingEntity;
2018-11-29 00:47:21 +01:00
import net.minecraft.entity.monster.IMob;
2019-11-04 19:08:49 +01:00
import net.minecraft.entity.passive.AnimalEntity;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2019-01-27 13:57:34 +01:00
import net.minecraft.util.math.AxisAlignedBB;
2018-11-29 00:47:21 +01:00
import net.minecraft.util.math.BlockPos;
2019-01-27 01:38:55 +01:00
import net.minecraft.util.math.MathHelper;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.Level;
2019-11-04 19:08:49 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-11-29 00:47:21 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
2019-01-27 01:38:55 +01:00
import net.minecraftforge.event.entity.living.LivingEvent;
2018-11-29 13:54:32 +01:00
import net.minecraftforge.event.entity.living.LivingExperienceDropEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2018-11-29 00:47:21 +01:00
2020-01-29 01:34:58 +01:00
public class BlockAnimalGenerator extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
2018-11-29 00:47:21 +01:00
public BlockAnimalGenerator() {
2021-12-04 15:40:09 +01:00
super("animal_generator", BlockEntityAnimalGenerator::new, Properties.create(Material.ROCK).hardnessAndResistance(3F).sound(SoundType.STONE));
2018-11-29 00:47:21 +01:00
MinecraftForge.EVENT_BUS.register(this);
}
2019-01-27 01:38:55 +01:00
@SubscribeEvent
public void onLivingUpdate(LivingEvent.LivingUpdateEvent event) {
2019-10-20 22:30:49 +02:00
LivingEntity entity = event.getEntityLiving();
2021-12-04 15:40:09 +01:00
if (entity.level.isClientSide || entity.level.getGameTime() % 40 != 0 || !(entity instanceof AnimalEntity) || entity instanceof IMob || entity instanceof INPC)
2019-01-27 01:38:55 +01:00
return;
2021-12-04 15:40:09 +01:00
CompoundTag data = entity.getPersistentData();
2019-11-04 19:08:49 +01:00
int timeAlive = data.getInt(NaturesAura.MOD_ID + ":time_alive");
data.putInt(NaturesAura.MOD_ID + ":time_alive", timeAlive + 40);
2019-01-27 01:38:55 +01:00
}
2018-11-29 00:47:21 +01:00
@SubscribeEvent
public void onEntityDeath(LivingDeathEvent event) {
2019-10-20 22:30:49 +02:00
LivingEntity entity = event.getEntityLiving();
2021-12-04 15:40:09 +01:00
if (entity.level.isClientSide || !(entity instanceof AnimalEntity) || entity instanceof IMob || entity instanceof INPC)
2018-11-29 00:47:21 +01:00
return;
BlockPos pos = entity.getPosition();
2021-12-04 15:40:09 +01:00
Helper.getTileEntitiesInArea(entity.level, pos, 5, tile -> {
if (!(tile instanceof BlockEntityAnimalGenerator))
2018-11-29 00:47:21 +01:00
return false;
2021-12-04 15:40:09 +01:00
BlockEntityAnimalGenerator gen = (BlockEntityAnimalGenerator) tile;
2019-01-27 01:38:55 +01:00
2021-12-04 15:40:09 +01:00
CompoundTag data = entity.getPersistentData();
2019-11-04 19:08:49 +01:00
data.putBoolean(NaturesAura.MOD_ID + ":no_drops", true);
2018-11-29 00:47:21 +01:00
if (gen.isBusy())
return false;
boolean child = entity.isChild();
2019-01-27 01:38:55 +01:00
float timeMod = child ? 0.5F : 1;
float amountMod = child ? 0.667F : 1;
2019-11-04 19:08:49 +01:00
int timeAlive = data.getInt(NaturesAura.MOD_ID + ":time_alive");
2019-01-29 22:50:27 +01:00
int time = Math.min(MathHelper.floor((timeAlive - 15000) / 500F * timeMod), 200);
int amount = Math.min(MathHelper.floor((timeAlive - 8000) / 2F * amountMod), 25000);
2019-01-27 01:38:55 +01:00
if (time <= 0 || amount <= 0)
return false;
2018-11-29 00:47:21 +01:00
gen.setGenerationValues(time, amount);
BlockPos genPos = gen.getPos();
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(entity.level, pos, 32, new PacketParticles(
2020-01-28 18:08:56 +01:00
(float) entity.getPosX(), (float) entity.getPosY(), (float) entity.getPosZ(), PacketParticles.Type.ANIMAL_GEN_CONSUME,
2018-11-29 00:47:21 +01:00
child ? 1 : 0,
(int) (entity.getEyeHeight() * 10F),
genPos.getX(), genPos.getY(), genPos.getZ()));
2019-11-04 19:08:49 +01:00
2018-11-29 00:47:21 +01:00
return true;
});
}
@SubscribeEvent
public void onEntityDrops(LivingDropsEvent event) {
2019-10-20 22:30:49 +02:00
LivingEntity entity = event.getEntityLiving();
2019-11-04 19:08:49 +01:00
if (entity.getPersistentData().getBoolean(NaturesAura.MOD_ID + ":no_drops"))
2018-11-29 13:54:32 +01:00
event.setCanceled(true);
}
@SubscribeEvent
public void onEntityExp(LivingExperienceDropEvent event) {
2019-10-20 22:30:49 +02:00
LivingEntity entity = event.getEntityLiving();
2019-11-04 19:08:49 +01:00
if (entity.getPersistentData().getBoolean(NaturesAura.MOD_ID + ":no_drops"))
2018-11-29 00:47:21 +01:00
event.setCanceled(true);
}
2019-01-27 13:57:34 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-04 15:40:09 +01:00
public AxisAlignedBB getVisualizationBounds(Level level, BlockPos pos) {
2019-01-27 13:57:34 +01:00
return new AxisAlignedBB(pos).grow(5);
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-04 15:40:09 +01:00
public int getVisualizationColor(Level level, BlockPos pos) {
2019-01-27 13:57:34 +01:00
return 0x11377a;
}
2020-01-29 01:34:58 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().cubeBottomTop(this.getBaseName(),
generator.modLoc("block/" + this.getBaseName()),
generator.modLoc("block/" + this.getBaseName() + "_bottom"),
generator.modLoc("block/" + this.getBaseName() + "_top")));
}
2018-11-29 00:47:21 +01:00
}