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;
|
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.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.LevelAccessor;
|
|
|
|
import net.minecraft.world.level.block.SoundType;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
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-15 16:24:53 +01:00
|
|
|
super("spawn_lamp", BlockEntitySpawnLamp::new, 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);
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:30:17 +01:00
|
|
|
@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:24:53 +01:00
|
|
|
LevelAccessor level = event.getWorld();
|
2018-11-24 15:06:18 +01:00
|
|
|
BlockPos pos = new BlockPos(event.getX(), event.getY(), event.getZ());
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!(level instanceof Level))
|
2020-02-16 15:18:15 +01:00
|
|
|
return;
|
2021-12-04 15:40:09 +01:00
|
|
|
LevelData data = (LevelData) ILevelData.getLevelData((Level) level);
|
|
|
|
for (BlockEntitySpawnLamp lamp : data.spawnLamps) {
|
2020-07-23 16:33:06 +02:00
|
|
|
if (lamp.isRemoved())
|
|
|
|
continue;
|
|
|
|
|
2018-11-24 15:06:18 +01:00
|
|
|
int range = lamp.getRadius();
|
|
|
|
if (range <= 0)
|
2020-02-15 13:07:43 +01:00
|
|
|
continue;
|
2018-11-24 15:06:18 +01:00
|
|
|
|
2021-12-15 16:24:53 +01:00
|
|
|
BlockPos lampPos = lamp.getBlockPos();
|
|
|
|
if (!new AABB(lampPos).inflate(range).contains(new Vec3(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5)))
|
2020-02-15 13:07:43 +01:00
|
|
|
continue;
|
2018-11-24 15:06:18 +01:00
|
|
|
|
2021-12-15 16:24:53 +01:00
|
|
|
Mob entity = (Mob) event.getEntityLiving();
|
|
|
|
if (entity.checkSpawnRules(level, event.getSpawnReason()) && entity.checkSpawnObstruction(level)) {
|
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot((Level) level, lampPos, 32, lampPos);
|
|
|
|
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);
|
2020-02-15 13:07:43 +01:00
|
|
|
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) {
|
2020-01-23 16:05:52 +01:00
|
|
|
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)
|
2021-12-15 14:26:42 +01:00
|
|
|
public AABB getVisualizationBounds(Level level, BlockPos pos) {
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockEntity tile = level.getBlockEntity(pos);
|
|
|
|
if (tile instanceof BlockEntitySpawnLamp) {
|
|
|
|
int 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
|
|
|
}
|