Update favs
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
package com.thevoxelbox.voxelsniper.jsap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.martiansoftware.jsap.JSAP;
|
||||
import com.martiansoftware.jsap.JSAPException;
|
||||
import com.martiansoftware.jsap.JSAPResult;
|
||||
import com.martiansoftware.jsap.Switch;
|
||||
import com.martiansoftware.util.StringUtils;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* JSAP parser with help generating code.
|
||||
*
|
||||
* @author MikeMatrix
|
||||
*/
|
||||
public class HelpJSAP extends JSAP
|
||||
{
|
||||
|
||||
private String name;
|
||||
private String explanation;
|
||||
private int screenWidth;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param explanation
|
||||
* @param screenWidth
|
||||
*/
|
||||
public HelpJSAP(final String name, final String explanation, final int screenWidth)
|
||||
{
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
this.explanation = explanation;
|
||||
this.screenWidth = screenWidth;
|
||||
|
||||
try
|
||||
{
|
||||
this.registerParameter(new Switch("help", JSAP.NO_SHORTFLAG, "help", "Displays this help page."));
|
||||
}
|
||||
catch (final JSAPException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param explanation
|
||||
* @param screenWidth
|
||||
* @param resourceName
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
* if an I/O error occurs
|
||||
* @throws com.martiansoftware.jsap.JSAPException
|
||||
* if the configuration is not valid
|
||||
*/
|
||||
public HelpJSAP(final String name, final String explanation, final int screenWidth, final String resourceName) throws IOException, JSAPException
|
||||
{
|
||||
super(resourceName);
|
||||
|
||||
this.name = name;
|
||||
this.explanation = explanation;
|
||||
this.screenWidth = screenWidth;
|
||||
|
||||
try
|
||||
{
|
||||
this.registerParameter(new Switch("help", JSAP.NO_SHORTFLAG, "help", "Displays this help page."));
|
||||
}
|
||||
catch (final JSAPException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param explanation
|
||||
* @param screenWidth
|
||||
* @param jsapXML
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
* if an I/O error occurs
|
||||
* @throws com.martiansoftware.jsap.JSAPException
|
||||
* if the configuration is not valid
|
||||
*/
|
||||
public HelpJSAP(final String name, final String explanation, final int screenWidth, final URL jsapXML) throws IOException, JSAPException
|
||||
{
|
||||
super(jsapXML);
|
||||
|
||||
this.name = name;
|
||||
this.explanation = explanation;
|
||||
this.screenWidth = screenWidth;
|
||||
|
||||
try
|
||||
{
|
||||
this.registerParameter(new Switch("help", JSAP.NO_SHORTFLAG, "help", "Displays this help page."));
|
||||
}
|
||||
catch (final JSAPException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the explanation
|
||||
*/
|
||||
public final String getExplanation()
|
||||
{
|
||||
return this.explanation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param explanation
|
||||
* the explanation to set
|
||||
*/
|
||||
public final void setExplanation(final String explanation)
|
||||
{
|
||||
this.explanation = explanation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public final String getName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* the name to set
|
||||
*/
|
||||
public final void setName(final String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the screenWidth
|
||||
*/
|
||||
public final int getScreenWidth()
|
||||
{
|
||||
return this.screenWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param screenWidth
|
||||
* the screenWidth to set
|
||||
*/
|
||||
public final void setScreenWidth(final int screenWidth)
|
||||
{
|
||||
this.screenWidth = screenWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param jsapResult
|
||||
*
|
||||
* @return if something has been written on writer.
|
||||
*/
|
||||
public final List<String> writeHelpOrErrorMessageIfRequired(final JSAPResult jsapResult)
|
||||
{
|
||||
if (!(jsapResult.success()) || jsapResult.getBoolean("help"))
|
||||
{
|
||||
List<String> returnValue = new LinkedList<String>();
|
||||
// To avoid spurious missing argument errors we never print errors if help is required.
|
||||
if (!jsapResult.getBoolean("help"))
|
||||
{
|
||||
for (final Iterator<?> err = jsapResult.getErrorMessageIterator(); err.hasNext(); )
|
||||
{
|
||||
returnValue.add(ChatColor.RED + "Error: " + ChatColor.DARK_RED + err.next());
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
returnValue.add(ChatColor.GOLD + "Usage:");
|
||||
|
||||
List<?> l = StringUtils.wrapToList(this.name + " " + this.getUsage(), this.screenWidth);
|
||||
for (final Object aL : l)
|
||||
{
|
||||
returnValue.add(" " + aL.toString());
|
||||
}
|
||||
|
||||
if (this.explanation != null)
|
||||
{
|
||||
returnValue.add("");
|
||||
l = StringUtils.wrapToList(this.explanation, this.screenWidth);
|
||||
for (final Object aL : l)
|
||||
{
|
||||
final String next = (String) aL;
|
||||
returnValue.add(ChatColor.AQUA + next);
|
||||
}
|
||||
}
|
||||
|
||||
returnValue.add("");
|
||||
returnValue.add(this.getHelp(this.screenWidth));
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.thevoxelbox.voxelsniper.jsap;
|
||||
|
||||
import com.martiansoftware.jsap.ParseException;
|
||||
import com.martiansoftware.jsap.StringParser;
|
||||
|
||||
/**
|
||||
* A {@link com.martiansoftware.jsap.StringParser} for parsing Integers. The parse() method delegates the actual parsing to Integer.decode(String).
|
||||
*
|
||||
* @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
|
||||
* @see com.martiansoftware.jsap.StringParser
|
||||
* @see Integer
|
||||
*/
|
||||
public class NullableIntegerStringParser extends StringParser
|
||||
{
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static final NullableIntegerStringParser INSTANCE = new NullableIntegerStringParser();
|
||||
|
||||
/**
|
||||
* Returns a {@link com.thevoxelbox.voxelsniper.jsap.NullableIntegerStringParser}.
|
||||
*
|
||||
*
|
||||
* Convenient access to the only instance returned by this method is available through {@link com.martiansoftware.jsap.JSAP#INTEGER_PARSER}.
|
||||
*
|
||||
* @return a {@link com.thevoxelbox.voxelsniper.jsap.NullableIntegerStringParser}.
|
||||
*/
|
||||
public static NullableIntegerStringParser getParser()
|
||||
{
|
||||
return new NullableIntegerStringParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new IntegerStringParser.
|
||||
*
|
||||
* @deprecated Use {@link #getParser()} or, even better, {@link com.martiansoftware.jsap.JSAP#INTEGER_PARSER}.
|
||||
*/
|
||||
public NullableIntegerStringParser()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the specified argument into an Integer. This method delegates the parsing to <code>Integer.decode(arg)</code>. If <code>Integer.decode()</code>
|
||||
* throws a NumberFormatException, it is encapsulated into a ParseException and re-thrown.
|
||||
*
|
||||
* @param arg
|
||||
* the argument to parse
|
||||
*
|
||||
* @return an Integer object with the value contained in the specified argument.
|
||||
*
|
||||
* @throws com.martiansoftware.jsap.ParseException
|
||||
* if <code>Integer.decode(arg)</code> throws a NumberFormatException.
|
||||
* @see Integer
|
||||
* @see com.martiansoftware.jsap.StringParser#parse(String)
|
||||
*/
|
||||
@Override
|
||||
public final Object parse(final String arg) throws ParseException
|
||||
{
|
||||
if (arg == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer result;
|
||||
try
|
||||
{
|
||||
result = Integer.decode(arg);
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
throw (new ParseException("Unable to convert '" + arg + "' to an Integer.", nfe));
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user