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

225 lines
9.9 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-01-13 17:38:15 +01:00
import java.util.ArrayList;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
2015-02-19 09:51:10 +01:00
import com.intellectualcrafters.plot.PlotSquared;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2015-01-10 02:00:26 +01:00
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.object.InfoInventory;
2015-02-22 08:13:27 +01:00
import com.intellectualcrafters.plot.object.Location;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
2015-02-21 12:38:44 +01:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.PlotWorld;
2015-02-22 08:13:27 +01:00
import com.intellectualcrafters.plot.util.BlockManager;
2015-02-20 12:23:48 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-02-20 07:28:21 +01:00
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
2014-12-22 16:44:13 +01:00
2014-09-22 13:02:14 +02:00
/**
* @author Citymonstret
*/
2015-02-20 07:34:19 +01:00
@SuppressWarnings({ "javadoc" })
public class Info extends SubCommand {
2014-11-05 04:42:08 +01:00
public Info() {
super(Command.INFO, "Display plot info", "info", CommandCategory.INFO, false);
}
2015-02-20 07:34:19 +01:00
2014-11-05 04:42:08 +01:00
@Override
2015-02-21 08:30:55 +01:00
public boolean execute(final PlotPlayer player, String... args) {
2014-11-05 04:42:08 +01:00
Plot plot;
2015-02-22 08:13:27 +01:00
String world;
2014-11-05 04:42:08 +01:00
if (player != null) {
2015-02-22 08:03:25 +01:00
Location loc = player.getLocation();
2015-02-22 08:13:27 +01:00
world = loc.getWorld();
2015-02-19 09:51:10 +01:00
if (!PlotSquared.isPlotWorld(world)) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.NOT_IN_PLOT_WORLD);
return false;
}
2015-02-22 08:13:27 +01:00
plot = MainUtil.getPlot(loc);
2015-02-22 08:03:25 +01:00
if (plot == null) {
2015-02-22 08:13:27 +01:00
return !sendMessage(player, C.NOT_IN_PLOT);
2014-11-05 04:42:08 +01:00
}
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
if (args.length < 2) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(null, C.INFO_SYNTAX_CONSOLE);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-20 09:55:04 +01:00
final PlotWorld plotworld = PlotSquared.getPlotWorld(args[0]);
2014-11-05 04:42:08 +01:00
if (plotworld == null) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.NOT_VALID_WORLD);
2014-11-05 04:42:08 +01:00
return false;
}
try {
final String[] split = args[1].split(";");
final PlotId id = new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
2015-02-22 08:13:27 +01:00
plot = MainUtil.getPlot(plotworld.worldname, id);
2014-11-05 04:42:08 +01:00
if (plot == null) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.NOT_VALID_PLOT_ID);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-22 08:13:27 +01:00
world = args[0];
2014-11-05 04:42:08 +01:00
if (args.length == 3) {
2015-02-20 07:34:19 +01:00
args = new String[] { args[2] };
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
args = new String[0];
}
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.INFO_SYNTAX_CONSOLE);
2014-11-05 04:42:08 +01:00
return false;
}
}
2014-12-16 06:03:20 +01:00
if ((args.length == 1) && args[0].equalsIgnoreCase("inv")) {
new InfoInventory(plot, player).build().display();
return true;
}
2014-11-05 04:42:08 +01:00
final boolean hasOwner = plot.hasOwner();
boolean containsEveryone;
boolean trustedEveryone;
// Wildcard player {added}
{
2014-12-16 06:03:20 +01:00
containsEveryone = (plot.helpers != null) && plot.helpers.contains(DBFunc.everyone);
trustedEveryone = (plot.trusted != null) && plot.trusted.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 owner = "none";
if (plot.owner != null) {
2015-01-06 17:08:29 +01:00
owner = UUIDHandler.getName(plot.owner);
2014-11-05 04:42:08 +01:00
}
if (owner == null) {
owner = plot.owner.toString();
}
String info = C.PLOT_INFO.s();
if (args.length == 1) {
info = getCaption(args[0].toLowerCase());
if (info == null) {
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, "&6Categories&7: &ahelpers&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;
}
}
info = format(info, world, plot, player);
2015-02-22 08:13:27 +01:00
MainUtil.sendMessage(player, C.PLOT_INFO_HEADER);
MainUtil.sendMessage(player, info, false);
2014-11-05 04:42:08 +01:00
return true;
}
2015-02-20 07:34:19 +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) {
2014-11-05 04:42:08 +01:00
case "helpers":
return C.PLOT_INFO_HELPERS.s();
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 "trusted":
return C.PLOT_INFO_TRUSTED.s();
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-20 07:34:19 +01:00
2015-02-22 08:13:27 +01:00
private String format(String info, final String world, final Plot plot, final PlotPlayer player) {
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();
2014-11-05 04:42:08 +01:00
final String alias = plot.settings.getAlias().length() > 0 ? plot.settings.getAlias() : "none";
2015-02-22 08:13:27 +01:00
final String biome = BlockManager.manager.getBiome(MainUtil.getPlotBottomLoc(world, plot.id).add(1, 0, 1));
2014-11-05 04:42:08 +01:00
final String helpers = getPlayerList(plot.helpers);
final String trusted = getPlayerList(plot.trusted);
final String denied = getPlayerList(plot.denied);
final String rating = String.format("%.1f", DBFunc.getRatings(plot));
2015-01-10 02:00:26 +01:00
final String flags = "&6" + (StringUtils.join(FlagManager.getPlotFlags(plot), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot), "&7, &6") : "none");
2015-02-22 08:13:27 +01:00
final boolean build = (player == null) || plot.isAdded(player.getUUID());
2014-10-24 07:50:48 +02:00
String owner = "none";
if (plot.owner != null) {
owner = UUIDHandler.getName(plot.owner);
2014-10-24 07:50:48 +02:00
}
if (owner == null) {
owner = plot.owner.toString();
}
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 + "");
2014-10-24 07:50:48 +02:00
info = info.replaceAll("%biome%", biome);
info = info.replaceAll("%owner%", owner);
info = info.replaceAll("%helpers%", helpers);
info = info.replaceAll("%trusted%", trusted);
info = info.replaceAll("%denied%", denied);
info = info.replaceAll("%rating%", rating);
info = info.replaceAll("%flags%", 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.");
return info;
2014-11-05 04:42:08 +01:00
}
2015-02-20 07:34:19 +01:00
2014-11-05 04:42:08 +01:00
private String getPlayerList(final ArrayList<UUID> l) {
if ((l == null) || (l.size() < 1)) {
return " none";
}
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(",", ""));
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
list.append(c.replace("%user%", getPlayerName(l.get(x))));
}
}
return list.toString();
}
2015-02-20 07:34:19 +01:00
2014-11-05 04:42:08 +01:00
private String getPlayerName(final UUID uuid) {
if (uuid == null) {
2014-12-22 16:39:17 +01:00
return "unknown";
2014-11-05 04:42:08 +01:00
}
if (uuid.equals(DBFunc.everyone) || uuid.toString().equalsIgnoreCase(DBFunc.everyone.toString())) {
return "everyone";
}
2015-02-20 07:34:19 +01:00
final String name = UUIDHandler.getName(uuid);
2014-12-22 16:44:13 +01:00
if (name == null) {
return "unknown";
}
return name;
2014-11-05 04:42:08 +01:00
}
2014-09-22 13:02:14 +02:00
}