PlotSquared/src/main/java/com/plotsquared/bukkit/util/BukkitHybridUtils.java

411 lines
19 KiB
Java
Raw Normal View History

2015-07-28 08:06:19 +02:00
package com.plotsquared.bukkit.util;
2015-02-21 04:43:08 +01:00
2015-07-30 16:25:16 +02:00
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
2015-07-28 08:06:19 +02:00
2015-02-21 04:43:08 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Material;
2015-02-21 04:43:08 +01:00
import org.bukkit.World;
import org.bukkit.block.Biome;
2015-02-21 04:43:08 +01:00
import org.bukkit.block.Block;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
2015-06-26 11:46:06 +02:00
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
2015-02-21 04:43:08 +01:00
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotAnalysis;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.ChunkManager;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.TaskManager;
2015-02-21 04:43:08 +01:00
2015-09-11 12:09:22 +02:00
public class BukkitHybridUtils extends HybridUtils
{
2015-07-03 11:30:26 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void analyzePlot(final Plot plot, final RunnableVal<PlotAnalysis> whenDone)
{
// int diff, int variety, int verticies, int rotation, int height_sd
/*
* diff: compare to base by looping through all blocks
* variety: add to hashset for each plotblock
* height_sd: loop over all blocks and get top block
2015-07-03 11:30:26 +02:00
*
* verticies: store air map and compare with neighbours
* for each block check the adjacent
* - Store all blocks then go through in second loop
* - recheck each block
2015-07-03 11:30:26 +02:00
*
*/
2015-09-11 12:09:22 +02:00
TaskManager.runTaskAsync(new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
final World world = Bukkit.getWorld(plot.world);
final ChunkGenerator gen = world.getGenerator();
if (gen == null) {
2015-09-11 12:09:22 +02:00
return;
}
2015-09-11 12:09:22 +02:00
final BiomeGrid base = new BiomeGrid()
{
@Override
public void setBiome(final int a, final int b, final Biome c)
{}
@Override
public Biome getBiome(final int a, final int b)
{
return null;
}
};
final Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1);
final Location top = MainUtil.getPlotTopLoc(plot.world, plot.id);
final int bx = bot.getX();
final int bz = bot.getZ();
final int tx = top.getX();
final int tz = top.getZ();
final int cbx = bx >> 4;
final int cbz = bz >> 4;
final int ctx = tx >> 4;
final int ctz = tz >> 4;
final Random r = new Random();
MainUtil.initCache();
2015-09-11 12:09:22 +02:00
final int width = (tx - bx) + 1;
final int length = (tz - bz) + 1;
System.gc();
System.gc();
final short[][][] oldblocks = new short[256][width][length];
final short[][][] newblocks = new short[256][width][length];
2015-09-11 12:09:22 +02:00
final Runnable run = new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
ChunkManager.chunkTask(bot, top, new RunnableVal<int[]>()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
// TODO [chunkx, chunkz, pos1x, pos1z, pos2x, pos2z, isedge]
2015-09-11 12:09:22 +02:00
final int X = value[0];
final int Z = value[1];
final short[][] result = gen.generateExtBlockSections(world, r, X, Z, base);
final int xb = ((X) << 4) - bx;
final int zb = ((Z) << 4) - bz;
for (int i = 0; i < result.length; i++)
{
if (result[i] == null)
{
for (int j = 0; j < 4096; j++)
{
final int x = MainUtil.x_loc[i][j] + xb;
if ((x < 0) || (x >= width))
{
continue;
}
final int z = MainUtil.z_loc[i][j] + zb;
if ((z < 0) || (z >= length))
{
continue;
}
final int y = MainUtil.y_loc[i][j];
oldblocks[y][x][z] = 0;
}
continue;
}
2015-09-11 12:09:22 +02:00
for (int j = 0; j < result[i].length; j++)
{
final int x = MainUtil.x_loc[i][j] + xb;
if ((x < 0) || (x >= width))
{
continue;
}
final int z = MainUtil.z_loc[i][j] + zb;
if ((z < 0) || (z >= length))
{
continue;
}
final int y = MainUtil.y_loc[i][j];
oldblocks[y][x][z] = result[i][j];
}
}
2015-09-11 12:09:22 +02:00
}
2015-09-11 12:09:22 +02:00
}, new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
TaskManager.runTaskAsync(new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
final int size = width * length;
final int[] changes = new int[size];
final int[] faces = new int[size];
final int[] data = new int[size];
final int[] air = new int[size];
final int[] variety = new int[size];
int i = 0;
2015-09-11 12:09:22 +02:00
for (int x = 0; x < width; x++)
{
for (int z = 0; z < length; z++)
{
final HashSet<Short> types = new HashSet<>();
for (int y = 0; y < 256; y++)
{
final short old = oldblocks[y][x][z];
final short now = newblocks[y][x][z];
if (old != now)
{
changes[i]++;
}
2015-09-11 12:09:22 +02:00
if (now == 0)
{
air[i]++;
}
2015-09-11 12:09:22 +02:00
else
{
// check verticies
// modifications_adjacent
2015-09-11 12:09:22 +02:00
if ((x > 0) && (z > 0) && (y > 0) && (x < (width - 1)) && (z < (length - 1)) && (y < 255))
{
if (newblocks[y - 1][x][z] == 0)
{
faces[i]++;
}
if (newblocks[y][x - 1][z] == 0)
{
faces[i]++;
}
if (newblocks[y][x][z - 1] == 0)
{
faces[i]++;
}
if (newblocks[y + 1][x][z] == 0)
{
faces[i]++;
}
if (newblocks[y][x + 1][z] == 0)
{
faces[i]++;
}
if (newblocks[y][x][z + 1] == 0)
{
faces[i]++;
}
}
2015-09-11 12:09:22 +02:00
final Material material = Material.getMaterial(now);
final Class<? extends MaterialData> md = material.getData();
if (md.equals(Directional.class))
{
data[i] += 8;
}
2015-09-11 12:09:22 +02:00
else if (!md.equals(MaterialData.class))
{
data[i]++;
}
types.add(now);
}
}
variety[i] = types.size();
i++;
}
}
// analyze plot
// put in analysis obj
2015-09-11 12:09:22 +02:00
// run whenDone
2015-09-11 12:09:22 +02:00
final PlotAnalysis analysis = new PlotAnalysis();
analysis.changes = (int) (MathMan.getMean(changes) * 100);
analysis.faces = (int) (MathMan.getMean(faces) * 100);
analysis.data = (int) (MathMan.getMean(data) * 100);
analysis.air = (int) (MathMan.getMean(air) * 100);
analysis.variety = (int) (MathMan.getMean(variety) * 100);
2015-09-11 12:09:22 +02:00
analysis.changes_sd = (int) (MathMan.getSD(changes, analysis.changes));
analysis.faces_sd = (int) (MathMan.getSD(faces, analysis.faces));
analysis.data_sd = (int) (MathMan.getSD(data, analysis.data));
analysis.air_sd = (int) (MathMan.getSD(air, analysis.air));
analysis.variety_sd = (int) (MathMan.getSD(variety, analysis.variety));
2015-09-11 12:09:22 +02:00
final List<Integer> result = new ArrayList<>();
result.add(analysis.changes);
result.add(analysis.faces);
result.add(analysis.data);
result.add(analysis.air);
result.add(analysis.variety);
2015-09-11 12:09:22 +02:00
result.add(analysis.changes_sd);
result.add(analysis.faces_sd);
result.add(analysis.data_sd);
result.add(analysis.air_sd);
result.add(analysis.variety_sd);
2015-09-11 12:09:22 +02:00
final Flag flag = new Flag(FlagManager.getFlag("analysis"), result);
FlagManager.addPlotFlag(plot, flag);
System.gc();
System.gc();
whenDone.value = analysis;
whenDone.run();
}
});
}
}, 5);
2015-09-11 12:09:22 +02:00
}
};
System.gc();
MainUtil.initCache();
2015-09-11 12:09:22 +02:00
ChunkManager.chunkTask(bot, top, new RunnableVal<int[]>()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
final int X = value[0];
final int Z = value[1];
world.loadChunk(X, Z);
int minX;
int minZ;
int maxX;
int maxZ;
2015-09-11 12:09:22 +02:00
if (X == cbx)
{
minX = bx & 15;
}
else
{
minX = 0;
}
if (Z == cbz)
{
minZ = bz & 15;
}
else
{
minZ = 0;
}
if (X == ctx)
{
maxX = tx & 15;
}
else
{
maxX = 16;
}
if (Z == ctz)
{
maxZ = tz & 15;
}
else
{
maxZ = 16;
}
final int cbx = X << 4;
final int cbz = Z << 4;
final int xb = (cbx) - bx;
final int zb = (cbz) - bz;
for (int x = minX; x <= maxX; x++)
{
final int xx = cbx + x;
for (int z = minZ; z <= maxZ; z++)
{
final int zz = cbz + z;
for (int y = 0; y < 256; y++)
{
final Block block = world.getBlockAt(xx, y, zz);
final int xr = xb + x;
final int zr = zb + z;
newblocks[y][xr][zr] = (short) block.getTypeId();
}
}
}
world.unloadChunkRequest(X, Z, true);
}
2015-09-11 12:09:22 +02:00
}, new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
TaskManager.runTaskAsync(run);
2015-06-26 11:46:06 +02:00
}
}, 5);
2015-07-03 11:30:26 +02:00
}
});
}
2015-07-03 11:30:26 +02:00
2015-02-21 04:43:08 +01:00
@Override
2015-09-11 12:09:22 +02:00
public int checkModified(final String worldname, final int x1, final int x2, final int y1, final int y2, final int z1, final int z2, final PlotBlock[] blocks)
{
2015-02-23 02:32:27 +01:00
final World world = BukkitUtil.getWorld(worldname);
2015-02-21 04:43:08 +01:00
int count = 0;
2015-09-11 12:09:22 +02:00
for (int y = y1; y <= y2; y++)
{
for (int x = x1; x <= x2; x++)
{
for (int z = z1; z <= z2; z++)
{
2015-02-21 04:43:08 +01:00
final Block block = world.getBlockAt(x, y, z);
final int id = block.getTypeId();
boolean same = false;
2015-09-11 12:09:22 +02:00
for (final PlotBlock p : blocks)
{
if (id == p.id)
{
2015-02-21 04:43:08 +01:00
same = true;
break;
}
}
2015-09-11 12:09:22 +02:00
if (!same)
{
2015-02-21 04:43:08 +01:00
count++;
}
}
}
}
return count;
}
2015-07-03 11:30:26 +02:00
2015-02-21 04:43:08 +01:00
@Override
2015-09-11 12:09:22 +02:00
public int get_ey(final String worldname, final int sx, final int ex, final int sz, final int ez, final int sy)
{
2015-02-23 02:32:27 +01:00
final World world = BukkitUtil.getWorld(worldname);
final int maxY = world.getMaxHeight();
2015-02-21 04:43:08 +01:00
int ey = sy;
2015-09-11 12:09:22 +02:00
for (int x = sx; x <= ex; x++)
{
for (int z = sz; z <= ez; z++)
{
for (int y = sy; y < maxY; y++)
{
if (y > ey)
{
2015-02-21 04:43:08 +01:00
final Block block = world.getBlockAt(x, y, z);
2015-09-11 12:09:22 +02:00
if (block.getTypeId() != 0)
{
2015-02-21 04:43:08 +01:00
ey = y;
}
}
}
}
}
return ey;
}
}