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

292 lines
14 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.Arrays;
2015-02-23 12:37:36 +01:00
import java.util.List;
2015-01-13 17:38:15 +01:00
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;
import com.intellectualcrafters.plot.config.Configuration;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.flag.AbstractFlag;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.listeners.PlotListener;
import com.intellectualcrafters.plot.object.BlockLoc;
2015-02-22 13:24:48 +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.PlotBlock;
import com.intellectualcrafters.plot.object.PlotManager;
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-01-23 01:14:14 +01:00
import com.intellectualcrafters.plot.object.StringWrapper;
2015-02-22 13:24:48 +01:00
import com.intellectualcrafters.plot.util.BlockManager;
2015-02-20 12:23:48 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-02-22 13:24:48 +01:00
import com.intellectualcrafters.plot.util.Permissions;
2015-02-20 07:28:21 +01:00
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
2014-09-22 13:02:14 +02:00
/**
* @author Citymonstret
*/
public class Set extends SubCommand {
2015-02-23 12:37:36 +01:00
public final static String[] values = new String[] { "biome", "alias", "home", "flag" };
2015-02-20 07:34:19 +01:00
public final static String[] aliases = new String[] { "b", "w", "wf", "f", "a", "h", "fl" };
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
public Set() {
super(Command.SET, "Set a plot value", "set {arg} {value...}", CommandCategory.ACTIONS, true);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
@SuppressWarnings("deprecation")
@Override
2015-02-21 08:30:55 +01:00
public boolean execute(final PlotPlayer plr, final String... args) {
2015-02-23 02:32:27 +01:00
final Location loc = plr.getLocation();
2015-02-21 13:01:15 +01:00
final Plot plot = MainUtil.getPlot(loc);
2015-02-22 08:03:25 +01:00
if (plot == null) {
return !sendMessage(plr, C.NOT_IN_PLOT);
}
2014-11-05 04:42:08 +01:00
if (!plot.hasOwner()) {
sendMessage(plr, C.PLOT_NOT_CLAIMED);
2014-10-21 11:58:52 +02:00
return false;
}
2015-02-22 13:24:48 +01:00
if (!plot.isAdded(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.set")) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
2014-11-05 04:42:08 +01:00
return false;
}
if (args.length < 1) {
2015-02-23 12:37:36 +01:00
PlotManager manager = PlotSquared.getPlotManager(loc.getWorld());
List<String> newValues = Arrays.asList(values);;
newValues.addAll(Arrays.asList(manager.getPlotComponents(PlotSquared.getPlotWorld(loc.getWorld()), plot.id)));
MainUtil.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + getArgumentList(newValues));
2014-11-05 04:42:08 +01:00
return false;
}
for (int i = 0; i < aliases.length; i++) {
if (aliases[i].equalsIgnoreCase(args[0])) {
args[0] = values[i];
break;
}
}
/* TODO: Implement option */
2014-11-09 12:51:17 +01:00
// final boolean advanced_permissions = true;
2015-02-21 12:24:46 +01:00
if (!Permissions.hasPermission(plr, "plots.set." + args[0].toLowerCase())) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set." + args[0].toLowerCase());
2014-11-09 12:51:17 +01:00
return false;
2014-11-05 04:42:08 +01:00
}
if (args[0].equalsIgnoreCase("flag")) {
if (args.length < 2) {
2015-02-23 02:32:27 +01:00
final String message = StringUtils.join(FlagManager.getFlags(plr), "&c, &6");
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NEED_KEY.s().replaceAll("%values%", message));
2014-11-05 04:42:08 +01:00
return false;
}
AbstractFlag af;
try {
af = FlagManager.getFlag(args[1].toLowerCase());
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
af = new AbstractFlag(args[1].toLowerCase());
}
2015-02-22 13:24:48 +01:00
if (!FlagManager.getFlags().contains(af)) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NOT_VALID_FLAG);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-21 12:24:46 +01:00
if (!Permissions.hasPermission(plr, "plots.set.flag." + args[1].toLowerCase())) {
MainUtil.sendMessage(plr, C.NO_PERMISSION);
2014-11-05 04:42:08 +01:00
return false;
}
if (args.length == 2) {
2015-01-08 15:08:50 +01:00
if (FlagManager.getPlotFlagAbs(plot, args[1].toLowerCase()) == null) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.FLAG_NOT_IN_PLOT);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-20 07:34:19 +01:00
final boolean result = FlagManager.removePlotFlag(plot, args[1].toLowerCase());
2015-01-08 15:08:50 +01:00
if (!result) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.FLAG_NOT_REMOVED);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.FLAG_REMOVED);
2014-11-05 04:42:08 +01:00
PlotListener.plotEntry(plr, plot);
return true;
}
try {
2015-02-20 07:34:19 +01:00
final String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ");
final Object parsed_value = af.parseValueRaw(value);
if (parsed_value == null) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, af.getValueDesc());
2014-11-05 04:42:08 +01:00
return false;
}
final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), parsed_value);
2015-02-20 07:34:19 +01:00
final boolean result = FlagManager.addPlotFlag(plot, flag);
2015-01-08 15:08:50 +01:00
if (!result) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.FLAG_NOT_ADDED);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.FLAG_ADDED);
2014-11-05 04:42:08 +01:00
PlotListener.plotEntry(plr, plot);
return true;
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, "&c" + e.getMessage());
2014-11-05 04:42:08 +01:00
return false;
}
}
if (args[0].equalsIgnoreCase("home")) {
if (args.length > 1) {
if (args[1].equalsIgnoreCase("none")) {
plot.settings.setPosition(null);
2015-02-22 07:52:03 +01:00
DBFunc.setPosition(loc.getWorld(), plot, "");
return true;
2014-11-05 04:42:08 +01:00
}
2015-02-21 12:24:46 +01:00
return MainUtil.sendMessage(plr, C.HOME_ARGUMENT);
2014-11-05 04:42:08 +01:00
}
//set to current location
2015-02-22 13:24:48 +01:00
final String world = plr.getLocation().getWorld();
2015-02-20 12:23:48 +01:00
final Location base = MainUtil.getPlotBottomLoc(world, plot.id);
2015-01-23 01:14:14 +01:00
base.setY(0);
2015-02-22 13:24:48 +01:00
final Location relative = plr.getLocation().subtract(base.getX(), base.getZ(), base.getY());
final BlockLoc blockloc = new BlockLoc(relative.getX(), relative.getY(), relative.getZ());
plot.settings.setPosition(blockloc);
2015-02-22 13:24:48 +01:00
DBFunc.setPosition(loc.getWorld(), plot, relative.getX() + "," + relative.getY() + "," + relative.getZ());
2015-02-21 12:24:46 +01:00
return MainUtil.sendMessage(plr, C.POSITION_SET);
2014-11-05 04:42:08 +01:00
}
if (args[0].equalsIgnoreCase("alias")) {
if (args.length < 2) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.MISSING_ALIAS);
2014-11-05 04:42:08 +01:00
return false;
}
final String alias = args[1];
2015-01-04 05:43:36 +01:00
if (alias.length() >= 50) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.ALIAS_TOO_LONG);
2015-01-04 05:43:36 +01:00
return false;
}
2015-02-22 13:24:48 +01:00
for (final Plot p : PlotSquared.getPlots(plr.getLocation().getWorld()).values()) {
2014-11-05 04:42:08 +01:00
if (p.settings.getAlias().equalsIgnoreCase(alias)) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.ALIAS_IS_TAKEN);
2014-11-05 04:42:08 +01:00
return false;
}
2015-01-23 01:14:14 +01:00
if (UUIDHandler.nameExists(new StringWrapper(alias))) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.ALIAS_IS_TAKEN);
2014-11-05 04:42:08 +01:00
return false;
}
}
2015-02-22 07:52:03 +01:00
DBFunc.setAlias(loc.getWorld(), plot, alias);
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.ALIAS_SET_TO.s().replaceAll("%alias%", alias));
2014-11-05 04:42:08 +01:00
return true;
}
if (args[0].equalsIgnoreCase("biome")) {
if (args.length < 2) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NEED_BIOME);
2014-11-05 04:42:08 +01:00
return true;
}
if (args[1].length() < 2) {
2014-11-01 16:22:22 +01:00
sendMessage(plr, C.NAME_LITTLE, "Biome", args[1].length() + "", "2");
return true;
}
2015-02-22 13:24:48 +01:00
final int biome = BlockManager.manager.getBiomeFromString(args[1]);
2014-11-05 04:42:08 +01:00
/*
* for (Biome b : Biome.values()) {
* if (b.toString().equalsIgnoreCase(args[1])) {
* biome = b;
* break;
* }
* }
*/
2015-02-22 13:24:48 +01:00
if (biome == -1) {
MainUtil.sendMessage(plr, getBiomeList(BlockManager.manager.getBiomeList()));
2014-11-05 04:42:08 +01:00
return true;
}
2015-02-22 13:24:48 +01:00
MainUtil.setBiome(plr.getLocation().getWorld(), plot, args[1].toUpperCase());
MainUtil.sendMessage(plr, C.BIOME_SET_TO.s() + args[1].toLowerCase());
2014-11-05 04:42:08 +01:00
return true;
}
// Get components
2015-02-22 13:24:48 +01:00
final String world = plr.getLocation().getWorld();
2015-02-20 09:55:04 +01:00
final PlotWorld plotworld = PlotSquared.getPlotWorld(world);
2015-02-20 07:34:19 +01:00
final PlotManager manager = PlotSquared.getPlotManager(world);
2015-02-22 13:24:48 +01:00
final String[] components = manager.getPlotComponents(plotworld, plot.id);
2015-02-20 07:34:19 +01:00
for (final String component : components) {
if (component.equalsIgnoreCase(args[0])) {
PlotBlock[] blocks;
try {
2015-02-23 01:05:25 +01:00
blocks = (PlotBlock[]) Configuration.BLOCKLIST.parseString(args[1]);
2015-02-20 07:34:19 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
try {
2015-02-23 01:05:25 +01:00
if (args.length < 2) {
MainUtil.sendMessage(plr, C.NEED_BLOCK);
return true;
}
blocks = new PlotBlock[] { new PlotBlock((short) BlockManager.manager.getBlockIdFromString(args[1]), (byte) 0) };
2015-02-20 07:34:19 +01:00
} catch (final Exception e2) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NOT_VALID_BLOCK);
2015-02-20 07:34:19 +01:00
return false;
}
2014-11-05 04:42:08 +01:00
}
2015-02-22 13:24:48 +01:00
manager.setComponent(plotworld, plot.id, component, blocks);
2015-02-23 08:08:49 +01:00
MainUtil.update(loc);
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.GENERATING_COMPONENT);
2014-11-01 16:22:22 +01:00
return true;
}
2014-11-05 04:42:08 +01:00
}
2014-10-25 14:46:04 +02:00
{
2014-11-09 12:51:17 +01:00
AbstractFlag af;
2014-10-25 14:46:04 +02:00
try {
af = new AbstractFlag(args[0].toLowerCase());
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-09 12:51:17 +01:00
af = new AbstractFlag("");
2014-10-25 14:46:04 +02:00
}
2014-10-25 14:59:08 +02:00
if (FlagManager.getFlags().contains(af)) {
2014-11-05 04:42:08 +01:00
final StringBuilder a = new StringBuilder();
if (args.length > 1) {
for (int x = 1; x < args.length; x++) {
2014-10-25 14:59:08 +02:00
a.append(" ").append(args[x]);
2014-11-05 04:42:08 +01:00
}
2014-10-25 14:46:04 +02:00
}
2015-02-22 13:24:48 +01:00
MainCommand.onCommand(plr, world, ("plot set flag " + args[0] + a.toString()).split(" "));
2014-10-25 14:46:04 +02:00
return true;
}
}
2015-02-23 12:37:36 +01:00
List<String> newValues = Arrays.asList(values);;
newValues.addAll(Arrays.asList(manager.getPlotComponents(plotworld, plot.id)));
MainUtil.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + getArgumentList(newValues));
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
private String getString(final String s) {
2015-02-22 13:24:48 +01:00
return MainUtil.colorise('&', C.BLOCK_LIST_ITEM.s().replaceAll("%mat%", s));
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-23 12:37:36 +01:00
private String getArgumentList(final List<String> newValues) {
2014-11-05 04:42:08 +01:00
final StringBuilder builder = new StringBuilder();
2015-02-23 12:37:36 +01:00
for (final String s : newValues) {
2014-11-05 04:42:08 +01:00
builder.append(getString(s));
}
return builder.toString().substring(1, builder.toString().length() - 1);
}
2015-02-23 02:32:27 +01:00
2015-02-22 13:24:48 +01:00
private String getBiomeList(final String[] biomes) {
2014-11-05 04:42:08 +01:00
final StringBuilder builder = new StringBuilder();
2015-02-22 13:24:48 +01:00
builder.append(MainUtil.colorise('&', C.NOT_VALID_BLOCK_LIST_HEADER.s()));
for (final String b : biomes) {
builder.append(getString(b));
2014-11-05 04:42:08 +01:00
}
return builder.toString().substring(1, builder.toString().length() - 1);
}
2014-09-22 13:02:14 +02:00
}