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

101 lines
4 KiB
Java
Raw Normal View History

2018-11-24 15:06:18 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2018-11-24 15:06:18 +01:00
import de.ellpeck.naturesaura.blocks.tiles.TileEntitySpawnLamp;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2020-01-23 16:05:52 +01:00
import net.minecraft.block.BlockState;
2018-11-27 21:38:42 +01:00
import net.minecraft.block.SoundType;
2018-11-24 15:06:18 +01:00
import net.minecraft.block.material.Material;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.MobEntity;
2019-01-27 13:57:34 +01:00
import net.minecraft.tileentity.TileEntity;
2018-11-24 15:06:18 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
2019-01-27 13:57:34 +01:00
import net.minecraft.util.math.Vec3d;
2020-01-23 16:05:52 +01:00
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
2019-11-04 19:08:49 +01:00
import net.minecraft.world.IWorld;
2018-11-24 15:06:18 +01:00
import net.minecraft.world.World;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-11-24 15:06:18 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;
2018-11-24 15:06:18 +01:00
2020-01-29 00:40:28 +01:00
public class BlockSpawnLamp extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
2018-11-24 15:06:18 +01:00
2020-01-23 16:05:52 +01:00
private static final VoxelShape SHAPE = VoxelShapes.create(4 / 16F, 0F, 4 / 16F, 12 / 16F, 13 / 16F, 12 / 16F);
2018-11-24 15:06:18 +01:00
public BlockSpawnLamp() {
2020-01-22 01:32:26 +01:00
super("spawn_lamp", TileEntitySpawnLamp::new, ModBlocks.prop(Material.IRON).hardnessAndResistance(3F).lightValue(15).sound(SoundType.METAL));
2018-11-24 15:06:18 +01:00
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onSpawn(LivingSpawnEvent.CheckSpawn event) {
if (event.getSpawner() != null)
return;
2019-11-04 19:08:49 +01:00
IWorld world = event.getWorld();
2018-11-24 15:06:18 +01:00
BlockPos pos = new BlockPos(event.getX(), event.getY(), event.getZ());
Helper.getTileEntitiesInArea(world, pos, 48, tile -> {
if (!(tile instanceof TileEntitySpawnLamp))
return false;
TileEntitySpawnLamp lamp = (TileEntitySpawnLamp) tile;
int range = lamp.getRadius();
if (range <= 0)
return false;
BlockPos lampPos = lamp.getPos();
2019-01-27 13:57:34 +01:00
if (!new AxisAlignedBB(lampPos).grow(range).contains(new Vec3d(pos)))
2018-11-24 15:06:18 +01:00
return false;
2019-10-20 22:30:49 +02:00
MobEntity entity = (MobEntity) event.getEntityLiving();
2020-01-21 21:04:44 +01:00
if (entity.canSpawn(world, event.getSpawnReason()) && entity.isNotColliding(world)) {
2018-11-24 15:06:18 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(world, lampPos, 32, lampPos);
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 200);
2018-11-24 15:06:18 +01:00
2020-01-22 23:21:52 +01:00
PacketHandler.sendToAllAround(world, lampPos, 32,
2020-01-26 00:16:06 +01:00
new PacketParticles(lampPos.getX(), lampPos.getY(), lampPos.getZ(), PacketParticles.Type.SPAWN_LAMP));
2018-11-24 15:06:18 +01:00
}
event.setResult(Event.Result.DENY);
return true;
});
}
2020-01-23 16:05:52 +01:00
@Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return SHAPE;
}
2018-11-24 15:06:18 +01:00
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) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntitySpawnLamp) {
int radius = ((TileEntitySpawnLamp) tile).getRadius();
if (radius > 0)
return new AxisAlignedBB(pos).grow(radius);
}
return null;
}
@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 0x825ee5;
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().getExistingFile(generator.modLoc(this.getBaseName())));
}
2018-11-24 15:06:18 +01:00
}