Skip to content

Commit

Permalink
Implement required biomes in default generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Apr 25, 2021
1 parent 9317c32 commit 217a930
Showing 1 changed file with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,20 @@ public GeneratorTierObject getGeneratorByID(String generatorId)
Location location,
GeneratorTierObject.GeneratorType generatorType)
{
// Gets biome from location.
final Biome biome = location.getWorld().getBiome(location.getBlockX(),
location.getBlockY(),
location.getBlockZ());

if (island == null)
{
// No islands at given location, find and use default generator tier.
return this.findDefaultGeneratorTier(location.getWorld(), generatorType);
return this.findDefaultGeneratorTier(location.getWorld(), generatorType, biome);
}

this.addIslandData(island);
GeneratorDataObject data = this.generatorDataCache.get(island.getUniqueId());

// Gets biome from location.
final Biome biome = location.getWorld().getBiome(location.getBlockX(),
location.getBlockY(),
location.getBlockZ());

// TODO: It is necessary to reset user cache when import new generators are don.
// I changed implementation for faster work, so it gets challenges on island data loading,
// but it cache it inside island data. So when someone overwrites generators in database,
Expand Down Expand Up @@ -480,7 +480,7 @@ else if (o1.getGeneratorType() != o2.getGeneratorType())
});

return optionalGenerator.orElse(
this.findDefaultGeneratorTier(location.getWorld(), generatorType));
this.findDefaultGeneratorTier(location.getWorld(), generatorType, biome));
}


Expand All @@ -490,10 +490,12 @@ else if (o1.getGeneratorType() != o2.getGeneratorType())
*
* @param world of type World
* @param generatorType of type GeneratorType
* @param biome of the generator location.
* @return GeneratorTierObject
*/
private @Nullable GeneratorTierObject findDefaultGeneratorTier(World world,
GeneratorTierObject.GeneratorType generatorType)
GeneratorTierObject.GeneratorType generatorType,
Biome biome)
{
String gameMode = this.addon.getPlugin().getIWM().getAddon(world).map(
gameModeAddon -> gameModeAddon.getDescription().getName()).orElse("");
Expand All @@ -508,16 +510,43 @@ else if (o1.getGeneratorType() != o2.getGeneratorType())
// Filter all default generators
// Filter generators with necessary type.
// Filter generators that starts with name.
// Sort generators by priority, type and name in reversed order
// Get a generator that has largest priority and has required biome
// Return return the generator tier with max value or null
return this.generatorTierCache.values().stream().
filter(GeneratorTierObject::isDefaultGenerator).
filter(generator -> generator.getGeneratorType().includes(generatorType)).
filter(generator -> generator.getUniqueId().startsWith(gameMode.toLowerCase())).
max(Comparator.comparingInt(GeneratorTierObject::getPriority).
thenComparing(GeneratorTierObject::getGeneratorType).
thenComparing(Comparator.comparing(GeneratorTierObject::getFriendlyName).reversed())).
orElse(null);
filter(GeneratorTierObject::isDefaultGenerator).
filter(generator -> generator.getGeneratorType().includes(generatorType)).
filter(generator -> generator.getUniqueId().startsWith(gameMode.toLowerCase())).
filter(generator -> generator.getRequiredBiomes().isEmpty() ||
generator.getRequiredBiomes().contains(biome)).
max((o1, o2) ->
{
// If required biomes is empty, the it works in all biomes.
boolean o1HasBiome = o1.getRequiredBiomes().isEmpty() ||
o1.getRequiredBiomes().contains(biome);
boolean o2HasBiome = o2.getRequiredBiomes().isEmpty() ||
o2.getRequiredBiomes().contains(biome);

if (o1HasBiome != o2HasBiome)
{
return Boolean.compare(o1HasBiome, o2HasBiome);
}
else if (o1.getPriority() != o2.getPriority())
{
// Larger priority must be in the end.
return Integer.compare(o1.getPriority(), o2.getPriority());
}
else if (o1.getGeneratorType() != o2.getGeneratorType())
{
// Compare by type. Generators which are more specified should be first.
return o1.getGeneratorType().compareTo(o2.getGeneratorType());
}
else
{
// Compare by unique id.
return o1.getUniqueId().compareTo(o2.getUniqueId());
}
}).
orElse(null);
}


Expand Down

0 comments on commit 217a930

Please sign in to comment.