mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-27 19:24:43 +02:00
Moved more packaged based on feedback
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
package com.plotsquared.bukkit.generator;
|
||||
|
||||
import com.plotsquared.generator.AugmentedUtils;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class BukkitAugmentedGenerator extends BlockPopulator {
|
||||
|
||||
private static BukkitAugmentedGenerator generator;
|
||||
|
||||
public static BukkitAugmentedGenerator get(World world) {
|
||||
for (BlockPopulator populator : world.getPopulators()) {
|
||||
if (populator instanceof BukkitAugmentedGenerator) {
|
||||
return (BukkitAugmentedGenerator) populator;
|
||||
}
|
||||
}
|
||||
if (generator == null) {
|
||||
generator = new BukkitAugmentedGenerator();
|
||||
}
|
||||
world.getPopulators().add(generator);
|
||||
return generator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk source) {
|
||||
AugmentedUtils.generate(source, world.getName(), source.getX(), source.getZ(), null);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.plotsquared.bukkit.generator;
|
||||
|
||||
import com.plotsquared.generator.HybridUtils;
|
||||
|
||||
public class BukkitHybridUtils extends HybridUtils {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,236 @@
|
||||
package com.plotsquared.bukkit.generator;
|
||||
|
||||
import com.plotsquared.bukkit.util.BukkitUtil;
|
||||
import com.plotsquared.bukkit.util.block.GenChunk;
|
||||
import com.plotsquared.PlotSquared;
|
||||
import com.plotsquared.generator.GeneratorWrapper;
|
||||
import com.plotsquared.generator.IndependentPlotGenerator;
|
||||
import com.plotsquared.location.ChunkWrapper;
|
||||
import com.plotsquared.plot.PlotArea;
|
||||
import com.plotsquared.plot.worlds.SingleWorldGenerator;
|
||||
import com.plotsquared.util.ChunkManager;
|
||||
import com.plotsquared.util.MainUtil;
|
||||
import com.plotsquared.queue.ScopedLocalBlockQueue;
|
||||
import com.sk89q.worldedit.math.BlockVector2;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
public class BukkitPlotGenerator extends ChunkGenerator
|
||||
implements GeneratorWrapper<ChunkGenerator> {
|
||||
|
||||
@SuppressWarnings("unused") public final boolean PAPER_ASYNC_SAFE = true;
|
||||
|
||||
private final IndependentPlotGenerator plotGenerator;
|
||||
private final ChunkGenerator platformGenerator;
|
||||
private final boolean full;
|
||||
private List<BlockPopulator> populators;
|
||||
private boolean loaded = false;
|
||||
|
||||
@Getter private final String levelName;
|
||||
|
||||
public BukkitPlotGenerator(String name, IndependentPlotGenerator generator) {
|
||||
if (generator == null) {
|
||||
throw new IllegalArgumentException("Generator may not be null!");
|
||||
}
|
||||
this.levelName = name;
|
||||
this.plotGenerator = generator;
|
||||
this.platformGenerator = this;
|
||||
this.populators = new ArrayList<>();
|
||||
this.populators.add(new BlockStatePopulator(this.plotGenerator));
|
||||
this.full = true;
|
||||
MainUtil.initCache();
|
||||
}
|
||||
|
||||
public BukkitPlotGenerator(final String world, final ChunkGenerator cg) {
|
||||
if (cg instanceof BukkitPlotGenerator) {
|
||||
throw new IllegalArgumentException("ChunkGenerator: " + cg.getClass().getName()
|
||||
+ " is already a BukkitPlotGenerator!");
|
||||
}
|
||||
this.levelName = world;
|
||||
this.full = false;
|
||||
this.platformGenerator = cg;
|
||||
this.plotGenerator = new DelegatePlotGenerator(cg, world);
|
||||
MainUtil.initCache();
|
||||
}
|
||||
|
||||
@Override public void augment(PlotArea area) {
|
||||
BukkitAugmentedGenerator.get(BukkitUtil.getWorld(area.getWorldName()));
|
||||
}
|
||||
|
||||
@Override public boolean isFull() {
|
||||
return this.full;
|
||||
}
|
||||
|
||||
@Override public IndependentPlotGenerator getPlotGenerator() {
|
||||
return this.plotGenerator;
|
||||
}
|
||||
|
||||
@Override public ChunkGenerator getPlatformGenerator() {
|
||||
return this.platformGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<BlockPopulator> getDefaultPopulators(@NotNull World world) {
|
||||
try {
|
||||
if (!this.loaded) {
|
||||
String name = world.getName();
|
||||
PlotSquared.get().loadWorld(name, this);
|
||||
Set<PlotArea> areas = PlotSquared.get().getPlotAreas(name);
|
||||
if (!areas.isEmpty()) {
|
||||
PlotArea area = areas.iterator().next();
|
||||
if (!area.isMobSpawning()) {
|
||||
if (!area.isSpawnEggs()) {
|
||||
world.setSpawnFlags(false, false);
|
||||
}
|
||||
world.setAmbientSpawnLimit(0);
|
||||
world.setAnimalSpawnLimit(0);
|
||||
world.setMonsterSpawnLimit(0);
|
||||
world.setWaterAnimalSpawnLimit(0);
|
||||
} else {
|
||||
world.setSpawnFlags(true, true);
|
||||
world.setAmbientSpawnLimit(-1);
|
||||
world.setAnimalSpawnLimit(-1);
|
||||
world.setMonsterSpawnLimit(-1);
|
||||
world.setWaterAnimalSpawnLimit(-1);
|
||||
}
|
||||
}
|
||||
this.loaded = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ArrayList<BlockPopulator> toAdd = new ArrayList<>();
|
||||
List<BlockPopulator> existing = world.getPopulators();
|
||||
if (populators == null && platformGenerator != null) {
|
||||
populators = new ArrayList<>(platformGenerator.getDefaultPopulators(world));
|
||||
}
|
||||
if (populators != null) {
|
||||
for (BlockPopulator populator : this.populators) {
|
||||
if (!existing.contains(populator)) {
|
||||
toAdd.add(populator);
|
||||
}
|
||||
}
|
||||
}
|
||||
return toAdd;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z,
|
||||
@NotNull BiomeGrid biome) {
|
||||
|
||||
GenChunk result = new GenChunk();
|
||||
if (this.getPlotGenerator() instanceof SingleWorldGenerator) {
|
||||
if (result.getChunkData() != null) {
|
||||
for (int chunkX = 0; chunkX < 16; chunkX++) {
|
||||
for (int chunkZ = 0; chunkZ < 16; chunkZ++) {
|
||||
for (int y = 0; y < world.getMaxHeight(); y++) {
|
||||
biome.setBiome(chunkX, y, chunkZ, Biome.PLAINS);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.getChunkData();
|
||||
}
|
||||
}
|
||||
// Set the chunk location
|
||||
result.setChunk(new ChunkWrapper(world.getName(), x, z));
|
||||
// Set the result data
|
||||
result.setChunkData(createChunkData(world));
|
||||
result.biomeGrid = biome;
|
||||
result.result = null;
|
||||
|
||||
// Catch any exceptions (as exceptions usually thrown)
|
||||
try {
|
||||
// Fill the result data if necessary
|
||||
if (this.platformGenerator != this) {
|
||||
return this.platformGenerator.generateChunkData(world, random, x, z, biome);
|
||||
} else {
|
||||
generate(BlockVector2.at(x, z), world, result);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Return the result data
|
||||
return result.getChunkData();
|
||||
}
|
||||
|
||||
private void generate(BlockVector2 loc, World world, ScopedLocalBlockQueue result) {
|
||||
// Load if improperly loaded
|
||||
if (!this.loaded) {
|
||||
String name = world.getName();
|
||||
PlotSquared.get().loadWorld(name, this);
|
||||
this.loaded = true;
|
||||
}
|
||||
// Process the chunk
|
||||
if (ChunkManager.preProcessChunk(loc, result)) {
|
||||
return;
|
||||
}
|
||||
PlotArea area = PlotSquared.get().getPlotArea(world.getName(), null);
|
||||
if (area == null && (area = PlotSquared.get().getPlotArea(this.levelName, null)) == null) {
|
||||
throw new IllegalStateException("Cannot regenerate chunk that does not belong to a plot area."
|
||||
+ " Location: " + loc + ", world: " + world);
|
||||
}
|
||||
try {
|
||||
this.plotGenerator.generateChunk(result, area);
|
||||
} catch (Throwable e) {
|
||||
// Recover from generator error
|
||||
e.printStackTrace();
|
||||
}
|
||||
ChunkManager.postProcessChunk(loc, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSpawn(@NotNull final World world, final int x, final int z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean shouldGenerateCaves() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean shouldGenerateDecorations() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isParallelCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean shouldGenerateMobs() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean shouldGenerateStructures() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
if (this.platformGenerator == this) {
|
||||
return this.plotGenerator.getName();
|
||||
}
|
||||
if (this.platformGenerator == null) {
|
||||
return "null";
|
||||
} else {
|
||||
return this.platformGenerator.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public boolean equals(final Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return toString().equals(obj.toString()) || toString().equals(obj.getClass().getName());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.plotsquared.bukkit.generator;
|
||||
|
||||
import com.plotsquared.bukkit.util.BukkitUtil;
|
||||
import com.plotsquared.PlotSquared;
|
||||
import com.plotsquared.generator.IndependentPlotGenerator;
|
||||
import com.plotsquared.location.Location;
|
||||
import com.plotsquared.plot.PlotArea;
|
||||
import com.plotsquared.plot.PlotId;
|
||||
import com.plotsquared.util.MathMan;
|
||||
import com.plotsquared.queue.ScopedLocalBlockQueue;
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import java.util.Random;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
final class DelegatePlotGenerator extends IndependentPlotGenerator {
|
||||
|
||||
private final ChunkGenerator chunkGenerator;
|
||||
private final String world;
|
||||
|
||||
public DelegatePlotGenerator(ChunkGenerator chunkGenerator, String world) {
|
||||
this.chunkGenerator = chunkGenerator;
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
@Override public void initialize(PlotArea area) {
|
||||
}
|
||||
|
||||
@Override public String getName() {
|
||||
return this.chunkGenerator.getClass().getName();
|
||||
}
|
||||
|
||||
@Override public PlotArea getNewPlotArea(String world, String id, PlotId min, PlotId max) {
|
||||
return PlotSquared.get().IMP.getDefaultGenerator().getNewPlotArea(world, id, min, max);
|
||||
}
|
||||
|
||||
@Override public void generateChunk(final ScopedLocalBlockQueue result, PlotArea settings) {
|
||||
World world = BukkitUtil.getWorld(this.world);
|
||||
Location min = result.getMin();
|
||||
int chunkX = min.getX() >> 4;
|
||||
int chunkZ = min.getZ() >> 4;
|
||||
Random random = new Random(MathMan.pair((short) chunkX, (short) chunkZ));
|
||||
try {
|
||||
ChunkGenerator.BiomeGrid grid = new ChunkGenerator.BiomeGrid() {
|
||||
@Override public void setBiome(@Range(from = 0, to = 15) int x,
|
||||
@Range(from = 0, to = 15) int z, @NotNull Biome biome) {
|
||||
result.setBiome(x, z, BukkitAdapter.adapt(biome));
|
||||
}
|
||||
|
||||
//do not annotate with Override until we discontinue support for 1.4.4
|
||||
public void setBiome(int x, int y, int z, @NotNull Biome biome) {
|
||||
result.setBiome(x, z, BukkitAdapter.adapt(biome));
|
||||
|
||||
}
|
||||
|
||||
@Override @NotNull public Biome getBiome(int x, int z) {
|
||||
return Biome.FOREST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Biome getBiome(int x, int y, int z) {
|
||||
return Biome.FOREST;
|
||||
}
|
||||
};
|
||||
chunkGenerator.generateChunkData(world, random, chunkX, chunkZ, grid);
|
||||
return;
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
for (BlockPopulator populator : chunkGenerator.getDefaultPopulators(world)) {
|
||||
populator.populate(world, random, world.getChunkAt(chunkX, chunkZ));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.plotsquared.bukkit.generator;
|
||||
|
||||
import com.plotsquared.PlotSquared;
|
||||
import com.plotsquared.generator.IndependentPlotGenerator;
|
||||
import com.plotsquared.location.ChunkWrapper;
|
||||
import com.plotsquared.plot.PlotArea;
|
||||
import com.plotsquared.queue.GlobalBlockQueue;
|
||||
import com.plotsquared.queue.LocalBlockQueue;
|
||||
import com.plotsquared.queue.ScopedLocalBlockQueue;
|
||||
import java.util.Random;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
final class BlockStatePopulator extends BlockPopulator {
|
||||
|
||||
private final IndependentPlotGenerator plotGenerator;
|
||||
private LocalBlockQueue queue;
|
||||
|
||||
public BlockStatePopulator(IndependentPlotGenerator plotGenerator) {
|
||||
this.plotGenerator = plotGenerator;
|
||||
}
|
||||
|
||||
@Override public void populate(@NotNull final World world, @NotNull final Random random,
|
||||
@NotNull final Chunk source) {
|
||||
if (this.queue == null) {
|
||||
this.queue = GlobalBlockQueue.IMP.getNewQueue(world.getName(), false);
|
||||
}
|
||||
final PlotArea area = PlotSquared.get().getPlotArea(world.getName(), null);
|
||||
final ChunkWrapper wrap = new ChunkWrapper(area.getWorldName(), source.getX(), source.getZ());
|
||||
final ScopedLocalBlockQueue chunk = this.queue.getForChunk(wrap.x, wrap.z);
|
||||
if (this.plotGenerator.populateChunk(chunk, area)) {
|
||||
this.queue.flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user