use of dev.rosewood.rosestacker.manager.EntityCacheManager in project RoseStacker by Rosewood-Development.
the class MobSpawningMethod method spawn.
@Override
public void spawn(StackedSpawner stackedSpawner) {
StackedSpawnerTile spawnerTile = stackedSpawner.getSpawnerTile();
SpawnerStackSettings stackSettings = stackedSpawner.getStackSettings();
// Mob spawning logic
List<ConditionTag> spawnRequirements = new ArrayList<>(stackSettings.getSpawnRequirements());
// Check general spawner conditions
List<ConditionTag> perSpawnConditions = spawnRequirements.stream().filter(ConditionTag::isRequiredPerSpawn).collect(Collectors.toList());
spawnRequirements.removeAll(perSpawnConditions);
Set<ConditionTag> invalidSpawnConditions = spawnRequirements.stream().filter(x -> !x.check(stackedSpawner, stackedSpawner.getBlock())).collect(Collectors.toSet());
if (Setting.SPAWNER_SPAWN_ONLY_PLAYER_PLACED.getBoolean() && !stackedSpawner.isPlacedByPlayer())
invalidSpawnConditions.add(NotPlayerPlacedConditionTag.INSTANCE);
boolean passedSpawnerChecks = invalidSpawnConditions.isEmpty();
// Will be removed when they pass
invalidSpawnConditions.addAll(perSpawnConditions);
// Spawn the mobs
int spawnAmount;
if (Setting.SPAWNER_SPAWN_COUNT_STACK_SIZE_RANDOMIZED.getBoolean()) {
if (stackSettings.getSpawnCountStackSizeMultiplier() != -1) {
int spawnerSpawnCount = Math.max(spawnerTile.getSpawnCount(), 0);
spawnAmount = StackerUtils.randomInRange(stackedSpawner.getStackSize(), spawnerSpawnCount);
} else {
spawnAmount = this.random.nextInt(spawnerTile.getSpawnCount()) + 1;
}
} else {
spawnAmount = spawnerTile.getSpawnCount();
}
EntityCacheManager entityCacheManager = RoseStacker.getInstance().getManager(EntityCacheManager.class);
StackManager stackManager = RoseStacker.getInstance().getManager(StackManager.class);
Bukkit.getScheduler().runTaskAsynchronously(RoseStacker.getInstance(), () -> {
Set<Location> spawnLocations = new HashSet<>();
int spawnRange = spawnerTile.getSpawnRange();
for (int i = 0; i < spawnAmount; i++) {
int attempts = 0;
while (attempts < Setting.SPAWNER_MAX_FAILED_SPAWN_ATTEMPTS.getInt()) {
int xOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
int yOffset = !Setting.SPAWNER_USE_VERTICAL_SPAWN_RANGE.getBoolean() ? this.random.nextInt(3) - 1 : this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
int zOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
Location spawnLocation = stackedSpawner.getLocation().clone().add(xOffset + 0.5, yOffset, zOffset + 0.5);
Block target = stackedSpawner.getLocation().clone().add(xOffset, yOffset, zOffset).getBlock();
boolean invalid = false;
for (ConditionTag conditionTag : perSpawnConditions) {
if (!conditionTag.check(stackedSpawner, target)) {
invalid = true;
} else {
invalidSpawnConditions.remove(conditionTag);
}
}
if (invalid) {
attempts++;
continue;
}
if (!passedSpawnerChecks)
break;
spawnLocations.add(spawnLocation);
break;
}
}
EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
Predicate<Entity> predicate = entity -> entity.getType() == entityType;
Collection<Entity> nearbyEntities = entityCacheManager.getNearbyEntities(stackedSpawner.getLocation(), stackSettings.getSpawnRange(), predicate);
List<StackedEntity> nearbyStackedEntities = new ArrayList<>();
for (Entity entity : nearbyEntities) {
StackedEntity stackedEntity = stackManager.getStackedEntity((LivingEntity) entity);
if (stackedEntity != null)
nearbyStackedEntities.add(stackedEntity);
}
int successfulSpawns = this.spawnEntitiesIntoNearbyStacks(stackedSpawner, spawnAmount, spawnLocations, nearbyStackedEntities, stackManager);
stackedSpawner.getLastInvalidConditions().clear();
if (successfulSpawns <= 0) {
if (invalidSpawnConditions.isEmpty()) {
stackedSpawner.getLastInvalidConditions().add(NoneConditionTag.class);
} else {
List<Class<? extends ConditionTag>> invalidSpawnConditionClasses = new ArrayList<>();
for (ConditionTag conditionTag : invalidSpawnConditions) invalidSpawnConditionClasses.add(conditionTag.getClass());
stackedSpawner.getLastInvalidConditions().addAll(invalidSpawnConditionClasses);
}
// Spawn particles indicating the spawn did not occur
stackedSpawner.getWorld().spawnParticle(Particle.SMOKE_NORMAL, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
} else {
// Spawn particles indicating the spawn occurred
stackedSpawner.getWorld().spawnParticle(Particle.FLAME, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
if (stackedSpawner.getBlock().getType() == Material.SPAWNER)
PersistentDataUtils.increaseSpawnCount(spawnerTile, successfulSpawns);
});
}
});
}
Aggregations