From 7dad81c6ed1794c8f5239fcf7746cfdc69a96443 Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 31 Jul 2013 18:41:09 +0800 Subject: [PATCH] Fix bounds check in ByIdArray --- .../npc/CitizensTraitFactory.java | 15 ++++- .../citizensnpcs/npc/network/EmptySocket.java | 2 +- .../java/net/citizensnpcs/util/ByIdArray.java | 2 +- .../net/citizensnpcs/util/StringHelper.java | 55 ------------------- 4 files changed, 16 insertions(+), 58 deletions(-) diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index 8ff3a96fa..30d90f07e 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/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; @@ -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 defaultTraits = Lists.newArrayList(); private final Map 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 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 INTERNAL_TRAITS = Sets.newHashSet(); diff --git a/src/main/java/net/citizensnpcs/npc/network/EmptySocket.java b/src/main/java/net/citizensnpcs/npc/network/EmptySocket.java index 72636ebd9..0a81fe224 100644 --- a/src/main/java/net/citizensnpcs/npc/network/EmptySocket.java +++ b/src/main/java/net/citizensnpcs/npc/network/EmptySocket.java @@ -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]; } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/util/ByIdArray.java b/src/main/java/net/citizensnpcs/util/ByIdArray.java index bf8d03cdb..4fef7bbdc 100644 --- a/src/main/java/net/citizensnpcs/util/ByIdArray.java +++ b/src/main/java/net/citizensnpcs/util/ByIdArray.java @@ -67,7 +67,7 @@ public class ByIdArray implements Iterable { @SuppressWarnings("unchecked") public T get(int index) { - if (index > elementData.length) + if (index >= elementData.length) return null; return (T) elementData[index]; } diff --git a/src/main/java/net/citizensnpcs/util/StringHelper.java b/src/main/java/net/citizensnpcs/util/StringHelper.java index b3f69449d..1d88b056a 100644 --- a/src/main/java/net/citizensnpcs/util/StringHelper.java +++ b/src/main/java/net/citizensnpcs/util/StringHelper.java @@ -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())); }