Search in sources :

Example 1 with ItemSpawnEvent

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

the class GlowWorld method dropItem.

/**
 * Spawn an item at the given {@link Location} without shooting effect.
 *
 * @param location the {@link Location} to spawn the item at
 * @param item     the {@link ItemStack} the item should have
 */
@Override
public GlowItem dropItem(Location location, ItemStack item) {
    checkNotNull(location);
    GlowItem entity = new GlowItem(location, item);
    ItemSpawnEvent event = EventFactory.getInstance().callEvent(new ItemSpawnEvent(entity));
    if (event.isCancelled()) {
        entity.remove();
    }
    return entity;
}
Also used : ItemSpawnEvent(org.bukkit.event.entity.ItemSpawnEvent) GlowItem(net.glowstone.entity.objects.GlowItem)

Example 2 with ItemSpawnEvent

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

Aggregations

GlowItem (net.glowstone.entity.objects.GlowItem)2 ItemSpawnEvent (org.bukkit.event.entity.ItemSpawnEvent)2 HeightmapType (com.destroystokyo.paper.HeightmapType)1 Message (com.flowpowered.network.Message)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 MoonPhase (io.papermc.paper.world.MoonPhase)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1