diff --git a/plugin/src/main/java/com/alttd/cometskyblock/worldgenerator/IslandGenerator.java b/plugin/src/main/java/com/alttd/cometskyblock/worldgenerator/IslandGenerator.java index 713362c..b38805d 100644 --- a/plugin/src/main/java/com/alttd/cometskyblock/worldgenerator/IslandGenerator.java +++ b/plugin/src/main/java/com/alttd/cometskyblock/worldgenerator/IslandGenerator.java @@ -1,25 +1,72 @@ package com.alttd.cometskyblock.worldgenerator; -import com.alttd.cometskyblock.CometSkyBlockPlugin; -import org.bukkit.World; -import org.bukkit.WorldCreator; +import com.alttd.cometskyblock.util.Mth; +import com.alttd.cometskyblock.util.WeightedChoice; +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) { - this.plugin = plugin; - } + private final WeightedChoice stones = new WeightedChoice().add(60, Material.STONE).add(10, Material.ANDESITE).add(10, Material.DIORITE).add(10, Material.GRANITE).add(10, Material.COAL_ORE); + private final WeightedChoice dirts = new WeightedChoice().add(80, Material.DIRT).add(20, Material.COARSE_DIRT); + private final WeightedChoice flora = new WeightedChoice().add(70, Material.SHORT_GRASS).add(10, Material.RED_TULIP).add(10, Material.OXEYE_DAISY).add(10, Material.CORNFLOWER); - 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); + @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); - World world = worldCreator.createWorld(); - }); + // 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); + } + } + } } }