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;
|
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;
|
2020-01-29 01:34:58 +01:00
|
|
|
import net.minecraft.client.renderer.RenderType;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.MobEntity;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.tileentity.BlockEntity;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.util.math.AABB;
|
2018-11-24 15:06:18 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-01-23 16:05:52 +01:00
|
|
|
import net.minecraft.util.math.shapes.ISelectionContext;
|
|
|
|
import net.minecraft.util.math.shapes.VoxelShape;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.util.math.shapes.Shapes;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.util.math.vector.Vector3d;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.level.BlockGetter;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.level.ILevel;
|
|
|
|
import net.minecraft.level.Level;
|
|
|
|
import net.minecraft.level.server.ServerLevel;
|
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-04 15:40:09 +01:00
|
|
|
super("spawn_lamp", BlockEntitySpawnLamp::new, Properties.create(Material.IRON).hardnessAndResistance(3F).setLightLevel(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-04 15:40:09 +01:00
|
|
|
ILevel level = event.getLevel();
|
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
|
|
|
|
|
|
|
BlockPos lampPos = lamp.getPos();
|
2021-12-15 14:26:42 +01:00
|
|
|
if (!new AABB(lampPos).grow(range).contains(new Vector3d(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
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
MobEntity entity = (MobEntity) event.getEntityLiving();
|
2021-12-04 15:40:09 +01:00
|
|
|
if (entity.canSpawn(level, event.getSpawnReason()) && entity.isNotColliding(level)) {
|
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(level, lampPos, 32, lampPos);
|
|
|
|
IAuraChunk.getAuraChunk(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 14:26:42 +01:00
|
|
|
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, ISelectionContext 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 14:26:42 +01:00
|
|
|
return new AABB(pos).grow(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() {
|
2020-09-22 03:17:02 +02:00
|
|
|
return RenderType::getCutoutMipped;
|
2020-01-29 01:34:58 +01:00
|
|
|
}
|
2018-11-24 15:06:18 +01:00
|
|
|
}
|