PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java

277 lines
13 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-09-22 13:02:14 +02:00
package com.intellectualcrafters.plot.commands;
2015-07-30 16:25:16 +02:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.regex.Matcher;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2015-07-27 19:50:04 +02:00
import com.intellectualcrafters.plot.flag.Flag;
2015-01-10 02:00:26 +01:00
import com.intellectualcrafters.plot.flag.FlagManager;
2015-07-18 14:21:36 +02:00
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
2015-07-28 08:06:19 +02:00
import com.intellectualcrafters.plot.object.PlotInventory;
import com.intellectualcrafters.plot.object.PlotItemStack;
2015-07-18 14:21:36 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2015-07-28 08:06:19 +02:00
@CommandDeclaration(
command = "info",
aliases = {"i"},
description = "Display plot info",
usage = "/plot info <id>",
category = CommandCategory.INFO
)
2015-02-20 07:34:19 +01:00
public class Info extends SubCommand {
2015-02-23 02:32:27 +01:00
2015-07-14 02:00:24 +02:00
public static String getPlayerList(final Collection<UUID> uuids) {
ArrayList<UUID> l = new ArrayList<>(uuids);
if ((l == null) || (l.size() < 1)) {
return C.NONE.s();
}
final String c = C.PLOT_USER_LIST.s();
final StringBuilder list = new StringBuilder();
for (int x = 0; x < l.size(); x++) {
if ((x + 1) == l.size()) {
list.append(c.replace("%user%", getPlayerName(l.get(x))).replace(",", ""));
} else {
list.append(c.replace("%user%", getPlayerName(l.get(x))));
}
}
return list.toString();
}
public static String getPlayerName(final UUID uuid) {
if (uuid == null) {
return "unknown";
}
if (uuid.equals(DBFunc.everyone) || uuid.toString().equalsIgnoreCase(DBFunc.everyone.toString())) {
return "everyone";
}
final String name = UUIDHandler.getName(uuid);
if (name == null) {
return "unknown";
}
return name;
}
2014-11-05 04:42:08 +01:00
@Override
2015-07-27 15:10:14 +02:00
public boolean onCommand(final PlotPlayer player, String[] args) {
String arg = null;
Plot plot;
if (args.length > 0) arg = args[0] + "";
2015-06-24 05:48:23 +02:00
if (arg != null) {
switch (arg) {
case "trusted":
case "alias":
case "inv":
case "biome":
case "denied":
case "flags":
case "id":
case "size":
case "members":
case "owner":
case "rating":
2015-07-27 17:14:38 +02:00
plot = MainUtil.getPlotFromString(player, null, false);
break;
default:
2015-07-27 17:14:38 +02:00
plot = MainUtil.getPlotFromString(player, arg, false);
if (args.length == 2) {
arg = args[1];
}
else {
arg = null;
}
break;
2015-06-24 05:48:23 +02:00
}
}
else {
2015-07-27 17:14:38 +02:00
plot = MainUtil.getPlotFromString(player, null, false);
}
if (plot == null && arg != null) {
2015-07-27 17:14:38 +02:00
plot = MainUtil.getPlotFromString(player, null, false);
}
if (plot == null) {
MainUtil.sendMessage(player, C.NOT_IN_PLOT);
return false;
}
if (arg != null) {
if (args.length == 1) {
args = new String[0];
2014-11-05 04:42:08 +01:00
}
else {
args = new String[] { args[1] };
2014-11-05 04:42:08 +01:00
}
}
2014-12-16 06:03:20 +01:00
if ((args.length == 1) && args[0].equalsIgnoreCase("inv")) {
2015-07-28 08:06:19 +02:00
PlotInventory inv = new PlotInventory(player) {
@Override
public boolean onClick(int index) {
// TODO InfoInventory not implemented yet!!!!!!!!
// See plot rating or musicsubcommand on examples
return false;
}
};
UUID uuid = player.getUUID();
String name = MainUtil.getName(plot.owner);
2015-07-30 19:24:01 +02:00
inv.setItem(1, new PlotItemStack(388, (short) 0, 1, "&cPlot Info", new String[] { "&cID: &6" + plot.getId().toString(), "&cOwner: &6" + name, "&cAlias: &6" + plot.getSettings().getAlias(), "&cBiome: &6" + plot.getBiome().toString().replaceAll("_", "").toLowerCase(), "&cCan Build: &6" + plot.isAdded(uuid), "&cIs Denied: &6" + plot.isDenied(uuid)}));
2015-07-28 08:06:19 +02:00
inv.setItem(1, new PlotItemStack(388, (short) 0, 1, "&cTrusted", new String[] {"&cAmount: &6" + plot.getTrusted().size(), "&8Click to view a list of the trusted users"}));
inv.setItem(1, new PlotItemStack(388, (short) 0, 1, "&cMembers", new String[] {"&cAmount: &6" + plot.getMembers().size(), "&8Click to view a list of plot members"}));
inv.setItem(1, new PlotItemStack(388, (short) 0, 1, "&cDenied", new String[] {"&cDenied", "&cAmount: &6" + plot.getDenied().size(), "&8Click to view a list of denied players"}));
inv.setItem(1, new PlotItemStack(388, (short) 0, 1, "&cFlags", new String[] {"&cFlags", "&cAmount: &6" + plot.getSettings().flags.size(), "&8Click to view a list of plot flags"}));
inv.openInventory();
return true;
}
2014-11-05 04:42:08 +01:00
final boolean hasOwner = plot.hasOwner();
boolean containsEveryone;
boolean trustedEveryone;
// Wildcard player {added}
{
2015-07-21 20:31:12 +02:00
containsEveryone = (plot.getTrusted() != null) && plot.getTrusted().contains(DBFunc.everyone);
trustedEveryone = (plot.getMembers() != null) && plot.getMembers().contains(DBFunc.everyone);
2014-11-05 04:42:08 +01:00
}
// Unclaimed?
if (!hasOwner && !containsEveryone && !trustedEveryone) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.PLOT_INFO_UNCLAIMED, (plot.id.x + ";" + plot.id.y));
2014-11-05 04:42:08 +01:00
return true;
}
String info = C.PLOT_INFO.s();
if (arg != null) {
info = getCaption(arg);
2014-11-05 04:42:08 +01:00
if (info == null) {
MainUtil.sendMessage(player, "&6Categories&7: &amembers&7, &aalias&7, &abiome&7, &adenied&7, &aflags&7, &aid&7, &asize&7, &atrusted&7, &aowner&7, &arating");
2014-11-05 04:42:08 +01:00
return false;
}
formatAndSend(info, plot.world, plot, player, true);
}
else {
formatAndSend(info, plot.world, plot, player, false);
2014-11-05 04:42:08 +01:00
}
return true;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
private String getCaption(final String string) {
2014-10-24 07:50:48 +02:00
switch (string) {
case "trusted":
return C.PLOT_INFO_TRUSTED.s();
2014-11-05 04:42:08 +01:00
case "alias":
return C.PLOT_INFO_ALIAS.s();
case "biome":
return C.PLOT_INFO_BIOME.s();
case "denied":
return C.PLOT_INFO_DENIED.s();
case "flags":
return C.PLOT_INFO_FLAGS.s();
case "id":
return C.PLOT_INFO_ID.s();
case "size":
return C.PLOT_INFO_SIZE.s();
case "members":
return C.PLOT_INFO_MEMBERS.s();
2014-11-05 04:42:08 +01:00
case "owner":
return C.PLOT_INFO_OWNER.s();
case "rating":
return C.PLOT_INFO_RATING.s();
default:
return null;
2014-10-24 07:50:48 +02:00
}
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
private void formatAndSend(String info, final String world, final Plot plot, final PlotPlayer player, final boolean full) {
2014-11-05 04:42:08 +01:00
final PlotId id = plot.id;
2015-02-22 08:13:27 +01:00
final PlotId id2 = MainUtil.getTopPlot(plot).id;
2015-02-21 13:52:50 +01:00
final int num = MainUtil.getPlotSelectionIds(id, id2).size();
2015-07-21 20:31:12 +02:00
final String alias = plot.getSettings().getAlias().length() > 0 ? plot.getSettings().getAlias() : C.NONE.s();
2015-04-27 16:53:07 +02:00
Location top = MainUtil.getPlotTopLoc(world, plot.id);
Location bot = MainUtil.getPlotBottomLoc(world, plot.id).add(1, 0, 1);
2015-07-28 08:06:19 +02:00
final String biome = BlockManager.manager.getBiome(plot.world, bot.getX() + ((top.getX() - bot.getX()) / 2), bot.getZ() + ((top.getZ() - bot.getZ()) / 2));
2015-07-21 20:31:12 +02:00
final String trusted = getPlayerList(plot.getTrusted());
final String members = getPlayerList(plot.getMembers());
final String denied = getPlayerList(plot.getDenied());
2015-07-14 02:00:24 +02:00
Flag descriptionFlag = FlagManager.getPlotFlag(plot, "description");
final String description = descriptionFlag == null ? C.NONE.s() : descriptionFlag.getValueString();
2015-07-30 16:25:16 +02:00
final String flags = StringMan.replaceFromMap("$2" + (StringMan.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "").length() > 0 ? StringMan.join(FlagManager.getPlotFlags(plot.world, plot.getSettings(), true).values(), "$1, $2") : C.NONE.s()), C.replacements);
2015-07-27 17:14:38 +02:00
final boolean build = plot.isAdded(player.getUUID());
2015-07-14 02:00:24 +02:00
String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners());
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%alias%", alias);
info = info.replaceAll("%id%", id.toString());
info = info.replaceAll("%id2%", id2.toString());
2014-11-05 04:42:08 +01:00
info = info.replaceAll("%num%", num + "");
info = info.replaceAll("%desc%", description);
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%biome%", biome);
info = info.replaceAll("%owner%", owner);
info = info.replaceAll("%members%", members);
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%trusted%", trusted);
2015-05-15 11:32:32 +02:00
info = info.replaceAll("%helpers%", members);
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%denied%", denied);
2015-05-04 11:52:37 +02:00
info = info.replaceAll("%flags%", Matcher.quoteReplacement(flags));
2014-11-05 04:42:08 +01:00
info = info.replaceAll("%build%", build + "");
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%desc%", "No description set.");
if (info.contains("%rating%")) {
2015-05-14 15:26:34 +02:00
final String newInfo = info;
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
int max = 10;
if (Settings.RATING_CATEGORIES != null && Settings.RATING_CATEGORIES.size() > 0) {
max = 8;
}
String info;
if (full && Settings.RATING_CATEGORIES != null && Settings.RATING_CATEGORIES.size() > 1) {
String rating = "";
String prefix = "";
double[] ratings = MainUtil.getAverageRatings(plot);
for (int i = 0; i < ratings.length; i++) {
rating += prefix + Settings.RATING_CATEGORIES.get(i) + "=" + String.format("%.1f", ratings[i]);
prefix = ",";
}
info = newInfo.replaceAll("%rating%", rating);
}
else {
info = newInfo.replaceAll("%rating%", String.format("%.1f", MainUtil.getAverageRating(plot)) + "/" + max);
}
2015-05-14 15:26:34 +02:00
MainUtil.sendMessage(player, C.PLOT_INFO_HEADER);
MainUtil.sendMessage(player, info, false);
2015-07-31 11:46:07 +02:00
MainUtil.sendMessage(player, C.PLOT_INFO_FOOTER);
}
});
2015-05-14 15:26:34 +02:00
return;
}
MainUtil.sendMessage(player, C.PLOT_INFO_HEADER);
MainUtil.sendMessage(player, info, false);
2014-11-05 04:42:08 +01:00
}
2014-09-22 13:02:14 +02:00
}