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

118 lines
4.5 KiB
Java
Raw Normal View History

2018-11-24 15:06:18 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntitySpawnLamp;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.misc.LevelData;
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-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomRenderType;
import net.minecraft.client.renderer.RenderType;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
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 01:34:58 +01:00
import java.util.function.Supplier;
public class BlockSpawnLamp extends BlockContainerImpl implements IVisualizable, ICustomBlockState, ICustomRenderType {
2018-11-24 15:06:18 +01:00
2021-12-04 15:40:09 +01:00
private static final VoxelShape SHAPE = Shapes.create(4 / 16F, 0F, 4 / 16F, 12 / 16F, 13 / 16F, 12 / 16F);
2018-11-24 15:06:18 +01:00
public BlockSpawnLamp() {
2021-12-19 15:32:45 +01:00
super("spawn_lamp", BlockEntitySpawnLamp.class, Properties.of(Material.METAL).strength(3F).lightLevel(s -> 15).sound(SoundType.METAL));
2018-11-24 15:06:18 +01:00
MinecraftForge.EVENT_BUS.register(this);
}
@Override
protected boolean hasWaterlogging() {
return true;
}
2018-11-24 15:06:18 +01:00
@SubscribeEvent
public void onSpawn(LivingSpawnEvent.CheckSpawn event) {
if (event.getSpawner() != null)
return;
2021-12-15 16:30:22 +01:00
var level = event.getWorld();
var pos = new BlockPos(event.getX(), event.getY(), event.getZ());
2021-12-04 15:40:09 +01:00
if (!(level instanceof Level))
return;
2021-12-15 16:30:22 +01:00
var data = (LevelData) ILevelData.getLevelData((Level) level);
for (var lamp : data.spawnLamps) {
if (lamp.isRemoved())
continue;
2021-12-15 16:30:22 +01:00
var range = lamp.getRadius();
2018-11-24 15:06:18 +01:00
if (range <= 0)
continue;
2018-11-24 15:06:18 +01:00
2021-12-15 16:30:22 +01:00
var lampPos = lamp.getBlockPos();
2021-12-15 16:24:53 +01:00
if (!new AABB(lampPos).inflate(range).contains(new Vec3(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5)))
continue;
2018-11-24 15:06:18 +01:00
2021-12-15 16:30:22 +01:00
var entity = (Mob) event.getEntityLiving();
2021-12-15 16:24:53 +01:00
if (entity.checkSpawnRules(level, event.getSpawnReason()) && entity.checkSpawnObstruction(level)) {
2021-12-15 16:30:22 +01:00
var spot = IAuraChunk.getHighestSpot((Level) level, lampPos, 32, lampPos);
2021-12-15 16:24:53 +01:00
IAuraChunk.getAuraChunk((Level) level, spot).drainAura(spot, 200);
2018-11-24 15:06:18 +01:00
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround((ServerLevel) level, 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);
break;
}
2018-11-24 15:06:18 +01:00
}
2020-01-23 16:05:52 +01:00
@Override
2021-12-15 16:24:53 +01:00
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
2022-06-27 15:24:04 +02:00
return BlockSpawnLamp.SHAPE;
2020-01-23 16:05:52 +01:00
}
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)
public AABB getVisualizationBounds(Level level, BlockPos pos) {
2021-12-15 16:30:22 +01:00
var tile = level.getBlockEntity(pos);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntitySpawnLamp) {
2021-12-15 16:30:22 +01:00
var radius = ((BlockEntitySpawnLamp) tile).getRadius();
2019-01-27 13:57:34 +01:00
if (radius > 0)
2021-12-15 16:24:53 +01:00
return new AABB(pos).inflate(radius);
2019-01-27 13:57:34 +01:00
}
return null;
}
@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 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())));
}
2020-01-29 01:34:58 +01:00
@Override
public Supplier<RenderType> getRenderType() {
2021-12-15 16:24:53 +01:00
return RenderType::cutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-11-24 15:06:18 +01:00
}