Add a removeByIndex utility method and a new arg reader.

This commit is contained in:
Olof Larsson 2013-10-01 11:34:45 +02:00
parent af539e1a51
commit 23ddea0772
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package com.massivecraft.mcore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
public class AREntityType extends ARAbstractSelect<EntityType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AREntityType i = new AREntityType();
public static AREntityType get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "entity type";
}
@SuppressWarnings("deprecation")
@Override
public EntityType select(String arg, CommandSender sender)
{
return EntityType.fromName(arg);
}
@SuppressWarnings("deprecation")
@Override
public Collection<String> altNames(CommandSender sender)
{
List<String> ret = new ArrayList<String>();
for (EntityType entityType : EntityType.values())
{
ret.add(entityType.getName());
}
return ret;
}
}

View File

@ -449,6 +449,36 @@ public class MUtil
return ret; return ret;
} }
// -------------------------------------------- //
// COLLECTION MANIPULATION
// -------------------------------------------- //
public static <T> T removeByIndex(Collection<T> coll, int index)
{
if (coll == null) throw new NullPointerException("coll");
if (coll instanceof List<?>)
{
return ((List<T>)coll).remove(index);
}
if (index < 0) throw new IndexOutOfBoundsException("index < 0");
if (index >= coll.size()) throw new IndexOutOfBoundsException("index > collection size");
int i = -1;
Iterator<T> iter = coll.iterator();
while (iter.hasNext())
{
i++;
T ret = iter.next();
if (i != index) continue;
iter.remove();
return ret;
}
return null;
}
// -------------------------------------------- // // -------------------------------------------- //
// LE NICE RANDOM // LE NICE RANDOM
// -------------------------------------------- // // -------------------------------------------- //