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

109 lines
4.2 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;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
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.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;
2023-07-08 12:32:27 +02:00
import net.minecraftforge.event.entity.living.MobSpawnEvent;
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
public class BlockSpawnLamp extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
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() {
2023-07-08 12:32:27 +02:00
super("spawn_lamp", BlockEntitySpawnLamp.class, Properties.of().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
2023-07-08 12:32:27 +02:00
public void onSpawn(MobSpawnEvent.PositionCheck event) {
2023-02-16 19:03:26 +01:00
var amountToUse = 200;
2018-11-24 15:06:18 +01:00
if (event.getSpawner() != null)
return;
2023-02-16 19:03:26 +01:00
var accessor = event.getLevel();
2023-07-08 12:32:27 +02:00
var pos = BlockPos.containing(event.getX(), event.getY(), event.getZ());
2023-02-16 19:03:26 +01:00
if (!(accessor instanceof Level level))
return;
2023-02-16 19:03:26 +01:00
var data = (LevelData) ILevelData.getLevelData(level);
2021-12-15 16:30:22 +01:00
for (var lamp : data.spawnLamps) {
2023-02-16 19:03:26 +01:00
if (lamp.isRemoved() || !lamp.canUseRightNow(amountToUse))
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
var entity = (Mob) event.getEntity();
2023-07-08 12:32:27 +02:00
if (entity.checkSpawnRules(level, event.getSpawnType()) && entity.checkSpawnObstruction(level)) {
2023-02-16 19:03:26 +01:00
var spot = IAuraChunk.getHighestSpot(level, lampPos, 32, lampPos);
IAuraChunk.getAuraChunk(level, spot).drainAura(spot, amountToUse);
2018-11-24 15:06:18 +01:00
2023-02-16 19:03:26 +01:00
PacketHandler.sendToAllAround(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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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())));
}
2018-11-24 15:06:18 +01:00
}