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

384 lines
18 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-18 14:21:36 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.PS;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Configuration;
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;
2015-04-26 08:29:58 +02:00
import com.intellectualcrafters.plot.listeners.APlotListener;
2015-07-18 14:21:36 +02:00
import com.intellectualcrafters.plot.object.BlockLoc;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.SetBlockQueue;
import com.intellectualcrafters.plot.util.StringComparison;
import com.intellectualcrafters.plot.util.StringMan;
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
@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;
}
if (!plot.isAdded(plr.getUUID())) {
if (!Permissions.hasPermission(plr, "plots.set.other")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.other");
return false;
}
2014-11-05 04:42:08 +01:00
}
if (args.length < 1) {
PlotManager manager = PS.get().getPlotManager(loc.getWorld());
2015-02-27 02:06:51 +01:00
ArrayList<String> newValues = new ArrayList<String>();
newValues.addAll(Arrays.asList(values));
newValues.addAll(Arrays.asList(manager.getPlotComponents(PS.get().getPlotWorld(loc.getWorld()), plot.id)));
2015-02-23 12:37:36 +01:00
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;
}
}
if (args[0].equalsIgnoreCase("flag")) {
if (args.length < 2) {
2015-07-14 09:18:31 +02:00
final String message = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getFlags(plr), "$1, $2")), C.replacements);
// 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-06-24 21:06:47 +02:00
if (!FlagManager.getFlags().contains(af) || FlagManager.isReserved(af.getKey())) {
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;
}
if (!Permissions.hasPermission(plr, "plots.set.flag." + args[1].toLowerCase())) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.flag." + args[1].toLowerCase());
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);
2015-04-26 08:29:58 +02:00
APlotListener.manager.plotEntry(plr, plot);
2014-11-05 04:42:08 +01:00
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);
2015-04-26 08:29:58 +02:00
APlotListener.manager.plotEntry(plr, plot);
2014-11-05 04:42:08 +01:00
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 (!Permissions.hasPermission(plr, "plots.set.home")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.home");
return false;
}
if (args.length > 1) {
if (args[1].equalsIgnoreCase("none")) {
2015-07-04 08:37:33 +02:00
plot.setHome(null);
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);
final Location relative = plr.getLocation().subtract(base.getX(), base.getY(), base.getZ());
2015-05-24 03:17:09 +02:00
final BlockLoc blockloc = new BlockLoc(relative.getX(), relative.getY(), relative.getZ(), relative.getYaw(), relative.getPitch());
2015-07-04 08:37:33 +02:00
plot.setHome(blockloc);
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("desc")) {
if (!Permissions.hasPermission(plr, "plots.set.desc")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.desc");
return false;
}
if (args.length < 2) {
MainUtil.sendMessage(plr, C.MISSING_DESC);
return false;
}
final StringBuilder desc = new StringBuilder();
for (int i = 1; i < args.length; i++) {
desc.append(args[i]).append(" ");
}
String descValue = desc.substring(0, desc.length() - 1);
Flag flag = new Flag(FlagManager.getFlag("description"), descValue);
final boolean result = FlagManager.addPlotFlag(plot, flag);
if (!result) {
MainUtil.sendMessage(plr, C.FLAG_NOT_ADDED);
return false;
}
MainUtil.sendMessage(plr, C.DESC_SET);
return true;
}
2014-11-05 04:42:08 +01:00
if (args[0].equalsIgnoreCase("alias")) {
if (!Permissions.hasPermission(plr, "plots.set.alias")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.alias");
return false;
}
2014-11-05 04:42:08 +01:00
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;
}
for (final Plot p : PS.get().getPlots(plr.getLocation().getWorld()).values()) {
2015-07-21 20:31:12 +02:00
if (p.getSettings().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-07-04 08:37:33 +02:00
plot.setAlias(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 (!Permissions.hasPermission(plr, "plots.set.biome")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set.biome");
return false;
}
2014-11-05 04:42:08 +01:00
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;
}
if (MainUtil.runners.containsKey(plot)) {
MainUtil.sendMessage(plr, C.WAIT_FOR_TIMER);
return false;
}
MainUtil.runners.put(plot, 1);
plot.setBiome(args[1].toUpperCase(), new Runnable() {
@Override
public void run() {
MainUtil.runners.remove(plot);
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();
final PlotWorld plotworld = PS.get().getPlotWorld(world);
final PlotManager manager = PS.get().getPlotManager(world);
2015-02-22 13:24:48 +01:00
final String[] components = manager.getPlotComponents(plotworld, plot.id);
boolean allowUnsafe = DebugAllowUnsafe.unsafeAllowed.contains(plr.getUUID());
2015-02-20 07:34:19 +01:00
for (final String component : components) {
if (component.equalsIgnoreCase(args[0])) {
if (!Permissions.hasPermission(plr, "plots.set." + component)) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set." + component);
2015-07-17 12:48:13 +02:00
return false;
}
PlotBlock[] blocks;
try {
if (args.length < 2) {
MainUtil.sendMessage(plr, C.NEED_BLOCK);
return true;
}
String[] split = args[1].split(",");
blocks = Configuration.BLOCKLIST.parseString(args[1]);
for (int i = 0; i < blocks.length; i++) {
PlotBlock block = blocks[i];
if (block == null) {
MainUtil.sendMessage(plr, C.NOT_VALID_BLOCK, split[i]);
String name;
if (split[i].contains("%")) {
name = split[i].split("%")[1];
}
else {
name = split[i];
}
StringComparison<PlotBlock>.ComparisonResult match = BlockManager.manager.getClosestBlock(name);
if (match != null) {
name = BlockManager.manager.getClosestMatchingName(match.best);
if (name != null) {
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, name.toLowerCase());
}
2015-04-14 15:55:00 +02:00
}
return false;
}
else if (!allowUnsafe && !BlockManager.manager.isBlockSolid(block)) {
MainUtil.sendMessage(plr, C.NOT_ALLOWED_BLOCK, block.toString());
return false;
}
}
if (!allowUnsafe) {
for (PlotBlock block : blocks) {
if (!BlockManager.manager.isBlockSolid(block)) {
MainUtil.sendMessage(plr, C.NOT_ALLOWED_BLOCK, block.toString());
return false;
}
2015-04-14 15:55:00 +02:00
}
2015-02-20 07:34:19 +01:00
}
} catch (final Exception e2) {
MainUtil.sendMessage(plr, C.NOT_VALID_BLOCK, args[1]);
return false;
2014-11-05 04:42:08 +01:00
}
2015-05-17 18:20:53 +02:00
if (MainUtil.runners.containsKey(plot)) {
MainUtil.sendMessage(plr, C.WAIT_FOR_TIMER);
return false;
}
MainUtil.runners.put(plot, 1);
2015-02-22 13:24:48 +01:00
manager.setComponent(plotworld, plot.id, component, blocks);
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.GENERATING_COMPONENT);
2015-05-17 18:20:53 +02:00
SetBlockQueue.addNotify(new Runnable() {
@Override
public void run() {
MainUtil.runners.remove(plot);
}
});
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-27 02:06:51 +01:00
ArrayList<String> newValues = new ArrayList<String>();
newValues.addAll(Arrays.asList(values));
newValues.addAll(Arrays.asList(manager.getPlotComponents(PS.get().getPlotWorld(loc.getWorld()), plot.id)));
2015-02-23 12:37:36 +01:00
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-07-17 12:48:13 +02:00
return StringMan.replaceAll(C.BLOCK_LIST_ITEM.s(), "%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-07-17 12:48:13 +02:00
builder.append(C.NEED_BIOME.s());
2015-02-22 13:24:48 +01:00
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
}