Move bukkit specific files

This commit is contained in:
Sauilitired
2015-07-26 16:51:12 +02:00
parent 85d68455e4
commit 9184010c9f
152 changed files with 355 additions and 548 deletions

View File

@ -0,0 +1,287 @@
package com.plotsquared.bukkit.generator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.generator.BlockPopulator;
import com.intellectualcrafters.plot.PS;
import com.plotsquared.bukkit.object.BlockWrapper;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.plotsquared.bukkit.object.PlotGenerator;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotLoc;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.RegionWrapper;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.TaskManager;
import com.plotsquared.bukkit.util.bukkit.BukkitChunkManager;
import com.plotsquared.bukkit.util.bukkit.BukkitSetBlockManager;
public class AugmentedPopulator extends BlockPopulator {
public static short[][] x_loc;
public static short[][] y_loc;
public static short[][] z_loc;
public final PlotWorld plotworld;
public final PlotManager manager;
public final PlotGenerator generator;
public final PlotCluster cluster;
public final Random r = new Random();
public final boolean p;
public final boolean b;
public final boolean o;
private final int bx;
private final int bz;
private final int tx;
private final int tz;
public AugmentedPopulator(final String world, final PlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) {
initCache();
this.cluster = cluster;
this.generator = generator;
this.plotworld = PS.get().getPlotWorld(world);
this.manager = generator.getPlotManager();
this.p = p;
this.b = b;
this.o = (this.plotworld.TERRAIN == 1) || (this.plotworld.TERRAIN == 2);
final World bukkitWorld = Bukkit.getWorld(world);
if (cluster != null) {
final Location bl = this.manager.getPlotBottomLocAbs(this.plotworld, cluster.getP1());
final Location tl = this.manager.getPlotTopLocAbs(this.plotworld, cluster.getP2()).add(1, 0, 1);
this.bx = bl.getX();
this.bz = bl.getZ();
this.tx = tl.getX();
this.tz = tl.getZ();
} else {
this.bx = Integer.MIN_VALUE;
this.bz = Integer.MIN_VALUE;
this.tx = Integer.MAX_VALUE;
this.tz = Integer.MAX_VALUE;
}
// Add the populator
if (this.o) {
bukkitWorld.getPopulators().add(0, this);
} else {
bukkitWorld.getPopulators().add(this);
}
}
public static void removePopulator(final String worldname, final PlotCluster cluster) {
final World world = Bukkit.getWorld(worldname);
for (final Iterator<BlockPopulator> iterator = world.getPopulators().iterator(); iterator.hasNext();) {
final BlockPopulator populator = iterator.next();
if (populator instanceof AugmentedPopulator) {
if (((AugmentedPopulator) populator).cluster.equals(cluster)) {
iterator.remove();
}
}
}
}
public static void initCache() {
if (x_loc == null) {
x_loc = new short[16][4096];
y_loc = new short[16][4096];
z_loc = new short[16][4096];
for (int i = 0; i < 16; i++) {
int i4 = i << 4;
for (int j = 0; j < 4096; j++) {
final int y = (i4) + (j >> 8);
final int a = (j - ((y & 0xF) << 8));
final int z1 = (a >> 4);
final int x1 = a - (z1 << 4);
x_loc[i][j] = (short) x1;
y_loc[i][j] = (short) y;
z_loc[i][j] = (short) z1;
}
}
}
}
public BlockWrapper get(final int x, final int z, final int i, final int j, final short[][] r, final boolean c) {
final int y = (i << 4) + (j >> 8);
final int a = (j - ((y & 0xF) << 8));
final int z1 = (a >> 4);
final int x1 = a - (z1 << 4);
if (r[i] == null) {
return (c && (((z + z1) < this.bz) || ((z + z1) > this.tz) || ((x + x1) < this.bx) || ((x + x1) > this.tx))) ? null : new BlockWrapper(x1, y, z1, (short) 0, (byte) 0);
} else {
return (c && (((z + z1) < this.bz) || ((z + z1) > this.tz) || ((x + x1) < this.bx) || ((x + x1) > this.tx))) ? null : new BlockWrapper(x1, y, z1, r[i][j], (byte) 0);
}
}
@Override
public void populate(final World world, final Random rand, final Chunk chunk) {
final int cx = chunk.getX();
final int cz = chunk.getZ();
final int bx = cx << 4;
final int bz = cz << 4;
final int tx = bx + 15;
final int tz = bz + 15;
final boolean inX1 = ((bx >= this.bx) && (bx <= this.tx));
final boolean inX2 = ((tx >= this.bx) && (tx <= this.tx));
final boolean inZ1 = ((bz >= this.bz) && (bz <= this.tz));
final boolean inZ2 = ((tz >= this.bz) && (tz <= this.tz));
final boolean inX = inX1 || inX2;
final boolean inZ = inZ1 || inZ2;
if (!inX || !inZ) {
return;
}
if (this.plotworld.TERRAIN == 3) {
int X = chunk.getX() << 4;
int Z = chunk.getZ() << 4;
if (ChunkManager.FORCE_PASTE) {
for (short x = 0; x < 16; x++) {
for (short z = 0; z < 16; z++) {
final PlotLoc loc = new PlotLoc((short) (X + x), (short) (Z + z));
final HashMap<Short, Short> blocks = ChunkManager.GENERATE_BLOCKS.get(loc);
HashMap<Short, Byte> datas = ChunkManager.GENERATE_DATA.get(loc);
for (final Entry<Short, Short> entry : blocks.entrySet()) {
int y = entry.getKey();
byte data;
if (datas != null) {
data = datas.get(y);
}
else {
data = 0;
}
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, blocks.get(y), data);
}
}
}
return;
}
if (ChunkManager.CURRENT_PLOT_CLEAR != null) {
PlotLoc loc;
for (Entry<PlotLoc, HashMap<Short, Byte>> entry : ChunkManager.GENERATE_DATA.entrySet()) {
HashMap<Short, Byte> datas = ChunkManager.GENERATE_DATA.get(entry.getKey());
for (Entry<Short, Byte> entry2 : entry.getValue().entrySet()) {
Short y = entry2.getKey();
byte data;
if (datas != null) {
data = datas.get(y);
}
else {
data = 0;
}
loc = entry.getKey();
int xx = loc.x - X;
int zz = loc.z - Z;
if (xx >= 0 && xx < 16) {
if (zz >= 0 && zz < 16) {
BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, entry2.getValue(), data);
}
}
}
}
}
return;
}
final boolean check;
check = !inX1 || !inX2 || !inZ1 || !inZ2;
if (this.plotworld.TERRAIN > 1) {
final PlotId plot1 = this.manager.getPlotIdAbs(this.plotworld, bx, 0, bz);
final PlotId plot2 = this.manager.getPlotIdAbs(this.plotworld, tx, 0, tz);
if ((plot1 != null) && (plot2 != null) && plot1.equals(plot2)) {
return;
}
}
if (this.o) {
populateBlocks(world, rand, cx, cz, bx, bz, check);
} else {
TaskManager.runTaskLater(new Runnable() {
@Override
public void run() {
populateBiome(world, bx, bz);
}
}, 20 + rand.nextInt(10));
TaskManager.runTaskLater(new Runnable() {
@Override
public void run() {
chunk.load(true);
populateBlocks(world, rand, cx, cz, bx, bz, check);
}
}, 40 + rand.nextInt(40));
}
}
private void populateBiome(final World world, final int x, final int z) {
final Biome biome = Biome.valueOf(this.plotworld.PLOT_BIOME);
if (this.b) {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
world.setBiome(x + i, z + j, biome);
}
}
}
}
private void populateBlocks(final World world, final Random rand, final int X, final int Z, final int x, final int z, final boolean check) {
final short[][] result = this.generator.generateExtBlockSections(world, rand, X, Z, null);
for (int i = 0; i < result.length; i++) {
if (result[i] != null) {
for (int j = 0; j < 4096; j++) {
int x1 = x_loc[i][j];
int y = y_loc[i][j];
int z1 = z_loc[i][j];
short id = result[i][j];
final int xx = x + x1;
final int zz = z + z1;
if (check && (((zz) < this.bz) || ((zz) > this.tz) || ((xx) < this.bx) || ((xx) > this.tx))) {
continue;
}
if (this.p) {
if (ChunkManager.CURRENT_PLOT_CLEAR != null) {
if (BukkitChunkManager.isIn(ChunkManager.CURRENT_PLOT_CLEAR, xx, zz)) {
continue;
}
} else if (this.manager.getPlotIdAbs(this.plotworld, xx, 0, zz) != null) {
continue;
}
}
BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, id, (byte) 0);
}
}
else {
short y_min = y_loc[i][0];
if (y_min < 128) {
for (int x1 = x; x1 < x + 16; x1++) {
for (int z1 = z; z1 < z + 16; z1++) {
if (check && (((z1) < this.bz) || ((z1) > this.tz) || ((x1) < this.bx) || ((x1) > this.tx))) {
continue;
}
if (this.p) {
if (ChunkManager.CURRENT_PLOT_CLEAR != null) {
if (BukkitChunkManager.isIn(ChunkManager.CURRENT_PLOT_CLEAR, x1, z1)) {
continue;
}
} else if (this.manager.getPlotIdAbs(this.plotworld, x1, 0, z1) != null) {
continue;
}
}
for (int y = y_min; y < y_min + 16; y++) {
BukkitSetBlockManager.setBlockManager.set(world, x1, y, z1, 0, (byte) 0);
}
}
}
}
}
}
for (final BlockPopulator populator : this.generator.getPopulators(world.getName())) {
Chunk chunk = world.getChunkAt(X, Z);
populator.populate(world, this.r, chunk);
}
}
public boolean isIn(final RegionWrapper plot, final int x, final int z) {
return ((x >= plot.minX) && (x <= plot.maxX) && (z >= plot.minZ) && (z <= plot.maxZ));
}
}

View File

@ -0,0 +1,297 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.plotsquared.bukkit.generator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import com.intellectualcrafters.plot.generator.HybridPlotManager;
import com.intellectualcrafters.plot.generator.HybridPlotWorld;
import com.intellectualcrafters.plot.generator.HybridPop;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Biome;
import com.plotsquared.bukkit.object.PlotGenerator;
import com.intellectualcrafters.plot.object.PlotLoc;
import com.intellectualcrafters.plot.object.PlotManager;
import com.plotsquared.bukkit.object.PlotPopulator;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.PseudoRandom;
import com.intellectualcrafters.plot.object.RegionWrapper;
/**
* The default generator is very messy, as we have decided to try externalize all calculations from within the loop. -
* You will see a lot of slower implementations have a single for loop. - This is perfectly fine to do, it will just
* mean world generation may take somewhat longer
*
* @author Citymonstret
* @author Empire92
*/
public class HybridGen extends PlotGenerator {
public HybridGen(String world) {
super(world);
}
/**
* Set to static to re-use the same managet for all Default World Generators
*/
private static PlotManager manager = null;
/**
* plotworld object
*/
public HybridPlotWorld plotworld = null;
/**
* Some generator specific variables (implementation dependent)
*/
int plotsize;
int pathsize;
short wall;
short wallfilling;
short roadblock;
int size;
Biome biome;
int roadheight;
int wallheight;
int plotheight;
short[] plotfloors;
short[] filling;
short pathWidthLower;
short pathWidthUpper;
boolean doState = false;
int maxY = 0;
short[][] cached;
/**
* Initialize variables, and create plotworld object used in calculations
*/
public void init(PlotWorld plotworld) {
if (plotworld != null) {
this.plotworld = (HybridPlotWorld) plotworld;
}
this.plotsize = this.plotworld.PLOT_WIDTH;
this.pathsize = this.plotworld.ROAD_WIDTH;
this.roadblock = this.plotworld.ROAD_BLOCK.id;
this.wallfilling = this.plotworld.WALL_FILLING.id;
this.size = this.pathsize + this.plotsize;
this.wall = this.plotworld.WALL_BLOCK.id;
this.plotfloors = new short[this.plotworld.TOP_BLOCK.length];
for (int i = 0; i < this.plotworld.TOP_BLOCK.length; i++) {
this.plotfloors[i] = this.plotworld.TOP_BLOCK[i].id;
}
this.filling = new short[this.plotworld.MAIN_BLOCK.length];
for (int i = 0; i < this.plotworld.MAIN_BLOCK.length; i++) {
this.filling[i] = this.plotworld.MAIN_BLOCK[i].id;
}
if ((this.filling.length > 1) || (this.plotfloors.length > 1)) {
this.doState = true;
}
this.wallheight = this.plotworld.WALL_HEIGHT;
this.roadheight = this.plotworld.ROAD_HEIGHT;
this.plotheight = this.plotworld.PLOT_HEIGHT;
if (this.pathsize == 0) {
this.pathWidthLower = (short) -1;
this.pathWidthUpper = (short) (this.plotsize + 1);
}
else {
if ((this.pathsize % 2) == 0) {
this.pathWidthLower = (short) (Math.floor(this.pathsize / 2) - 1);
} else {
this.pathWidthLower = (short) (Math.floor(this.pathsize / 2));
}
this.pathWidthUpper = (short) (this.pathWidthLower + this.plotsize + 1);
}
this.biome = Biome.valueOf(this.plotworld.PLOT_BIOME);
try {
this.maxY = Bukkit.getWorld(plotworld.worldname).getMaxHeight();
} catch (final NullPointerException e) {}
if (this.maxY == 0) {
this.maxY = 256;
}
// create cached chunk (for optimized chunk generation)
if (!this.plotworld.PLOT_SCHEMATIC) {
this.cached = new short[(plotheight + 16) / 16][];
for (int i = 0; i < cached.length; i++) {
cached[i] = new short[4096];
}
PseudoRandom random = new PseudoRandom();
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
cached[CACHE_I[plotheight][x][z]][CACHE_J[plotheight][x][z]] = plotfloors[random.random(plotfloors.length)];
if (this.plotworld.PLOT_BEDROCK) {
cached[CACHE_I[0][x][z]][CACHE_J[0][x][z]] = 7;
}
for (int y = 1; y < plotheight; y++) {
cached[CACHE_I[y][x][z]][CACHE_J[y][x][z]] = filling[random.random(filling.length)];
}
}
}
}
}
/**
* Return the plot manager for this type of generator, or create one For square plots you may as well use the
* default plot manager which comes with PlotSquared
*/
public PlotManager getPlotManager() {
if (HybridGen.manager == null) {
HybridGen.manager = new HybridPlotManager();
}
return HybridGen.manager;
}
/**
* Get a new plotworld class For square plots you can use the DefaultPlotWorld class which comes with PlotSquared
*/
public PlotWorld getNewPlotWorld(final String world) {
if (this.plotworld == null) {
this.plotworld = new HybridPlotWorld(world);
}
return this.plotworld;
}
/**
* Return the block populator
*/
public List<PlotPopulator> getPopulators(final String world) {
// You can have as many populators as you would like, e.g. tree
// populator, ore populator
return Arrays.asList((PlotPopulator) new HybridPop(this.plotworld));
}
/**
* This part is a fucking mess. - Refer to a proper tutorial if you would like to learn how to make a world
* generator
*/
public void generateChunk(final World world, RegionWrapper region, final PseudoRandom random, final int cx, final int cz, final BiomeGrid biomes) {
int sx = (short) ((this.X - this.plotworld.ROAD_OFFSET_X) % this.size);
int sz = (short) ((this.Z - this.plotworld.ROAD_OFFSET_Z) % this.size);
if (sx < 0) {
sx += this.size;
}
if (sz < 0) {
sz += this.size;
}
if (biomes != null) {
for (short x = 0; x < 16; x++) {
for (short z = 0; z < 16; z++) {
biomes.setBiome(x, z, this.biome);
}
}
}
if (cached != null) {
if (sx > pathWidthLower && sz > pathWidthLower && sx + 15 < pathWidthUpper&& sz + 15 < pathWidthUpper) {
setResult(cached);
return;
}
}
if (this.plotworld.PLOT_BEDROCK) {
for (short x = 0; x < 16; x++) {
for (short z = 0; z < 16; z++) {
setBlock(x, 0, z, (short) 7);
}
}
}
if (region != null) {
for (short x = 0; x < 16; x++) {
final int absX = ((sx + x) % this.size);
for (short z = 0; z < 16; z++) {
if (contains(region, x, z)) {
for (short y = 1; y < this.plotheight; y++) {
setBlock(x, y, z, this.filling);
}
setBlock(x, this.plotheight, z, this.plotfloors);
final int absZ = ((sz + z) % this.size);
final PlotLoc loc = new PlotLoc(absX, absZ);
final HashMap<Short, Short> blocks = plotworld.G_SCH.get(loc);
if (blocks != null) {
for (final Entry<Short, Short> entry : blocks.entrySet()) {
setBlock(x, this.plotheight + entry.getKey(), z, entry.getValue());
}
}
}
}
}
return;
}
for (short x = 0; x < 16; x++) {
final int absX = ((sx + x) % this.size);
final boolean gx = absX > this.pathWidthLower;
final boolean lx = absX < this.pathWidthUpper;
for (short z = 0; z < 16; z++) {
final int absZ = ((sz + z) % this.size);
final boolean gz = absZ > this.pathWidthLower;
final boolean lz = absZ < this.pathWidthUpper;
// inside plot
if (gx && gz && lx && lz) {
for (short y = 1; y < this.plotheight; y++) {
setBlock(x, y, z, this.filling);
}
setBlock(x, this.plotheight, z, this.plotfloors);
if (this.plotworld.PLOT_SCHEMATIC) {
final PlotLoc loc = new PlotLoc(absX, absZ);
final HashMap<Short, Short> blocks = this.plotworld.G_SCH.get(loc);
if (blocks != null) {
for (final Entry<Short, Short> entry : blocks.entrySet()) {
setBlock(x, this.plotheight + entry.getKey(), z, entry.getValue());
}
}
}
} else if (pathsize != 0) {
// wall
if (((absX >= this.pathWidthLower) && (absX <= this.pathWidthUpper) && (absZ >= this.pathWidthLower) && (absZ <= this.pathWidthUpper))) {
for (short y = 1; y <= this.wallheight; y++) {
setBlock(x, y, z, this.wallfilling);
}
if (!this.plotworld.ROAD_SCHEMATIC_ENABLED) {
setBlock(x, this.wallheight + 1, z, this.wall);
}
}
// road
else {
for (short y = 1; y <= this.roadheight; y++) {
setBlock(x, y, z, this.roadblock);
}
}
if (this.plotworld.ROAD_SCHEMATIC_ENABLED) {
final PlotLoc loc = new PlotLoc(absX, absZ);
final HashMap<Short, Short> blocks = this.plotworld.G_SCH.get(loc);
if (blocks != null) {
for (final Entry<Short, Short> entry : blocks.entrySet()) {
setBlock(x, this.roadheight + entry.getKey(), z, entry.getValue());
}
}
}
}
}
}
return;
}
}