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

101 lines
2.8 KiB
Java
Raw Normal View History

2015-01-26 05:16:10 +01:00
package com.intellectualcrafters.plot.util;
import java.util.HashMap;
import java.util.HashSet;
2015-01-27 22:51:56 +01:00
import org.bukkit.Location;
import com.intellectualcrafters.plot.PlotMain;
2015-01-26 05:16:10 +01:00
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.intellectualcrafters.plot.object.PlotClusterId;
import com.intellectualcrafters.plot.object.PlotId;
2015-01-27 22:51:56 +01:00
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotWorld;
2015-01-26 05:16:10 +01:00
public class ClusterManager {
public static HashMap<String, HashSet<PlotCluster>> clusters;
private static PlotCluster last;
public static boolean contains(PlotCluster cluster, PlotId id) {
2015-01-26 06:05:56 +01:00
if (cluster.getP1().x <= id.x && cluster.getP1().y <= id.y && cluster.getP2().x >= id.x && cluster.getP2().y >= id.y) {
2015-01-26 05:16:10 +01:00
return true;
}
return false;
}
2015-01-27 22:51:56 +01:00
public static boolean contains(PlotCluster cluster, Location loc) {
String world = loc.getWorld().getName();
PlotManager manager = PlotMain.getPlotManager(world);
PlotWorld plotworld = PlotMain.getWorldSettings(world);
Location bot = manager.getPlotBottomLocAbs(plotworld, cluster.getP1());
Location top = manager.getPlotTopLocAbs(plotworld, cluster.getP2()).add(1,0,1);
if (bot.getBlockX() < loc.getBlockX() && bot.getBlockZ() < loc.getBlockZ() && top.getBlockX() > loc.getBlockX() && top.getBlockZ() > loc.getBlockZ()) {
return true;
}
return false;
}
2015-01-26 05:16:10 +01:00
public static PlotCluster getCluster(Plot plot) {
2015-01-27 22:51:56 +01:00
return getCluster(plot.world, plot.id);
}
public static PlotCluster getCluster(Location loc) {
String world = loc.getWorld().getName();
if (last != null && last.world.equals(world)) {
if (contains(last, loc)) {
return last;
}
}
if (clusters == null) {
return null;
}
HashSet<PlotCluster> local = clusters.get(world);
if (local == null) {
return null;
}
for (PlotCluster cluster : local) {
if (contains(cluster, loc)) {
last = cluster;
return cluster;
}
}
return null;
}
public static PlotCluster getCluster(String world, PlotId id) {
if (last != null && last.world.equals(world)) {
if (contains(last, id)) {
2015-01-26 05:16:10 +01:00
return last;
}
}
if (clusters == null) {
return null;
}
2015-01-27 22:51:56 +01:00
HashSet<PlotCluster> local = clusters.get(world);
2015-01-26 05:16:10 +01:00
if (local == null) {
return null;
}
for (PlotCluster cluster : local) {
if (contains(cluster, id)) {
last = cluster;
return cluster;
}
}
return null;
}
public static boolean removeCluster(PlotCluster cluster) {
if (clusters != null) {
if (clusters.containsKey(cluster.world)) {
clusters.get(cluster.world).remove(cluster);
return true;
}
}
return false;
}
public static PlotClusterId getClusterId(PlotCluster cluster) {
2015-01-26 06:05:56 +01:00
return new PlotClusterId(cluster.getP1(), cluster.getP2());
2015-01-26 05:16:10 +01:00
}
}