Search in sources :

Example 1 with SpawnReason

use of org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason 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)

Example 2 with SpawnReason

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

the class GlowWorld method spawnGlowEntity.

/**
 * Spawns an entity.
 *
 * @param location the {@link Location} to spawn the entity at
 * @param clazz    the class of the {@link Entity} to spawn
 * @param reason   the reason for the spawning of the entity
 * @return an instance of the spawned {@link Entity}
 * @throws IllegalArgumentException TODO: document the reason this can happen
 */
public <T extends GlowEntity, E extends Entity> GlowEntity spawnGlowEntity(Location location, Class<T> clazz, SpawnReason reason, @Nullable Consumer<E> function) throws IllegalArgumentException {
    checkNotNull(location);
    checkNotNull(clazz);
    GlowEntity entity = null;
    try {
        if (EntityRegistry.getEntity(clazz) != null) {
            entity = EntityStorage.create(clazz, location);
        }
        if (entity != null && function != null) {
            function.accept((E) entity);
        }
        EntitySpawnEvent spawnEvent = null;
        if (entity instanceof LivingEntity) {
            spawnEvent = EventFactory.getInstance().callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason));
        } else if (!(entity instanceof Item)) {
            // ItemSpawnEvent is called elsewhere
            spawnEvent = EventFactory.getInstance().callEvent(new EntitySpawnEvent(entity));
        }
        if (spawnEvent != null && spawnEvent.isCancelled()) {
            // TODO: separate spawning and construction for better event cancellation
            entity.remove();
        } else {
            List<Message> spawnMessage = entity.createSpawnMessage();
            final GlowEntity finalEntity = entity;
            getRawPlayers().stream().filter(player -> player.canSeeEntity(finalEntity)).forEach(player -> player.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
        }
    } catch (NoSuchMethodError | IllegalAccessError e) {
        GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e);
    } catch (Throwable t) {
        GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", t);
    }
    if (entity != null) {
        return entity;
    }
    throw new UnsupportedOperationException("Not supported yet.");
}
Also used : TreeType(org.bukkit.TreeType) GlowBlock(net.glowstone.block.GlowBlock) Arrays(java.util.Arrays) BlockType(net.glowstone.block.blocktype.BlockType) DragonBattle(org.bukkit.boss.DragonBattle) ItemTable(net.glowstone.block.ItemTable) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent) ItemSpawnEvent(org.bukkit.event.entity.ItemSpawnEvent) EntityManager(net.glowstone.entity.EntityManager) MaterialData(org.bukkit.material.MaterialData) Location(org.bukkit.Location) CustomEntityDescriptor(net.glowstone.entity.CustomEntityDescriptor) World(org.bukkit.World) Map(java.util.Map) WorldSaveEvent(org.bukkit.event.world.WorldSaveEvent) WorldConfig(net.glowstone.util.config.WorldConfig) Chunk(org.bukkit.Chunk) BoundingBox(net.glowstone.entity.physics.BoundingBox) ChunkDataMessage(net.glowstone.net.message.play.game.ChunkDataMessage) Material(org.bukkit.Material) LightningStrikeEvent(org.bukkit.event.weather.LightningStrikeEvent) BlockData(org.bukkit.block.data.BlockData) Entity(org.bukkit.entity.Entity) GlowEffect(net.glowstone.constants.GlowEffect) StructureType(org.bukkit.StructureType) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) ItemStack(org.bukkit.inventory.ItemStack) WorldFinalValues(net.glowstone.io.WorldMetadataService.WorldFinalValues) EntityStorage(net.glowstone.io.entity.EntityStorage) ChunkLock(net.glowstone.chunk.ChunkManager.ChunkLock) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) GlowFallingBlock(net.glowstone.entity.objects.GlowFallingBlock) FallingBlock(org.bukkit.entity.FallingBlock) GlowStructure(net.glowstone.generator.structures.GlowStructure) GameRule(org.bukkit.GameRule) GlowLightningStrike(net.glowstone.entity.GlowLightningStrike) Message(com.flowpowered.network.Message) ArrayList(java.util.ArrayList) GameRules(net.glowstone.constants.GameRules) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Raid(org.bukkit.Raid) MetadataStore(org.bukkit.metadata.MetadataStore) ChunkSection(net.glowstone.chunk.ChunkSection) SoundCategory(org.bukkit.SoundCategory) StructureGrowEvent(org.bukkit.event.world.StructureGrowEvent) IOException(java.io.IOException) File(java.io.File) HeightMap(org.bukkit.HeightMap) Vector(org.bukkit.util.Vector) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) ChunkManager(net.glowstone.chunk.ChunkManager) SpawnChangeEvent(org.bukkit.event.world.SpawnChangeEvent) EntityRegistry(net.glowstone.entity.EntityRegistry) MetadataStoreBase(org.bukkit.metadata.MetadataStoreBase) Plugin(org.bukkit.plugin.Plugin) GameRuleManager(net.glowstone.util.GameRuleManager) ChunkSnapshot(org.bukkit.ChunkSnapshot) BlockChangeDelegate(org.bukkit.BlockChangeDelegate) GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) MoonPhase(io.papermc.paper.world.MoonPhase) Biome(org.bukkit.block.Biome) WorldInitEvent(org.bukkit.event.world.WorldInitEvent) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) ChunkGenerator(org.bukkit.generator.ChunkGenerator) Block(org.bukkit.block.Block) Difficulty(org.bukkit.Difficulty) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) NotImplementedException(org.apache.commons.lang.NotImplementedException) ToString(lombok.ToString) GlowBiome(net.glowstone.constants.GlowBiome) BlockPopulator(org.bukkit.generator.BlockPopulator) WorldStorageProvider(net.glowstone.io.WorldStorageProvider) ConcurrentSet(net.glowstone.util.collection.ConcurrentSet) GlowBiomeClimate(net.glowstone.constants.GlowBiomeClimate) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) Predicate(java.util.function.Predicate) GlowChunk(net.glowstone.chunk.GlowChunk) Collection(java.util.Collection) Sound(org.bukkit.Sound) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) MetadataValue(org.bukkit.metadata.MetadataValue) Key(net.glowstone.chunk.GlowChunk.Key) NotNull(org.jetbrains.annotations.NotNull) FluidCollisionMode(org.bukkit.FluidCollisionMode) EmptySnapshot(net.glowstone.chunk.GlowChunkSnapshot.EmptySnapshot) Setter(lombok.Setter) ThunderChangeEvent(org.bukkit.event.weather.ThunderChangeEvent) CommandFunction(net.glowstone.data.CommandFunction) Getter(lombok.Getter) Item(org.bukkit.entity.Item) MessageDigest(java.security.MessageDigest) NonNls(org.jetbrains.annotations.NonNls) WorldLoadEvent(org.bukkit.event.world.WorldLoadEvent) CompletableFuture(java.util.concurrent.CompletableFuture) WorldCreator(org.bukkit.WorldCreator) WeatherChangeEvent(org.bukkit.event.weather.WeatherChangeEvent) Arrow(org.bukkit.entity.Arrow) Level(java.util.logging.Level) Consumer(org.bukkit.util.Consumer) HashSet(java.util.HashSet) ServerDifficultyMessage(net.glowstone.net.message.play.player.ServerDifficultyMessage) Effect(org.bukkit.Effect) LightningStrike(org.bukkit.entity.LightningStrike) RayUtil(net.glowstone.util.RayUtil) TickUtil(net.glowstone.util.TickUtil) WorldUnloadEvent(org.bukkit.event.world.WorldUnloadEvent) GlowItem(net.glowstone.entity.objects.GlowItem) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) LinkedList(java.util.LinkedList) AbstractArrow(org.bukkit.entity.AbstractArrow) StandardMessenger(org.bukkit.plugin.messaging.StandardMessenger) NamespacedKey(org.bukkit.NamespacedKey) HeightmapType(com.destroystokyo.paper.HeightmapType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) BlockState(org.bukkit.block.BlockState) GlowParticle(net.glowstone.constants.GlowParticle) GlowEntity(net.glowstone.entity.GlowEntity) WorldType(org.bukkit.WorldType) Particle(org.bukkit.Particle) RayTraceResult(org.bukkit.util.RayTraceResult) GlowTree(net.glowstone.constants.GlowTree) GlowSound(net.glowstone.constants.GlowSound) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) Collections(java.util.Collections) ChunkDataMessage(net.glowstone.net.message.play.game.ChunkDataMessage) Message(com.flowpowered.network.Message) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) ServerDifficultyMessage(net.glowstone.net.message.play.player.ServerDifficultyMessage) LivingEntity(org.bukkit.entity.LivingEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) Item(org.bukkit.entity.Item) GlowItem(net.glowstone.entity.objects.GlowItem) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) GlowEntity(net.glowstone.entity.GlowEntity) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent)

Example 3 with SpawnReason

use of org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason in project InfernalMobs by NyaaCat.

the class ConfigReader method getEnabledSpawnReasons.

public static List<SpawnReason> getEnabledSpawnReasons() {
    List<SpawnReason> ret = new ArrayList<>();
    for (String str : cfg().getStringList("enabledSpawnReasons")) {
        try {
            SpawnReason reason = SpawnReason.valueOf(str);
            ret.add(reason);
        } catch (Exception ex) {
            InfernalMobs.instance.getLogger().warning(str + " is not a valid spawn reason");
        }
    }
    return ret;
}
Also used : SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) ArrayList(java.util.ArrayList)

Aggregations

SpawnReason (org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason)3 Message (com.flowpowered.network.Message)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Level (java.util.logging.Level)2 Collectors (java.util.stream.Collectors)2 Getter (lombok.Getter)2 Setter (lombok.Setter)2 ToString (lombok.ToString)2 GlowBlock (net.glowstone.block.GlowBlock)2 ItemTable (net.glowstone.block.ItemTable)2 BlockType (net.glowstone.block.blocktype.BlockType)2 ChunkManager (net.glowstone.chunk.ChunkManager)2 ChunkLock (net.glowstone.chunk.ChunkManager.ChunkLock)2 ChunkSection (net.glowstone.chunk.ChunkSection)2 GlowChunk (net.glowstone.chunk.GlowChunk)2 Key (net.glowstone.chunk.GlowChunk.Key)2 EmptySnapshot (net.glowstone.chunk.GlowChunkSnapshot.EmptySnapshot)2 GlowFallingBlock (net.glowstone.entity.objects.GlowFallingBlock)2