ArgReader improvements and an extra MassiveCommand utility command

This commit is contained in:
Olof Larsson 2015-03-30 17:11:44 +02:00
parent a144e160e8
commit 182b48abd4
6 changed files with 278 additions and 2 deletions

View File

@ -25,6 +25,11 @@ public class PotionEffectWrap
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }
@SuppressWarnings("deprecation")
public void setPotionEffectType(PotionEffectType potionEffectType) { this.setId(potionEffectType.getId());}
@SuppressWarnings("deprecation")
public PotionEffectType getPotionEffectType() { return PotionEffectType.getById(this.getId()); }
protected int amplifier;
public int getAmplifier() { return this.amplifier; }
public void setAmplifier(int amplifier) { this.amplifier = amplifier; }
@ -139,4 +144,36 @@ public class PotionEffectWrap
addEffects(entity, potionEffectWraps);
}
// -------------------------------------------- //
// TO STRING
// -------------------------------------------- //
public String getListLine()
{
// Create Ret
StringBuilder ret = new StringBuilder();
// Type Name (ID)
ret.append(this.getPotionEffectType().getName());
ret.append(' ');
// Amplifier
ret.append(this.amplifier);
ret.append(' ');
// Duration
ret.append(this.duration);
ret.append(' ');
// Ambient
ret.append(this.ambient);
ret.append(' ');
// Particles
ret.append(this.particles);
// Return Ret
return ret.toString();
}
}

View File

@ -21,6 +21,7 @@ import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.cmd.arg.ArgReader;
import com.massivecraft.massivecore.cmd.req.Req;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.util.PermUtil;
import com.massivecraft.massivecore.util.Txt;
@ -165,10 +166,12 @@ public class MassiveCommand
// The different names this commands will react to
protected List<String> aliases;
public List<String> getAliases() { return this.aliases; }
public void setAliases(List<String> aliases) { this.aliases = aliases; }
public void addAliases(String... aliases) { this.aliases.addAll(Arrays.asList(aliases)); }
public void setAliases(Collection<String> aliases) { this.aliases = new MassiveList<String>(aliases); }
public void setAliases(String... aliases) { this.setAliases(Arrays.asList(aliases)); }
public void addAliases(Collection<String> aliases) { this.aliases.addAll(aliases); }
public void addAliases(String... aliases) { this.addAliases(Arrays.asList(aliases)); }
// FIELD: requiredArgs
// These args must always be sent

View File

@ -0,0 +1,68 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
public class ARCombined extends ArgReaderAbstract<List<?>>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected List<ArgReader<?>> inners;
public List<ArgReader<?>> getInners() { return this.inners; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ARCombined get(Collection<ArgReader<?>> inners) { return new ARCombined(inners); }
public static ARCombined get(ArgReader<?>... inners) { return new ARCombined(inners); }
public ARCombined(Collection<ArgReader<?>> inners)
{
this.inners = new MassiveList<ArgReader<?>>(inners);
}
public ARCombined(ArgReader<?>... inners)
{
this(Arrays.asList(inners));
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public List<?> read(String arg, CommandSender sender) throws MassiveException
{
// Create Ret
List<Object> ret = new MassiveList<Object>();
// Fill Ret
List<String> parts = Arrays.asList(arg.split(","));
if (parts.size() > this.getInners().size())
{
throw new MassiveException().addMsg("<b>Too many parts!");
}
for (int i = 0; i < parts.size(); i++)
{
String part = parts.get(i);
ArgReader<?> argReader = this.getInners().get(i);
Object asdf = argReader.read(part, sender);
ret.add(asdf);
}
// Return Ret
return ret;
}
}

View File

@ -0,0 +1,80 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.potion.PotionEffectType;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
public class ARPotionEffectType extends ARAbstractSelect<PotionEffectType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ARPotionEffectType i = new ARPotionEffectType();
public static ARPotionEffectType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "potion effect type";
}
@Override
public PotionEffectType select(String str, CommandSender sender) throws MassiveException
{
// Prepare
str = getComparable(str);
// Match Name
for (PotionEffectType potionEffectType : PotionEffectType.values())
{
String name = getComparable(potionEffectType);
if (str.equals(name)) return potionEffectType;
}
// :(
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
// Create Ret
List<String> ret = new MassiveList<String>();
// Match Name
for (PotionEffectType potionEffectType : PotionEffectType.values())
{
if (potionEffectType == null) continue;
String name = potionEffectType.getName();
if (name == null) continue;
ret.add(name);
}
// Return Ret
return ret;
}
public static String getComparable(PotionEffectType potionEffectType)
{
if (potionEffectType == null) return null;
return getComparable(potionEffectType.getName());
}
public static String getComparable(String string)
{
if (string == null) return null;
return string.toLowerCase();
}
}

View File

@ -0,0 +1,80 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.potion.PotionEffectType;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.PotionEffectWrap;
public class ARPotionEffectWrap extends ArgReaderAbstract<PotionEffectWrap>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ARPotionEffectWrap i = new ARPotionEffectWrap();
public static ARPotionEffectWrap get() { return i; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected final ARCombined combined = new ARCombined(
ARPotionEffectType.get(),
ARInteger.get(),
ARInteger.get(),
ARBoolean.get(),
ARBoolean.get()
);
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public PotionEffectWrap read(String arg, CommandSender sender) throws MassiveException
{
// Create Ret
PotionEffectWrap ret = new PotionEffectWrap();
// Fill Ret
List<?> parts = combined.read(arg, sender);
for (int i = 0 ; i < parts.size() ; i++)
{
Object part = parts.get(i);
if (i == 0)
{
PotionEffectType potionEffectType = (PotionEffectType)part;
ret.setPotionEffectType(potionEffectType);
}
else if (i == 1)
{
Integer amplifier = (Integer)part;
ret.setAmplifier(amplifier);
}
else if (i == 2)
{
Integer duration = (Integer)part;
ret.setDuration(duration);
}
else if (i == 3)
{
Boolean ambient = (Boolean)part;
ret.setAmbient(ambient);
}
else if (i == 4)
{
Boolean particles = (Boolean)part;
ret.setParticles(particles);
}
}
// Return Ret
return ret;
}
}

View File

@ -4,6 +4,8 @@ import java.io.Serializable;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.cmd.MassiveCommand;
public abstract class ReqAbstract implements Req, Serializable
{
private static final long serialVersionUID = 1L;
@ -20,4 +22,10 @@ public abstract class ReqAbstract implements Req, Serializable
return this.createErrorMessage(sender, null);
}
public static String getDesc(MassiveCommand command)
{
if (command == null) return "do that";
return command.getDesc();
}
}