Search in sources :

Example 26 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class MixinWorldEntitySpawner method check.

private static boolean check(BlockPos pos, World world) {
    EntityType entityType = spawnerEntityType;
    if (entityType == null) {
        // Basically, we can't throw our own event.
        return true;
    }
    Vector3d vector3d = new Vector3d(pos.getX(), pos.getY(), pos.getZ());
    Transform<org.spongepowered.api.world.World> transform = new Transform<>((org.spongepowered.api.world.World) world, vector3d);
    Sponge.getCauseStackManager().pushCause(world);
    ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), entityType, transform);
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    return !event.isCancelled();
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) World(net.minecraft.world.World) Transform(org.spongepowered.api.entity.Transform)

Example 27 with EntityType

use of org.spongepowered.api.entity.EntityType in project SpongeCommon by SpongePowered.

the class MixinWorldEntitySpawner method findChunksForSpawning.

/**
 * @author blood - February 18th, 2017
 * @reason Refactor entire method for optimizations and spawn limits.
 *
 * @param worldServerIn The world
 * @param spawnHostileMobs If hostile entities can spawn
 * @param spawnPeacefulMobs If passive entities can spawn
 * @param spawnOnSetTickRate If tickrate has been reached for spawning passives
 * @return The amount of entities spawned
 */
@Overwrite
public int findChunksForSpawning(WorldServer worldServerIn, boolean spawnHostileMobs, boolean spawnPeacefulMobs, boolean spawnOnSetTickRate) {
    if (!spawnHostileMobs && !spawnPeacefulMobs) {
        return 0;
    }
    try (PhaseContext<?> context = GenerationPhase.State.WORLD_SPAWNER_SPAWNING.createPhaseContext().world(worldServerIn).buildAndSwitch()) {
        Iterator<Chunk> chunkIterator = this.eligibleSpawnChunks.iterator();
        while (chunkIterator.hasNext()) {
            Chunk chunk = chunkIterator.next();
            ((IMixinChunk) chunk).setIsSpawning(false);
            chunkIterator.remove();
        }
        IMixinWorldServer spongeWorld = ((IMixinWorldServer) worldServerIn);
        spongeWorld.getTimingsHandler().mobSpawn.startTiming();
        int chunkSpawnCandidates = 0;
        final int mobSpawnRange = Math.min(((IMixinWorldServer) worldServerIn).getActiveConfig().getConfig().getWorld().getMobSpawnRange(), ((org.spongepowered.api.world.World) worldServerIn).getViewDistance());
        // Vanilla uses a div count of 289 (17x17) which assumes the view distance is 8.
        // Since we allow for custom ranges, we need to adjust the div count based on the
        // mob spawn range set by server.
        final int MOB_SPAWN_COUNT_DIV = (2 * mobSpawnRange + 1) * (2 * mobSpawnRange + 1);
        for (EntityPlayer entityplayer : worldServerIn.playerEntities) {
            // We treat players who do not affect spawning as "spectators"
            if (!((IMixinEntityPlayer) entityplayer).affectsSpawning() || entityplayer.isSpectator()) {
                continue;
            }
            int playerPosX = MathHelper.floor(entityplayer.posX / 16.0D);
            int playerPosZ = MathHelper.floor(entityplayer.posZ / 16.0D);
            for (int i = -mobSpawnRange; i <= mobSpawnRange; ++i) {
                for (int j = -mobSpawnRange; j <= mobSpawnRange; ++j) {
                    boolean flag = i == -mobSpawnRange || i == mobSpawnRange || j == -mobSpawnRange || j == mobSpawnRange;
                    final Chunk chunk = ((IMixinChunkProviderServer) worldServerIn.getChunkProvider()).getLoadedChunkWithoutMarkingActive(i + playerPosX, j + playerPosZ);
                    if (chunk == null || (chunk.unloadQueued && !((IMixinChunk) chunk).isPersistedChunk())) {
                        // Don't attempt to spawn in an unloaded chunk
                        continue;
                    }
                    final IMixinChunk spongeChunk = (IMixinChunk) chunk;
                    ++chunkSpawnCandidates;
                    final ChunkPos chunkPos = chunk.getPos();
                    if (!flag && worldServerIn.getWorldBorder().contains(chunkPos)) {
                        PlayerChunkMapEntry playerchunkmapentry = worldServerIn.getPlayerChunkMap().getEntry(chunkPos.x, chunkPos.z);
                        if (playerchunkmapentry != null && playerchunkmapentry.isSentToPlayers() && !spongeChunk.isSpawning()) {
                            this.eligibleSpawnChunks.add(chunk);
                            spongeChunk.setIsSpawning(true);
                        }
                    }
                }
            }
        }
        // If there are no eligible chunks, return early
        if (this.eligibleSpawnChunks.size() == 0) {
            spongeWorld.getTimingsHandler().mobSpawn.stopTiming();
            return 0;
        }
        int totalSpawned = 0;
        final long worldTotalTime = worldServerIn.getTotalWorldTime();
        final SpongeConfig<? extends GeneralConfigBase> activeConfig = ((IMixinWorldServer) worldServerIn).getActiveConfig();
        labelOuterLoop: for (EnumCreatureType enumCreatureType : EnumCreatureType.values()) {
            int limit = 0;
            int tickRate = 0;
            if (enumCreatureType == EnumCreatureType.MONSTER) {
                limit = activeConfig.getConfig().getSpawner().getMonsterSpawnLimit();
                tickRate = activeConfig.getConfig().getSpawner().getMonsterTickRate();
            } else if (enumCreatureType == EnumCreatureType.CREATURE) {
                limit = activeConfig.getConfig().getSpawner().getAnimalSpawnLimit();
                tickRate = activeConfig.getConfig().getSpawner().getAnimalTickRate();
            } else if (enumCreatureType == EnumCreatureType.WATER_CREATURE) {
                limit = activeConfig.getConfig().getSpawner().getAquaticSpawnLimit();
                tickRate = activeConfig.getConfig().getSpawner().getAquaticTickRate();
            } else if (enumCreatureType == EnumCreatureType.AMBIENT) {
                limit = activeConfig.getConfig().getSpawner().getAmbientSpawnLimit();
                tickRate = activeConfig.getConfig().getSpawner().getAmbientTickRate();
            }
            if (limit == 0 || tickRate == 0 || (worldTotalTime % tickRate) != 0L) {
                continue;
            }
            if ((!enumCreatureType.getPeacefulCreature() || spawnPeacefulMobs) && (enumCreatureType.getPeacefulCreature() || spawnHostileMobs)) {
                int entityCount = SpongeImplHooks.countEntities(worldServerIn, enumCreatureType, true);
                int maxCount = limit * chunkSpawnCandidates / MOB_SPAWN_COUNT_DIV;
                if (entityCount > maxCount) {
                    continue labelOuterLoop;
                }
                chunkIterator = this.eligibleSpawnChunks.iterator();
                int mobLimit = maxCount - entityCount + 1;
                labelChunkStart: while (chunkIterator.hasNext() && mobLimit > 0) {
                    final Chunk chunk = chunkIterator.next();
                    final BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
                    final BlockPos blockpos = getRandomChunkPosition(worldServerIn, chunk);
                    int k1 = blockpos.getX();
                    int l1 = blockpos.getY();
                    int i2 = blockpos.getZ();
                    IBlockState iblockstate = worldServerIn.getBlockState(blockpos);
                    if (!iblockstate.isNormalCube()) {
                        int spawnCount = 0;
                        for (int spawnLimit = 0; spawnLimit < 3; ++spawnLimit) {
                            int l2 = k1;
                            int i3 = l1;
                            int j3 = i2;
                            Biome.SpawnListEntry spawnListEntry = null;
                            IEntityLivingData ientitylivingdata = null;
                            int l3 = MathHelper.ceil(Math.random() * 4.0D);
                            for (int i4 = 0; i4 < l3; ++i4) {
                                l2 += worldServerIn.rand.nextInt(6) - worldServerIn.rand.nextInt(6);
                                i3 += worldServerIn.rand.nextInt(1) - worldServerIn.rand.nextInt(1);
                                j3 += worldServerIn.rand.nextInt(6) - worldServerIn.rand.nextInt(6);
                                mutableBlockPos.setPos(l2, i3, j3);
                                final double spawnX = l2 + 0.5F;
                                final double spawnY = i3;
                                final double spawnZ = j3 + 0.5F;
                                if (!worldServerIn.isAnyPlayerWithinRangeAt(spawnX, spawnY, spawnZ, 24.0D) && worldServerIn.getSpawnPoint().distanceSq(spawnX, spawnY, spawnZ) >= 576.0D) {
                                    if (spawnListEntry == null) {
                                        spawnListEntry = worldServerIn.getSpawnListEntryForTypeAt(enumCreatureType, mutableBlockPos);
                                        if (spawnListEntry == null) {
                                            break;
                                        }
                                    }
                                    final EntityType entityType = EntityTypeRegistryModule.getInstance().getForClass(spawnListEntry.entityClass);
                                    if (entityType != null) {
                                        Vector3d vector3d = new Vector3d(spawnX, spawnY, spawnZ);
                                        Transform<org.spongepowered.api.world.World> transform = new Transform<>((org.spongepowered.api.world.World) worldServerIn, vector3d);
                                        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), entityType, transform);
                                        if (SpongeImpl.postEvent(event)) {
                                            continue;
                                        }
                                    }
                                    if (worldServerIn.canCreatureTypeSpawnHere(enumCreatureType, spawnListEntry, mutableBlockPos) && WorldEntitySpawner.canCreatureTypeSpawnAtLocation(EntitySpawnPlacementRegistry.getPlacementForEntity(spawnListEntry.entityClass), worldServerIn, mutableBlockPos)) {
                                        EntityLiving entityliving;
                                        try {
                                            entityliving = spawnListEntry.entityClass.getConstructor(new Class<?>[] { World.class }).newInstance(worldServerIn);
                                        } catch (Exception exception) {
                                            exception.printStackTrace();
                                            continue labelOuterLoop;
                                        }
                                        entityliving.setLocationAndAngles(spawnX, spawnY, spawnZ, worldServerIn.rand.nextFloat() * 360.0F, 0.0F);
                                        final boolean entityNotColliding = entityliving.isNotColliding();
                                        final SpawnerSpawnType type = SpongeImplHooks.canEntitySpawnHere(entityliving, entityNotColliding);
                                        if (type != SpawnerSpawnType.NONE) {
                                            if (type == SpawnerSpawnType.NORMAL) {
                                                ientitylivingdata = entityliving.onInitialSpawn(worldServerIn.getDifficultyForLocation(new BlockPos(entityliving)), ientitylivingdata);
                                            }
                                            if (entityNotColliding) {
                                                ++spawnCount;
                                                worldServerIn.spawnEntity(entityliving);
                                            } else {
                                                entityliving.setDead();
                                            }
                                            mobLimit--;
                                            if (mobLimit <= 0 || spawnCount >= SpongeImplHooks.getMaxSpawnPackSize(entityliving)) {
                                                continue labelChunkStart;
                                            }
                                        }
                                        totalSpawned += spawnCount;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        spongeWorld.getTimingsHandler().mobSpawn.stopTiming();
        return totalSpawned;
    }
}
Also used : EntityLiving(net.minecraft.entity.EntityLiving) World(net.minecraft.world.World) Biome(net.minecraft.world.biome.Biome) SpawnerSpawnType(org.spongepowered.common.util.SpawnerSpawnType) ChunkPos(net.minecraft.util.math.ChunkPos) BlockPos(net.minecraft.util.math.BlockPos) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) IBlockState(net.minecraft.block.state.IBlockState) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) EnumCreatureType(net.minecraft.entity.EnumCreatureType) IEntityLivingData(net.minecraft.entity.IEntityLivingData) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) PlayerChunkMapEntry(net.minecraft.server.management.PlayerChunkMapEntry) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Transform(org.spongepowered.api.entity.Transform) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 28 with EntityType

use of org.spongepowered.api.entity.EntityType in project modules-extra by CubeEngine.

the class Spawner method initPerm.

private void initPerm(EntityType... types) {
    for (EntityType type : types) {
        Permission child = pm.register(Spawner.class, type.getName(), "Allows creating " + type.getName() + " spawners", eggPerms);
        this.perms.put(type, child);
    }
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) Permission(org.cubeengine.libcube.service.permission.Permission)

Example 29 with EntityType

use of org.spongepowered.api.entity.EntityType in project modules-extra by CubeEngine.

the class LookupCommands method readEntities.

private boolean readEntities(QueryParameter params, String entity, User user) {
    if (entity == null) {
        return true;
    }
    String[] names = StringUtils.explode(",", entity);
    for (String name : names) {
        boolean negate = name.startsWith("!");
        if (negate) {
            name = name.substring(1);
        }
        EntityType entityType = Match.entity().mob(name);
        if (entityType == null) {
            user.sendTranslated(NEGATIVE, "Unknown EntityType: {name#entity}", name);
            return false;
        }
        if (negate) {
            params.excludeEntity(entityType);
        } else {
            params.includeEntity(entityType);
        }
    }
    return true;
}
Also used : EntityType(org.bukkit.entity.EntityType) EntityType(org.spongepowered.api.entity.EntityType)

Example 30 with EntityType

use of org.spongepowered.api.entity.EntityType in project core by CubeEngine.

the class EntityMatcher method any.

/**
 * Tries to match an EntityType for given string
 *
 * @param name the name to match
 *
 * @return the found EntityType
 */
public EntityType any(String name, Locale locale) {
    if (name == null) {
        return null;
    }
    // 1.11 change // TODO there are more...
    if ("minecraft:entityhorse".equalsIgnoreCase(name)) {
        name = "minecraft:horse";
    }
    if ("minecraft:minecartchest".equals(name)) {
        name = "minecraft:chest_minecart";
    }
    if ("minecraft:minecarthopper".equals(name)) {
        name = "minecraft:hopper_minecart";
    }
    if ("minecraft:itemframe".equals(name)) {
        name = "minecraft:item_frame";
    }
    EntityType entity = Sponge.getRegistry().getType(EntityType.class, name).orElse(null);
    if (entity != null) {
        return entity;
    }
    try {
        return legacyIds.get(Short.parseShort(name));
    } catch (NumberFormatException ignored) {
    }
    name = name.toLowerCase();
    // Minecraft IDs
    entity = anyFromMap(name, this.ids);
    if (entity == null) {
        // Use default language translation
        entity = anyFromMap(name, this.translations);
    }
    if (entity == null && locale != null) {
        Map<String, EntityType> translations = new HashMap<>();
        for (EntityType type : Sponge.getRegistry().getAllOf(EntityType.class)) {
            translations.put(type.getTranslation().get(locale, type), type);
        }
        // Use Language Translations
        entity = anyFromMap(name, translations);
    }
    return entity;
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) HashMap(java.util.HashMap)

Aggregations

EntityType (org.spongepowered.api.entity.EntityType)33 Vector3d (com.flowpowered.math.vector.Vector3d)7 World (net.minecraft.world.World)6 Entity (org.spongepowered.api.entity.Entity)6 Transform (org.spongepowered.api.entity.Transform)6 ConstructEntityEvent (org.spongepowered.api.event.entity.ConstructEntityEvent)6 SpongeEntityType (org.spongepowered.common.entity.SpongeEntityType)4 Entity (net.minecraft.entity.Entity)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 Player (org.spongepowered.api.entity.living.player.Player)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 World (org.spongepowered.api.world.World)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 NBTTagList (net.minecraft.nbt.NBTTagList)2