Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 8c46c65a9ec94b8603e7494646e7be6d7632e949 0 parents
@sk89q sk89q authored
18 INSTALL.txt
@@ -0,0 +1,18 @@
+Installation
+------------
+
+This plugin requires Hey0's server modification.
+
+ 1. Create a "plugins" folder inside your "bin" folder.
+
+ 2. Copy CommandHelper.jar into "plugins".
+
+ 3. Add "CommandHelper" to the "plugins" line of your server.properties file.
+ If it's not already there, add the line. The line should look like this:
+
+ plugins=CommandHelper
+
+ If you have multiple plugins, separate plugin names with commas.
+
+ *** IMPORTANT: *** CommandHelper must be at the BEGINNING of that list
+ if you have other plugins installed.
166 LICENSE.txt
@@ -0,0 +1,166 @@
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
8 README.txt
@@ -0,0 +1,8 @@
+CommandHelper
+Copyright (c) 2010 sk89q <http://www.sk89q.com>
+Licensed under the GNU Lesser General Public License v3
+
+Introduction
+------------
+
+Command related tools.
3  manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
59 src/CommandHelper.java
@@ -0,0 +1,59 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * Entry point for the plugin for hey0's mod.
+ *
+ * @author sk89q
+ */
+public class CommandHelper extends Plugin {
+ /**
+ * Listener for the plugin system.
+ */
+ private static final CommandHelperListener listener =
+ new CommandHelperListener();
+
+ /**
+ * Initializes the plugin.
+ */
+ @Override
+ public void initialize() {
+ PluginLoader loader = etc.getLoader();
+
+ loader.addListener(PluginLoader.Hook.DISCONNECT, listener, this,
+ PluginListener.Priority.HIGH);
+ loader.addListener(PluginLoader.Hook.COMMAND, listener, this,
+ PluginListener.Priority.HIGH);
+ }
+
+ /**
+ * Enables the plugin.
+ */
+ @Override
+ public void enable() {
+ listener.loadGlobalAliases();
+ }
+
+ /**
+ * Disables the plugin.
+ */
+ @Override
+ public void disable() {
+ }
+}
388 src/CommandHelperListener.java
@@ -0,0 +1,388 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Arrays;
+import java.io.*;
+import com.sk89q.commandhelper.*;
+
+/**
+ * Event listener for Hey0's server mod.
+ *
+ * @author sk89q
+ */
+public class CommandHelperListener extends PluginListener {
+ /**
+ * Logger.
+ */
+ private static final Logger logger = Logger.getLogger("Minecraft");
+
+ /**
+ * Sessions.
+ */
+ private Map<String,CommandHelperSession> sessions =
+ new HashMap<String,CommandHelperSession>();
+ /**
+ * List of global aliases.
+ */
+ private Map<String,String[]> globalAliases =
+ new HashMap<String,String[]>();
+
+ /**
+ * Load global aliases.
+ */
+ public void loadGlobalAliases() {
+ globalAliases = CommandHelperSession.readAliases("global-aliases.txt");
+ }
+
+ /**
+ * Find a global alias. May return null.
+ *
+ * @param command
+ * @return
+ */
+ public String[] findGlobalAlias(String command) {
+ return globalAliases.get(command.toLowerCase());
+ }
+
+ /**
+ * Get session.
+ *
+ * @param player
+ * @return
+ */
+ private CommandHelperSession getSession(Player player) {
+ if (sessions.containsKey(player.getName())) {
+ return sessions.get(player.getName());
+ } else {
+ CommandHelperSession session = new CommandHelperSession(player.getName());
+ sessions.put(player.getName(), session);
+ return session;
+ }
+ }
+
+ /**
+ * Checks to make sure that there are enough but not too many arguments.
+ *
+ * @param args
+ * @param min
+ * @param max -1 for no maximum
+ * @param cmd command name
+ * @throws InsufficientArgumentsException
+ */
+ private void checkArgs(String[] args, int min, int max, String cmd)
+ throws InsufficientArgumentsException {
+ if (args.length <= min) {
+ throw new InsufficientArgumentsException("Minimum " + min + " arguments");
+ } else if (max != -1 && args.length - 1 > max) {
+ throw new InsufficientArgumentsException("Maximum " + max + " arguments");
+ }
+ }
+
+ /**
+ *
+ * @param player
+ * @param split
+ * @return whether the command was processed
+ */
+ @Override
+ public boolean onCommand(Player player, String[] split) {
+ try {
+ return runCommand(player, split);
+ } catch (InsufficientArgumentsException e) {
+ player.sendMessage(Colors.Rose + e.getMessage());
+ return true;
+ }
+ }
+
+ /**
+ * Runs commands.
+ *
+ * @param player
+ * @param split
+ * @return
+ */
+ private boolean runCommand(Player player, String[] split) throws InsufficientArgumentsException {
+ CommandHelperSession session = getSession(player);
+
+ // Repeat command
+ if (split[0].equals("/.")) {
+ if (session.getLastCommand() != null) {
+ player.sendMessage(Colors.LightGray + session.getLastCommand());
+ execCommand(player, session.getLastCommand());
+ } else {
+ player.sendMessage(Colors.Rose + "No previous command.");
+ }
+ return true;
+
+ // Each group
+ } else if (split[0].equalsIgnoreCase("/each")
+ && player.canUseCommand("/each")) {
+ checkArgs(split, 2, -1, split[0]);
+
+ PlayerFilter filter = SimplePlayerFilter.parse(split[1]);
+
+ // Get arguments
+ String[] newSplit = new String[split.length - 2];
+ System.arraycopy(split, 2, newSplit, 0, split.length - 2);
+
+ // Perform command
+ int affected = 0;
+ for (Player pl : filter) {
+ affected++;
+
+ // Substitute
+ for (int i = 2; i < split.length; i++) {
+ if (split[i].equals("^")) {
+ newSplit[i - 2] = pl.getName();
+ }
+ }
+
+ execCommand(player, joinString(newSplit, " "));
+ }
+
+ // Tell the user if no users were matched
+ if (affected == 0) {
+ player.sendMessage(Colors.Rose + "No users were matched.");
+ }
+
+ return true;
+
+ // Perform
+ } else if (split[0].equalsIgnoreCase("/perform")
+ && player.canUseCommand("/perform")) {
+ checkArgs(split, 2, -1, split[0]);
+
+ PlayerFilter filter = SimplePlayerFilter.parse(split[1]);
+
+ // Get arguments
+ String[] newSplit = new String[split.length - 2];
+ System.arraycopy(split, 2, newSplit, 0, split.length - 2);
+
+ // Perform command
+ int affected = 0;
+ for (Player pl : filter) {
+ affected++;
+
+ // Substitute
+ for (int i = 2; i < split.length; i++) {
+ if (split[i].equals("^")) {
+ newSplit[i - 2] = pl.getName();
+ }
+ }
+
+ execCommand(pl, joinString(newSplit, " "));
+ }
+
+ // Tell the user if no users were matched
+ if (affected == 0) {
+ player.sendMessage(Colors.Rose + "No users were matched.");
+ }
+
+ return true;
+
+ // Save alias
+ } else if (split[0].equalsIgnoreCase("/alias")
+ && player.canUseCommand("/alias")) {
+ checkArgs(split, 2, -1, split[0]);
+
+ // Get alias name
+ String aliasName = split[1];
+ if (aliasName.charAt(0) != '/') {
+ aliasName = "/" + aliasName;
+ }
+
+ // Get arguments
+ String[] newSplit = new String[split.length - 2];
+ System.arraycopy(split, 2, newSplit, 0, split.length - 2);
+
+ // Set alias
+ String[] commands = new String[]{ joinString(newSplit, " ") };
+ getSession(player).setAlias(aliasName, commands);
+
+ player.sendMessage(Colors.Yellow + "Alias " + aliasName + " set.");
+ session.saveAliases();
+
+ return true;
+
+ // Delete alias
+ } else if (split[0].equalsIgnoreCase("/delalias")
+ && player.canUseCommand("/alias")) {
+ checkArgs(split, 1, 1, split[0]);
+
+ // Get alias name
+ String aliasName = split[1];
+ if (aliasName.charAt(0) != '/') {
+ aliasName = "/" + aliasName;
+ }
+
+ getSession(player).removeAlias(aliasName);
+
+ player.sendMessage(Colors.Yellow + "Alias " + aliasName + " removed.");
+ session.saveAliases();
+
+ return true;
+
+ // Reload global aliases
+ } else if (split[0].equalsIgnoreCase("/reloadaliases")
+ && player.canUseCommand("/reloadaliases")) {
+ checkArgs(split, 0, 0, split[0]);
+
+ loadGlobalAliases();
+
+ player.sendMessage(Colors.Yellow + "Aliases reloaded.");
+ session.saveAliases();
+
+ return true;
+
+ // Catch aliases
+ } else {
+ session.setLastCommand(joinString(split, " "));
+
+ if (player.canUseCommand(split[0])) {
+ String[] commands = getSession(player).findAlias(split[0]);
+ String[] arguments = new String[split.length - 1];
+ System.arraycopy(split, 1, arguments, 0, split.length - 1);
+
+ if (commands != null) {
+ execCommands(player, commands, arguments, false);
+ return true;
+ } else {
+ commands = findGlobalAlias(split[0]);
+
+ if (commands != null) {
+ execCommands(player, commands, arguments, true);
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ *
+ * @param player
+ */
+ @Override
+ public void onDisconnect(Player player) {
+ sessions.remove(player.getName());
+ }
+
+ /**
+ * Execute a command.
+ *
+ * @param cmd
+ */
+ private void execCommand(Player player, String cmd) {
+ player.getUser().a.a(new bg(cmd));
+ }
+
+ /**
+ * Execute a command.
+ *
+ * @param cmd
+ */
+ private void execScriptableCommand(Player player, String cmd) {
+ if (cmd.charAt(0) != '@') {
+ execCommand(player, cmd);
+ return;
+ }
+
+ String[] args = cmd.split(" ");
+
+ if (args[0].equalsIgnoreCase("@read")) {
+ if (args.length >= 2) {
+ try {
+ String[] newArgs = new String[args.length - 1];
+ System.arraycopy(args, 1, newArgs, 0, args.length - 1);
+ FileReader input = new FileReader(joinString(newArgs, " "));
+ BufferedReader reader = new BufferedReader(input);
+ String line;
+ while ((line = reader.readLine()) != null) {
+ player.sendMessage(line);
+ }
+ } catch (IOException e) {
+ logger.log(Level.WARNING, "@read: Could not read "
+ + args[1] + ": " + e.getMessage());
+ }
+ } else {
+ logger.log(Level.WARNING, "@read requires 2 arguments");
+ }
+ } else {
+ logger.log(Level.WARNING, "Unknown CommandHelper instruction: "
+ + args[0]);
+ }
+ }
+
+ /**
+ * Execute a command.
+ *
+ * @param cmd
+ */
+ private void execCommands(Player player, String[] commands,
+ String[] args, boolean scriptable) {
+ for (String cmd : commands) {
+ String[] parts = cmd.split(" ");
+
+ for (int i = 0; i < parts.length; i++) {
+ if (parts[i].matches("%[0-9]+")) {
+ int n = Integer.parseInt(parts[i].substring(1)) - 1;
+ if (n < args.length && n >= 0) {
+ parts[i] = args[n];
+ } else {
+ parts[i] = "";
+ }
+ }
+ }
+
+ cmd = joinString(parts, " ");
+
+ if (scriptable) {
+ execScriptableCommand(player, cmd);
+ } else {
+ execCommand(player, cmd);
+ }
+ }
+ }
+
+ /**
+ * Joins a string from an array of strings.
+ *
+ * @param str
+ * @param delimiter
+ * @return
+ */
+ public static String joinString(String[] str, String delimiter) {
+ if (str.length == 0) {
+ return "";
+ }
+ StringBuilder buffer = new StringBuilder(str[0]);
+ for (int i = 1; i < str.length; i++) {
+ buffer.append(delimiter).append(str[i]);
+ }
+ return buffer.toString();
+ }
+}
255 src/CommandHelperSession.java
@@ -0,0 +1,255 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+import java.io.*;
+
+/**
+ * Command history, etc.
+ *
+ * @author sk89q
+ */
+public class CommandHelperSession {
+ /**
+ * Logger.
+ */
+ private static final Logger logger = Logger.getLogger("Minecraft");
+
+ /**
+ * Player name.
+ */
+ private String name;
+ /**
+ * Last command used.
+ */
+ private String lastCommand;
+ /**
+ * List of aliases.
+ */
+ private Map<String,String[]> aliases =
+ new HashMap<String,String[]>();
+
+ /**
+ * Construct the instance.
+ *
+ * @param name
+ */
+ public CommandHelperSession(String name) {
+ this.name = name;
+ loadAliases();
+ }
+
+ /**
+ * @return the last command
+ */
+ public String getLastCommand() {
+ return lastCommand;
+ }
+
+ /**
+ * @param lastCommand the last command to set
+ */
+ public void setLastCommand(String lastCommand) {
+ this.lastCommand = lastCommand;
+ }
+
+ /**
+ * Find an alias. May return null.
+ *
+ * @param command
+ * @return
+ */
+ public String[] findAlias(String command) {
+ return aliases.get(command.toLowerCase());
+ }
+
+ /**
+ * Set an alias.
+ *
+ * @param command
+ * @param split
+ * @return
+ */
+ public void setAlias(String command, String[] commands) {
+ aliases.put(command.toLowerCase(), commands);
+ }
+
+ /**
+ * Remove an alias.
+ *
+ * @param command
+ */
+ public void removeAlias(String command) {
+ aliases.remove(command.toLowerCase());
+ }
+
+ /**
+ * Returns true if the player's name is valid.
+ *
+ * @return
+ */
+ private boolean isValidName() {
+ if (name.length() < 1 || name.length() > 40) {
+ return false;
+ }
+ if (name.matches("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]")) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Load aliases.
+ */
+ public void loadAliases() {
+ if (!isValidName()) { return; }
+ aliases = readAliases("aliases" + File.separator + name + ".txt");
+ }
+
+ /**
+ * Save aliases.
+ */
+ public void saveAliases() {
+ if (!isValidName()) { return; }
+ writeAliases("aliases" + File.separator + name + ".txt", aliases);
+ }
+
+ /**
+ * Read a file containing cauldron recipes.
+ *
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public static Map<String,String[]> readAliases(String path) {
+ File file = new File(path);
+ FileReader input = null;
+ Map<String,List<String>> aliases = new HashMap<String,List<String>>();
+ String alias = null;
+
+ try {
+ input = new FileReader(file);
+ BufferedReader buff = new BufferedReader(input);
+
+ String line;
+ while ((line = buff.readLine()) != null) {
+ line = line.trim();
+
+ // Blank lines
+ if (line.length() == 0) {
+ continue;
+ }
+
+ // Comment
+ if (line.charAt(0) == ';' || line.charAt(0) == '#' || line.equals("")) {
+ continue;
+ }
+
+ // Alias
+ if (line.charAt(0) == ':') {
+ alias = line.substring(1).toLowerCase();
+
+ // Alias contents
+ } else if (alias == null) {
+ logger.log(Level.WARNING, "Alias command '" + line
+ + "' not under any alias");
+ } else {
+ List<String> commands;
+ if (!aliases.containsKey(alias)) {
+ commands = new ArrayList<String>();
+ aliases.put(alias, commands);
+ } else {
+ commands = aliases.get(alias);
+ }
+ commands.add(line);
+ }
+ }
+
+ Map<String,String[]> outAliases = new HashMap<String,String[]>();
+
+ for (Map.Entry<String,List<String>> entry : aliases.entrySet()) {
+ outAliases.put(entry.getKey(), entry.getValue().toArray(new String[]{}));
+ }
+
+ return outAliases;
+ } catch (FileNotFoundException e) {
+ return new HashMap<String,String[]>();
+ } catch (IOException e) {
+ logger.log(Level.WARNING, "Failed to load aliases: "
+ + e.getMessage());
+ return new HashMap<String,String[]>();
+ } finally {
+ try {
+ if (input != null) {
+ input.close();
+ }
+ } catch (IOException e2) {
+ }
+ }
+ }
+
+ /**
+ * Write aliases to file.
+ *
+ * @param path
+ * @param aliases
+ */
+ public static void writeAliases(String path, Map<String,String[]> aliases) {
+ File file = new File(path);
+ FileWriter output = null;
+
+ // Make parent directory
+ String parentPath = file.getParent();
+ if (parentPath != null) {
+ (new File(parentPath)).mkdirs();
+ }
+
+ try {
+ output = new FileWriter(file);
+ BufferedWriter buff = new BufferedWriter(output);
+ buff.write("# Generated automatically\r\n");
+ buff.write("# Manual changes will likely be overwritten\r\n");
+
+ for (Map.Entry<String,String[]> entry : aliases.entrySet()) {
+ buff.write(":" + entry.getKey() + "\r\n");
+
+ for (String command : entry.getValue()) {
+ buff.write(command + "\r\n");
+ }
+ }
+
+ buff.close();
+ } catch (IOException e) {
+ logger.log(Level.WARNING, "Failed to write aliases: "
+ + e.getMessage());
+ } finally {
+ try {
+ if (output != null) {
+ output.close();
+ }
+ } catch (IOException e2) {
+ }
+ }
+ }
+}
37 src/FriendlyFilter.java
@@ -0,0 +1,37 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ *
+ * @author sk89q
+ */
+public class FriendlyFilter extends PlayerFilter {
+ /**
+ * Checks to see if a player matches this query.
+ *
+ * @param player
+ * @return
+ */
+ public boolean matches(Player player) {
+ return true;
+ }
+}
62 src/PlayerFilter.java
@@ -0,0 +1,62 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+/**
+ *
+ * @author sk89q
+ */
+public abstract class PlayerFilter implements Iterable<Player> {
+ /**
+ * Evaluate the query, getting a list of players.
+ *
+ * @return
+ */
+ public List<Player> evaluate() {
+ List<Player> players = new ArrayList<Player>();
+
+ for (Player player : etc.getServer().getPlayerList()) {
+ if (matches(player)) {
+ players.add(player);
+ }
+ }
+
+ return players;
+ }
+
+ /**
+ * Returns true if this query matches this player.
+ *
+ * @param player
+ * @return
+ */
+ public abstract boolean matches(Player player);
+
+ /**
+ * Returns an iterator.
+ *
+ * @return
+ */
+ public Iterator<Player> iterator() {
+ return evaluate().iterator();
+ }
+}
119 src/SimplePlayerFilter.java
@@ -0,0 +1,119 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ *
+ * @author sk89q
+ */
+public class SimplePlayerFilter extends PlayerFilter {
+ /**
+ * Store a list of groups to implicitly match a player against.
+ */
+ private Set<String> groups;
+ /**
+ * Store a list of groups to explicitly match a player against.
+ */
+ private Set<String> exclusiveGroups;
+ /**
+ * Store a list of player names to match against.
+ */
+ private Set<String> players;
+
+ /**
+ * Construct the object.
+ *
+ * @param groups
+ * @param exclusiveGroups
+ * @param players
+ */
+ private SimplePlayerFilter(Set<String> groups, Set<String> exclusiveGroups,
+ Set<String> players) {
+ this.groups = groups;
+ this.exclusiveGroups = exclusiveGroups;
+ this.players = players;
+ }
+
+ /**
+ * Checks to see if a player matches this query.
+ *
+ * @param player
+ * @return
+ */
+ public boolean matches(Player player) {
+ String name = player.getName();
+
+ if (players.contains(name)) {
+ return true;
+ }
+
+ String[] playerGroups = player.getGroups();
+ int numGroups = playerGroups.length;
+
+ if (numGroups == 1) {
+ if (exclusiveGroups.contains(playerGroups[0])) {
+ return true;
+ }
+ }
+
+ for (String group : playerGroups) {
+ if (groups.contains(group)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Parse a string.
+ *
+ * @param query
+ * @return
+ */
+ public static PlayerFilter parse(String query) {
+ if (query.equals("*")) {
+ return new FriendlyFilter();
+ }
+
+ String[] parts = query.split(",");
+
+ Set<String> groups = new HashSet<String>();
+ Set<String> exclusiveGroups = new HashSet<String>();
+ Set<String> players = new HashSet<String>();
+
+ for (String part : parts) {
+ if (part.length() == 0) {
+ continue;
+
+ }
+ if (part.charAt(0) == '+') {
+ exclusiveGroups.add(part);
+ } else if (part.charAt(0) == '~') {
+ players.add(part);
+ } else {
+ groups.add(part);
+ }
+ }
+
+ return new SimplePlayerFilter(groups, exclusiveGroups, players);
+ }
+}
30 src/com/sk89q/commandhelper/InsufficientArgumentsException.java
@@ -0,0 +1,30 @@
+// $Id$
+/*
+ * CommandHelper
+ * Copyright (C) 2010 sk89q <http://www.sk89q.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+*/
+
+package com.sk89q.commandhelper;
+
+/**
+ *
+ * @author sk89q
+ */
+public class InsufficientArgumentsException extends Exception {
+ public InsufficientArgumentsException(String error) {
+ super(error);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.