Fix bounds check in ByIdArray

This commit is contained in:
fullwall 2013-07-31 18:41:09 +08:00
parent 2603c3ae2c
commit 7dad81c6ed
4 changed files with 16 additions and 58 deletions

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -38,10 +39,12 @@ import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.trait.waypoint.Waypoints;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class CitizensTraitFactory implements TraitFactory {
private final List<TraitInfo> defaultTraits = Lists.newArrayList();
private final Map<String, TraitInfo> registered = Maps.newHashMap();
public CitizensTraitFactory() {
@ -66,7 +69,7 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(Spawned.class).withName("spawned"));
registerTrait(TraitInfo.create(Speech.class).withName("speech"));
registerTrait(TraitInfo.create(Text.class).withName("text"));
registerTrait(TraitInfo.create(MobType.class).withName("type"));
registerTrait(TraitInfo.create(MobType.class).withName("type").asDefaultTrait());
registerTrait(TraitInfo.create(Waypoints.class).withName("waypoints"));
registerTrait(TraitInfo.create(WoolColor.class).withName("woolcolor"));
registerTrait(TraitInfo.create(WolfModifiers.class).withName("wolfmodifiers"));
@ -77,6 +80,13 @@ public class CitizensTraitFactory implements TraitFactory {
}
}
@Override
public void addDefaultTraits(NPC npc) {
for (TraitInfo info : defaultTraits) {
npc.addTrait(create(info));
}
}
public void addPlotters(Graph graph) {
for (Map.Entry<String, TraitInfo> entry : registered.entrySet()) {
if (INTERNAL_TRAITS.contains(entry.getKey()))
@ -135,6 +145,9 @@ public class CitizensTraitFactory implements TraitFactory {
if (registered.containsKey(info.getTraitName()))
throw new IllegalArgumentException("trait name already registered");
registered.put(info.getTraitName(), info);
if (info.isDefaultTrait()) {
defaultTraits.add(info);
}
}
private static final Set<String> INTERNAL_TRAITS = Sets.newHashSet();

View File

@ -17,5 +17,5 @@ public class EmptySocket extends Socket {
return new ByteArrayOutputStream(10);
}
private static final byte[] EMPTY = new byte[20];
private static final byte[] EMPTY = new byte[50];
}

View File

@ -67,7 +67,7 @@ public class ByIdArray<T> implements Iterable<T> {
@SuppressWarnings("unchecked")
public T get(int index) {
if (index > elementData.length)
if (index >= elementData.length)
return null;
return (T) elementData[index];
}

View File

@ -6,61 +6,6 @@ import net.citizensnpcs.api.util.Colorizer;
import org.bukkit.ChatColor;
public class StringHelper {
public static String capitalize(Object string) {
String capitalize = string.toString();
return capitalize.length() == 0 ? "" : Character.toUpperCase(capitalize.charAt(0))
+ capitalize.substring(1, capitalize.length());
}
public static int getLevenshteinDistance(String s, String t) {
if (s == null || t == null)
throw new IllegalArgumentException("Strings must not be null");
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0)
return m;
else if (m == 0)
return n;
int p[] = new int[n + 1]; // 'previous' cost array, horizontally
int d[] = new int[n + 1]; // cost array, horizontally
int _d[]; // placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i <= n; i++)
p[i] = i;
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
d[0] = j;
for (i = 1; i <= n; i++) {
cost = s.charAt(i - 1) == t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left
// and up +cost
d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
public static String wrap(Object string) {
return wrap(string, Colorizer.parseColors(Setting.MESSAGE_COLOUR.asString()));
}