ArgReaders use exceptions
Now null checks for arguments is no longer required. A few arg readers was also improved slightly.
This commit is contained in:
parent
59edd6917f
commit
47b2eeccb4
@ -1,7 +1,14 @@
|
||||
package com.massivecraft.massivecore.cmd;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -10,7 +17,6 @@ import org.bukkit.plugin.Plugin;
|
||||
import com.massivecraft.massivecore.Lang;
|
||||
import com.massivecraft.massivecore.MassiveCore;
|
||||
import com.massivecraft.massivecore.cmd.arg.ArgReader;
|
||||
import com.massivecraft.massivecore.cmd.arg.ArgResult;
|
||||
import com.massivecraft.massivecore.cmd.req.Req;
|
||||
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
|
||||
import com.massivecraft.massivecore.mixin.Mixin;
|
||||
@ -336,6 +342,12 @@ public class MassiveCommand
|
||||
if ( ! isValidCall(this.sender, this.getArgs())) return;
|
||||
perform();
|
||||
}
|
||||
catch (MassiveCommandException ex)
|
||||
{
|
||||
// Sometimes arg readers (or commands themself)
|
||||
// throw exceptions, to stop executing and notify the user.
|
||||
Mixin.msgOne(sender, ex.getErrorMsgs());
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Unset Sender Variables
|
||||
@ -360,7 +372,7 @@ public class MassiveCommand
|
||||
}
|
||||
|
||||
// This is where the command action is performed.
|
||||
public void perform()
|
||||
public void perform() throws MassiveCommandException
|
||||
{
|
||||
// Per default we just act as the help command!
|
||||
List<MassiveCommand> commandChain = new ArrayList<MassiveCommand>(this.getCommandChain());
|
||||
@ -608,13 +620,13 @@ public class MassiveCommand
|
||||
return this.getArgs().get(idx);
|
||||
}
|
||||
|
||||
public <T> T arg(int idx, ArgReader<T> argReader)
|
||||
public <T> T arg(int idx, ArgReader<T> argReader) throws MassiveCommandException
|
||||
{
|
||||
String str = this.arg(idx);
|
||||
return this.arg(str, argReader);
|
||||
}
|
||||
|
||||
public <T> T arg(int idx, ArgReader<T> argReader, T defaultNotSet)
|
||||
public <T> T arg(int idx, ArgReader<T> argReader, T defaultNotSet) throws MassiveCommandException
|
||||
{
|
||||
String str = this.arg(idx);
|
||||
return this.arg(str, argReader, defaultNotSet);
|
||||
@ -631,13 +643,13 @@ public class MassiveCommand
|
||||
return Txt.implode(this.getArgs().subList(from, to), " ");
|
||||
}
|
||||
|
||||
public <T> T argConcatFrom(int idx, ArgReader<T> argReader)
|
||||
public <T> T argConcatFrom(int idx, ArgReader<T> argReader) throws MassiveCommandException
|
||||
{
|
||||
String str = this.argConcatFrom(idx);
|
||||
return this.arg(str, argReader);
|
||||
}
|
||||
|
||||
public <T> T argConcatFrom(int idx, ArgReader<T> argReader, T defaultNotSet)
|
||||
public <T> T argConcatFrom(int idx, ArgReader<T> argReader, T defaultNotSet) throws MassiveCommandException
|
||||
{
|
||||
String str = this.argConcatFrom(idx);
|
||||
return this.arg(str, argReader, defaultNotSet);
|
||||
@ -645,19 +657,18 @@ public class MassiveCommand
|
||||
|
||||
// Core & Other
|
||||
|
||||
public <T> T arg(ArgReader<T> argReader)
|
||||
public <T> T arg(ArgReader<T> argReader) throws MassiveCommandException
|
||||
{
|
||||
return this.arg(null, argReader);
|
||||
}
|
||||
|
||||
public <T> T arg(String str, ArgReader<T> argReader)
|
||||
public <T> T arg(String str, ArgReader<T> argReader) throws MassiveCommandException
|
||||
{
|
||||
ArgResult<T> result = argReader.read(str, this.sender);
|
||||
if (result.hasErrors()) this.msg(result.getErrors());
|
||||
return result.getResult();
|
||||
T result = argReader.read(str, this.sender);
|
||||
return result;
|
||||
}
|
||||
|
||||
public <T> T arg(String str, ArgReader<T> argReader, T defaultNotSet)
|
||||
public <T> T arg(String str, ArgReader<T> argReader, T defaultNotSet) throws MassiveCommandException
|
||||
{
|
||||
if (str == null) return defaultNotSet;
|
||||
return this.arg(str, argReader);
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.massivecraft.massivecore.cmd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class MassiveCommandException extends RuntimeException
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// -------------------------------------------- //
|
||||
// FIELDS
|
||||
// -------------------------------------------- //
|
||||
|
||||
private List<String> errorMsgs = new ArrayList<String>();
|
||||
public List<String> getErrorMsgs() { return this.errorMsgs; }
|
||||
public void setErrorMsgs(List<String> msgs) { this.errorMsgs = msgs; }
|
||||
public void addErrorMsg(String msg) { this.errorMsgs.add(msg); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// CONSTRUCTORS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public MassiveCommandException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MassiveCommandException(String msg)
|
||||
{
|
||||
this.errorMsgs.add(msg);
|
||||
}
|
||||
|
||||
public MassiveCommandException(Collection<String> msgs)
|
||||
{
|
||||
this.errorMsgs.addAll(msgs);
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,8 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -16,17 +18,17 @@ public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read(String arg, CommandSender sender)
|
||||
public T read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<T> result = new ArgResult<T>();
|
||||
T result;
|
||||
|
||||
try
|
||||
{
|
||||
result.setResult(this.convert(arg));
|
||||
result = this.convert(arg);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result.getErrors().add("<b>Invalid "+this.typename()+" \"<h>"+arg+"\"<b>.");
|
||||
throw new MassiveCommandException("<b>Invalid " + this.typename() + " \"<h>" + arg + "\"<b>.");
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -4,6 +4,7 @@ import java.util.Collection;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
|
||||
public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
|
||||
@ -22,25 +23,28 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read(String arg, CommandSender sender)
|
||||
public T read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<T> result = new ArgResult<T>(this.select(arg, sender));
|
||||
T result = this.select(arg, sender);
|
||||
|
||||
if (!result.hasResult())
|
||||
if (result == null)
|
||||
{
|
||||
result.getErrors().add("<b>No "+this.typename()+" matches \"<h>"+arg+"<b>\".");
|
||||
MassiveCommandException errors = new MassiveCommandException();
|
||||
errors.addErrorMsg("<b>No " + this.typename() + " matches \"<h>"+arg+"<b>\".");
|
||||
if (this.canList(sender))
|
||||
{
|
||||
Collection<String> names = this.altNames(sender);
|
||||
if (names.size() == 0)
|
||||
{
|
||||
result.getErrors().add("<i>Note: There is no "+this.typename()+" available.");
|
||||
errors.addErrorMsg("<i>Note: There is no "+this.typename()+" available.");
|
||||
}
|
||||
else
|
||||
{
|
||||
result.getErrors().add("<i>Use "+Txt.implodeCommaAndDot(names, "<h>%s", "<i>, ", " <i>or ", "<i>."));
|
||||
errors.addErrorMsg("<i>Use "+Txt.implodeCommaAndDot(names, "<h>%s", "<i>, ", " <i>or ", "<i>."));
|
||||
}
|
||||
}
|
||||
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -35,8 +35,6 @@ public class ARChatColor extends ARAbstractSelect<ChatColor>
|
||||
|
||||
for (ChatColor cc : ChatColor.values())
|
||||
{
|
||||
|
||||
|
||||
String ccstr = getToCompare(cc.name());
|
||||
if ( ! ccstr.equals(arg)) continue;
|
||||
ret = cc;
|
||||
|
@ -34,32 +34,22 @@ public class ARList<T> extends ArgReaderAbstract<List<T>>
|
||||
|
||||
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
|
||||
@Override
|
||||
public ArgResult<List<T>> read(String arg, CommandSender sender)
|
||||
public List<T> read(String arg, CommandSender sender)
|
||||
{
|
||||
// Split into inner args
|
||||
String[] innerArgs = arg.split("\\s+");
|
||||
|
||||
// Create Ret
|
||||
ArgResult<List<T>> ret = new ArgResult<List<T>>();
|
||||
List<T> result = new ArrayList<T>();
|
||||
List<T> ret = new ArrayList<T>();
|
||||
|
||||
// For Each
|
||||
for (String innerArg : innerArgs)
|
||||
{
|
||||
ArgResult<T> innerArgResult = this.getInnerArgReader().read(innerArg, sender);
|
||||
T innerArgResult = this.getInnerArgReader().read(innerArg, sender);
|
||||
|
||||
if (innerArgResult.hasErrors())
|
||||
{
|
||||
ret.setErrors(innerArgResult.getErrors());
|
||||
return ret;
|
||||
}
|
||||
|
||||
result.add(innerArgResult.getResult());
|
||||
ret.add(innerArgResult);
|
||||
}
|
||||
|
||||
// Set Result
|
||||
ret.setResult(result);
|
||||
|
||||
// Return Ret
|
||||
return ret;
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public class ARMaterial extends ArgReaderAbstract<Material>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -17,15 +19,18 @@ public class ARMaterial extends ArgReaderAbstract<Material>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<Material> read(String arg, CommandSender sender)
|
||||
public Material read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<Material> result = new ArgResult<Material>(Material.matchMaterial(arg));
|
||||
if (!result.hasResult())
|
||||
Material ret = Material.matchMaterial(arg);
|
||||
if (ret == null)
|
||||
{
|
||||
result.getErrors().add("<b>No material matches <h>"+arg+"<b>.");
|
||||
result.getErrors().add("<i>Suggestion: <aqua>http://www.minecraftwiki.net/wiki/Data_values");
|
||||
MassiveCommandException errors = new MassiveCommandException();
|
||||
errors.addErrorMsg("<b>No material matches <h>"+arg+"<b>.");
|
||||
errors.addErrorMsg("<i>Suggestion: <aqua>http://www.minecraftwiki.net/wiki/Data_values");
|
||||
throw errors;
|
||||
}
|
||||
return result;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.util.TimeDiffUtil;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
|
||||
@ -19,17 +20,18 @@ public class ARMillisDiff extends ArgReaderAbstract<Long>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<Long> read(String arg, CommandSender sender)
|
||||
public Long read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<Long> ret = new ArgResult<Long>();
|
||||
Long ret;
|
||||
try
|
||||
{
|
||||
ret.setResult(TimeDiffUtil.millis(arg));
|
||||
ret = TimeDiffUtil.millis(arg);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ret.setErrors(Txt.parse("<b>")+e.getMessage());
|
||||
throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public class ARPermission extends ArgReaderAbstract<Permission>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -18,20 +20,20 @@ public class ARPermission extends ArgReaderAbstract<Permission>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<Permission> read(String arg, CommandSender sender)
|
||||
public Permission read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<Permission> ret = new ArgResult<Permission>();
|
||||
Permission ret = null;
|
||||
|
||||
for (Permission permission : Bukkit.getPluginManager().getPermissions())
|
||||
{
|
||||
if (!permission.getName().equals(arg)) continue;
|
||||
ret.setResult(permission);
|
||||
if ( ! permission.getName().equals(arg)) continue;
|
||||
ret = permission;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ret.hasResult())
|
||||
if (ret == null)
|
||||
{
|
||||
ret.setErrors("<b>No permission with the name \"<h>"+arg+"<b>\" was found.");
|
||||
throw new MassiveCommandException("<b>No permission with the name \"<h>" + arg + "<b>\" was found.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -4,6 +4,7 @@ import java.util.Collection;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.mixin.Mixin;
|
||||
import com.massivecraft.massivecore.store.SenderIdSource;
|
||||
import com.massivecraft.massivecore.util.IdUtil;
|
||||
@ -43,23 +44,23 @@ public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read(String arg, CommandSender sender)
|
||||
public T read(String arg, CommandSender sender)
|
||||
{
|
||||
// Create Ret
|
||||
ArgResult<T> ret = new ArgResult<T>();
|
||||
T ret;
|
||||
|
||||
// arg --> senderId
|
||||
String senderId = this.getSenderIdFor(arg);
|
||||
// All of our subclasses return null if senderId is null
|
||||
// Thus we don't need to check for that being null, but only check ret
|
||||
|
||||
// Populate Ret
|
||||
if (senderId == null)
|
||||
ret = this.getResultForSenderId(senderId);
|
||||
|
||||
if (ret == null)
|
||||
{
|
||||
// No alternatives found
|
||||
ret.setErrors("<b>No player matches \"<h>"+arg+"<b>\".");
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.setResult(this.getResultForSenderId(senderId));
|
||||
throw new MassiveCommandException("<b>No player matches \"<h>"+arg+"<b>\".");
|
||||
}
|
||||
|
||||
// Return Ret
|
||||
|
@ -40,34 +40,28 @@ public class ARSet<T> extends ArgReaderAbstract<Set<T>>
|
||||
|
||||
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
|
||||
@Override
|
||||
public ArgResult<Set<T>> read(String arg, CommandSender sender)
|
||||
public Set<T> read(String arg, CommandSender sender)
|
||||
{
|
||||
// Split into inner args
|
||||
String[] innerArgs = arg.split("\\s+");
|
||||
|
||||
// Create Ret
|
||||
ArgResult<Set<T>> ret = new ArgResult<Set<T>>();
|
||||
Set<T> result = new LinkedHashSet<T>();
|
||||
Set<T> ret = new LinkedHashSet<T>();
|
||||
|
||||
boolean duplicates = false;
|
||||
|
||||
// For Each
|
||||
for (String innerArg : innerArgs)
|
||||
{
|
||||
ArgResult<T> innerArgResult = this.getInnerArgReader().read(innerArg, sender);
|
||||
T innerArgResult = this.getInnerArgReader().read(innerArg, sender);
|
||||
|
||||
if (innerArgResult.hasErrors())
|
||||
{
|
||||
ret.setErrors(innerArgResult.getErrors());
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (warnOnDuplicates && ! result.add(innerArgResult.getResult()))
|
||||
{
|
||||
sender.sendMessage(Txt.parse("<i>An argument was passed in twice and got removed."));
|
||||
}
|
||||
duplicates = ( ! ret.add(innerArgResult) || duplicates);
|
||||
}
|
||||
|
||||
// Set Result
|
||||
ret.setResult(result);
|
||||
if (warnOnDuplicates && duplicates)
|
||||
{
|
||||
sender.sendMessage(Txt.parse("<i>Some duplicate arguments were removed"));
|
||||
}
|
||||
|
||||
// Return Ret
|
||||
return ret;
|
||||
|
@ -3,6 +3,8 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public class ARSound extends ArgReaderAbstract<Sound>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -17,13 +19,15 @@ public class ARSound extends ArgReaderAbstract<Sound>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<Sound> read(String arg, CommandSender sender)
|
||||
public Sound read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<Sound> result = new ArgResult<Sound>(getSoundFromString(arg));
|
||||
if (!result.hasResult())
|
||||
Sound result = getSoundFromString(arg);
|
||||
if (result == null)
|
||||
{
|
||||
result.getErrors().add("<b>No sound matches \"<h>"+arg+"<b>\".");
|
||||
result.getErrors().add("<aqua>https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/Sound.java");
|
||||
MassiveCommandException errors = new MassiveCommandException();
|
||||
errors.addErrorMsg("<b>No sound matches \"<h>"+arg+"<b>\".");
|
||||
errors.addErrorMsg("<aqua>https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Sound.java");
|
||||
throw errors;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.SoundEffect;
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
|
||||
public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
|
||||
@ -19,17 +20,17 @@ public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<SoundEffect> read(String arg, CommandSender sender)
|
||||
public SoundEffect read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<SoundEffect> ret = new ArgResult<SoundEffect>();
|
||||
SoundEffect ret;
|
||||
|
||||
try
|
||||
{
|
||||
ret.setResult(SoundEffect.valueOf(arg));
|
||||
ret = SoundEffect.valueOf(arg);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ret.setErrors(Txt.parse("<b>") + e.getMessage());
|
||||
throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -7,8 +7,13 @@ import java.util.List;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.SoundEffect;
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
|
||||
/**
|
||||
* @deprecated use ARList
|
||||
*/
|
||||
@Deprecated
|
||||
public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -23,9 +28,9 @@ public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<List<SoundEffect>> read(String arg, CommandSender sender)
|
||||
public List<SoundEffect> read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<List<SoundEffect>> ret = new ArgResult<List<SoundEffect>>();
|
||||
List<SoundEffect> ret = new ArrayList<SoundEffect>();
|
||||
List<SoundEffect> result = new ArrayList<SoundEffect>();
|
||||
|
||||
arg = arg.trim();
|
||||
@ -37,11 +42,11 @@ public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
|
||||
{
|
||||
result.add(SoundEffect.valueOf(soundString));
|
||||
}
|
||||
ret.setResult(result);
|
||||
ret = result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ret.setErrors(Txt.parse("<b>") + e.getMessage());
|
||||
throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
public class ARString extends ARAbstractPrimitive<String>
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class ARString extends ArgReaderAbstract<String>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
@ -14,13 +16,7 @@ public class ARString extends ARAbstractPrimitive<String>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public String typename()
|
||||
{
|
||||
return "string";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convert(String arg) throws Exception
|
||||
public String read(String arg, CommandSender sender)
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.util.Collection;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.Multiverse;
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
import com.massivecraft.massivecore.util.Txt;
|
||||
|
||||
public class ARUniverse extends ArgReaderAbstract<String>
|
||||
@ -31,20 +32,21 @@ public class ARUniverse extends ArgReaderAbstract<String>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<String> read(String arg, CommandSender sender)
|
||||
public String read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<String> result = new ArgResult<String>();
|
||||
String result = new String();
|
||||
|
||||
if (multiverse.containsUniverse(arg))
|
||||
{
|
||||
result.setResult(arg);
|
||||
result = arg;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.getErrors().add("<b>No universe \"<h>"+arg+"<b>\" exists in multiverse <h>"+this.multiverse.getId()+"<b>.");
|
||||
|
||||
MassiveCommandException errors = new MassiveCommandException();
|
||||
errors.addErrorMsg("<b>No universe \"<h>" + arg + "<b>\" exists in multiverse <h>" + this.multiverse.getId() + "<b>.");
|
||||
Collection<String> names = new ArrayList<String>(multiverse.getUniverses());
|
||||
result.getErrors().add("<i>Use "+Txt.implodeCommaAndDot(names, "<h>%s", "<i>, ", " <i>or ", "<i>."));
|
||||
errors.addErrorMsg("<i>Use " + Txt.implodeCommaAndDot(names, "<h>%s", "<i>, ", " <i>or ", "<i>."));
|
||||
throw errors;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public class ARWorld extends ArgReaderAbstract<World>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -18,27 +20,19 @@ public class ARWorld extends ArgReaderAbstract<World>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<World> read(String arg, CommandSender sender)
|
||||
public World read(String arg, CommandSender sender)
|
||||
{
|
||||
ArgResult<World> ret = new ArgResult<World>();
|
||||
World ret;
|
||||
|
||||
ArgResult<String> inner = ARWorldId.get().read(arg, sender);
|
||||
if (inner.hasErrors())
|
||||
{
|
||||
ret.setErrors(inner.getErrors());
|
||||
return ret;
|
||||
}
|
||||
String inner = ARWorldId.get().read(arg, sender);
|
||||
|
||||
String worldId = inner.getResult();
|
||||
String worldId = inner;
|
||||
|
||||
World world = Bukkit.getWorld(worldId);
|
||||
if (world == null)
|
||||
ret = Bukkit.getWorld(worldId);
|
||||
|
||||
if (ret == null)
|
||||
{
|
||||
ret.setErrors("<b>The world could not be found.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.setResult(world);
|
||||
throw new MassiveCommandException("<b>The world could not be found.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -33,13 +33,13 @@ public class ARWorldId extends ARAbstractSelect<String>
|
||||
|
||||
for (String worldId : visibleWorldIds)
|
||||
{
|
||||
if (!Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
if (arg.equalsIgnoreCase(worldId)) return worldId;
|
||||
}
|
||||
|
||||
for (String worldId : visibleWorldIds)
|
||||
{
|
||||
if (!Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
for (String worldAlias : Mixin.getWorldAliases(worldId))
|
||||
{
|
||||
if (arg.equalsIgnoreCase(worldAlias)) return worldId;
|
||||
@ -55,7 +55,7 @@ public class ARWorldId extends ARAbstractSelect<String>
|
||||
List<String> ret = new ArrayList<String>();
|
||||
for (String worldId : Mixin.getWorldIds())
|
||||
{
|
||||
if (!Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
|
||||
ret.add(Mixin.getWorldDisplayName(worldId));
|
||||
}
|
||||
return ret;
|
||||
|
@ -39,7 +39,7 @@ public class ARWorldType extends ARAbstractSelect<WorldType>
|
||||
arg = arg.replace(".", "");
|
||||
arg = arg.replace("normal", "");
|
||||
arg = arg.replace("default", "");
|
||||
arg = arg.replace("large", "");
|
||||
arg = arg.replace("biomes", "");
|
||||
|
||||
if (arg.equals(""))
|
||||
{
|
||||
|
@ -2,10 +2,12 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public interface ArgReader<T>
|
||||
{
|
||||
public ArgResult<T> read(String arg, CommandSender sender);
|
||||
public ArgResult<T> read(CommandSender sender);
|
||||
public ArgResult<T> read(String arg);
|
||||
public ArgResult<T> read();
|
||||
public T read(String arg, CommandSender sender) throws MassiveCommandException;
|
||||
public T read(CommandSender sender) throws MassiveCommandException;
|
||||
public T read(String arg) throws MassiveCommandException;
|
||||
public T readArg() throws MassiveCommandException;
|
||||
}
|
@ -2,6 +2,8 @@ package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommandException;
|
||||
|
||||
public abstract class ArgReaderAbstract<T> implements ArgReader<T>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
@ -9,19 +11,19 @@ public abstract class ArgReaderAbstract<T> implements ArgReader<T>
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read(CommandSender sender)
|
||||
public T read(CommandSender sender) throws MassiveCommandException
|
||||
{
|
||||
return this.read(null, sender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read(String arg)
|
||||
public T read(String arg) throws MassiveCommandException
|
||||
{
|
||||
return this.read(arg, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgResult<T> read()
|
||||
public T readArg() throws MassiveCommandException
|
||||
{
|
||||
return this.read(null, null);
|
||||
}
|
||||
|
@ -1,72 +0,0 @@
|
||||
package com.massivecraft.massivecore.cmd.arg;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ArgResult<T>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// FIELD: RESULT
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected T result = null;
|
||||
public T getResult() { return this.result; }
|
||||
public void setResult(T result) { this.result = result; }
|
||||
public boolean hasResult() { return this.getResult() != null; }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// FIELD: ERRORS
|
||||
// -------------------------------------------- //
|
||||
|
||||
protected List<String> errors = new ArrayList<String>();
|
||||
public List<String> getErrors() { return this.errors; }
|
||||
|
||||
public void setErrors(List<String> val)
|
||||
{
|
||||
if (val == null)
|
||||
{
|
||||
this.errors = new ArrayList<String>();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.errors = val;
|
||||
}
|
||||
}
|
||||
|
||||
public void setErrors(String... val)
|
||||
{
|
||||
this.setErrors(Arrays.asList(val));
|
||||
}
|
||||
|
||||
public boolean hasErrors()
|
||||
{
|
||||
return this.errors.size() > 0;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
public ArgResult()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ArgResult(T result)
|
||||
{
|
||||
this.setResult(result);
|
||||
}
|
||||
|
||||
public ArgResult(T result, List<String> errors)
|
||||
{
|
||||
this.setResult(result);
|
||||
this.setErrors(errors);
|
||||
}
|
||||
|
||||
public ArgResult(T result, String... errors)
|
||||
{
|
||||
this.setResult(result);
|
||||
this.setErrors(errors);
|
||||
}
|
||||
}
|
@ -5,7 +5,8 @@ import java.util.List;
|
||||
import com.massivecraft.massivecore.MassiveCorePerm;
|
||||
import com.massivecraft.massivecore.SoundEffect;
|
||||
import com.massivecraft.massivecore.cmd.MassiveCommand;
|
||||
import com.massivecraft.massivecore.cmd.arg.ARSoundEffects;
|
||||
import com.massivecraft.massivecore.cmd.arg.ARList;
|
||||
import com.massivecraft.massivecore.cmd.arg.ARSoundEffect;
|
||||
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
|
||||
import com.massivecraft.massivecore.cmd.req.ReqIsPlayer;
|
||||
|
||||
@ -37,7 +38,7 @@ public class CmdMassiveCoreHearsound extends MassiveCommand
|
||||
public void perform()
|
||||
{
|
||||
// Args
|
||||
List<SoundEffect> soundEffects = this.argConcatFrom(0, ARSoundEffects.get());
|
||||
List<SoundEffect> soundEffects = this.argConcatFrom(0, ARList.get(ARSoundEffect.get()));
|
||||
if (soundEffects == null) return;
|
||||
|
||||
// Apply
|
||||
|
Loading…
Reference in New Issue
Block a user