Skip to content

Instantly share code, notes, and snippets.

@deathmarine
Created July 28, 2020 01:15
Show Gist options
  • Save deathmarine/a82e8111a74adeacb6177b184328d5f2 to your computer and use it in GitHub Desktop.
Save deathmarine/a82e8111a74adeacb6177b184328d5f2 to your computer and use it in GitHub Desktop.
Islands Bukkit Mod Class for Islands 2012
package com.modcrafting.islands;
import com.modcrafting.populators.GrassPopulator;
import com.modcrafting.populators.OrePopulator;
import com.modcrafting.populators.CavePopulator;
import com.modcrafting.populators.DungeonPopulator;
import com.modcrafting.populators.TreePopulator;
import java.util.ArrayList;
import org.bukkit.generator.BlockPopulator;
import java.util.List;
import org.bukkit.block.Biome;
import org.bukkit.util.noise.PerlinOctaveGenerator;
import org.bukkit.util.noise.SimplexOctaveGenerator;
import java.util.Random;
import org.bukkit.World;
import org.bukkit.Material;
import org.bukkit.generator.ChunkGenerator;
public class Chunker extends ChunkGenerator
{
void setBlock(final int x, final int y, final int z, final byte[][] chunk, final Material material) {
if (x >= 16 || x < 0 || z >= 16 || z < 0 || y >= 256 || y < 0) {
return;
}
if (chunk[y >> 4] == null) {
chunk[y >> 4] = new byte[4096];
}
if (y > 256 || y < 0 || x > 16 || x < 0 || z > 16 || z < 0) {
return;
}
try {
chunk[y >> 4][(y & 0xF) << 8 | z << 4 | x] = (byte)material.getId();
}
catch (Exception ex) {}
}
byte getBlock(final int x, final int y, final int z, final byte[][] chunk) {
if (y >= 256 || y < 0 || x >= 16 || x < 0 || z >= 16 || z < 0) {
return 0;
}
if (chunk[y >> 4] == null) {
return 0;
}
try {
return chunk[y >> 4][(y & 0xF) << 8 | z << 4 | x];
}
catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public byte[][] generateBlockSections(final World world, final Random rand, final int ChunkX, final int ChunkZ, final ChunkGenerator.BiomeGrid biome) {
final byte[][] chunk = new byte[world.getMaxHeight() / 16][];
final SimplexOctaveGenerator bg = new SimplexOctaveGenerator(world, 18);
bg.setScale(0.0078125);
final SimplexOctaveGenerator g = new SimplexOctaveGenerator(world, 36);
g.setScale(0.015625);
final PerlinOctaveGenerator e = new PerlinOctaveGenerator(world, 32);
e.setScale(0.03125);
final PerlinOctaveGenerator c = new PerlinOctaveGenerator(world, 38);
c.setScale(0.015625);
final SimplexOctaveGenerator d = new SimplexOctaveGenerator(world, 18);
d.setScale(0.0078125);
final int max = world.getMaxHeight();
for (int x = 0; x < 16; ++x) {
for (int z = 0; z < 16; ++z) {
final Biome b = biome.getBiome(x, z);
final int rx = x + ChunkX * 16;
final int rz = z + ChunkZ * 16;
final int h = (int)(bg.noise((double)rx, (double)rz, 0.75, 0.75) * 38.0 + 58.0);
final int bh = (int)(g.noise((double)rx, (double)rz, 0.5, 0.5) * 24.0 + 56.0);
final int ch = (int)(c.noise((double)rx, (double)rz, 0.75, 0.25) + g.noise((double)rx, (double)rz, 1.25, 0.75) + bg.noise((double)rx, (double)rz, 0.25, 0.5) * 18.0 + 20.0);
final int eg = (int)(e.noise((double)rx, (double)rz, 0.5, 0.5) + g.noise((double)rx, (double)rz, 1.25, 0.75) * 38.0 + 64.0);
final int ds = (int)(d.noise((double)rx, (double)rz, 0.25, 0.75) + c.noise((double)rx, (double)rz, 0.75, 0.25) + g.noise((double)rx, (double)rz, 1.25, 0.75) + bg.noise((double)rx, (double)rz, 0.25, 0.5) * 32.0 + 25.0);
for (int y = 2; y < max; ++y) {
if (y < h || y < bh || y < eg) {
this.setBlock(x, y, z, chunk, Material.STONE);
}
if (y > ch * -1 + 95 || y > ds * -1 + 101) {
this.setBlock(x, y, z, chunk, Material.AIR);
}
if (y > 70) {
int it = 0;
final int maxX = Math.max(x + 1, x - 1);
final int maxY = Math.max(y + 1, y - 1);
final int maxZ = Math.max(z + 1, z - 1);
final int minX = Math.max(x + 1, x - 1);
final int minY = Math.max(y + 1, y - 1);
final int minZ = Math.max(z + 1, z - 1);
for (int i = 0; i <= Math.abs(maxX - minX); ++i) {
for (int ii = 0; ii <= Math.abs(maxZ - minZ); ++ii) {
for (int iii = 0; iii <= Math.abs(maxY - minY); ++iii) {
final int bt = this.getBlock(minX + i, minY + iii, minZ + ii, chunk);
if (bt == Material.AIR.getId() || bt == Material.STATIONARY_WATER.getId()) {
++it;
}
}
}
}
if (it > 5) {
this.setBlock(x, y, z, chunk, Material.AIR);
}
}
}
Material b2;
Material b3;
if (b.equals((Object)Biome.DESERT) || b.equals((Object)Biome.DESERT_HILLS) || b.equals((Object)Biome.BEACH)) {
b2 = Material.SAND;
b3 = Material.SANDSTONE;
}
else {
b2 = Material.GRASS;
b3 = Material.DIRT;
}
for (int y2 = 74; y2 < max; ++y2) {
final int thisblock = this.getBlock(x, y2, z, chunk);
final int blockabove = this.getBlock(x, y2 + 1, z, chunk);
if (thisblock != Material.AIR.getId() && blockabove == Material.AIR.getId()) {
this.setBlock(x, y2, z, chunk, b2);
final int y3 = rand.nextInt(5) + 1;
final int y4 = rand.nextInt(5) + 1;
if (y2 < 128) {
if (this.getBlock(x, y2 - y3, z, chunk) != Material.AIR.getId()) {
this.setBlock(x, y2 - y3, z, chunk, b3);
}
if (this.getBlock(x, y2 - y4, z, chunk) != Material.AIR.getId()) {
this.setBlock(x, y2 - y4, z, chunk, b3);
}
}
}
}
for (int y2 = 1; y2 < 75; ++y2) {
final int thisblock = this.getBlock(x, y2, z, chunk);
if (thisblock == Material.AIR.getId()) {
this.setBlock(x, y2, z, chunk, Material.STATIONARY_WATER);
final int blockbelow = this.getBlock(x, y2 - 1, z, chunk);
if (blockbelow == Material.GRASS.getId()) {
this.setBlock(x, y2 - 1, z, chunk, Material.SAND);
}
}
}
this.setBlock(x, 0, z, chunk, Material.BEDROCK);
}
}
return chunk;
}
public List<BlockPopulator> getDefaultPopulators(final World world) {
final ArrayList<BlockPopulator> pops = new ArrayList<BlockPopulator>();
pops.add(new TreePopulator());
pops.add(new DungeonPopulator());
pops.add(new CavePopulator());
pops.add(new OrePopulator());
pops.add(new GrassPopulator());
return pops;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment