PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java

283 lines
10 KiB
Java
Raw Normal View History

2015-01-26 05:16:10 +01:00
package com.intellectualcrafters.plot.util;
2015-07-27 19:50:04 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.*;
import com.plotsquared.bukkit.generator.AugmentedPopulator;
import com.plotsquared.bukkit.util.bukkit.BukkitUtil;
2015-07-03 11:30:26 +02:00
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
2015-07-27 19:50:04 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
2015-01-26 05:16:10 +01:00
public class ClusterManager {
2015-02-20 07:34:19 +01:00
public static HashMap<String, HashSet<PlotCluster>> clusters;
public static PlotCluster last;
2015-07-03 11:30:26 +02:00
private static HashSet<String> regenerating = new HashSet<>();
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static boolean contains(final PlotCluster cluster, final PlotId id) {
2015-07-03 11:30:26 +02:00
return (cluster.getP1().x <= id.x) && (cluster.getP1().y <= id.y) && (cluster.getP2().x >= id.x) && (cluster.getP2().y >= id.y);
2015-02-20 07:34:19 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static HashSet<PlotCluster> getClusters(final World world) {
return getClusters(world.getName());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static HashSet<PlotCluster> getClusters(final String world) {
if (clusters.containsKey(world)) {
return clusters.get(world);
}
return new HashSet<>();
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static Location getHome(final PlotCluster cluster) {
final BlockLoc home = cluster.settings.getPosition();
Location toReturn;
if (home.y == 0) {
// default pos
final PlotId center = getCenterPlot(cluster);
2015-02-20 12:23:48 +01:00
toReturn = MainUtil.getPlotHome(cluster.world, center);
2015-02-20 07:34:19 +01:00
if (toReturn.getY() == 0) {
final PlotManager manager = PS.get().getPlotManager(cluster.world);
final PlotWorld plotworld = PS.get().getPlotWorld(cluster.world);
2015-02-20 12:23:48 +01:00
final Location loc = manager.getSignLoc(plotworld, MainUtil.getPlot(cluster.world, center));
2015-02-20 07:34:19 +01:00
toReturn.setY(loc.getY());
}
} else {
toReturn = getClusterBottom(cluster).add(home.x, home.y, home.z);
}
2015-07-28 08:06:19 +02:00
final int max = MainUtil.getHeighestBlock(cluster.world, toReturn.getX(), toReturn.getZ());
2015-02-20 07:34:19 +01:00
if (max > toReturn.getY()) {
toReturn.setY(max);
}
return toReturn;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotId getCenterPlot(final PlotCluster cluster) {
final PlotId bot = cluster.getP1();
final PlotId top = cluster.getP2();
return new PlotId((bot.x + top.x) / 2, (bot.y + top.y) / 2);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static Location getClusterBottom(final PlotCluster cluster) {
final String world = cluster.world;
final PlotWorld plotworld = PS.get().getPlotWorld(world);
final PlotManager manager = PS.get().getPlotManager(world);
return manager.getPlotBottomLocAbs(plotworld, cluster.getP1());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static Location getClusterTop(final PlotCluster cluster) {
final String world = cluster.world;
final PlotWorld plotworld = PS.get().getPlotWorld(world);
final PlotManager manager = PS.get().getPlotManager(world);
return manager.getPlotTopLocAbs(plotworld, cluster.getP2());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotCluster getCluster(final String world, final String name) {
if (!clusters.containsKey(world)) {
return null;
}
for (final PlotCluster cluster : clusters.get(world)) {
if (cluster.getName().equals(name)) {
return cluster;
}
}
return null;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static boolean contains(final PlotCluster cluster, final Location loc) {
final String world = loc.getWorld();
final PlotManager manager = PS.get().getPlotManager(world);
final PlotWorld plotworld = PS.get().getPlotWorld(world);
2015-02-20 07:34:19 +01:00
final Location bot = manager.getPlotBottomLocAbs(plotworld, cluster.getP1());
final Location top = manager.getPlotTopLocAbs(plotworld, cluster.getP2()).add(1, 0, 1);
2015-07-03 11:30:26 +02:00
return (bot.getX() < loc.getX()) && (bot.getZ() < loc.getZ()) && (top.getX() > loc.getX()) && (top.getZ() > loc.getZ());
2015-02-20 07:34:19 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static HashSet<PlotCluster> getIntersects(final String world, final PlotClusterId id) {
if (!clusters.containsKey(world)) {
return new HashSet<>();
}
final HashSet<PlotCluster> list = new HashSet<PlotCluster>();
for (final PlotCluster cluster : clusters.get(world)) {
if (intersects(cluster, id)) {
list.add(cluster);
}
}
return list;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static boolean intersects(final PlotCluster cluster, final PlotClusterId id) {
final PlotId pos1 = cluster.getP1();
final PlotId pos2 = cluster.getP2();
2015-07-03 11:30:26 +02:00
return (pos1.x <= id.pos2.x) && (pos2.x >= id.pos1.x) && (pos1.y <= id.pos2.y) && (pos2.y >= id.pos1.y);
2015-01-29 04:20:29 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotCluster getCluster(final Plot plot) {
return getCluster(plot.world, plot.id);
2015-01-29 04:20:29 +01:00
}
2015-04-26 08:29:58 +02:00
public static PlotCluster getClusterAbs(final Location loc) {
String world = loc.getWorld();
2015-02-20 07:34:19 +01:00
if ((last != null) && last.world.equals(world)) {
if (contains(last, loc)) {
return last;
}
}
if (clusters == null) {
return null;
}
final HashSet<PlotCluster> local = clusters.get(world);
if (local == null) {
return null;
}
for (final PlotCluster cluster : local) {
if (contains(cluster, loc)) {
last = cluster;
return cluster;
}
}
return null;
}
2015-04-26 08:29:58 +02:00
public static PlotCluster getCluster(final Location loc) {
final String world = loc.getWorld();
PlotManager manager = PS.get().getPlotManager(world);
2015-07-27 19:28:39 +02:00
if (manager == null) {
return null;
}
PlotId id = manager.getPlotIdAbs(PS.get().getPlotWorld(world), loc.getX(), loc.getY(), loc.getZ());
2015-04-26 08:29:58 +02:00
if (id != null) {
return getCluster(world, id);
}
return getClusterAbs(loc);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotCluster getCluster(final String world, final PlotId id) {
if ((last != null) && last.world.equals(world)) {
if (contains(last, id)) {
return last;
}
}
if (clusters == null) {
return null;
}
final HashSet<PlotCluster> local = clusters.get(world);
if (local == null) {
return null;
}
for (final PlotCluster cluster : local) {
if (contains(cluster, id)) {
last = cluster;
return cluster;
}
}
return null;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static boolean removeCluster(final PlotCluster cluster) {
if (clusters != null) {
if (clusters.containsKey(cluster.world)) {
clusters.get(cluster.world).remove(cluster);
return true;
}
}
return false;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotClusterId getClusterId(final PlotCluster cluster) {
return new PlotClusterId(cluster.getP1(), cluster.getP2());
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static AugmentedPopulator getPopulator(final PlotCluster cluster) {
final World world = Bukkit.getWorld(cluster.world);
for (final BlockPopulator populator : world.getPopulators()) {
2015-01-31 08:09:48 +01:00
if (populator instanceof AugmentedPopulator) {
if (((AugmentedPopulator) populator).cluster.equals(cluster)) {
return (AugmentedPopulator) populator;
}
}
}
2015-02-20 07:34:19 +01:00
return null;
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static PlotId estimatePlotId(final Location loc) {
final PlotId a = new PlotId(0, 0);
final PlotId b = new PlotId(1, 1);
int xw;
int zw;
final String world = loc.getWorld();
final PlotWorld plotworld = PS.get().getPlotWorld(world);
2015-02-20 07:34:19 +01:00
if (plotworld == null) {
xw = 39;
zw = 39;
} else {
final PlotManager manager = PS.get().getPlotManager(world);
2015-02-20 07:34:19 +01:00
final Location al = manager.getPlotBottomLocAbs(plotworld, a);
final Location bl = manager.getPlotBottomLocAbs(plotworld, b);
xw = bl.getX() - al.getX();
zw = bl.getZ() - al.getZ();
}
final int x = loc.getX();
final int z = loc.getZ();
return new PlotId((x / xw) + 1, (z / zw) + 1);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static void regenCluster(final PlotCluster cluster) {
if (regenerating.contains(cluster.world + ":" + cluster.getName())) {
return;
}
regenerating.add(cluster.world + ":" + cluster.getName());
final int interval = 1;
int i = 0;
final Random rand = new Random();
2015-01-31 08:09:48 +01:00
final World world = Bukkit.getWorld(cluster.world);
final PlotWorld plotworld = PS.get().getPlotWorld(cluster.world);
2015-02-20 07:34:19 +01:00
final Location bot = getClusterBottom(cluster);
final Location top = getClusterTop(cluster);
final int minChunkX = bot.getX() >> 4;
final int maxChunkX = (top.getX() >> 4) + 1;
final int minChunkZ = bot.getZ() >> 4;
final int maxChunkZ = (top.getZ() >> 4) + 1;
final AugmentedPopulator populator = getPopulator(cluster);
final ArrayList<Chunk> chunks = new ArrayList<>();
TaskManager.runTaskLater(new Runnable() {
2015-01-31 08:09:48 +01:00
@Override
public void run() {
ClusterManager.regenerating.remove(cluster.world + ":" + cluster.getName());
2015-02-21 13:52:50 +01:00
final PlotPlayer owner = UUIDHandler.getPlayer(cluster.owner);
2015-01-31 08:09:48 +01:00
if (owner != null) {
2015-02-21 13:52:50 +01:00
MainUtil.sendMessage(owner, C.CLEARING_DONE);
2015-01-31 08:09:48 +01:00
}
}
2015-02-20 07:34:19 +01:00
}, (interval * chunks.size()) + 20);
// chunks
for (int x = minChunkX; x <= maxChunkX; x++) {
2015-01-31 08:09:48 +01:00
for (int z = minChunkZ; z <= maxChunkZ; z++) {
final Chunk chunk = world.getChunkAt(x, z);
chunks.add(chunk);
}
}
2015-02-20 07:34:19 +01:00
for (final Chunk chunk : chunks) {
i += interval;
TaskManager.runTaskLater(new Runnable() {
2015-01-31 08:09:48 +01:00
@Override
public void run() {
2015-02-20 07:34:19 +01:00
if ((populator == null) || (plotworld.TYPE == 0)) {
ChunkLoc loc = new ChunkLoc(chunk.getX(), chunk.getZ());
ChunkManager.manager.regenerateChunk(world.getName(), loc);
MainUtil.update(world.getName(), loc);
2015-02-20 07:34:19 +01:00
} else {
2015-01-31 08:09:48 +01:00
populator.populate(world, rand, chunk);
}
}
}, i);
2015-02-20 07:34:19 +01:00
}
}
}