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;
|
2018-11-29 00:47:21 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityAnimalGenerator;
|
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;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
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;
|
2019-01-27 13:57:34 +01:00
|
|
|
import net.minecraft.world.World;
|
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() {
|
2020-01-29 01:34:58 +01:00
|
|
|
super("animal_generator", TileEntityAnimalGenerator::new, ModBlocks.prop(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();
|
2019-11-04 19:08:49 +01:00
|
|
|
if (entity.world.isRemote || !(entity instanceof AnimalEntity) || entity instanceof IMob || entity instanceof INPC)
|
2019-01-27 01:38:55 +01:00
|
|
|
return;
|
2019-11-04 19:08:49 +01:00
|
|
|
CompoundNBT data = entity.getPersistentData();
|
|
|
|
int timeAlive = data.getInt(NaturesAura.MOD_ID + ":time_alive");
|
|
|
|
data.putInt(NaturesAura.MOD_ID + ":time_alive", timeAlive + 1);
|
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();
|
2019-11-04 19:08:49 +01:00
|
|
|
if (entity.world.isRemote || !(entity instanceof AnimalEntity) || entity instanceof IMob || entity instanceof INPC)
|
2018-11-29 00:47:21 +01:00
|
|
|
return;
|
|
|
|
BlockPos pos = entity.getPosition();
|
|
|
|
Helper.getTileEntitiesInArea(entity.world, pos, 5, tile -> {
|
|
|
|
if (!(tile instanceof TileEntityAnimalGenerator))
|
|
|
|
return false;
|
|
|
|
TileEntityAnimalGenerator gen = (TileEntityAnimalGenerator) tile;
|
2019-01-27 01:38:55 +01:00
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
CompoundNBT data = entity.getPersistentData();
|
|
|
|
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();
|
|
|
|
PacketHandler.sendToAllAround(entity.world, 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)
|
2019-01-27 13:57:34 +01:00
|
|
|
public AxisAlignedBB getVisualizationBounds(World world, BlockPos pos) {
|
|
|
|
return new AxisAlignedBB(pos).grow(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-01-27 13:57:34 +01:00
|
|
|
public int getVisualizationColor(World world, BlockPos pos) {
|
|
|
|
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
|
|
|
}
|