Add -c flag to '/npc anchor --save anchor_name' to save location of the cursor.

This commit is contained in:
Jeremy Schroeder 2012-12-27 22:28:39 -05:00
parent b8878eda5d
commit d6c6b3864f
2 changed files with 1290 additions and 1272 deletions

View File

@ -1,242 +1,252 @@
// $Id$ // $Id$
/* /*
* Copyright (C) 2010 sk89q <http://www.sk89q.com> * Copyright (C) 2010 sk89q <http://www.sk89q.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package net.citizensnpcs.command; package net.citizensnpcs.command;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
public class CommandContext { public class CommandContext {
protected String[] args; protected String[] args;
protected final Set<Character> flags = new HashSet<Character>(); protected final Set<Character> flags = new HashSet<Character>();
private Location location = null; private Location location = null;
private final CommandSender sender; private final CommandSender sender;
protected final Map<String, String> valueFlags = Maps.newHashMap(); protected final Map<String, String> valueFlags = Maps.newHashMap();
public CommandContext(CommandSender sender, String[] args) { public CommandContext(CommandSender sender, String[] args) {
this.sender = sender; this.sender = sender;
int i = 1; int i = 1;
for (; i < args.length; i++) { for (; i < args.length; i++) {
// initial pass for quotes // initial pass for quotes
args[i] = args[i].trim(); args[i] = args[i].trim();
if (args[i].length() == 0) { if (args[i].length() == 0) {
// Ignore this // Ignore this
continue; continue;
} else if (args[i].charAt(0) == '\'' || args[i].charAt(0) == '"') { } else if (args[i].charAt(0) == '\'' || args[i].charAt(0) == '"') {
char quote = args[i].charAt(0); char quote = args[i].charAt(0);
String quoted = args[i].substring(1); // remove initial quote String quoted = args[i].substring(1); // remove initial quote
for (int inner = i + 1; inner < args.length; inner++) { for (int inner = i + 1; inner < args.length; inner++) {
if (args[inner].isEmpty()) if (args[inner].isEmpty())
continue; continue;
String test = args[inner].trim(); String test = args[inner].trim();
quoted += " " + test; quoted += " " + test;
if (test.charAt(test.length() - 1) == quote) { if (test.charAt(test.length() - 1) == quote) {
args[i] = quoted.substring(0, quoted.length() - 1); args[i] = quoted.substring(0, quoted.length() - 1);
for (int j = i + 1; j != inner; ++j) for (int j = i + 1; j != inner; ++j)
args[j] = ""; args[j] = "";
// remove ending quote // remove ending quote
break; break;
} }
} }
} }
} }
for (i = 1; i < args.length; ++i) { for (i = 1; i < args.length; ++i) {
// second pass for flags // second pass for flags
if (args[i].length() == 0) if (args[i].length() == 0)
continue; continue;
if (i + 1 < args.length && args[i].length() > 2 && args[i].matches("^--[a-zA-Z]+$")) { if (i + 1 < args.length && args[i].length() > 2 && args[i].matches("^--[a-zA-Z]+$")) {
int inner = i + 1; int inner = i + 1;
while (args[inner].length() == 0) { while (args[inner].length() == 0) {
// later args may have been quoted // later args may have been quoted
++inner; ++inner;
if (inner >= args.length) { if (inner >= args.length) {
inner = -1; inner = -1;
break; break;
} }
} }
if (inner != -1) { if (inner != -1) {
valueFlags.put(args[i].toLowerCase().replaceFirst("--", ""), args[inner]); valueFlags.put(args[i].toLowerCase().replaceFirst("--", ""), args[inner]);
args[i] = ""; args[i] = "";
args[inner] = ""; args[inner] = "";
} }
} else if (args[i].charAt(0) == '-' && args[i].matches("^-[a-zA-Z]+$")) { } else if (args[i].charAt(0) == '-' && args[i].matches("^-[a-zA-Z]+$")) {
for (int k = 1; k < args[i].length(); k++) for (int k = 1; k < args[i].length(); k++)
flags.add(args[i].charAt(k)); flags.add(args[i].charAt(k));
args[i] = ""; args[i] = "";
} }
} }
List<String> copied = Lists.newArrayList(); List<String> copied = Lists.newArrayList();
for (String arg : args) { for (String arg : args) {
arg = arg.trim(); arg = arg.trim();
if (arg == null || arg.isEmpty()) if (arg == null || arg.isEmpty())
continue; continue;
copied.add(arg.trim()); copied.add(arg.trim());
} }
this.args = copied.toArray(new String[copied.size()]); this.args = copied.toArray(new String[copied.size()]);
} }
public CommandContext(String[] args) { public CommandContext(String[] args) {
this(null, args); this(null, args);
} }
public int argsLength() { public int argsLength() {
return args.length - 1; return args.length - 1;
} }
public String getCommand() { public String getCommand() {
return args[0]; return args[0];
} }
public double getDouble(int index) throws NumberFormatException { public double getDouble(int index) throws NumberFormatException {
return Double.parseDouble(args[index + 1]); return Double.parseDouble(args[index + 1]);
} }
public double getDouble(int index, double def) throws NumberFormatException { public double getDouble(int index, double def) throws NumberFormatException {
return index + 1 < args.length ? Double.parseDouble(args[index + 1]) : def; return index + 1 < args.length ? Double.parseDouble(args[index + 1]) : def;
} }
public String getFlag(String ch) { public String getFlag(String ch) {
return valueFlags.get(ch); return valueFlags.get(ch);
} }
public String getFlag(String ch, String def) { public String getFlag(String ch, String def) {
final String value = valueFlags.get(ch); final String value = valueFlags.get(ch);
if (value == null) { if (value == null) {
return def; return def;
} }
return value; return value;
} }
public double getFlagDouble(String ch) throws NumberFormatException { public double getFlagDouble(String ch) throws NumberFormatException {
return Double.parseDouble(valueFlags.get(ch)); return Double.parseDouble(valueFlags.get(ch));
} }
public double getFlagDouble(String ch, double def) throws NumberFormatException { public double getFlagDouble(String ch, double def) throws NumberFormatException {
final String value = valueFlags.get(ch); final String value = valueFlags.get(ch);
if (value == null) { if (value == null) {
return def; return def;
} }
return Double.parseDouble(value); return Double.parseDouble(value);
} }
public int getFlagInteger(String ch) throws NumberFormatException { public int getFlagInteger(String ch) throws NumberFormatException {
return Integer.parseInt(valueFlags.get(ch)); return Integer.parseInt(valueFlags.get(ch));
} }
public int getFlagInteger(String ch, int def) throws NumberFormatException { public int getFlagInteger(String ch, int def) throws NumberFormatException {
final String value = valueFlags.get(ch); final String value = valueFlags.get(ch);
if (value == null) { if (value == null) {
return def; return def;
} }
return Integer.parseInt(value); return Integer.parseInt(value);
} }
public Set<Character> getFlags() { public Set<Character> getFlags() {
return flags; return flags;
} }
public int getInteger(int index) throws NumberFormatException { public int getInteger(int index) throws NumberFormatException {
return Integer.parseInt(args[index + 1]); return Integer.parseInt(args[index + 1]);
} }
public int getInteger(int index, int def) throws NumberFormatException { public int getInteger(int index, int def) throws NumberFormatException {
if (index + 1 < args.length) { if (index + 1 < args.length) {
try { try {
return Integer.parseInt(args[index + 1]); return Integer.parseInt(args[index + 1]);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
} }
} }
return def; return def;
} }
public String getJoinedStrings(int initialIndex) { public String getJoinedStrings(int initialIndex) {
return getJoinedStrings(initialIndex, ' '); return getJoinedStrings(initialIndex, ' ');
} }
public String getJoinedStrings(int initialIndex, char delimiter) { public String getJoinedStrings(int initialIndex, char delimiter) {
initialIndex = initialIndex + 1; initialIndex = initialIndex + 1;
StringBuilder buffer = new StringBuilder(args[initialIndex]); StringBuilder buffer = new StringBuilder(args[initialIndex]);
for (int i = initialIndex + 1; i < args.length; i++) for (int i = initialIndex + 1; i < args.length; i++)
buffer.append(delimiter).append(args[i]); buffer.append(delimiter).append(args[i]);
return buffer.toString().trim(); return buffer.toString().trim();
} }
public String[] getPaddedSlice(int index, int padding) { public String[] getPaddedSlice(int index, int padding) {
String[] slice = new String[args.length - index + padding]; String[] slice = new String[args.length - index + padding];
System.arraycopy(args, index, slice, padding, args.length - index); System.arraycopy(args, index, slice, padding, args.length - index);
return slice; return slice;
} }
public Location getSenderLocation() { public Location getSenderLocation() {
if (location != null || sender == null) if (location != null || sender == null)
return location; return location;
if (sender instanceof Player) if (sender instanceof Player)
location = ((Player) sender).getLocation(); location = ((Player) sender).getLocation();
else if (sender instanceof BlockCommandSender) else if (sender instanceof BlockCommandSender)
location = ((BlockCommandSender) sender).getBlock().getLocation(); location = ((BlockCommandSender) sender).getBlock().getLocation();
return location; return location;
} }
public String[] getSlice(int index) { public Location getSenderTargetBlockLocation() {
String[] slice = new String[args.length - index]; if (location != null || sender == null)
System.arraycopy(args, index, slice, 0, args.length - index); return location;
return slice; if (sender instanceof Player)
} location = ((Player) sender).getTargetBlock(null, 50).getLocation();
else if (sender instanceof BlockCommandSender)
public String getString(int index) { location = ((BlockCommandSender) sender).getBlock().getLocation();
return args[index + 1]; return location;
} }
public String getString(int index, String def) { public String[] getSlice(int index) {
return index + 1 < args.length ? args[index + 1] : def; String[] slice = new String[args.length - index];
} System.arraycopy(args, index, slice, 0, args.length - index);
return slice;
public Map<String, String> getValueFlags() { }
return valueFlags;
} public String getString(int index) {
return args[index + 1];
public boolean hasFlag(char ch) { }
return flags.contains(ch);
} public String getString(int index, String def) {
return index + 1 < args.length ? args[index + 1] : def;
public boolean hasValueFlag(String ch) { }
return valueFlags.containsKey(ch);
} public Map<String, String> getValueFlags() {
return valueFlags;
public int length() { }
return args.length;
} public boolean hasFlag(char ch) {
return flags.contains(ch);
public boolean matches(String command) { }
return args[0].equalsIgnoreCase(command);
} public boolean hasValueFlag(String ch) {
return valueFlags.containsKey(ch);
}
public int length() {
return args.length;
}
public boolean matches(String command) {
return args[0].equalsIgnoreCase(command);
}
} }

File diff suppressed because it is too large Load Diff