From 839d00f9d6ca09fc7b2b4fe86b7778e91ee40040 Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Sun, 29 Jan 2012 11:10:40 +0100 Subject: Add some testing --- src/main/java/org/bukkit/Achievement.java | 27 ++++-- src/main/java/org/bukkit/Art.java | 34 ++++--- src/main/java/org/bukkit/ChatColor.java | 35 ++++--- src/main/java/org/bukkit/CoalType.java | 19 ++-- src/main/java/org/bukkit/CropState.java | 31 ++++--- src/main/java/org/bukkit/Difficulty.java | 11 ++- src/main/java/org/bukkit/DyeColor.java | 47 +++++----- src/main/java/org/bukkit/Effect.java | 30 +++++- src/main/java/org/bukkit/EntityEffect.java | 47 ++++++++-- src/main/java/org/bukkit/GameMode.java | 11 ++- src/main/java/org/bukkit/GrassSpecies.java | 21 +++-- src/main/java/org/bukkit/Instrument.java | 23 ++--- src/main/java/org/bukkit/Material.java | 49 +++++----- src/main/java/org/bukkit/Note.java | 141 ++++++++++++++++------------- src/main/java/org/bukkit/Statistic.java | 23 ++++- src/main/java/org/bukkit/TreeSpecies.java | 24 ++--- src/main/java/org/bukkit/WorldType.java | 19 ++-- 17 files changed, 360 insertions(+), 232 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/bukkit/Achievement.java b/src/main/java/org/bukkit/Achievement.java index 06b41a1a..4226ca8f 100644 --- a/src/main/java/org/bukkit/Achievement.java +++ b/src/main/java/org/bukkit/Achievement.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents an achievement, which may be given to players */ @@ -27,8 +28,8 @@ public enum Achievement { /** * The offset used to distinguish Achievements and Statistics */ - public final static int STATISTIC_OFFSET = 5242880; - private final static Map achievements = new HashMap(); + public final static int STATISTIC_OFFSET = 0x500000; + private final static Map BY_ID = Maps.newHashMap(); private final int id; private Achievement(int id) { @@ -53,14 +54,28 @@ public enum Achievement { * * @param id ID of the achievement to return * @return Achievement with the given ID + * @deprecated use {@link Achievement#getById(int)} */ + @Deprecated public static Achievement getAchievement(int id) { - return achievements.get(id); + return BY_ID.get(id); + } + + /** + * Gets the achievement associated with the given ID. + *

+ * Note that the ID must already be offset using {@link #STATISTIC_OFFSET} + * + * @param id ID of the achievement to return + * @return Achievement with the given ID + */ + public static Achievement getById(int id) { + return BY_ID.get(id); } static { - for (Achievement ach : values()) { - achievements.put(ach.getId(), ach); + for (Achievement achievement : values()) { + BY_ID.put(achievement.id, achievement); } } } diff --git a/src/main/java/org/bukkit/Art.java b/src/main/java/org/bukkit/Art.java index 10fad07b..02ac8983 100644 --- a/src/main/java/org/bukkit/Art.java +++ b/src/main/java/org/bukkit/Art.java @@ -2,6 +2,10 @@ package org.bukkit; import java.util.HashMap; +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; + /** * Represents the art on a painting */ @@ -20,26 +24,21 @@ public enum Art { CREEBET(11, 2, 1), WANDERER(12, 1, 2), GRAHAM(13, 1, 2), - MATCH(14, 4, 2), + MATCH(14, 2, 2), BUST(15, 2, 2), STAGE(16, 2, 2), VOID(17, 2, 2), SKULL_AND_ROSES(18, 2, 2), - FIGHTERS(19, 2, 2), + FIGHTERS(19, 4, 2), POINTER(20, 4, 4), PIGSCENE(21, 4, 4), BURNINGSKULL(22, 4, 4), SKELETON(23, 4, 3), DONKEYKONG(24, 4, 3); + private int id, width, height; - private static HashMap names = new HashMap(); - private static HashMap ids = new HashMap(); - static { - for (Art art : Art.values()) { - ids.put(art.id, art); - names.put(art.toString(), art); - } - } + private static final HashMap BY_NAME = Maps.newHashMap(); + private static final HashMap BY_ID = Maps.newHashMap(); private Art(int id, int width, int height) { this.id = id; @@ -81,16 +80,27 @@ public enum Art { * @return The painting */ public static Art getById(int id) { - return ids.get(id); + return BY_ID.get(id); } /** * Get a painting by its unique name + *

+ * This ignores underscores and capitalization * * @param name The name * @return The painting */ public static Art getByName(String name) { - return names.get(name); + Validate.notNull(name, "Name cannot be null"); + + return BY_NAME.get(name.toLowerCase().replaceAll("_", "")); + } + + static { + for (Art art : values()) { + BY_ID.put(art.id, art); + BY_NAME.put(art.toString().toLowerCase().replaceAll("_", ""), art); + } } } diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java index b0ece128..eff64afc 100644 --- a/src/main/java/org/bukkit/ChatColor.java +++ b/src/main/java/org/bukkit/ChatColor.java @@ -1,13 +1,16 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; /** * All supported color values for chat */ public enum ChatColor { - /** * Represents black */ @@ -77,14 +80,19 @@ public enum ChatColor { */ MAGIC('k', 0x10); + public static final char COLOR_CHAR = '\u00A7'; + private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK]"); + private final int intCode; private final char code; - private final static Map colors = new HashMap(); - private final static Map lookup = new HashMap(); + private final String toString; + private final static Map BY_ID = Maps.newHashMap(); + private final static Map BY_CHAR = Maps.newHashMap(); private ChatColor(char code, int intCode) { this.code = code; this.intCode = intCode; + this.toString = new String(new char[] {COLOR_CHAR, code}); } /** @@ -108,7 +116,7 @@ public enum ChatColor { @Override public String toString() { - return String.format("\u00A7%c", code); + return toString; } /** @@ -119,7 +127,7 @@ public enum ChatColor { * @deprecated Use {@link #getByChar(char)} */ public static ChatColor getByCode(final int code) { - return colors.get(code); + return BY_ID.get(code); } /** @@ -129,7 +137,7 @@ public enum ChatColor { * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist */ public static ChatColor getByChar(char code) { - return lookup.get(code); + return BY_CHAR.get(code); } /** @@ -139,7 +147,10 @@ public enum ChatColor { * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist */ public static ChatColor getByChar(String code) { - return lookup.get(code.charAt(0)); + Validate.notNull(code, "Code cannot be null"); + Validate.isTrue(code.length() > 0, "Code must have at least one char"); + + return BY_CHAR.get(code.charAt(0)); } /** @@ -153,13 +164,13 @@ public enum ChatColor { return null; } - return input.replaceAll("(?i)\u00A7[0-9A-FK]", ""); + return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); } static { - for (ChatColor color : ChatColor.values()) { - colors.put(color.getCode(), color); - lookup.put(color.getChar(), color); + for (ChatColor color : values()) { + BY_ID.put(color.intCode, color); + BY_CHAR.put(color.code, color); } } } diff --git a/src/main/java/org/bukkit/CoalType.java b/src/main/java/org/bukkit/CoalType.java index befb117f..70404c11 100644 --- a/src/main/java/org/bukkit/CoalType.java +++ b/src/main/java/org/bukkit/CoalType.java @@ -1,20 +1,21 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the two types of coal */ public enum CoalType { - COAL((byte) 0x0), - CHARCOAL((byte) 0x1); + COAL(0x0), + CHARCOAL(0x1); private final byte data; - private final static Map types = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private CoalType(byte data) { - this.data = data; + private CoalType(final int data) { + this.data = (byte) data; } /** @@ -35,12 +36,12 @@ public enum CoalType { * it doesn't exist */ public static CoalType getByData(final byte data) { - return types.get(data); + return BY_DATA.get(data); } static { - for (CoalType type : CoalType.values()) { - types.put(type.getData(), type); + for (CoalType type : values()) { + BY_DATA.put(type.data, type); } } } diff --git a/src/main/java/org/bukkit/CropState.java b/src/main/java/org/bukkit/CropState.java index 8167f967..96ba24d8 100644 --- a/src/main/java/org/bukkit/CropState.java +++ b/src/main/java/org/bukkit/CropState.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different growth states of crops */ @@ -11,41 +12,41 @@ public enum CropState { /** * State when first seeded */ - SEEDED((byte) 0x0), + SEEDED(0x0), /** * First growth stage */ - GERMINATED((byte) 0x1), + GERMINATED(0x1), /** * Second growth stage */ - VERY_SMALL((byte) 0x2), + VERY_SMALL(0x2), /** * Third growth stage */ - SMALL((byte) 0x3), + SMALL(0x3), /** * Fourth growth stage */ - MEDIUM((byte) 0x4), + MEDIUM(0x4), /** * Fifth growth stage */ - TALL((byte) 0x5), + TALL(0x5), /** * Almost ripe stage */ - VERY_TALL((byte) 0x6), + VERY_TALL(0x6), /** * Ripe stage */ - RIPE((byte) 0x7); + RIPE(0x7); private final byte data; - private final static Map states = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private CropState(final byte data) { - this.data = data; + private CropState(final int data) { + this.data = (byte) data; } /** @@ -66,12 +67,12 @@ public enum CropState { * it doesn't exist */ public static CropState getByData(final byte data) { - return states.get(data); + return BY_DATA.get(data); } static { - for (CropState s : CropState.values()) { - states.put(s.getData(), s); + for (CropState cropState : values()) { + BY_DATA.put(cropState.getData(), cropState); } } } diff --git a/src/main/java/org/bukkit/Difficulty.java b/src/main/java/org/bukkit/Difficulty.java index da2876b0..cc885e08 100644 --- a/src/main/java/org/bukkit/Difficulty.java +++ b/src/main/java/org/bukkit/Difficulty.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the various difficulty levels that are available. */ @@ -28,7 +29,7 @@ public enum Difficulty { HARD(3); private final int value; - private final static Map difficulties = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private Difficulty(final int value) { this.value = value; @@ -50,12 +51,12 @@ public enum Difficulty { * @return Associative {@link Difficulty} with the given value, or null if it doesn't exist */ public static Difficulty getByValue(final int value) { - return difficulties.get(value); + return BY_ID.get(value); } static { - for (Difficulty diff : Difficulty.values()) { - difficulties.put(diff.getValue(), diff); + for (Difficulty diff : values()) { + BY_ID.put(diff.value, diff); } } } diff --git a/src/main/java/org/bukkit/DyeColor.java b/src/main/java/org/bukkit/DyeColor.java index b216dd8e..531323fe 100644 --- a/src/main/java/org/bukkit/DyeColor.java +++ b/src/main/java/org/bukkit/DyeColor.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * All supported color values for dyes and cloth */ @@ -11,73 +12,73 @@ public enum DyeColor { /** * Represents white dye */ - WHITE((byte) 0x0), + WHITE(0x0), /** * Represents orange dye */ - ORANGE((byte) 0x1), + ORANGE(0x1), /** * Represents magenta dye */ - MAGENTA((byte) 0x2), + MAGENTA(0x2), /** * Represents light blue dye */ - LIGHT_BLUE((byte) 0x3), + LIGHT_BLUE(0x3), /** * Represents yellow dye */ - YELLOW((byte) 0x4), + YELLOW(0x4), /** * Represents lime dye */ - LIME((byte) 0x5), + LIME(0x5), /** * Represents pink dye */ - PINK((byte) 0x6), + PINK(0x6), /** * Represents gray dye */ - GRAY((byte) 0x7), + GRAY(0x7), /** * Represents silver dye */ - SILVER((byte) 0x8), + SILVER(0x8), /** * Represents cyan dye */ - CYAN((byte) 0x9), + CYAN(0x9), /** * Represents purple dye */ - PURPLE((byte) 0xA), + PURPLE(0xA), /** * Represents blue dye */ - BLUE((byte) 0xB), + BLUE(0xB), /** * Represents brown dye */ - BROWN((byte) 0xC), + BROWN(0xC), /** * Represents green dye */ - GREEN((byte) 0xD), + GREEN(0xD), /** * Represents red dye */ - RED((byte) 0xE), + RED(0xE), /** * Represents black dye */ - BLACK((byte) 0xF); + BLACK(0xF); private final byte data; - private final static Map colors = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private DyeColor(final byte data) { - this.data = data; + private DyeColor(final int data) { + this.data = (byte) data; } /** @@ -96,12 +97,12 @@ public enum DyeColor { * @return The {@link DyeColor} representing the given value, or null if it doesn't exist */ public static DyeColor getByData(final byte data) { - return colors.get(data); + return BY_DATA.get(data); } static { - for (DyeColor color : DyeColor.values()) { - colors.put(color.getData(), color); + for (DyeColor color : values()) { + BY_DATA.put(color.getData(), color); } } } diff --git a/src/main/java/org/bukkit/Effect.java b/src/main/java/org/bukkit/Effect.java index 028c27dd..14c197df 100644 --- a/src/main/java/org/bukkit/Effect.java +++ b/src/main/java/org/bukkit/Effect.java @@ -1,12 +1,16 @@ package org.bukkit; +import java.util.Map; + +import com.google.common.collect.Maps; + /** * A list of effects that the server is able to send to players. */ public enum Effect { - BOW_FIRE(1002), - CLICK1(1001), CLICK2(1000), + CLICK1(1001), + BOW_FIRE(1002), DOOR_TOGGLE(1003), EXTINGUISH(1004), RECORD_PLAY(1005), @@ -20,12 +24,34 @@ public enum Effect { MOBSPAWNER_FLAMES(2004); private final int id; + private static final Map BY_ID = Maps.newHashMap(); Effect(int id) { this.id = id; } + /** + * Gets the ID for this effect. + * + * @return ID of this effect + */ public int getId() { return this.id; } + + /** + * Gets the Effect associated with the given ID. + * + * @param id ID of the Effect to return + * @return Effect with the given ID + */ + public static Effect getById(int id) { + return BY_ID.get(id); + } + + static { + for (Effect effect : values()) { + BY_ID.put(effect.id, effect); + } + } } diff --git a/src/main/java/org/bukkit/EntityEffect.java b/src/main/java/org/bukkit/EntityEffect.java index 4a04e96a..fbf21fb3 100644 --- a/src/main/java/org/bukkit/EntityEffect.java +++ b/src/main/java/org/bukkit/EntityEffect.java @@ -1,5 +1,9 @@ package org.bukkit; +import java.util.Map; + +import com.google.common.collect.Maps; + /** * A list of all Effects that can happen to entities. */ @@ -8,46 +12,73 @@ public enum EntityEffect { /** * When mobs get hurt. */ - HURT((byte) 2), + HURT(2), /** * When a mob dies. *

* This will cause client-glitches! */ - DEATH((byte) 3), + DEATH(3), /** * The smoke when taming a wolf fails. *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_SMOKE((byte) 6), + WOLF_SMOKE(6), /** * The hearts when taming a wolf succeeds. *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_HEARTS((byte) 7), + WOLF_HEARTS(7), /** * When a wolf shakes (after being wet). *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_SHAKE((byte) 8); + WOLF_SHAKE(8), + + /** + * When a sheep eats a LONG_GRASS block. + */ + SHEEP_EAT(10); private final byte data; + private final static Map BY_DATA = Maps.newHashMap(); - EntityEffect(byte data) { - this.data = data; + EntityEffect(final int data) { + this.data = (byte) data; } /** - * @return The data-value that is sent to the client to play this effect. + * Gets the EntityEffect with the given data value + * + * @param data Data value to fetch + * @return The {@link EntityEffect} representing the given value, or null if + * it doesn't exist */ public byte getData() { return data; } + + /** + * Gets the EntityEffect with the given data value + * + * @param data Data value to fetch + * @return The {@link EntityEffect} representing the given value, or null if it doesn't exist + */ + public static EntityEffect getByData(final byte data) { + return BY_DATA.get(data); + } + + + static { + for (EntityEffect entityEffect : values()) { + BY_DATA.put(entityEffect.data, entityEffect); + } + } } diff --git a/src/main/java/org/bukkit/GameMode.java b/src/main/java/org/bukkit/GameMode.java index 2c516750..b81b069c 100644 --- a/src/main/java/org/bukkit/GameMode.java +++ b/src/main/java/org/bukkit/GameMode.java @@ -1,10 +1,11 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; import org.bukkit.entity.HumanEntity; +import com.google.common.collect.Maps; + /** * Represents the various type of game modes that {@link HumanEntity}s may have */ @@ -20,7 +21,7 @@ public enum GameMode { SURVIVAL(0); private final int value; - private final static Map modes = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private GameMode(final int value) { this.value = value; @@ -42,12 +43,12 @@ public enum GameMode { * @return Associative {@link GameMode} with the given value, or null if it doesn't exist */ public static GameMode getByValue(final int value) { - return modes.get(value); + return BY_ID.get(value); } static { - for (GameMode mode : GameMode.values()) { - modes.put(mode.getValue(), mode); + for (GameMode mode : values()) { + BY_ID.put(mode.getValue(), mode); } } } diff --git a/src/main/java/org/bukkit/GrassSpecies.java b/src/main/java/org/bukkit/GrassSpecies.java index d7387513..3f2cce42 100644 --- a/src/main/java/org/bukkit/GrassSpecies.java +++ b/src/main/java/org/bukkit/GrassSpecies.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different types of grass. */ @@ -11,21 +12,21 @@ public enum GrassSpecies { /** * Represents the dead looking grass. */ - DEAD((byte) 0x0), + DEAD(0x0), /** * Represents the normal grass species. */ - NORMAL((byte) 0x1), + NORMAL(0x1), /** * Represents the fern-looking grass species. */ - FERN_LIKE((byte) 0x2); + FERN_LIKE(0x2); private final byte data; - private final static Map species = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private GrassSpecies(final byte data) { - this.data = data; + private GrassSpecies(final int data) { + this.data = (byte) data; } /** @@ -46,12 +47,12 @@ public enum GrassSpecies { * it doesn't exist */ public static GrassSpecies getByData(final byte data) { - return species.get(data); + return BY_DATA.get(data); } static { - for (GrassSpecies s : GrassSpecies.values()) { - species.put(s.getData(), s); + for (GrassSpecies grassSpecies : values()) { + BY_DATA.put(grassSpecies.getData(), grassSpecies); } } } diff --git a/src/main/java/org/bukkit/Instrument.java b/src/main/java/org/bukkit/Instrument.java index e54d0a2a..0d19db67 100644 --- a/src/main/java/org/bukkit/Instrument.java +++ b/src/main/java/org/bukkit/Instrument.java @@ -1,21 +1,22 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + public enum Instrument { - PIANO((byte) 0x0), // All other - BASS_DRUM((byte) 0x1), // Stone - SNARE_DRUM((byte) 0x2), // Sand - STICKS((byte) 0x3), // Glass - BASS_GUITAR((byte) 0x4); // Wood + PIANO(0x0), // All other + BASS_DRUM(0x1), // Stone + SNARE_DRUM(0x2), // Sand + STICKS(0x3), // Glass + BASS_GUITAR(0x4); // Wood private final byte type; - private final static Map types = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private Instrument(byte type) { - this.type = type; + private Instrument(final int type) { + this.type = (byte) type; } public byte getType() { @@ -23,12 +24,12 @@ public enum Instrument { } public static Instrument getByType(final byte type) { - return types.get(type); + return BY_DATA.get(type); } static { for (Instrument instrument : Instrument.values()) { - types.put(instrument.getType(), instrument); + BY_DATA.put(instrument.getType(), instrument); } } } diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java index 23747603..e9fadb05 100644 --- a/src/main/java/org/bukkit/Material.java +++ b/src/main/java/org/bukkit/Material.java @@ -2,13 +2,16 @@ package org.bukkit; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; + +import org.apache.commons.lang.Validate; import org.bukkit.material.*; import org.bukkit.util.Java15Compat; +import com.google.common.collect.Maps; + /** * An enum of all material ids accepted by the official server + client */ @@ -76,12 +79,12 @@ public enum Material { SOIL(60, MaterialData.class), FURNACE(61, Furnace.class), BURNING_FURNACE(62, Furnace.class), - SIGN_POST(63, 1, Sign.class), + SIGN_POST(63, 64, Sign.class), WOODEN_DOOR(64, Door.class), LADDER(65, Ladder.class), RAILS(66, Rails.class), COBBLESTONE_STAIRS(67, Stairs.class), - WALL_SIGN(68, 1, Sign.class), + WALL_SIGN(68, 64, Sign.class), LEVER(69, Lever.class), STONE_PLATE(70, PressurePlate.class), IRON_DOOR_BLOCK(71, Door.class), @@ -105,7 +108,7 @@ public enum Material { GLOWSTONE(89), PORTAL(90), JACK_O_LANTERN(91, Pumpkin.class), - CAKE_BLOCK(92, 1, Cake.class), + CAKE_BLOCK(92, 64, Cake.class), DIODE_BLOCK_OFF(93, Diode.class), DIODE_BLOCK_ON(94, Diode.class), LOCKED_CHEST(95), @@ -179,11 +182,11 @@ public enum Material { SEEDS(295), WHEAT(296), BREAD(297), - LEATHER_HELMET(298, 1, 50), + LEATHER_HELMET(298, 1, 55), LEATHER_CHESTPLATE(299, 1, 80), LEATHER_LEGGINGS(300, 1, 75), LEATHER_BOOTS(301, 1, 65), - CHAINMAIL_HELMET(302, 1, 166), + CHAINMAIL_HELMET(302, 1, 165), CHAINMAIL_CHESTPLATE(303, 1, 240), CHAINMAIL_LEGGINGS(304, 1, 225), CHAINMAIL_BOOTS(305, 1, 195), @@ -279,8 +282,8 @@ public enum Material { private final int id; private final Class data; - private static Material[] lookupId = new Material[3200]; - private static final Map lookupName = new HashMap(); + private static Material[] byId = new Material[383]; + private final static Map BY_NAME = Maps.newHashMap(); private final int maxStack; private final short durability; @@ -301,14 +304,14 @@ public enum Material { } private Material(final int id, final int stack, final Class data) { - this(id, stack, -1, data); + this(id, stack, 0, data); } private Material(final int id, final int stack, final int durability, final Class data) { this.id = id; this.durability = (short) durability; this.maxStack = stack; - this.data = data; + this.data = data == null ? MaterialData.class : data; } /** @@ -344,7 +347,7 @@ public enum Material { * @return MaterialData associated with this Material */ public Class getData() { - return (data == null) ? MaterialData.class : data; + return data; } /** @@ -355,10 +358,6 @@ public enum Material { * @return New MaterialData with the given data */ public MaterialData getNewData(final byte raw) { - if (data == null) { - return new MaterialData(id, raw); - } - try { Constructor ctor = data.getConstructor(int.class, byte.class); @@ -420,8 +419,8 @@ public enum Material { * @return Material if found, or null */ public static Material getMaterial(final int id) { - if (lookupId.length > id) { - return lookupId[id]; + if (byId.length > id) { + return byId[id]; } else { return null; } @@ -436,7 +435,7 @@ public enum Material { * @return Material if found, or null */ public static Material getMaterial(final String name) { - return lookupName.get(name); + return BY_NAME.get(name); } /** @@ -448,6 +447,8 @@ public enum Material { * @return Material if found, or null */ public static Material matchMaterial(final String name) { + Validate.notNull(name, "Name cannot be null"); + Material result = null; try { @@ -458,7 +459,7 @@ public enum Material { String filtered = name.toUpperCase(); filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); - result = lookupName.get(filtered); + result = BY_NAME.get(filtered); } return result; @@ -466,13 +467,13 @@ public enum Material { static { for (Material material : values()) { - if (lookupId.length > material.id) { - lookupId[material.id] = material; + if (byId.length > material.id) { + byId[material.id] = material; } else { - lookupId = Java15Compat.Arrays_copyOfRange(lookupId, 0, material.id + 2); - lookupId[material.id] = material; + byId = Java15Compat.Arrays_copyOfRange(byId, 0, material.id + 2); + byId[material.id] = material; } - lookupName.put(material.name(), material); + BY_NAME.put(material.name(), material); } } } diff --git a/src/main/java/org/bukkit/Note.java b/src/main/java/org/bukkit/Note.java index d8b88124..fd84c14c 100644 --- a/src/main/java/org/bukkit/Note.java +++ b/src/main/java/org/bukkit/Note.java @@ -1,8 +1,11 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; + /** * A note class to store a specific note. */ @@ -12,22 +15,23 @@ public class Note { * An enum holding tones. */ public enum Tone { - F((byte) -0x1, true), - G((byte) 0x1, true), - A((byte) 0x3, true), - B((byte) 0x5, false), - C((byte) 0x6, true), - D((byte) 0x8, true), - E((byte) 0xA, false); + G(0x1, true), + A(0x3, true), + B(0x5, false), + C(0x6, true), + D(0x8, true), + E(0xA, false), + F(0xB, true); private final boolean sharpable; private final byte id; - private static final Map tones = new HashMap(); + + private static final Map BY_DATA = Maps.newHashMap(); /** The number of tones including sharped tones. */ - public static final byte TONES_COUNT; + public static final byte TONES_COUNT = 12; - private Tone(byte id, boolean sharpable) { - this.id = id; + private Tone(int id, boolean sharpable) { + this.id = (byte) (id % TONES_COUNT); this.sharpable = sharpable; } @@ -45,17 +49,13 @@ public class Note { * sharped id of the tone. If the tone couldn't be sharped it always * return the not sharped id of this tone. * - * @param sharped - * Set to true to return the sharped id. + * @param sharped Set to true to return the sharped id. * @return the id of this tone. */ public byte getId(boolean sharped) { - byte tempId = (byte) (sharped && sharpable ? id + 1 : id); + byte id = (byte) (sharped && sharpable ? this.id + 1 : this.id); - while (tempId < 0) { - tempId += TONES_COUNT; - } - return (byte) (tempId % TONES_COUNT); + return (byte) (id % TONES_COUNT); } /** @@ -70,11 +70,9 @@ public class Note { /** * Returns if this tone id is the sharped id of the tone. * - * @param id - * the id of the tone. + * @param id the id of the tone. * @return if the tone id is the sharped id of the tone. - * @throws IllegalArgumentException - * if neither the tone nor the semitone have the id. + * @throws IllegalArgumentException if neither the tone nor the semitone have the id. */ public boolean isSharped(byte id) { if (id == getId(false)) { @@ -90,34 +88,35 @@ public class Note { /** * Returns the tone to id. Also returning the semitones. * - * @param id - * the id of the tone. + * @param id the id of the tone. * @return the tone to id. + * @deprecated use {@link #getById(byte)} */ + @Deprecated public static Tone getToneById(byte id) { - return tones.get(id); + return BY_DATA.get(id); + } + + /** + * Returns the tone to id. Also returning the semitones. + * + * @param id the id of the tone. + * @return the tone to id. + */ + public static Tone getById(byte id) { + return BY_DATA.get(id); } static { - byte lowest = F.id; - byte highest = F.id; - for (Tone tone : Tone.values()) { - byte id = tone.id; - tones.put(id, tone); - if (id < lowest) { - lowest = id; - } + for (Tone tone : values()) { + int id = tone.id % TONES_COUNT; + BY_DATA.put((byte) id, tone); + if (tone.isSharpable()) { - id++; - tones.put(id, tone); - } - if (id > highest) { - highest = id; + id = (id + 1) % TONES_COUNT; + BY_DATA.put((byte) id, tone); } } - - TONES_COUNT = (byte) (highest - lowest + 1); - tones.put((byte) (TONES_COUNT - 1), F); } } @@ -126,36 +125,29 @@ public class Note { /** * Creates a new note. * - * @param note - * Internal note id. {@link #getId()} always return this value. + * @param note Internal note id. {@link #getId()} always return this value. * The value has to be in the interval [0; 24]. */ - public Note(byte note) { - if (note < 0 || note > 24) { - throw new IllegalArgumentException("The note value has to be between 0 and 24."); - } - this.note = note; + public Note(int note) { + Validate.isTrue(note >= 0 && note <= 24, "The note value has to be between 0 and 24."); + + this.note = (byte) note; } /** * Creates a new note. * - * @param octave - * The octave where the note is in. Has to be 0 - 2. - * @param note - * The tone within the octave. If the octave is 2 the note has to - * be F#. - * @param sharped - * Set it the tone is sharped (e.g. for F#). + * @param octave The octave where the note is in. Has to be 0 - 2. + * @param tone The tone within the octave. If the octave is 2 the note has to be F#. + * @param sharped Set it the tone is sharped (e.g. for F#). */ - public Note(byte octave, Tone note, boolean sharped) { - if (sharped && !note.isSharpable()) { - throw new IllegalArgumentException("This tone could not be sharped."); - } - if (octave < 0 || octave > 2 || (octave == 2 && !(note == Tone.F && sharped))) { + public Note(int octave, Tone tone, boolean sharped) { + Validate.isTrue(!(sharped && !tone.isSharpable()), "This tone could not be sharped."); + if (octave < 0 || octave > 2 || (octave == 2 && !(tone == Tone.F && sharped))) { throw new IllegalArgumentException("Tone and octave have to be between F#0 and F#2"); } - this.note = (byte) (octave * Tone.TONES_COUNT + note.getId(sharped)); + + this.note = (byte) (octave * Tone.TONES_COUNT + tone.getId(sharped)); } /** @@ -186,7 +178,7 @@ public class Note { * @return the tone of this note. */ public Tone getTone() { - return Tone.getToneById(getToneByte()); + return Tone.getById(getToneByte()); } /** @@ -196,7 +188,28 @@ public class Note { */ public boolean isSharped() { byte note = getToneByte(); - return Tone.getToneById(note).isSharped(note); + return Tone.getById(note).isSharped(note); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + note; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Note other = (Note) obj; + if (note != other.note) + return false; + return true; + } } diff --git a/src/main/java/org/bukkit/Statistic.java b/src/main/java/org/bukkit/Statistic.java index e7195bb6..41d1b838 100644 --- a/src/main/java/org/bukkit/Statistic.java +++ b/src/main/java/org/bukkit/Statistic.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents a countable statistic, which is collected by the client */ @@ -17,7 +18,7 @@ public enum Statistic { USE_ITEM(6908288, false), BREAK_ITEM(16973824, true); - private final static Map statistics = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private final int id; private final boolean isSubstat; private final boolean isBlock; @@ -70,14 +71,26 @@ public enum Statistic { * * @param id ID of the statistic to return * @return statistic with the given ID + * @deprecated See {@link #getById(int)} */ + @Deprecated public static Statistic getStatistic(int id) { - return statistics.get(id); + return BY_ID.get(id); + } + + /** + * Gets the statistic associated with the given ID. + * + * @param id ID of the statistic to return + * @return statistic with the given ID + */ + public static Statistic getById(int id) { + return BY_ID.get(id); } static { - for (Statistic stat : values()) { - statistics.put(stat.getId(), stat); + for (Statistic statistic : values()) { + BY_ID.put(statistic.id, statistic); } } } diff --git a/src/main/java/org/bukkit/TreeSpecies.java b/src/main/java/org/bukkit/TreeSpecies.java index cc286731..d01b79f4 100644 --- a/src/main/java/org/bukkit/TreeSpecies.java +++ b/src/main/java/org/bukkit/TreeSpecies.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different species of trees regardless of size. */ @@ -11,21 +12,21 @@ public enum TreeSpecies { /** * Represents the common tree species. */ - GENERIC((byte) 0x0), + GENERIC(0x0), /** * Represents the darker barked/leaved tree species. */ - REDWOOD((byte) 0x1), + REDWOOD(0x1), /** * Represents birches. */ - BIRCH((byte) 0x2); + BIRCH(0x2); private final byte data; - private final static Map species = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private TreeSpecies(final byte data) { - this.data = data; + private TreeSpecies(final int data) { + this.data = (byte) data; } /** @@ -40,18 +41,17 @@ public enum TreeSpecies { /** * Gets the TreeSpecies with the given data value * - * @param data - * Data value to fetch + * @param data Data value to fetch * @return The {@link TreeSpecies} representing the given value, or null if * it doesn't exist */ public static TreeSpecies getByData(final byte data) { - return species.get(data); + return BY_DATA.get(data); } static { - for (TreeSpecies s : TreeSpecies.values()) { - species.put(s.getData(), s); + for (TreeSpecies species : values()) { + BY_DATA.put(species.data, species); } } } diff --git a/src/main/java/org/bukkit/WorldType.java b/src/main/java/org/bukkit/WorldType.java index 36ad85ef..93c0957c 100644 --- a/src/main/java/org/bukkit/WorldType.java +++ b/src/main/java/org/bukkit/WorldType.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents various types of worlds that may exist */ @@ -10,15 +11,9 @@ public enum WorldType { NORMAL("DEFAULT"), FLAT("FLAT"); - private final static Map lookup = new HashMap(); + private final static Map BY_NAME = Maps.newHashMap(); private final String name; - static { - for (WorldType type : values()) { - lookup.put(type.name, type); - } - } - private WorldType(String name) { this.name = name; } @@ -39,6 +34,12 @@ public enum WorldType { * @return Requested WorldType, or null if not found */ public static WorldType getByName(String name) { - return lookup.get(name.toUpperCase()); + return BY_NAME.get(name.toUpperCase()); + } + + static { + for (WorldType type : values()) { + BY_NAME.put(type.name, type); + } } } -- cgit v1.2.3