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

464 lines
20 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;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.events.PlotFlagAddEvent;
import com.intellectualcrafters.plot.events.PlotFlagRemoveEvent;
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;
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.PlotWorld;
import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper;
import com.intellectualcrafters.plot.util.StringComparison;
2014-12-18 03:15:11 +01:00
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
2014-12-18 03:15:11 +01:00
import org.bukkit.Material;
import org.bukkit.World;
2014-12-18 03:15:11 +01:00
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2014-09-22 13:02:14 +02:00
/**
* @author Citymonstret
*/
public class Set extends SubCommand {
2014-09-22 13:02:14 +02:00
2014-12-18 03:15:11 +01:00
public final static String[] values = new String[]{"biome", "wall", "wall_filling", "floor", "alias", "home", "flag"};
public final static String[] aliases = new String[]{"b", "w", "wf", "f", "a", "h", "fl"};
2014-11-09 12:51:17 +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);
}
2014-11-05 04:42:08 +01:00
@SuppressWarnings("deprecation")
@Override
public boolean execute(final Player plr, final String... args) {
if (!PlayerFunctions.isInPlot(plr)) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT);
return false;
}
final Plot plot = PlayerFunctions.getCurrentPlot(plr);
if (!plot.hasOwner()) {
sendMessage(plr, C.PLOT_NOT_CLAIMED);
2014-10-21 11:58:52 +02:00
return false;
}
2015-01-12 17:45:50 +01:00
if (!plot.hasRights(plr) && !PlotMain.hasPermission(plr, "plots.admin.command.set")) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
}
if (args.length < 1) {
2014-11-09 12:51:17 +01:00
PlayerFunctions.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + getArgumentList(values));
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;
if (!PlotMain.hasPermission(plr, "plots.set." + args[0].toLowerCase())) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.set." + args[0].toLowerCase());
return false;
2014-11-05 04:42:08 +01:00
}
if (args[0].equalsIgnoreCase("flag")) {
if (args.length < 2) {
String message = StringUtils.join(FlagManager.getFlags(plr), "&c, &6");
if (PlotMain.worldGuardListener != null) {
if (message.equals("")) {
message = StringUtils.join(PlotMain.worldGuardListener.str_flags, "&c, &6");
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
message += "," + StringUtils.join(PlotMain.worldGuardListener.str_flags, "&c, &6");
}
}
PlayerFunctions.sendMessage(plr, C.NEED_KEY.s().replaceAll("%values%", message));
return false;
}
2014-09-29 19:20:48 +02:00
2014-11-05 04:42:08 +01:00
AbstractFlag af;
2014-10-19 10:19:38 +02:00
2014-11-05 04:42:08 +01:00
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());
}
if (!FlagManager.getFlags().contains(af) && ((PlotMain.worldGuardListener == null) || !PlotMain.worldGuardListener.str_flags.contains(args[1].toLowerCase()))) {
PlayerFunctions.sendMessage(plr, C.NOT_VALID_FLAG);
return false;
}
if (!PlotMain.hasPermission(plr, "plots.set.flag." + args[1].toLowerCase())) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION);
return false;
}
if (args.length == 2) {
2015-01-08 15:08:50 +01:00
if (FlagManager.getPlotFlagAbs(plot, args[1].toLowerCase()) == null) {
2014-11-05 04:42:08 +01:00
if (PlotMain.worldGuardListener != null) {
if (PlotMain.worldGuardListener.str_flags.contains(args[1].toLowerCase())) {
PlotMain.worldGuardListener.removeFlag(plr, plr.getWorld(), plot, args[1]);
return false;
}
}
PlayerFunctions.sendMessage(plr, C.FLAG_NOT_IN_PLOT);
return false;
}
2015-01-08 15:08:50 +01:00
boolean result = FlagManager.removePlotFlag(plot, args[1].toLowerCase());
if (!result) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.FLAG_NOT_REMOVED);
return false;
}
PlayerFunctions.sendMessage(plr, C.FLAG_REMOVED);
PlotListener.plotEntry(plr, plot);
return true;
}
try {
String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ");
value = af.parseValue(value);
if (value == null) {
PlayerFunctions.sendMessage(plr, af.getValueDesc());
return false;
}
2014-09-29 19:20:48 +02:00
2014-11-05 04:42:08 +01:00
if ((FlagManager.getFlag(args[1].toLowerCase()) == null) && (PlotMain.worldGuardListener != null)) {
PlotMain.worldGuardListener.addFlag(plr, plr.getWorld(), plot, args[1], value);
return false;
}
final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value);
2015-01-08 15:08:50 +01:00
boolean result = FlagManager.addPlotFlag(plot, flag);
if (!result) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.FLAG_NOT_ADDED);
return false;
}
PlayerFunctions.sendMessage(plr, C.FLAG_ADDED);
PlotListener.plotEntry(plr, plot);
return true;
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, "&c" + e.getMessage());
return false;
}
}
if (args[0].equalsIgnoreCase("home")) {
if (args.length > 1) {
if (args[1].equalsIgnoreCase("none")) {
plot.settings.setPosition(null);
DBFunc.setPosition(plr.getWorld().getName(), plot, "");
return true;
2014-11-05 04:42:08 +01:00
}
return PlayerFunctions.sendMessage(plr, C.HOME_ARGUMENT);
2014-11-05 04:42:08 +01:00
}
//set to current location
World world = plr.getWorld();
Location base = PlotHelper.getPlotBottomLoc(world, plot.id);
int y = PlotHelper.getHeighestBlock(world, base.getBlockX(), base.getBlockZ());
base.setY(y);
Location relative = plr.getLocation().subtract(base);
BlockLoc blockloc = new BlockLoc(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ());
plot.settings.setPosition(blockloc);
DBFunc.setPosition(plr.getWorld().getName(), plot, relative.getBlockX() + "," + relative.getBlockY() + "," + relative.getBlockZ());
return PlayerFunctions.sendMessage(plr, C.POSITION_SET);
2014-11-05 04:42:08 +01:00
}
2014-11-05 04:42:08 +01:00
if (args[0].equalsIgnoreCase("alias")) {
if (args.length < 2) {
PlayerFunctions.sendMessage(plr, C.MISSING_ALIAS);
return false;
}
final String alias = args[1];
2015-01-04 05:43:36 +01:00
if (alias.length() >= 50) {
PlayerFunctions.sendMessage(plr, C.ALIAS_TOO_LONG);
return false;
}
2014-11-05 04:42:08 +01:00
for (final Plot p : PlotMain.getPlots(plr.getWorld()).values()) {
if (p.settings.getAlias().equalsIgnoreCase(alias)) {
PlayerFunctions.sendMessage(plr, C.ALIAS_IS_TAKEN);
return false;
}
if (Bukkit.getOfflinePlayer(alias).hasPlayedBefore()) {
PlayerFunctions.sendMessage(plr, C.ALIAS_IS_TAKEN);
return false;
}
}
DBFunc.setAlias(plr.getWorld().getName(), plot, alias);
PlayerFunctions.sendMessage(plr, C.ALIAS_SET_TO.s().replaceAll("%alias%", alias));
return true;
}
if (args[0].equalsIgnoreCase("biome")) {
if (args.length < 2) {
PlayerFunctions.sendMessage(plr, C.NEED_BIOME);
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;
}
2014-11-09 00:00:48 +01:00
2014-11-16 10:48:18 +01:00
final Biome biome = Biome.valueOf(new StringComparison(args[1], Biome.values()).getBestMatch());
2014-11-05 04:42:08 +01:00
/*
* for (Biome b : Biome.values()) {
* if (b.toString().equalsIgnoreCase(args[1])) {
* biome = b;
* break;
* }
* }
*/
2014-11-01 15:59:35 +01:00
2014-11-05 04:42:08 +01:00
if (biome == null) {
PlayerFunctions.sendMessage(plr, getBiomeList(Arrays.asList(Biome.values())));
return true;
}
PlotHelper.setBiome(plr.getWorld(), plot, biome);
PlayerFunctions.sendMessage(plr, C.BIOME_SET_TO.s() + biome.toString().toLowerCase());
return true;
}
if (args[0].equalsIgnoreCase("wall")) {
final PlotWorld plotworld = PlotMain.getWorldSettings(plr.getWorld());
if (plotworld == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT_WORLD);
return true;
}
if (args.length < 2) {
PlayerFunctions.sendMessage(plr, C.NEED_BLOCK);
return true;
}
if (args[1].length() < 2) {
2014-11-01 16:22:22 +01:00
sendMessage(plr, C.NAME_LITTLE, "Material", args[1].length() + "", "2");
return true;
}
2014-11-07 11:30:29 +01:00
Material material;
try {
material = getMaterial(args[1], PlotWorld.BLOCKS);
2014-12-18 03:15:11 +01:00
} catch (final NullPointerException e) {
2014-11-07 11:30:29 +01:00
material = null;
}
2014-11-05 04:42:08 +01:00
/*
* for (Material m : PlotWorld.BLOCKS) {
* if (m.toString().equalsIgnoreCase(args[1])) {
* material = m;
* break;
* }
* }
*/
if (material == null) {
PlayerFunctions.sendMessage(plr, getBlockList(PlotWorld.BLOCKS));
return true;
}
byte data = 0;
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
if (args.length > 2) {
try {
data = (byte) Integer.parseInt(args[2]);
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.NOT_VALID_DATA);
return true;
}
}
PlayerFunctions.sendMessage(plr, C.GENERATING_WALL);
PlotHelper.adjustWall(plr, plot, new PlotBlock((short) material.getId(), data));
return true;
}
if (args[0].equalsIgnoreCase("floor")) {
if (args.length < 2) {
PlayerFunctions.sendMessage(plr, C.NEED_BLOCK);
return true;
}
final PlotWorld plotworld = PlotMain.getWorldSettings(plr.getWorld());
if (plotworld == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT_WORLD);
return true;
}
//
2014-12-18 03:15:11 +01:00
@SuppressWarnings("unchecked") final ArrayList<Material> materials = (ArrayList<Material>) ((ArrayList<Material>) PlotWorld.BLOCKS).clone();
2014-11-05 04:42:08 +01:00
materials.add(Material.AIR);
//
final String[] strings = args[1].split(",");
//
int index = 0;
//
2014-11-09 12:51:17 +01:00
Material m;
2014-11-05 04:42:08 +01:00
//
final PlotBlock[] blocks = new PlotBlock[strings.length];
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
for (String s : strings) {
s = s.replaceAll(",", "");
final String[] ss = s.split(";");
ss[0] = ss[0].replaceAll(";", "");
if (ss[0].length() < 2) {
2014-11-01 16:22:22 +01:00
sendMessage(plr, C.NAME_LITTLE, "Material", ss[0].length() + "", "2");
return true;
}
2014-11-01 15:59:35 +01:00
m = getMaterial(ss[0], materials);
2014-11-05 04:42:08 +01:00
/*
* for (Material ma : materials) {
* if (ma.toString().equalsIgnoreCase(ss[0])) {
* m = ma;
* }
* }
*/
if (m == null) {
PlayerFunctions.sendMessage(plr, C.NOT_VALID_BLOCK);
return true;
}
if (ss.length == 1) {
2014-11-05 04:42:08 +01:00
blocks[index] = new PlotBlock((short) m.getId(), (byte) 0);
2014-12-18 03:15:11 +01:00
} else {
2014-11-09 12:51:17 +01:00
byte b;
2014-11-05 04:42:08 +01:00
try {
b = (byte) Integer.parseInt(ss[1]);
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.NOT_VALID_DATA);
return true;
}
blocks[index] = new PlotBlock((short) m.getId(), b);
}
index++;
}
PlotHelper.setFloor(plr, plot, blocks);
return true;
}
if (args[0].equalsIgnoreCase("wall_filling")) {
if (args.length < 2) {
PlayerFunctions.sendMessage(plr, C.NEED_BLOCK);
return true;
}
final PlotWorld plotworld = PlotMain.getWorldSettings(plr.getWorld());
if (plotworld == null) {
PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT_WORLD);
return true;
}
if (args[1].length() < 2) {
2014-11-01 16:22:22 +01:00
sendMessage(plr, C.NAME_LITTLE, "Material", args[1].length() + "", "2");
return true;
}
2014-11-05 04:42:08 +01:00
final Material material = getMaterial(args[1], PlotWorld.BLOCKS);
/*
* for (Material m : PlotWorld.BLOCKS) {
* if (m.toString().equalsIgnoreCase(args[1])) {
* material = m;
* break;
* }
* }
*/
2014-11-01 15:59:35 +01:00
2014-11-05 04:42:08 +01:00
if (material == null) {
PlayerFunctions.sendMessage(plr, getBlockList(PlotWorld.BLOCKS));
return true;
}
byte data = 0;
2014-11-05 04:42:08 +01:00
if (args.length > 2) {
try {
data = (byte) Integer.parseInt(args[2]);
2014-12-18 03:15:11 +01:00
} catch (final Exception e) {
2014-11-05 04:42:08 +01:00
PlayerFunctions.sendMessage(plr, C.NOT_VALID_DATA);
return true;
}
}
PlotHelper.adjustWallFilling(plr, plot, new PlotBlock((short) material.getId(), data));
return true;
}
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
}
2014-10-25 14:59:08 +02:00
plr.performCommand("plot set flag " + args[0] + a.toString());
2014-10-25 14:46:04 +02:00
return true;
}
}
PlayerFunctions.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + getArgumentList(values));
2014-11-05 04:42:08 +01:00
return false;
}
2014-11-05 04:42:08 +01:00
private String getMaterial(final Material m) {
return ChatColor.translateAlternateColorCodes('&', C.BLOCK_LIST_ITEM.s().replaceAll("%mat%", m.toString().toLowerCase()));
}
2014-11-05 04:42:08 +01:00
private String getBiome(final Biome b) {
return ChatColor.translateAlternateColorCodes('&', C.BLOCK_LIST_ITEM.s().replaceAll("%mat%", b.toString().toLowerCase()));
}
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
private String getString(final String s) {
return ChatColor.translateAlternateColorCodes('&', C.BLOCK_LIST_ITEM.s().replaceAll("%mat%", s));
}
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
private String getArgumentList(final String[] strings) {
final StringBuilder builder = new StringBuilder();
for (final String s : strings) {
builder.append(getString(s));
}
return builder.toString().substring(1, builder.toString().length() - 1);
}
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
private String getBiomeList(final List<Biome> biomes) {
final StringBuilder builder = new StringBuilder();
builder.append(ChatColor.translateAlternateColorCodes('&', C.NOT_VALID_BLOCK_LIST_HEADER.s()));
for (final Biome b : biomes) {
builder.append(getBiome(b));
}
return builder.toString().substring(1, builder.toString().length() - 1);
}
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
private Material getMaterial(final String input, final List<Material> blocks) {
2014-11-16 10:48:18 +01:00
return Material.valueOf(new StringComparison(input, blocks.toArray()).getBestMatch());
2014-11-01 15:59:35 +01:00
}
2014-11-05 04:42:08 +01:00
private String getBlockList(final List<Material> blocks) {
final StringBuilder builder = new StringBuilder();
builder.append(ChatColor.translateAlternateColorCodes('&', C.NOT_VALID_BLOCK_LIST_HEADER.s()));
for (final Material b : blocks) {
builder.append(getMaterial(b));
}
return builder.toString().substring(1, builder.toString().length() - 1);
}
2014-09-22 13:02:14 +02:00
}