Finished adding cluster commands. TODO: testing

This commit is contained in:
boy0001 2015-01-29 17:02:13 +11:00
parent 61898b0157
commit cafaa31228
9 changed files with 319 additions and 19 deletions

View File

@ -168,17 +168,31 @@ public class Auto extends SubCommand {
return sendMessage(plr, C.NOT_IN_PLOT);
}
PlotCluster cluster = ClusterManager.getCluster(loc);
// Must be standing in a cluster
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
PlotId bot = cluster.getP1();
PlotId top = cluster.getP2();
PlotId id = new PlotId((bot.x + top.x) / 2, (bot.y + top.y) / 2);
PlotId origin = new PlotId((bot.x + top.x) / 2, (bot.y + top.y) / 2);
PlotId id = new PlotId(0, 0);
int width = Math.max(top.x - bot.x, top.y - bot.y);
int max = width * width;
// TODO finish cluster auto claiming
//
for (int i = 0; i <= max; i++) {
PlotId currentId = new PlotId(origin.x + id.x, origin.y + id.y);
Plot current = PlotHelper.getPlot(world, currentId);
if (current != null && !current.hasOwner() && cluster.equals(ClusterManager.getCluster(current))) {
Claim.claimPlot(plr, plot, true, true);
return true;
}
id = getNextPlot(id, 1);
}
// no free plots
PlayerFunctions.sendMessage(plr, C.NO_FREE_PLOTS);
return false;
}
boolean br = false;

View File

@ -25,15 +25,18 @@ import java.util.HashSet;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.BlockLoc;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.intellectualcrafters.plot.object.PlotClusterId;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.util.ClusterManager;
import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper;
@ -50,7 +53,7 @@ public class Cluster extends SubCommand {
if (!ClusterManager.clusters.containsKey(plr.getWorld().getName())) {
return false;
}
// list, create, delete, resize, invite, kick, leave, helpers, tp
// list, create, delete, resize, invite, kick, leave, helpers, tp, sethome
if (args.length == 0) {
// return arguments
@ -127,6 +130,14 @@ public class Cluster extends SubCommand {
cluster.settings.setAlias(name);
DBFunc.createCluster(world, cluster);
ClusterManager.clusters.get(world).add(cluster);
// Add any existing plots to the current cluster
for (Plot plot : PlotMain.getPlots(plr.getWorld()).values()) {
PlotCluster current = ClusterManager.getCluster(plot);
if (cluster.equals(current)) {
cluster.invited.add(plot.owner);
DBFunc.setInvited(world, cluster, plot.owner);
}
}
PlayerFunctions.sendMessage(plr, C.CLUSTER_ADDED);
return true;
}
@ -144,6 +155,22 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
if (!toDelete.owner.equals(UUIDHandler.getUUID(plr))) {
if (!PlotMain.hasPermission(plr, "plots.cluster.delete.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.delete.other");
return false;
}
}
PlotWorld plotworld = PlotMain.getWorldSettings(plr.getWorld());
if (plotworld.REQUIRE_CLUSTER) {
for (Plot plot : PlotMain.getPlots(plr.getWorld()).values()) {
PlotCluster cluster = ClusterManager.getCluster(plot);
if (toDelete.equals(cluster)) {
String world = plr.getWorld().getName();
DBFunc.delete(world, plot);
}
}
}
String world_delete = plr.getWorld().getName();
ClusterManager.clusters.get(world_delete).remove(toDelete);
DBFunc.delete(toDelete);
@ -172,6 +199,12 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
if (!cluster.hasHelperRights(UUIDHandler.getUUID(plr))) {
if (!PlotMain.hasPermission(plr, "plots.cluster.resize.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.resize.other");
return false;
}
}
//check if overlap
PlotClusterId id = new PlotClusterId(pos1, pos2);
HashSet<PlotCluster> intersects = ClusterManager.getIntersects(plr.getWorld().getName(), id);
@ -199,6 +232,12 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
if (!cluster.hasHelperRights(UUIDHandler.getUUID(plr))) {
if (!PlotMain.hasPermission(plr, "plots.cluster.invite.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.invite.other");
return false;
}
}
// check uuid
UUID uuid = UUIDHandler.getUUID(args[1]);
if (uuid == null) {
@ -212,7 +251,7 @@ public class Cluster extends SubCommand {
DBFunc.setInvited(world, cluster, uuid);
Player player = UUIDHandler.uuidWrapper.getPlayer(uuid);
if (player != null) {
PlayerFunctions.sendMessage(plr, C.CLUSTER_INVITED, cluster.getName());
PlayerFunctions.sendMessage(player, C.CLUSTER_INVITED, cluster.getName());
}
}
PlayerFunctions.sendMessage(plr, C.CLUSTER_ADDED_USER);
@ -227,6 +266,46 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster kick <player>");
return false;
}
PlotCluster cluster = ClusterManager.getCluster(plr.getLocation());
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
if (!cluster.hasHelperRights(UUIDHandler.getUUID(plr))) {
if (!PlotMain.hasPermission(plr, "plots.cluster.kick.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.kick.other");
return false;
}
}
// check uuid
UUID uuid = UUIDHandler.getUUID(args[1]);
if (uuid == null) {
PlayerFunctions.sendMessage(plr, C.INVALID_PLAYER, args[1]);
return false;
}
// Can't kick if the player is yourself, the owner, or not added to the cluster
if (uuid.equals(UUIDHandler.getUUID(plr)) || uuid.equals(cluster.owner) || !cluster.hasRights(uuid)) {
PlayerFunctions.sendMessage(plr, C.CANNOT_KICK_PLAYER, cluster.getName());
return false;
}
if (cluster.helpers.contains(uuid)) {
cluster.helpers.remove(uuid);
DBFunc.removeHelper(cluster, uuid);
}
cluster.invited.remove(uuid);
DBFunc.removeInvited(cluster, uuid);
Player player = UUIDHandler.uuidWrapper.getPlayer(uuid);
if (player != null) {
PlayerFunctions.sendMessage(player, C.CLUSTER_REMOVED, cluster.getName());
}
for (Plot plot : PlotMain.getPlots(plr.getWorld(), uuid)) {
PlotCluster current = ClusterManager.getCluster(plot);
if (current != null && current.equals(cluster)) {
String world = plr.getWorld().getName();
DBFunc.delete(world, plot);
}
}
PlayerFunctions.sendMessage(plr, C.CLUSTER_KICKED_USER);
return true;
}
case "leave": {
@ -238,6 +317,44 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster leave [name]");
return false;
}
PlotCluster cluster;
if (args.length == 2) {
cluster = ClusterManager.getCluster(plr.getWorld().getName(), args[1]);
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.INVALID_CLUSTER, args[1]);
return false;
}
}
else {
cluster = ClusterManager.getCluster(plr.getLocation());
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
}
UUID uuid = UUIDHandler.getUUID(plr);
if (!cluster.hasRights(uuid)) {
PlayerFunctions.sendMessage(plr, C.CLUSTER_NOT_ADDED);
return false;
}
if (uuid.equals(cluster.owner)) {
PlayerFunctions.sendMessage(plr, C.CLUSTER_CANNOT_LEAVE);
return false;
}
if (cluster.helpers.contains(uuid)) {
cluster.helpers.remove(uuid);
DBFunc.removeHelper(cluster, uuid);
}
cluster.invited.remove(uuid);
DBFunc.removeInvited(cluster, uuid);
PlayerFunctions.sendMessage(plr, C.CLUSTER_REMOVED, cluster.getName());
for (Plot plot : PlotMain.getPlots(plr.getWorld(), uuid)) {
PlotCluster current = ClusterManager.getCluster(plot);
if (current != null && current.equals(cluster)) {
String world = plr.getWorld().getName();
DBFunc.delete(world, plot);
}
}
return true;
}
case "helpers": {
@ -249,7 +366,26 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster helpers <add|remove> <player>");
return false;
}
return true;
PlotCluster cluster = ClusterManager.getCluster(plr.getLocation());
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
UUID uuid = UUIDHandler.getUUID(args[2]);
if (uuid == null) {
PlayerFunctions.sendMessage(plr, C.INVALID_PLAYER, args[1]);
return false;
}
if (args[1].toLowerCase().equals("add")) {
cluster.helpers.add(uuid);
return PlayerFunctions.sendMessage(plr, C.CLUSTER_ADDED_HELPER);
}
if (args[1].toLowerCase().equals("remove")) {
cluster.helpers.remove(uuid);
return PlayerFunctions.sendMessage(plr, C.CLUSTER_REMOVED_HELPER);
}
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster helpers <add|remove> <player>");
return false;
}
case "tp": {
if (!PlotMain.hasPermission(plr, "plots.cluster.tp")) {
@ -260,7 +396,20 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster tp <name>");
return false;
}
return true;
PlotCluster cluster = ClusterManager.getCluster(plr.getWorld().getName(), args[1]);
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.INVALID_CLUSTER, args[1]);
return false;
}
UUID uuid = UUIDHandler.getUUID(plr);
if (!cluster.hasRights(uuid)) {
if (!PlotMain.hasPermission(plr, "plots.cluster.tp.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.tp.other");
return false;
}
}
plr.teleport(ClusterManager.getHome(cluster));
return PlayerFunctions.sendMessage(plr, C.CLUSTER_TELEPORTING);
}
case "info": {
if (!PlotMain.hasPermission(plr, "plots.cluster.info")) {
@ -271,8 +420,68 @@ public class Cluster extends SubCommand {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster info [name]");
return false;
}
PlotCluster cluster;
if (args.length == 2) {
cluster = ClusterManager.getCluster(plr.getWorld().getName(), args[1]);
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.INVALID_CLUSTER, args[1]);
return false;
}
}
else {
cluster = ClusterManager.getCluster(plr.getLocation());
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
}
String id = cluster.toString();
String owner = UUIDHandler.getName(cluster.owner);
if (owner == null) {
owner = "unknown";
}
String name = cluster.getName();
String size = (cluster.getP2().x - cluster.getP1().x) + "x" + (cluster.getP2().y - cluster.getP1().y);
String rights = cluster.hasRights(UUIDHandler.getUUID(plr)) + "";
String message = C.CLUSTER_INFO.s();
message = message.replaceAll("%id%", id);
message = message.replaceAll("%owner%", owner);
message = message.replaceAll("%name%", name);
message = message.replaceAll("%size%", size);
message = message.replaceAll("%rights%", rights);
PlayerFunctions.sendMessage(plr, message);
return true;
}
case "sethome": {
if (!PlotMain.hasPermission(plr, "plots.cluster.sethome")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.sethome");
return false;
}
if (args.length != 1 && args.length != 2) {
PlayerFunctions.sendMessage(plr, C.COMMAND_SYNTAX, "/plot cluster sethome");
return false;
}
PlotCluster cluster = ClusterManager.getCluster(plr.getLocation());
if (cluster == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_CLUSTER);
return false;
}
if (!cluster.hasHelperRights(UUIDHandler.getUUID(plr))) {
if (!PlotMain.hasPermission(plr, "plots.cluster.sethome.other")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.cluster.sethome.other");
return false;
}
}
Location base = ClusterManager.getClusterBottom(cluster);
base.setY(0);
Location relative = plr.getLocation().subtract(base);
BlockLoc blockloc = new BlockLoc(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ());
cluster.settings.setPosition(blockloc);
DBFunc.setPosition(cluster, relative.getBlockX() + "," + relative.getBlockY() + "," + relative.getBlockZ());
return PlayerFunctions.sendMessage(plr, C.POSITION_SET);
}
}
PlayerFunctions.sendMessage(plr, C.CLUSTER_AVAILABLE_ARGS);
return false;

View File

@ -40,7 +40,7 @@ public enum C {
/*
* Cluster
*/
CLUSTER_AVAILABLE_ARGS("&6The following sub commands are available: &clist, create, delete, resize, invite, kick, leave, helpers, info, tp"),
CLUSTER_AVAILABLE_ARGS("&6The following sub commands are available: &clist, create, delete, resize, invite, kick, leave, helpers, info, tp, sethome"),
CLUSTER_LIST_HEADING("&cThere are %s clusters in this world"),
CLUSTER_LIST_ELEMENT("&7 - &6%s\n"),
CLUSTER_INTERSECTION("&6The proposed area overlaps with %s existing cluster/s"),
@ -48,7 +48,17 @@ public enum C {
CLUSTER_DELETED("&6Successfully deleted the cluster."),
CLUSTER_RESIZED("&6Successfully resized the cluster."),
CLUSTER_ADDED_USER("&6Successfully added user to the cluster."),
CANNOT_KICK_PLAYER("&cYou cannot kick that player"),
CLUSTER_INVITED("&6You have been invited to the following cluster: %s."),
CLUSTER_REMOVED("&cYou have been removed from cluster %s"),
CLUSTER_KICKED_USER("&6Successfully kicked the user"),
INVALID_CLUSTER("&cInvalid cluster name: %s"),
CLUSTER_NOT_ADDED("&cThat player was not added to the plot cluster"),
CLUSTER_CANNOT_LEAVE("&cYou must delete or transfer ownership before leaving"),
CLUSTER_ADDED_HELPER("&6Successfully added a helper to the cluster"),
CLUSTER_REMOVED_HELPER("&6Successfully removed a helper to the cluster"),
CLUSTER_TELEPORTING("&6Teleporting..."),
CLUSTER_INFO("&6Current cluster: %id%\nName: %name%\nOwner: %owner%\nSize: %size%\nRights: %rights%"),
/*
* Border
*/
@ -168,7 +178,7 @@ public enum C {
* Position
*/
MISSING_POSITION("&cYou need to specify a position. Possible values: &6none"),
POSITION_SET("&cPlot home position set to your current location"),
POSITION_SET("&Home position set to your current location"),
HOME_ARGUMENT("&cUse /plot set home [none]"),
INVALID_POSITION("&cThat is not a valid position value"),
/*
@ -221,8 +231,9 @@ public enum C {
/*
* No <plot>
*/
NO_FREE_PLOTS("&cThere are no free plots available"),
NOT_IN_PLOT("&cYou're not in a plot"),
NOT_IN_CLUSTER("&cYou're not in a plot cluster"),
NOT_IN_CLUSTER("&cYou must be within a plot cluster to perform that action"),
NOT_IN_PLOT_WORLD("&cYou're not in a plot world"),
NOT_VALID_WORLD("&cThat is not a valid world (case sensitive)"),
NOT_VALID_PLOT_WORLD("&cThat is not a valid plot world (case sensitive)"),

View File

@ -239,11 +239,10 @@ public interface AbstractDB {
public void removeTrusted(final String world, final Plot plot, final UUID uuid);
/**
*
* @param world
* @param cluster
* @param uuid
*/
public void removeInvited(final String world, final PlotCluster cluster, final UUID uuid);
public void removeInvited(final PlotCluster cluster, final UUID uuid);
/**
* @param plot Plot Object
* @param uuid Player that should be removed

View File

@ -275,8 +275,8 @@ public class DBFunc {
* @param plot
* @param uuid
*/
public static void removeInvited(final String world, final PlotCluster cluster, final UUID uuid) {
dbManager.removeInvited(world, cluster, uuid);
public static void removeInvited(final PlotCluster cluster, final UUID uuid) {
dbManager.removeInvited(cluster, uuid);
}
/**

View File

@ -1697,7 +1697,7 @@ public class SQLManager implements AbstractDB {
}
@Override
public void removeInvited(String world, final PlotCluster cluster, final UUID uuid) {
public void removeInvited(final PlotCluster cluster, final UUID uuid) {
TaskManager.runTask(new Runnable() {
@Override
public void run() {

View File

@ -45,7 +45,11 @@ public class PlotCluster {
}
public boolean hasRights(UUID uuid) {
return (invited.contains(uuid)|| invited.contains(DBFunc.everyone) || helpers.contains(uuid) || helpers.contains(DBFunc.everyone));
return (owner.equals(uuid) || invited.contains(uuid)|| invited.contains(DBFunc.everyone) || helpers.contains(uuid) || helpers.contains(DBFunc.everyone));
}
public boolean hasHelperRights(UUID uuid) {
return (owner.equals(uuid) || helpers.contains(uuid) || helpers.contains(DBFunc.everyone));
}
public String getName() {

View File

@ -3,10 +3,12 @@ package com.intellectualcrafters.plot.util;
import java.util.HashMap;
import java.util.HashSet;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.object.BlockLoc;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.intellectualcrafters.plot.object.PlotClusterId;
@ -36,6 +38,63 @@ public class ClusterManager {
return new HashSet<>();
}
public static Location getHome(PlotCluster cluster) {
World world = Bukkit.getWorld(cluster.world);
BlockLoc home = cluster.settings.getPosition();
Location toReturn;
if (home.y == 0) {
// default pos
PlotId center = getCenterPlot(cluster);
toReturn = PlotHelper.getPlotHome(world, center);
if (toReturn.getBlockY() == 0) {
final PlotManager manager = PlotMain.getPlotManager(world);
final PlotWorld plotworld = PlotMain.getWorldSettings(world);
final Location loc = manager.getSignLoc(world, plotworld, PlotHelper.getPlot(world, center));
toReturn.setY(loc.getY());
}
}
else {
toReturn = getClusterBottom(cluster).add(home.x, home.y, home.z);
}
int max = world.getHighestBlockAt(toReturn).getY();
if (max > toReturn.getBlockY()) {
toReturn.setY(max);
}
return toReturn;
}
public static PlotId getCenterPlot(PlotCluster cluster) {
PlotId bot = cluster.getP1();
PlotId top = cluster.getP2();
return new PlotId((bot.x + top.x) / 2, (bot.y + top.y) / 2);
}
public static Location getClusterBottom(PlotCluster cluster) {
String world = cluster.world;
final PlotWorld plotworld = PlotMain.getWorldSettings(world);
final PlotManager manager = PlotMain.getPlotManager(world);
return manager.getPlotBottomLocAbs(plotworld, cluster.getP1());
}
public static Location getClusterTop(PlotCluster cluster) {
String world = cluster.world;
final PlotWorld plotworld = PlotMain.getWorldSettings(world);
final PlotManager manager = PlotMain.getPlotManager(world);
return manager.getPlotTopLocAbs(plotworld, cluster.getP2());
}
public static PlotCluster getCluster(String world, String name) {
if (!clusters.containsKey(world)) {
return null;
}
for (PlotCluster cluster : clusters.get(world)) {
if (cluster.getName().equals(name)) {
return cluster;
}
}
return null;
}
public static boolean contains(PlotCluster cluster, Location loc) {
String world = loc.getWorld().getName();
PlotManager manager = PlotMain.getPlotManager(world);

View File

@ -15,6 +15,7 @@ import com.google.common.collect.HashBiMap;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper;
import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
@ -140,6 +141,10 @@ public class UUIDHandler {
StringWrapper nameWrap = new StringWrapper(name);
add(nameWrap, uuid);
}
// add the Everyone '*' UUID
add(new StringWrapper("*"), DBFunc.everyone);
PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
}
@ -186,7 +191,6 @@ public class UUIDHandler {
if (name == null) {
return null;
}
// check online
Player player = Bukkit.getPlayer(name);
if (player != null) {