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:
Magnus Ulf 2015-01-24 22:13:28 +01:00 committed by Olof Larsson
parent 59edd6917f
commit 47b2eeccb4
23 changed files with 194 additions and 207 deletions

View File

@ -1,7 +1,14 @@
package com.massivecraft.massivecore.cmd; 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.Map.Entry;
import java.util.Set;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -10,7 +17,6 @@ import org.bukkit.plugin.Plugin;
import com.massivecraft.massivecore.Lang; import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.cmd.arg.ArgReader; 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.Req;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm; import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mixin.Mixin;
@ -336,6 +342,12 @@ public class MassiveCommand
if ( ! isValidCall(this.sender, this.getArgs())) return; if ( ! isValidCall(this.sender, this.getArgs())) return;
perform(); 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 finally
{ {
// Unset Sender Variables // Unset Sender Variables
@ -360,7 +372,7 @@ public class MassiveCommand
} }
// This is where the command action is performed. // 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! // Per default we just act as the help command!
List<MassiveCommand> commandChain = new ArrayList<MassiveCommand>(this.getCommandChain()); List<MassiveCommand> commandChain = new ArrayList<MassiveCommand>(this.getCommandChain());
@ -608,13 +620,13 @@ public class MassiveCommand
return this.getArgs().get(idx); 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); String str = this.arg(idx);
return this.arg(str, argReader); 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); String str = this.arg(idx);
return this.arg(str, argReader, defaultNotSet); return this.arg(str, argReader, defaultNotSet);
@ -631,13 +643,13 @@ public class MassiveCommand
return Txt.implode(this.getArgs().subList(from, to), " "); 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); String str = this.argConcatFrom(idx);
return this.arg(str, argReader); 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); String str = this.argConcatFrom(idx);
return this.arg(str, argReader, defaultNotSet); return this.arg(str, argReader, defaultNotSet);
@ -645,19 +657,18 @@ public class MassiveCommand
// Core & Other // Core & Other
public <T> T arg(ArgReader<T> argReader) public <T> T arg(ArgReader<T> argReader) throws MassiveCommandException
{ {
return this.arg(null, argReader); 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); T result = argReader.read(str, this.sender);
if (result.hasErrors()) this.msg(result.getErrors()); return result;
return result.getResult();
} }
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; if (str == null) return defaultNotSet;
return this.arg(str, argReader); return this.arg(str, argReader);

View File

@ -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);
}
}

View File

@ -2,6 +2,8 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T> public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -16,17 +18,17 @@ public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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 try
{ {
result.setResult(this.convert(arg)); result = this.convert(arg);
} }
catch (Exception e) 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; return result;

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T> public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
@ -22,25 +23,28 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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)) if (this.canList(sender))
{ {
Collection<String> names = this.altNames(sender); Collection<String> names = this.altNames(sender);
if (names.size() == 0) 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 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; return result;

View File

@ -35,8 +35,6 @@ public class ARChatColor extends ARAbstractSelect<ChatColor>
for (ChatColor cc : ChatColor.values()) for (ChatColor cc : ChatColor.values())
{ {
String ccstr = getToCompare(cc.name()); String ccstr = getToCompare(cc.name());
if ( ! ccstr.equals(arg)) continue; if ( ! ccstr.equals(arg)) continue;
ret = cc; ret = cc;

View File

@ -34,32 +34,22 @@ public class ARList<T> extends ArgReaderAbstract<List<T>>
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false). // NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
@Override @Override
public ArgResult<List<T>> read(String arg, CommandSender sender) public List<T> read(String arg, CommandSender sender)
{ {
// Split into inner args // Split into inner args
String[] innerArgs = arg.split("\\s+"); String[] innerArgs = arg.split("\\s+");
// Create Ret // Create Ret
ArgResult<List<T>> ret = new ArgResult<List<T>>(); List<T> ret = new ArrayList<T>();
List<T> result = new ArrayList<T>();
// For Each // For Each
for (String innerArg : innerArgs) for (String innerArg : innerArgs)
{ {
ArgResult<T> innerArgResult = this.getInnerArgReader().read(innerArg, sender); T innerArgResult = this.getInnerArgReader().read(innerArg, sender);
if (innerArgResult.hasErrors()) ret.add(innerArgResult);
{
ret.setErrors(innerArgResult.getErrors());
return ret;
}
result.add(innerArgResult.getResult());
} }
// Set Result
ret.setResult(result);
// Return Ret // Return Ret
return ret; return ret;
} }

View File

@ -3,6 +3,8 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public class ARMaterial extends ArgReaderAbstract<Material> public class ARMaterial extends ArgReaderAbstract<Material>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -17,15 +19,18 @@ public class ARMaterial extends ArgReaderAbstract<Material>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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)); Material ret = Material.matchMaterial(arg);
if (!result.hasResult()) if (ret == null)
{ {
result.getErrors().add("<b>No material matches <h>"+arg+"<b>."); MassiveCommandException errors = new MassiveCommandException();
result.getErrors().add("<i>Suggestion: <aqua>http://www.minecraftwiki.net/wiki/Data_values"); 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;
} }
} }

View File

@ -2,6 +2,7 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.util.TimeDiffUtil; import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
@ -19,17 +20,18 @@ public class ARMillisDiff extends ArgReaderAbstract<Long>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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 try
{ {
ret.setResult(TimeDiffUtil.millis(arg)); ret = TimeDiffUtil.millis(arg);
} }
catch (Exception e) catch (Exception e)
{ {
ret.setErrors(Txt.parse("<b>")+e.getMessage()); throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
} }
return ret; return ret;
} }

View File

@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public class ARPermission extends ArgReaderAbstract<Permission> public class ARPermission extends ArgReaderAbstract<Permission>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -18,20 +20,20 @@ public class ARPermission extends ArgReaderAbstract<Permission>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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()) for (Permission permission : Bukkit.getPluginManager().getPermissions())
{ {
if (!permission.getName().equals(arg)) continue; if ( ! permission.getName().equals(arg)) continue;
ret.setResult(permission); ret = permission;
break; 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; return ret;

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSource; import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
@ -43,23 +44,23 @@ public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public ArgResult<T> read(String arg, CommandSender sender) public T read(String arg, CommandSender sender)
{ {
// Create Ret // Create Ret
ArgResult<T> ret = new ArgResult<T>(); T ret;
// arg --> senderId // arg --> senderId
String senderId = this.getSenderIdFor(arg); 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 // Populate Ret
if (senderId == null) ret = this.getResultForSenderId(senderId);
if (ret == null)
{ {
// No alternatives found // No alternatives found
ret.setErrors("<b>No player matches \"<h>"+arg+"<b>\"."); throw new MassiveCommandException("<b>No player matches \"<h>"+arg+"<b>\".");
}
else
{
ret.setResult(this.getResultForSenderId(senderId));
} }
// Return Ret // Return Ret

View File

@ -40,34 +40,28 @@ public class ARSet<T> extends ArgReaderAbstract<Set<T>>
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false). // NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
@Override @Override
public ArgResult<Set<T>> read(String arg, CommandSender sender) public Set<T> read(String arg, CommandSender sender)
{ {
// Split into inner args // Split into inner args
String[] innerArgs = arg.split("\\s+"); String[] innerArgs = arg.split("\\s+");
// Create Ret // Create Ret
ArgResult<Set<T>> ret = new ArgResult<Set<T>>(); Set<T> ret = new LinkedHashSet<T>();
Set<T> result = new LinkedHashSet<T>();
boolean duplicates = false;
// For Each // For Each
for (String innerArg : innerArgs) for (String innerArg : innerArgs)
{ {
ArgResult<T> innerArgResult = this.getInnerArgReader().read(innerArg, sender); T innerArgResult = this.getInnerArgReader().read(innerArg, sender);
if (innerArgResult.hasErrors()) duplicates = ( ! ret.add(innerArgResult) || duplicates);
{
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."));
}
} }
// Set Result if (warnOnDuplicates && duplicates)
ret.setResult(result); {
sender.sendMessage(Txt.parse("<i>Some duplicate arguments were removed"));
}
// Return Ret // Return Ret
return ret; return ret;

View File

@ -3,6 +3,8 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public class ARSound extends ArgReaderAbstract<Sound> public class ARSound extends ArgReaderAbstract<Sound>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -17,13 +19,15 @@ public class ARSound extends ArgReaderAbstract<Sound>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public ArgResult<Sound> read(String arg, CommandSender sender) public Sound read(String arg, CommandSender sender)
{ {
ArgResult<Sound> result = new ArgResult<Sound>(getSoundFromString(arg)); Sound result = getSoundFromString(arg);
if (!result.hasResult()) if (result == null)
{ {
result.getErrors().add("<b>No sound matches \"<h>"+arg+"<b>\"."); MassiveCommandException errors = new MassiveCommandException();
result.getErrors().add("<aqua>https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/Sound.java"); 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; return result;
} }

View File

@ -3,6 +3,7 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.SoundEffect; import com.massivecraft.massivecore.SoundEffect;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class ARSoundEffect extends ArgReaderAbstract<SoundEffect> public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
@ -19,17 +20,17 @@ public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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 try
{ {
ret.setResult(SoundEffect.valueOf(arg)); ret = SoundEffect.valueOf(arg);
} }
catch (Exception e) catch (Exception e)
{ {
ret.setErrors(Txt.parse("<b>") + e.getMessage()); throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
} }
return ret; return ret;
} }

View File

@ -7,8 +7,13 @@ import java.util.List;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.SoundEffect; import com.massivecraft.massivecore.SoundEffect;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
/**
* @deprecated use ARList
*/
@Deprecated
public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>> public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -23,9 +28,9 @@ public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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>(); List<SoundEffect> result = new ArrayList<SoundEffect>();
arg = arg.trim(); arg = arg.trim();
@ -37,11 +42,11 @@ public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
{ {
result.add(SoundEffect.valueOf(soundString)); result.add(SoundEffect.valueOf(soundString));
} }
ret.setResult(result); ret = result;
} }
catch (Exception e) catch (Exception e)
{ {
ret.setErrors(Txt.parse("<b>") + e.getMessage()); throw new MassiveCommandException(Txt.parse("<b>") + e.getMessage());
} }
return ret; return ret;
} }

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg; 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 // INSTANCE & CONSTRUCT
@ -14,13 +16,7 @@ public class ARString extends ARAbstractPrimitive<String>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public String typename() public String read(String arg, CommandSender sender)
{
return "string";
}
@Override
public String convert(String arg) throws Exception
{ {
return arg; return arg;
} }

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.Multiverse; import com.massivecraft.massivecore.Multiverse;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class ARUniverse extends ArgReaderAbstract<String> public class ARUniverse extends ArgReaderAbstract<String>
@ -31,20 +32,21 @@ public class ARUniverse extends ArgReaderAbstract<String>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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)) if (multiverse.containsUniverse(arg))
{ {
result.setResult(arg); result = arg;
} }
else 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()); 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; return result;

View File

@ -4,6 +4,8 @@ import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public class ARWorld extends ArgReaderAbstract<World> public class ARWorld extends ArgReaderAbstract<World>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -18,27 +20,19 @@ public class ARWorld extends ArgReaderAbstract<World>
// -------------------------------------------- // // -------------------------------------------- //
@Override @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); String inner = ARWorldId.get().read(arg, sender);
if (inner.hasErrors())
{
ret.setErrors(inner.getErrors());
return ret;
}
String worldId = inner.getResult(); String worldId = inner;
World world = Bukkit.getWorld(worldId); ret = Bukkit.getWorld(worldId);
if (world == null)
if (ret == null)
{ {
ret.setErrors("<b>The world could not be found."); throw new MassiveCommandException("<b>The world could not be found.");
}
else
{
ret.setResult(world);
} }
return ret; return ret;

View File

@ -33,13 +33,13 @@ public class ARWorldId extends ARAbstractSelect<String>
for (String worldId : visibleWorldIds) for (String worldId : visibleWorldIds)
{ {
if (!Mixin.canSeeWorld(sender, worldId)) continue; if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
if (arg.equalsIgnoreCase(worldId)) return worldId; if (arg.equalsIgnoreCase(worldId)) return worldId;
} }
for (String worldId : visibleWorldIds) for (String worldId : visibleWorldIds)
{ {
if (!Mixin.canSeeWorld(sender, worldId)) continue; if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
for (String worldAlias : Mixin.getWorldAliases(worldId)) for (String worldAlias : Mixin.getWorldAliases(worldId))
{ {
if (arg.equalsIgnoreCase(worldAlias)) return worldId; if (arg.equalsIgnoreCase(worldAlias)) return worldId;
@ -55,7 +55,7 @@ public class ARWorldId extends ARAbstractSelect<String>
List<String> ret = new ArrayList<String>(); List<String> ret = new ArrayList<String>();
for (String worldId : Mixin.getWorldIds()) for (String worldId : Mixin.getWorldIds())
{ {
if (!Mixin.canSeeWorld(sender, worldId)) continue; if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
ret.add(Mixin.getWorldDisplayName(worldId)); ret.add(Mixin.getWorldDisplayName(worldId));
} }
return ret; return ret;

View File

@ -39,7 +39,7 @@ public class ARWorldType extends ARAbstractSelect<WorldType>
arg = arg.replace(".", ""); arg = arg.replace(".", "");
arg = arg.replace("normal", ""); arg = arg.replace("normal", "");
arg = arg.replace("default", ""); arg = arg.replace("default", "");
arg = arg.replace("large", ""); arg = arg.replace("biomes", "");
if (arg.equals("")) if (arg.equals(""))
{ {

View File

@ -2,10 +2,12 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public interface ArgReader<T> public interface ArgReader<T>
{ {
public ArgResult<T> read(String arg, CommandSender sender); public T read(String arg, CommandSender sender) throws MassiveCommandException;
public ArgResult<T> read(CommandSender sender); public T read(CommandSender sender) throws MassiveCommandException;
public ArgResult<T> read(String arg); public T read(String arg) throws MassiveCommandException;
public ArgResult<T> read(); public T readArg() throws MassiveCommandException;
} }

View File

@ -2,6 +2,8 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommandException;
public abstract class ArgReaderAbstract<T> implements ArgReader<T> public abstract class ArgReaderAbstract<T> implements ArgReader<T>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -9,19 +11,19 @@ public abstract class ArgReaderAbstract<T> implements ArgReader<T>
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public ArgResult<T> read(CommandSender sender) public T read(CommandSender sender) throws MassiveCommandException
{ {
return this.read(null, sender); return this.read(null, sender);
} }
@Override @Override
public ArgResult<T> read(String arg) public T read(String arg) throws MassiveCommandException
{ {
return this.read(arg, null); return this.read(arg, null);
} }
@Override @Override
public ArgResult<T> read() public T readArg() throws MassiveCommandException
{ {
return this.read(null, null); return this.read(null, null);
} }

View File

@ -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);
}
}

View File

@ -5,7 +5,8 @@ import java.util.List;
import com.massivecraft.massivecore.MassiveCorePerm; import com.massivecraft.massivecore.MassiveCorePerm;
import com.massivecraft.massivecore.SoundEffect; import com.massivecraft.massivecore.SoundEffect;
import com.massivecraft.massivecore.cmd.MassiveCommand; 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.ReqHasPerm;
import com.massivecraft.massivecore.cmd.req.ReqIsPlayer; import com.massivecraft.massivecore.cmd.req.ReqIsPlayer;
@ -37,7 +38,7 @@ public class CmdMassiveCoreHearsound extends MassiveCommand
public void perform() public void perform()
{ {
// Args // Args
List<SoundEffect> soundEffects = this.argConcatFrom(0, ARSoundEffects.get()); List<SoundEffect> soundEffects = this.argConcatFrom(0, ARList.get(ARSoundEffect.get()));
if (soundEffects == null) return; if (soundEffects == null) return;
// Apply // Apply