Skip to content

Instantly share code, notes, and snippets.

@hube12
Created July 23, 2021 16:29
Show Gist options
  • Save hube12/22285395e22326c536afe8172adf7b7a to your computer and use it in GitHub Desktop.
Save hube12/22285395e22326c536afe8172adf7b7a to your computer and use it in GitHub Desktop.
Get Loot until limit
public static List<BPos> getLootUntil(RegionStructure<?,?> structure, BPos center, TerrainGenerator terrainGenerator, ChunkRand chunkRand, Predicate<Item> itemPredicate, int limit){
Generator.GeneratorFactory<?> factory = Generators.get(structure.getClass());
Generator structureGenerator = factory.create(terrainGenerator.getVersion());
if(structureGenerator.getPossibleLootItems().stream().noneMatch(itemPredicate)) {
return null;
}
int chunkInRegion = structure.getSpacing();
int regionSize = chunkInRegion * 16;
SpiralIterator<RPos> spiralIterator = new SpiralIterator<>(
new RPos(center.toRegionPos(regionSize).getX(), center.toRegionPos(regionSize).getZ(), regionSize),
new BPos(-30000000, 0, -30000000).toRegionPos(regionSize), new BPos(30000000, 0, 30000000).toRegionPos(regionSize),
1, (x, y, z) -> new RPos(x, z, regionSize)
);
Stream<Pair<CPos,Integer>> lootPos = StreamSupport.stream(spiralIterator.spliterator(), false)
.map(rPos -> structure.getInRegion(terrainGenerator.getWorldSeed(), rPos.getX(), rPos.getZ(), chunkRand))
.filter(Objects::nonNull)
.filter(cPos -> structure.canSpawn(cPos, terrainGenerator.getBiomeSource()) && structure.canGenerate(cPos, terrainGenerator))
.map(cPos -> {
if (structureGenerator.generate(terrainGenerator, cPos, chunkRand)){
return new Pair<>(cPos,((ILoot)structure).getLoot(terrainGenerator.getWorldSeed(), structureGenerator, chunkRand, false)
.stream().mapToInt(chestContent -> chestContent.getCount(itemPredicate)).sum());
}
return null;
})
.filter(Objects::nonNull);
AtomicInteger value=new AtomicInteger(0);
return StreamEx.of(lootPos)
.takeWhile(
cPosIntegerPair -> value.addAndGet(cPosIntegerPair.getSecond())<limit
).map(Pair::getFirst)
.map(CPos::toBlockPos)
.collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment