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

109 lines
5.1 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;
import com.intellectualcrafters.plot.PS;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
2015-02-22 13:51:02 +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;
2015-02-22 13:51:02 +01:00
import com.intellectualcrafters.plot.util.BlockManager;
2015-02-20 12:23:48 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.MathMan;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.Argument;
import com.plotsquared.general.commands.CommandDeclaration;
2014-11-09 12:51:17 +01:00
@CommandDeclaration(
command = "tp",
description = "Teleport to a plot",
permission = "plots.tp",
usage = "/plot tp <alias|id>",
requiredType = RequiredType.NONE,
category = CommandCategory.TELEPORT
)
2014-09-22 13:02:14 +02:00
public class TP extends SubCommand {
2014-11-05 04:42:08 +01:00
public TP() {
requiredArguments = new Argument[] {
Argument.String
};
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
@Override
2015-07-27 15:10:14 +02:00
public boolean onCommand(final PlotPlayer plr, final String[] args) {
2014-11-05 04:42:08 +01:00
final String id = args[0];
PlotId plotid;
2015-02-23 02:32:27 +01:00
final Location loc = plr.getLocation();
final String pworld = loc.getWorld();
2015-02-22 13:51:02 +01:00
String world = pworld;
2014-11-05 04:42:08 +01:00
if (args.length == 2) {
2015-02-22 13:51:02 +01:00
if (BlockManager.manager.isWorld(args[1])) {
world = args[1];
2014-11-05 04:42:08 +01:00
}
}
if (!PS.get().isPlotWorld(world)) {
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.NOT_IN_PLOT_WORLD);
2014-11-05 04:42:08 +01:00
return false;
}
Plot temp;
if ((temp = isAlias(world, id)) != null) {
2015-02-21 15:13:34 +01:00
MainUtil.teleportPlayer(plr, plr.getLocation(), temp);
2014-11-05 04:42:08 +01:00
return true;
}
try {
plotid = new PlotId(Integer.parseInt(id.split(";")[0]), Integer.parseInt(id.split(";")[1]));
2015-02-21 15:13:34 +01:00
MainUtil.teleportPlayer(plr, plr.getLocation(), MainUtil.getPlot(world, plotid));
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.NOT_VALID_PLOT_ID);
2014-11-05 04:42:08 +01:00
}
return false;
}
2015-02-23 02:32:27 +01:00
2015-02-22 13:51:02 +01:00
private Plot isAlias(final String world, String a) {
2014-11-05 04:42:08 +01:00
int index = 0;
if (a.contains(";")) {
final String[] split = a.split(";");
2015-07-30 16:25:16 +02:00
if ((split[1].length() > 0) && MathMan.isInteger(split[1])) {
2014-11-05 04:42:08 +01:00
index = Integer.parseInt(split[1]);
}
a = split[0];
}
2015-02-22 13:51:02 +01:00
final PlotPlayer player = UUIDHandler.getPlayer(a);
2014-11-05 04:42:08 +01:00
if (player != null) {
final java.util.Set<Plot> plotMainPlots = PS.get().getPlots(world, player);
2014-12-16 06:03:20 +01:00
final Plot[] plots = plotMainPlots.toArray(new Plot[plotMainPlots.size()]);
2014-11-05 04:42:08 +01:00
if (plots.length > index) {
return plots[index];
}
return null;
}
for (final Plot p : PS.get().getPlotsInWorld(world)) {
2015-07-21 20:31:12 +02:00
if ((p.getSettings().getAlias().length() > 0) && p.getSettings().getAlias().equalsIgnoreCase(a)) {
2014-11-05 04:42:08 +01:00
return p;
}
}
return null;
}
2014-09-22 13:02:14 +02:00
}