Search in sources :

Example 1 with EntitySpawnEvent

use of org.bukkit.event.entity.EntitySpawnEvent in project Glowstone by GlowstoneMC.

the class GlowWorld method spawn.

public GlowEntity spawn(Location location, Class<? extends GlowEntity> clazz, SpawnReason reason) throws IllegalArgumentException {
    GlowEntity entity = null;
    if (TNTPrimed.class.isAssignableFrom(clazz)) {
        entity = new GlowTNTPrimed(location, null);
    }
    if (entity == null) {
        try {
            Constructor<? extends GlowEntity> constructor = clazz.getConstructor(Location.class);
            entity = constructor.newInstance(location);
            GlowEntity impl = entity;
            // function.accept(entity); TODO: work on type mismatches
            EntitySpawnEvent spawnEvent;
            if (entity instanceof LivingEntity) {
                spawnEvent = EventFactory.callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason));
            } else {
                spawnEvent = EventFactory.callEvent(new EntitySpawnEvent(entity));
            }
            if (!spawnEvent.isCancelled()) {
                List<Message> spawnMessage = entity.createSpawnMessage();
                getRawPlayers().stream().filter(player -> player.canSeeEntity(impl)).forEach(player -> player.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
            } else {
                // TODO: separate spawning and construction for better event cancellation
                entity.remove();
            }
        } catch (NoSuchMethodException e) {
            GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
            GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", e);
        }
    }
    if (entity != null) {
        return entity;
    }
    throw new UnsupportedOperationException("Not supported yet.");
}
Also used : Plugin(org.bukkit.plugin.Plugin) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) ItemTable(net.glowstone.block.ItemTable) GameRuleManager(net.glowstone.util.GameRuleManager) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent) Biome(org.bukkit.block.Biome) ChunkGenerator(org.bukkit.generator.ChunkGenerator) MaterialData(org.bukkit.material.MaterialData) org.bukkit(org.bukkit) AnvilWorldStorageProvider(net.glowstone.io.anvil.AnvilWorldStorageProvider) Block(org.bukkit.block.Block) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) ToString(lombok.ToString) WorldConfig(net.glowstone.util.config.WorldConfig) BoundingBox(net.glowstone.entity.physics.BoundingBox) org.bukkit.entity(org.bukkit.entity) LightningStrikeEvent(org.bukkit.event.weather.LightningStrikeEvent) BlockPopulator(org.bukkit.generator.BlockPopulator) WorldStorageProvider(net.glowstone.io.WorldStorageProvider) ConcurrentSet(net.glowstone.util.collection.ConcurrentSet) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) GlowChunk(net.glowstone.chunk.GlowChunk) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ItemStack(org.bukkit.inventory.ItemStack) InvocationTargetException(java.lang.reflect.InvocationTargetException) WorldFinalValues(net.glowstone.io.WorldMetadataService.WorldFinalValues) MetadataValue(org.bukkit.metadata.MetadataValue) ChunkLock(net.glowstone.chunk.ChunkManager.ChunkLock) Key(net.glowstone.chunk.GlowChunk.Key) GlowFallingBlock(net.glowstone.entity.objects.GlowFallingBlock) GlowStructure(net.glowstone.generator.structures.GlowStructure) EmptySnapshot(net.glowstone.chunk.GlowChunkSnapshot.EmptySnapshot) Setter(lombok.Setter) ThunderChangeEvent(org.bukkit.event.weather.ThunderChangeEvent) java.util(java.util) Getter(lombok.Getter) Message(com.flowpowered.network.Message) WeatherChangeEvent(org.bukkit.event.weather.WeatherChangeEvent) Constructor(java.lang.reflect.Constructor) Level(java.util.logging.Level) Consumer(org.bukkit.util.Consumer) ServerDifficultyMessage(net.glowstone.net.message.play.player.ServerDifficultyMessage) RayUtil(net.glowstone.util.RayUtil) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) GlowItem(net.glowstone.entity.objects.GlowItem) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) org.bukkit.event.world(org.bukkit.event.world) MetadataStore(org.bukkit.metadata.MetadataStore) ChunkSection(net.glowstone.chunk.ChunkSection) StandardMessenger(org.bukkit.plugin.messaging.StandardMessenger) BlockState(org.bukkit.block.BlockState) IOException(java.io.IOException) File(java.io.File) Vector(org.bukkit.util.Vector) net.glowstone.constants(net.glowstone.constants) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) ChunkManager(net.glowstone.chunk.ChunkManager) MetadataStoreBase(org.bukkit.metadata.MetadataStoreBase) net.glowstone.entity(net.glowstone.entity) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) Message(com.flowpowered.network.Message) ServerDifficultyMessage(net.glowstone.net.message.play.player.ServerDifficultyMessage) InvocationTargetException(java.lang.reflect.InvocationTargetException) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent)

Aggregations

Message (com.flowpowered.network.Message)1 File (java.io.File)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Level (java.util.logging.Level)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1 ToString (lombok.ToString)1 GlowBlock (net.glowstone.block.GlowBlock)1 ItemTable (net.glowstone.block.ItemTable)1 BlockType (net.glowstone.block.blocktype.BlockType)1 ChunkManager (net.glowstone.chunk.ChunkManager)1 ChunkLock (net.glowstone.chunk.ChunkManager.ChunkLock)1 ChunkSection (net.glowstone.chunk.ChunkSection)1 GlowChunk (net.glowstone.chunk.GlowChunk)1