2018-11-29 00:47:21 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2019-01-28 15:43:21 +01:00
|
|
|
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;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.animal.Animal;
|
|
|
|
import net.minecraft.world.entity.npc.Npc;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.SoundType;
|
|
|
|
import net.minecraft.world.phys.AABB;
|
2024-02-03 14:56:07 +01:00
|
|
|
import net.neoforged.api.distmarker.Dist;
|
|
|
|
import net.neoforged.api.distmarker.OnlyIn;
|
|
|
|
import net.neoforged.neoforge.common.NeoForge;
|
|
|
|
import net.neoforged.neoforge.event.entity.living.*;
|
|
|
|
import net.neoforged.neoforge.event.entity.living.LivingExperienceDropEvent;
|
|
|
|
import net.neoforged.bus.api.SubscribeEvent;
|
|
|
|
import net.neoforged.neoforge.event.entity.living.LivingEvent;
|
2018-11-29 00:47:21 +01:00
|
|
|
|
2020-01-29 01:34:58 +01:00
|
|
|
public class BlockAnimalGenerator extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
|
2021-12-15 14:26:42 +01:00
|
|
|
|
2018-11-29 00:47:21 +01:00
|
|
|
public BlockAnimalGenerator() {
|
2023-07-08 12:32:27 +02:00
|
|
|
super("animal_generator", BlockEntityAnimalGenerator.class, Properties.of().strength(3F).sound(SoundType.STONE));
|
2018-11-29 00:47:21 +01:00
|
|
|
|
2024-02-03 14:56:07 +01:00
|
|
|
NeoForge.EVENT_BUS.register(this);
|
2018-11-29 00:47:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 01:38:55 +01:00
|
|
|
@SubscribeEvent
|
2022-08-01 16:14:37 +02:00
|
|
|
public void onLivingUpdate(LivingEvent.LivingTickEvent event) {
|
|
|
|
var entity = event.getEntity();
|
2023-07-08 12:32:27 +02:00
|
|
|
if (entity.level().isClientSide || entity.level().getGameTime() % 40 != 0 || !(entity instanceof Animal) || entity instanceof Npc)
|
2019-01-27 01:38:55 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var data = entity.getPersistentData();
|
|
|
|
var timeAlive = data.getInt(NaturesAura.MOD_ID + ":time_alive");
|
2020-08-13 14:14:45 +02:00
|
|
|
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) {
|
2022-08-01 16:14:37 +02:00
|
|
|
var entity = event.getEntity();
|
2023-07-08 12:32:27 +02:00
|
|
|
if (entity.level().isClientSide || !(entity instanceof Animal) || entity instanceof Npc)
|
2018-11-29 00:47:21 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var pos = entity.blockPosition();
|
2023-07-08 12:32:27 +02:00
|
|
|
Helper.getBlockEntitiesInArea(entity.level(), pos, 5, tile -> {
|
2021-12-15 14:26:42 +01:00
|
|
|
if (!(tile instanceof BlockEntityAnimalGenerator gen))
|
2018-11-29 00:47:21 +01:00
|
|
|
return false;
|
2019-01-27 01:38:55 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var 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;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var child = entity.isBaby();
|
|
|
|
var timeMod = child ? 0.5F : 1;
|
|
|
|
var amountMod = child ? 0.667F : 1;
|
2019-01-27 01:38:55 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var timeAlive = data.getInt(NaturesAura.MOD_ID + ":time_alive");
|
|
|
|
var time = Math.min(Mth.floor((timeAlive - 15000) / 500F * timeMod), 200);
|
|
|
|
var amount = Math.min(Mth.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);
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var genPos = gen.getBlockPos();
|
2023-07-08 12:32:27 +02:00
|
|
|
PacketHandler.sendToAllAround(entity.level(), pos, 32, new PacketParticles(
|
2021-12-15 14:26:42 +01:00
|
|
|
(float) entity.getX(), (float) entity.getY(), (float) entity.getZ(), 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) {
|
2022-08-01 16:14:37 +02:00
|
|
|
var entity = event.getEntity();
|
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) {
|
2022-08-01 16:14:37 +02:00
|
|
|
var entity = event.getEntity();
|
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-15 14:26:42 +01:00
|
|
|
public AABB getVisualizationBounds(Level level, BlockPos pos) {
|
|
|
|
return new AABB(pos).inflate(5);
|
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 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")));
|
|
|
|
}
|
2023-07-08 12:32:27 +02:00
|
|
|
}
|