From 23ddea07726cfb1861d2ebe30695d3d34746649d Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 1 Oct 2013 11:34:45 +0200 Subject: [PATCH] Add a removeByIndex utility method and a new arg reader. --- .../mcore/cmd/arg/AREntityType.java | 48 +++++++++++++++++++ src/com/massivecraft/mcore/util/MUtil.java | 30 ++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/com/massivecraft/mcore/cmd/arg/AREntityType.java diff --git a/src/com/massivecraft/mcore/cmd/arg/AREntityType.java b/src/com/massivecraft/mcore/cmd/arg/AREntityType.java new file mode 100644 index 00000000..79855fb3 --- /dev/null +++ b/src/com/massivecraft/mcore/cmd/arg/AREntityType.java @@ -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 +{ + // -------------------------------------------- // + // 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 altNames(CommandSender sender) + { + List ret = new ArrayList(); + for (EntityType entityType : EntityType.values()) + { + ret.add(entityType.getName()); + } + return ret; + } + +} diff --git a/src/com/massivecraft/mcore/util/MUtil.java b/src/com/massivecraft/mcore/util/MUtil.java index 6efe917c..0ba1b2f9 100644 --- a/src/com/massivecraft/mcore/util/MUtil.java +++ b/src/com/massivecraft/mcore/util/MUtil.java @@ -449,6 +449,36 @@ public class MUtil return ret; } + // -------------------------------------------- // + // COLLECTION MANIPULATION + // -------------------------------------------- // + + public static T removeByIndex(Collection coll, int index) + { + if (coll == null) throw new NullPointerException("coll"); + + if (coll instanceof List) + { + return ((List)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 iter = coll.iterator(); + while (iter.hasNext()) + { + i++; + T ret = iter.next(); + if (i != index) continue; + iter.remove(); + return ret; + } + + return null; + } + // -------------------------------------------- // // LE NICE RANDOM // -------------------------------------------- //