Skip to content

Commit

Permalink
Fix bounds check in ByIdArray
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 31, 2013
1 parent da1147b commit 141e665
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 58 deletions.
15 changes: 14 additions & 1 deletion src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java
@@ -1,5 +1,6 @@
package net.citizensnpcs.npc;

import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -38,10 +39,12 @@
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() {
Expand All @@ -66,7 +69,7 @@ public CitizensTraitFactory() {
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"));
Expand All @@ -77,6 +80,13 @@ public CitizensTraitFactory() {
}
}

@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()))
Expand Down Expand Up @@ -135,6 +145,9 @@ public void registerTrait(TraitInfo info) {
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();
Expand Down
Expand Up @@ -17,5 +17,5 @@ public OutputStream getOutputStream() {
return new ByteArrayOutputStream(10);
}

private static final byte[] EMPTY = new byte[20];
private static final byte[] EMPTY = new byte[50];
}
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/util/ByIdArray.java
Expand Up @@ -67,7 +67,7 @@ private void fastRemove(int index) {

@SuppressWarnings("unchecked")
public T get(int index) {
if (index > elementData.length)
if (index >= elementData.length)
return null;
return (T) elementData[index];
}
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/net/citizensnpcs/util/StringHelper.java
Expand Up @@ -6,61 +6,6 @@
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()));
}
Expand Down

0 comments on commit 141e665

Please sign in to comment.