Skip to content

Commit

Permalink
fix: polyfill canGenerateStructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarium150 committed Feb 12, 2022
1 parent 65af61f commit 3da7fef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {
apply plugin: 'net.minecraftforge.gradle'

def previousVersion = '1.2.2'
def baseVersion = '1.3.0-beta.1'
def baseVersion = '1.3.0'
def minecraftVersion = '1.18.1'
version = "${minecraftVersion}-${baseVersion}" as Object
group = 'io.github.samarium150'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.StructureSettings;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -16,10 +22,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

/**
* Utilities related to structures
Expand Down Expand Up @@ -132,14 +136,38 @@ public static String getLocalizedStructureName(@Nonnull StructureFeature<?> stru
*/
@Nonnull
public static List<String> getDimensions(@Nonnull ServerLevel world, StructureFeature<?> structure) {
// final List<String> dims = new ArrayList<>();
// MinecraftServer server = world.getServer();
// server.getAllLevels().forEach(w->{
// // canGenerateStructure method doesn't exist in 1.18
// if (w.getChunkSource().getGenerator().getBiomeSource().canGenerateStructure(structure))
// dims.add(w.dimension().location().toString());
// });
return new ArrayList<>();
final List<String> dims = new ArrayList<>();
MinecraftServer server = world.getServer();
server.getAllLevels().forEach(w -> {
if (canGenerateStructure(w, structure))
dims.add(w.dimension().location().toString());
});
return dims;
}

/**
* Polyfill for native canGenerateStructure method, which doesn't exist in 1.18
* w.getChunkSource().getGenerator().getBiomeSource().canGenerateStructure(structure)
* @param world player's server world
* @param structure the given structure
* @return can be generated or not in the given world
*/
@Nonnull
private static Boolean canGenerateStructure(@Nonnull ServerLevel world, StructureFeature<?> structure) {
ChunkGenerator generator = world.getChunkSource().getGenerator();
StructureSettings settings = generator.getSettings();
if (structure == StructureFeature.STRONGHOLD)
return world.dimension() == Level.OVERWORLD;
Set<String> biomes = generator.getBiomeSource().possibleBiomes().stream()
.map(Biome::getRegistryName)
.filter(Objects::nonNull)
.map(ResourceLocation::toString)
.collect(Collectors.toSet());
for (ResourceKey<Biome> biome : settings.structures(structure).values()) {
if (biomes.contains(biome.location().toString()))
return true;
}
return false;
}

/**
Expand Down

0 comments on commit 3da7fef

Please sign in to comment.