Update IslandGenerator.java and have it generate random islands

This commit is contained in:
Len 2024-01-21 13:34:13 +01:00
parent 0d841607f5
commit 6fb2e2769e

View File

@ -1,25 +1,72 @@
package com.alttd.cometskyblock.worldgenerator; package com.alttd.cometskyblock.worldgenerator;
import com.alttd.cometskyblock.CometSkyBlockPlugin; import com.alttd.cometskyblock.util.Mth;
import org.bukkit.World; import com.alttd.cometskyblock.util.WeightedChoice;
import org.bukkit.WorldCreator; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.LimitedRegion;
import org.bukkit.generator.WorldInfo;
import org.jetbrains.annotations.NotNull;
public class IslandGenerator { import java.util.Random;
private final CometSkyBlockPlugin plugin; public class IslandGenerator extends BlockPopulator {
public IslandGenerator(CometSkyBlockPlugin plugin) { private final WeightedChoice<Material> stones = new WeightedChoice<Material>().add(60, Material.STONE).add(10, Material.ANDESITE).add(10, Material.DIORITE).add(10, Material.GRANITE).add(10, Material.COAL_ORE);
this.plugin = plugin; private final WeightedChoice<Material> dirts = new WeightedChoice<Material>().add(80, Material.DIRT).add(20, Material.COARSE_DIRT);
private final WeightedChoice<Material> flora = new WeightedChoice<Material>().add(70, Material.SHORT_GRASS).add(10, Material.RED_TULIP).add(10, Material.OXEYE_DAISY).add(10, Material.CORNFLOWER);
@Override
public void populate(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull LimitedRegion limitedRegion) {
// Always generate an island at 0 0 and add spread some random in the world
if ((x == 0 && z == 0) || random.nextInt(10000) < 1) {
// set starting values
int startY = 95;
int startX = x * 16;
int startZ = z * 16;
int treeX = Mth.randomBetweenInclusive(random, -3, 3);
int treeZ = Mth.randomBetweenInclusive(random, -3, 3);
// Code altered from Place() in net.minecraft.world.level.levelgen.feature.EndIslandFeature
float f = (float)random.nextInt(3) + 4.0F;
for(int i = 0; f > 0.5F; --i) {
for(int j = Mth.floor(-f); j <= Mth.ceil(f); ++j) {
for(int k = Mth.floor(-f); k <= Mth.ceil(f); ++k) {
if ((float)(j * j + k * k) <= (f + 1.0F) * (f + 1.0F)) {
if (limitedRegion.isInRegion(startX + j, startY + i, startZ + k)) {
Material material;
if (i == 0) {
material = Material.GRASS_BLOCK;
if (random.nextInt(10) < 3) {
limitedRegion.setType(startX + j, startY + i + 1, startZ + k, flora.next());
}
} else if (i == -1) {
material = dirts.next();
} else if (i == -2) {
material = random.nextInt(10) < 3 ? stones.next() : dirts.next();
} else {
material = stones.next();
}
limitedRegion.setType(startX + j, startY + i, startZ + k, material);
}
}
}
}
f -= (float)random.nextInt(2) + 0.5F;
}
// try to add a tree
if (limitedRegion.isInRegion(startX + treeX, startY + 1, startX + treeZ)) {
Location location = new Location(null, startX + treeX, startY + 1, startX + treeZ);
// always set a dirt block under the tree location
limitedRegion.setType(location.clone().add(0, -1, 0), Material.DIRT);
if (!limitedRegion.generateTree(location, random, TreeType.TREE)) {
limitedRegion.setType(startX + treeX, startY, startX + treeZ, Material.OAK_SAPLING);
}
}
} }
public void create(String islandName) {
plugin.getServer().getScheduler().runTask(plugin, () -> {
WorldCreator worldCreator = new WorldCreator(islandName);
worldCreator.generator(new CometVoidGenerator());
worldCreator.environment(World.Environment.NORMAL);
worldCreator.generateStructures(true);
World world = worldCreator.createWorld();
});
} }
} }