Add some testing

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot 2012-01-29 11:10:40 +01:00
parent ae0eb736d1
commit ac2271958e
34 changed files with 871 additions and 281 deletions

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents an achievement, which may be given to players * Represents an achievement, which may be given to players
*/ */
@ -27,8 +28,8 @@ public enum Achievement {
/** /**
* The offset used to distinguish Achievements and Statistics * The offset used to distinguish Achievements and Statistics
*/ */
public final static int STATISTIC_OFFSET = 5242880; public final static int STATISTIC_OFFSET = 0x500000;
private final static Map<Integer, Achievement> achievements = new HashMap<Integer, Achievement>(); private final static Map<Integer, Achievement> BY_ID = Maps.newHashMap();
private final int id; private final int id;
private Achievement(int id) { private Achievement(int id) {
@ -53,14 +54,28 @@ public enum Achievement {
* *
* @param id ID of the achievement to return * @param id ID of the achievement to return
* @return Achievement with the given ID * @return Achievement with the given ID
* @deprecated use {@link Achievement#getById(int)}
*/ */
@Deprecated
public static Achievement getAchievement(int id) { public static Achievement getAchievement(int id) {
return achievements.get(id); return BY_ID.get(id);
}
/**
* Gets the achievement associated with the given ID.
* <p />
* 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 { static {
for (Achievement ach : values()) { for (Achievement achievement : values()) {
achievements.put(ach.getId(), ach); BY_ID.put(achievement.id, achievement);
} }
} }
} }

View file

@ -2,6 +2,10 @@ package org.bukkit;
import java.util.HashMap; import java.util.HashMap;
import org.apache.commons.lang.Validate;
import com.google.common.collect.Maps;
/** /**
* Represents the art on a painting * Represents the art on a painting
*/ */
@ -20,26 +24,21 @@ public enum Art {
CREEBET(11, 2, 1), CREEBET(11, 2, 1),
WANDERER(12, 1, 2), WANDERER(12, 1, 2),
GRAHAM(13, 1, 2), GRAHAM(13, 1, 2),
MATCH(14, 4, 2), MATCH(14, 2, 2),
BUST(15, 2, 2), BUST(15, 2, 2),
STAGE(16, 2, 2), STAGE(16, 2, 2),
VOID(17, 2, 2), VOID(17, 2, 2),
SKULL_AND_ROSES(18, 2, 2), SKULL_AND_ROSES(18, 2, 2),
FIGHTERS(19, 2, 2), FIGHTERS(19, 4, 2),
POINTER(20, 4, 4), POINTER(20, 4, 4),
PIGSCENE(21, 4, 4), PIGSCENE(21, 4, 4),
BURNINGSKULL(22, 4, 4), BURNINGSKULL(22, 4, 4),
SKELETON(23, 4, 3), SKELETON(23, 4, 3),
DONKEYKONG(24, 4, 3); DONKEYKONG(24, 4, 3);
private int id, width, height; private int id, width, height;
private static HashMap<String, Art> names = new HashMap<String, Art>(); private static final HashMap<String, Art> BY_NAME = Maps.newHashMap();
private static HashMap<Integer, Art> ids = new HashMap<Integer, Art>(); private static final HashMap<Integer, Art> BY_ID = Maps.newHashMap();
static {
for (Art art : Art.values()) {
ids.put(art.id, art);
names.put(art.toString(), art);
}
}
private Art(int id, int width, int height) { private Art(int id, int width, int height) {
this.id = id; this.id = id;
@ -81,16 +80,27 @@ public enum Art {
* @return The painting * @return The painting
*/ */
public static Art getById(int id) { public static Art getById(int id) {
return ids.get(id); return BY_ID.get(id);
} }
/** /**
* Get a painting by its unique name * Get a painting by its unique name
* <p />
* This ignores underscores and capitalization
* *
* @param name The name * @param name The name
* @return The painting * @return The painting
*/ */
public static Art getByName(String name) { 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);
}
} }
} }

View file

@ -1,13 +1,16 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; 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 * All supported color values for chat
*/ */
public enum ChatColor { public enum ChatColor {
/** /**
* Represents black * Represents black
*/ */
@ -77,14 +80,19 @@ public enum ChatColor {
*/ */
MAGIC('k', 0x10); 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 int intCode;
private final char code; private final char code;
private final static Map<Integer, ChatColor> colors = new HashMap<Integer, ChatColor>(); private final String toString;
private final static Map<Character, ChatColor> lookup = new HashMap<Character, ChatColor>(); private final static Map<Integer, ChatColor> BY_ID = Maps.newHashMap();
private final static Map<Character, ChatColor> BY_CHAR = Maps.newHashMap();
private ChatColor(char code, int intCode) { private ChatColor(char code, int intCode) {
this.code = code; this.code = code;
this.intCode = intCode; this.intCode = intCode;
this.toString = new String(new char[] {COLOR_CHAR, code});
} }
/** /**
@ -108,7 +116,7 @@ public enum ChatColor {
@Override @Override
public String toString() { public String toString() {
return String.format("\u00A7%c", code); return toString;
} }
/** /**
@ -119,7 +127,7 @@ public enum ChatColor {
* @deprecated Use {@link #getByChar(char)} * @deprecated Use {@link #getByChar(char)}
*/ */
public static ChatColor getByCode(final int code) { 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 * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist
*/ */
public static ChatColor getByChar(char code) { 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 * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist
*/ */
public static ChatColor getByChar(String code) { 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 null;
} }
return input.replaceAll("(?i)\u00A7[0-9A-FK]", ""); return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
} }
static { static {
for (ChatColor color : ChatColor.values()) { for (ChatColor color : values()) {
colors.put(color.getCode(), color); BY_ID.put(color.intCode, color);
lookup.put(color.getChar(), color); BY_CHAR.put(color.code, color);
} }
} }
} }

View file

@ -1,20 +1,21 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents the two types of coal * Represents the two types of coal
*/ */
public enum CoalType { public enum CoalType {
COAL((byte) 0x0), COAL(0x0),
CHARCOAL((byte) 0x1); CHARCOAL(0x1);
private final byte data; private final byte data;
private final static Map<Byte, CoalType> types = new HashMap<Byte, CoalType>(); private final static Map<Byte, CoalType> BY_DATA = Maps.newHashMap();
private CoalType(byte data) { private CoalType(final int data) {
this.data = data; this.data = (byte) data;
} }
/** /**
@ -35,12 +36,12 @@ public enum CoalType {
* it doesn't exist * it doesn't exist
*/ */
public static CoalType getByData(final byte data) { public static CoalType getByData(final byte data) {
return types.get(data); return BY_DATA.get(data);
} }
static { static {
for (CoalType type : CoalType.values()) { for (CoalType type : values()) {
types.put(type.getData(), type); BY_DATA.put(type.data, type);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents the different growth states of crops * Represents the different growth states of crops
*/ */
@ -11,41 +12,41 @@ public enum CropState {
/** /**
* State when first seeded * State when first seeded
*/ */
SEEDED((byte) 0x0), SEEDED(0x0),
/** /**
* First growth stage * First growth stage
*/ */
GERMINATED((byte) 0x1), GERMINATED(0x1),
/** /**
* Second growth stage * Second growth stage
*/ */
VERY_SMALL((byte) 0x2), VERY_SMALL(0x2),
/** /**
* Third growth stage * Third growth stage
*/ */
SMALL((byte) 0x3), SMALL(0x3),
/** /**
* Fourth growth stage * Fourth growth stage
*/ */
MEDIUM((byte) 0x4), MEDIUM(0x4),
/** /**
* Fifth growth stage * Fifth growth stage
*/ */
TALL((byte) 0x5), TALL(0x5),
/** /**
* Almost ripe stage * Almost ripe stage
*/ */
VERY_TALL((byte) 0x6), VERY_TALL(0x6),
/** /**
* Ripe stage * Ripe stage
*/ */
RIPE((byte) 0x7); RIPE(0x7);
private final byte data; private final byte data;
private final static Map<Byte, CropState> states = new HashMap<Byte, CropState>(); private final static Map<Byte, CropState> BY_DATA = Maps.newHashMap();
private CropState(final byte data) { private CropState(final int data) {
this.data = data; this.data = (byte) data;
} }
/** /**
@ -66,12 +67,12 @@ public enum CropState {
* it doesn't exist * it doesn't exist
*/ */
public static CropState getByData(final byte data) { public static CropState getByData(final byte data) {
return states.get(data); return BY_DATA.get(data);
} }
static { static {
for (CropState s : CropState.values()) { for (CropState cropState : values()) {
states.put(s.getData(), s); BY_DATA.put(cropState.getData(), cropState);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents the various difficulty levels that are available. * Represents the various difficulty levels that are available.
*/ */
@ -28,7 +29,7 @@ public enum Difficulty {
HARD(3); HARD(3);
private final int value; private final int value;
private final static Map<Integer, Difficulty> difficulties = new HashMap<Integer, Difficulty>(); private final static Map<Integer, Difficulty> BY_ID = Maps.newHashMap();
private Difficulty(final int value) { private Difficulty(final int value) {
this.value = 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 * @return Associative {@link Difficulty} with the given value, or null if it doesn't exist
*/ */
public static Difficulty getByValue(final int value) { public static Difficulty getByValue(final int value) {
return difficulties.get(value); return BY_ID.get(value);
} }
static { static {
for (Difficulty diff : Difficulty.values()) { for (Difficulty diff : values()) {
difficulties.put(diff.getValue(), diff); BY_ID.put(diff.value, diff);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* All supported color values for dyes and cloth * All supported color values for dyes and cloth
*/ */
@ -11,73 +12,73 @@ public enum DyeColor {
/** /**
* Represents white dye * Represents white dye
*/ */
WHITE((byte) 0x0), WHITE(0x0),
/** /**
* Represents orange dye * Represents orange dye
*/ */
ORANGE((byte) 0x1), ORANGE(0x1),
/** /**
* Represents magenta dye * Represents magenta dye
*/ */
MAGENTA((byte) 0x2), MAGENTA(0x2),
/** /**
* Represents light blue dye * Represents light blue dye
*/ */
LIGHT_BLUE((byte) 0x3), LIGHT_BLUE(0x3),
/** /**
* Represents yellow dye * Represents yellow dye
*/ */
YELLOW((byte) 0x4), YELLOW(0x4),
/** /**
* Represents lime dye * Represents lime dye
*/ */
LIME((byte) 0x5), LIME(0x5),
/** /**
* Represents pink dye * Represents pink dye
*/ */
PINK((byte) 0x6), PINK(0x6),
/** /**
* Represents gray dye * Represents gray dye
*/ */
GRAY((byte) 0x7), GRAY(0x7),
/** /**
* Represents silver dye * Represents silver dye
*/ */
SILVER((byte) 0x8), SILVER(0x8),
/** /**
* Represents cyan dye * Represents cyan dye
*/ */
CYAN((byte) 0x9), CYAN(0x9),
/** /**
* Represents purple dye * Represents purple dye
*/ */
PURPLE((byte) 0xA), PURPLE(0xA),
/** /**
* Represents blue dye * Represents blue dye
*/ */
BLUE((byte) 0xB), BLUE(0xB),
/** /**
* Represents brown dye * Represents brown dye
*/ */
BROWN((byte) 0xC), BROWN(0xC),
/** /**
* Represents green dye * Represents green dye
*/ */
GREEN((byte) 0xD), GREEN(0xD),
/** /**
* Represents red dye * Represents red dye
*/ */
RED((byte) 0xE), RED(0xE),
/** /**
* Represents black dye * Represents black dye
*/ */
BLACK((byte) 0xF); BLACK(0xF);
private final byte data; private final byte data;
private final static Map<Byte, DyeColor> colors = new HashMap<Byte, DyeColor>(); private final static Map<Byte, DyeColor> BY_DATA = Maps.newHashMap();
private DyeColor(final byte data) { private DyeColor(final int data) {
this.data = 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 * @return The {@link DyeColor} representing the given value, or null if it doesn't exist
*/ */
public static DyeColor getByData(final byte data) { public static DyeColor getByData(final byte data) {
return colors.get(data); return BY_DATA.get(data);
} }
static { static {
for (DyeColor color : DyeColor.values()) { for (DyeColor color : values()) {
colors.put(color.getData(), color); BY_DATA.put(color.getData(), color);
} }
} }
} }

View file

@ -1,12 +1,16 @@
package org.bukkit; 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. * A list of effects that the server is able to send to players.
*/ */
public enum Effect { public enum Effect {
BOW_FIRE(1002),
CLICK1(1001),
CLICK2(1000), CLICK2(1000),
CLICK1(1001),
BOW_FIRE(1002),
DOOR_TOGGLE(1003), DOOR_TOGGLE(1003),
EXTINGUISH(1004), EXTINGUISH(1004),
RECORD_PLAY(1005), RECORD_PLAY(1005),
@ -20,12 +24,34 @@ public enum Effect {
MOBSPAWNER_FLAMES(2004); MOBSPAWNER_FLAMES(2004);
private final int id; private final int id;
private static final Map<Integer, Effect> BY_ID = Maps.newHashMap();
Effect(int id) { Effect(int id) {
this.id = id; this.id = id;
} }
/**
* Gets the ID for this effect.
*
* @return ID of this effect
*/
public int getId() { public int getId() {
return this.id; 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);
}
}
} }

View file

@ -1,5 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* A list of all Effects that can happen to entities. * A list of all Effects that can happen to entities.
*/ */
@ -8,46 +12,73 @@ public enum EntityEffect {
/** /**
* When mobs get hurt. * When mobs get hurt.
*/ */
HURT((byte) 2), HURT(2),
/** /**
* When a mob dies. * When a mob dies.
* <p> * <p>
* <b>This will cause client-glitches! * <b>This will cause client-glitches!
*/ */
DEATH((byte) 3), DEATH(3),
/** /**
* The smoke when taming a wolf fails. * The smoke when taming a wolf fails.
* <p> * <p>
* Without client-mods this will be ignored if the entity is not a wolf. * 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. * The hearts when taming a wolf succeeds.
* <p> * <p>
* Without client-mods this will be ignored if the entity is not a wolf. * 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). * When a wolf shakes (after being wet).
* <p> * <p>
* Without client-mods this will be ignored if the entity is not a wolf. * 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 byte data;
private final static Map<Byte, EntityEffect> BY_DATA = Maps.newHashMap();
EntityEffect(byte data) { EntityEffect(final int data) {
this.data = 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() { public byte getData() {
return data; 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);
}
}
} }

View file

@ -1,10 +1,11 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import com.google.common.collect.Maps;
/** /**
* Represents the various type of game modes that {@link HumanEntity}s may have * Represents the various type of game modes that {@link HumanEntity}s may have
*/ */
@ -20,7 +21,7 @@ public enum GameMode {
SURVIVAL(0); SURVIVAL(0);
private final int value; private final int value;
private final static Map<Integer, GameMode> modes = new HashMap<Integer, GameMode>(); private final static Map<Integer, GameMode> BY_ID = Maps.newHashMap();
private GameMode(final int value) { private GameMode(final int value) {
this.value = 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 * @return Associative {@link GameMode} with the given value, or null if it doesn't exist
*/ */
public static GameMode getByValue(final int value) { public static GameMode getByValue(final int value) {
return modes.get(value); return BY_ID.get(value);
} }
static { static {
for (GameMode mode : GameMode.values()) { for (GameMode mode : values()) {
modes.put(mode.getValue(), mode); BY_ID.put(mode.getValue(), mode);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents the different types of grass. * Represents the different types of grass.
*/ */
@ -11,21 +12,21 @@ public enum GrassSpecies {
/** /**
* Represents the dead looking grass. * Represents the dead looking grass.
*/ */
DEAD((byte) 0x0), DEAD(0x0),
/** /**
* Represents the normal grass species. * Represents the normal grass species.
*/ */
NORMAL((byte) 0x1), NORMAL(0x1),
/** /**
* Represents the fern-looking grass species. * Represents the fern-looking grass species.
*/ */
FERN_LIKE((byte) 0x2); FERN_LIKE(0x2);
private final byte data; private final byte data;
private final static Map<Byte, GrassSpecies> species = new HashMap<Byte, GrassSpecies>(); private final static Map<Byte, GrassSpecies> BY_DATA = Maps.newHashMap();
private GrassSpecies(final byte data) { private GrassSpecies(final int data) {
this.data = data; this.data = (byte) data;
} }
/** /**
@ -46,12 +47,12 @@ public enum GrassSpecies {
* it doesn't exist * it doesn't exist
*/ */
public static GrassSpecies getByData(final byte data) { public static GrassSpecies getByData(final byte data) {
return species.get(data); return BY_DATA.get(data);
} }
static { static {
for (GrassSpecies s : GrassSpecies.values()) { for (GrassSpecies grassSpecies : values()) {
species.put(s.getData(), s); BY_DATA.put(grassSpecies.getData(), grassSpecies);
} }
} }
} }

View file

@ -1,21 +1,22 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
public enum Instrument { public enum Instrument {
PIANO((byte) 0x0), // All other PIANO(0x0), // All other
BASS_DRUM((byte) 0x1), // Stone BASS_DRUM(0x1), // Stone
SNARE_DRUM((byte) 0x2), // Sand SNARE_DRUM(0x2), // Sand
STICKS((byte) 0x3), // Glass STICKS(0x3), // Glass
BASS_GUITAR((byte) 0x4); // Wood BASS_GUITAR(0x4); // Wood
private final byte type; private final byte type;
private final static Map<Byte, Instrument> types = new HashMap<Byte, Instrument>(); private final static Map<Byte, Instrument> BY_DATA = Maps.newHashMap();
private Instrument(byte type) { private Instrument(final int type) {
this.type = type; this.type = (byte) type;
} }
public byte getType() { public byte getType() {
@ -23,12 +24,12 @@ public enum Instrument {
} }
public static Instrument getByType(final byte type) { public static Instrument getByType(final byte type) {
return types.get(type); return BY_DATA.get(type);
} }
static { static {
for (Instrument instrument : Instrument.values()) { for (Instrument instrument : Instrument.values()) {
types.put(instrument.getType(), instrument); BY_DATA.put(instrument.getType(), instrument);
} }
} }
} }

View file

@ -2,13 +2,16 @@ package org.bukkit;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.commons.lang.Validate;
import org.bukkit.material.*; import org.bukkit.material.*;
import org.bukkit.util.Java15Compat; import org.bukkit.util.Java15Compat;
import com.google.common.collect.Maps;
/** /**
* An enum of all material ids accepted by the official server + client * An enum of all material ids accepted by the official server + client
*/ */
@ -76,12 +79,12 @@ public enum Material {
SOIL(60, MaterialData.class), SOIL(60, MaterialData.class),
FURNACE(61, Furnace.class), FURNACE(61, Furnace.class),
BURNING_FURNACE(62, Furnace.class), BURNING_FURNACE(62, Furnace.class),
SIGN_POST(63, 1, Sign.class), SIGN_POST(63, 64, Sign.class),
WOODEN_DOOR(64, Door.class), WOODEN_DOOR(64, Door.class),
LADDER(65, Ladder.class), LADDER(65, Ladder.class),
RAILS(66, Rails.class), RAILS(66, Rails.class),
COBBLESTONE_STAIRS(67, Stairs.class), COBBLESTONE_STAIRS(67, Stairs.class),
WALL_SIGN(68, 1, Sign.class), WALL_SIGN(68, 64, Sign.class),
LEVER(69, Lever.class), LEVER(69, Lever.class),
STONE_PLATE(70, PressurePlate.class), STONE_PLATE(70, PressurePlate.class),
IRON_DOOR_BLOCK(71, Door.class), IRON_DOOR_BLOCK(71, Door.class),
@ -105,7 +108,7 @@ public enum Material {
GLOWSTONE(89), GLOWSTONE(89),
PORTAL(90), PORTAL(90),
JACK_O_LANTERN(91, Pumpkin.class), 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_OFF(93, Diode.class),
DIODE_BLOCK_ON(94, Diode.class), DIODE_BLOCK_ON(94, Diode.class),
LOCKED_CHEST(95), LOCKED_CHEST(95),
@ -179,11 +182,11 @@ public enum Material {
SEEDS(295), SEEDS(295),
WHEAT(296), WHEAT(296),
BREAD(297), BREAD(297),
LEATHER_HELMET(298, 1, 50), LEATHER_HELMET(298, 1, 55),
LEATHER_CHESTPLATE(299, 1, 80), LEATHER_CHESTPLATE(299, 1, 80),
LEATHER_LEGGINGS(300, 1, 75), LEATHER_LEGGINGS(300, 1, 75),
LEATHER_BOOTS(301, 1, 65), LEATHER_BOOTS(301, 1, 65),
CHAINMAIL_HELMET(302, 1, 166), CHAINMAIL_HELMET(302, 1, 165),
CHAINMAIL_CHESTPLATE(303, 1, 240), CHAINMAIL_CHESTPLATE(303, 1, 240),
CHAINMAIL_LEGGINGS(304, 1, 225), CHAINMAIL_LEGGINGS(304, 1, 225),
CHAINMAIL_BOOTS(305, 1, 195), CHAINMAIL_BOOTS(305, 1, 195),
@ -279,8 +282,8 @@ public enum Material {
private final int id; private final int id;
private final Class<? extends MaterialData> data; private final Class<? extends MaterialData> data;
private static Material[] lookupId = new Material[3200]; private static Material[] byId = new Material[383];
private static final Map<String, Material> lookupName = new HashMap<String, Material>(); private final static Map<String, Material> BY_NAME = Maps.newHashMap();
private final int maxStack; private final int maxStack;
private final short durability; private final short durability;
@ -301,14 +304,14 @@ public enum Material {
} }
private Material(final int id, final int stack, final Class<? extends MaterialData> data) { private Material(final int id, final int stack, final Class<? extends MaterialData> data) {
this(id, stack, -1, data); this(id, stack, 0, data);
} }
private Material(final int id, final int stack, final int durability, final Class<? extends MaterialData> data) { private Material(final int id, final int stack, final int durability, final Class<? extends MaterialData> data) {
this.id = id; this.id = id;
this.durability = (short) durability; this.durability = (short) durability;
this.maxStack = stack; 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 * @return MaterialData associated with this Material
*/ */
public Class<? extends MaterialData> getData() { public Class<? extends MaterialData> getData() {
return (data == null) ? MaterialData.class : data; return data;
} }
/** /**
@ -355,10 +358,6 @@ public enum Material {
* @return New MaterialData with the given data * @return New MaterialData with the given data
*/ */
public MaterialData getNewData(final byte raw) { public MaterialData getNewData(final byte raw) {
if (data == null) {
return new MaterialData(id, raw);
}
try { try {
Constructor<? extends MaterialData> ctor = data.getConstructor(int.class, byte.class); Constructor<? extends MaterialData> ctor = data.getConstructor(int.class, byte.class);
@ -420,8 +419,8 @@ public enum Material {
* @return Material if found, or null * @return Material if found, or null
*/ */
public static Material getMaterial(final int id) { public static Material getMaterial(final int id) {
if (lookupId.length > id) { if (byId.length > id) {
return lookupId[id]; return byId[id];
} else { } else {
return null; return null;
} }
@ -436,7 +435,7 @@ public enum Material {
* @return Material if found, or null * @return Material if found, or null
*/ */
public static Material getMaterial(final String name) { 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 * @return Material if found, or null
*/ */
public static Material matchMaterial(final String name) { public static Material matchMaterial(final String name) {
Validate.notNull(name, "Name cannot be null");
Material result = null; Material result = null;
try { try {
@ -458,7 +459,7 @@ public enum Material {
String filtered = name.toUpperCase(); String filtered = name.toUpperCase();
filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", "");
result = lookupName.get(filtered); result = BY_NAME.get(filtered);
} }
return result; return result;
@ -466,13 +467,13 @@ public enum Material {
static { static {
for (Material material : values()) { for (Material material : values()) {
if (lookupId.length > material.id) { if (byId.length > material.id) {
lookupId[material.id] = material; byId[material.id] = material;
} else { } else {
lookupId = Java15Compat.Arrays_copyOfRange(lookupId, 0, material.id + 2); byId = Java15Compat.Arrays_copyOfRange(byId, 0, material.id + 2);
lookupId[material.id] = material; byId[material.id] = material;
} }
lookupName.put(material.name(), material); BY_NAME.put(material.name(), material);
} }
} }
} }

View file

@ -1,8 +1,11 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.Validate;
import com.google.common.collect.Maps;
/** /**
* A note class to store a specific note. * A note class to store a specific note.
*/ */
@ -12,22 +15,23 @@ public class Note {
* An enum holding tones. * An enum holding tones.
*/ */
public enum Tone { public enum Tone {
F((byte) -0x1, true), G(0x1, true),
G((byte) 0x1, true), A(0x3, true),
A((byte) 0x3, true), B(0x5, false),
B((byte) 0x5, false), C(0x6, true),
C((byte) 0x6, true), D(0x8, true),
D((byte) 0x8, true), E(0xA, false),
E((byte) 0xA, false); F(0xB, true);
private final boolean sharpable; private final boolean sharpable;
private final byte id; private final byte id;
private static final Map<Byte, Note.Tone> tones = new HashMap<Byte, Note.Tone>();
/** The number of tones including sharped tones. */
public static final byte TONES_COUNT;
private Tone(byte id, boolean sharpable) { private static final Map<Byte, Note.Tone> BY_DATA = Maps.newHashMap();
this.id = id; /** The number of tones including sharped tones. */
public static final byte TONES_COUNT = 12;
private Tone(int id, boolean sharpable) {
this.id = (byte) (id % TONES_COUNT);
this.sharpable = sharpable; this.sharpable = sharpable;
} }
@ -45,17 +49,13 @@ public class Note {
* sharped id of the tone. If the tone couldn't be sharped it always * sharped id of the tone. If the tone couldn't be sharped it always
* return the not sharped id of this tone. * return the not sharped id of this tone.
* *
* @param sharped * @param sharped Set to true to return the sharped id.
* Set to true to return the sharped id.
* @return the id of this tone. * @return the id of this tone.
*/ */
public byte getId(boolean sharped) { 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) { return (byte) (id % TONES_COUNT);
tempId += TONES_COUNT;
}
return (byte) (tempId % TONES_COUNT);
} }
/** /**
@ -70,11 +70,9 @@ public class Note {
/** /**
* Returns if this tone id is the sharped id of the tone. * Returns if this tone id is the sharped id of the tone.
* *
* @param id * @param id the id of the tone.
* the id of the tone.
* @return if the tone id is the sharped id of the tone. * @return if the tone id is the sharped id of the tone.
* @throws IllegalArgumentException * @throws IllegalArgumentException if neither the tone nor the semitone have the id.
* if neither the tone nor the semitone have the id.
*/ */
public boolean isSharped(byte id) { public boolean isSharped(byte id) {
if (id == getId(false)) { if (id == getId(false)) {
@ -90,34 +88,35 @@ public class Note {
/** /**
* Returns the tone to id. Also returning the semitones. * Returns the tone to id. Also returning the semitones.
* *
* @param id * @param id the id of the tone.
* the id of the tone. * @return the tone to id.
* @deprecated use {@link #getById(byte)}
*/
@Deprecated
public static Tone getToneById(byte 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. * @return the tone to id.
*/ */
public static Tone getToneById(byte id) { public static Tone getById(byte id) {
return tones.get(id); return BY_DATA.get(id);
} }
static { static {
byte lowest = F.id; for (Tone tone : values()) {
byte highest = F.id; int id = tone.id % TONES_COUNT;
for (Tone tone : Tone.values()) { BY_DATA.put((byte) id, tone);
byte id = tone.id;
tones.put(id, tone);
if (id < lowest) {
lowest = id;
}
if (tone.isSharpable()) { if (tone.isSharpable()) {
id++; id = (id + 1) % TONES_COUNT;
tones.put(id, tone); BY_DATA.put((byte) id, tone);
}
if (id > highest) {
highest = id;
} }
} }
TONES_COUNT = (byte) (highest - lowest + 1);
tones.put((byte) (TONES_COUNT - 1), F);
} }
} }
@ -126,36 +125,29 @@ public class Note {
/** /**
* Creates a new note. * Creates a new note.
* *
* @param note * @param note Internal note id. {@link #getId()} always return this value.
* Internal note id. {@link #getId()} always return this value.
* The value has to be in the interval [0;&nbsp;24]. * The value has to be in the interval [0;&nbsp;24].
*/ */
public Note(byte note) { public Note(int note) {
if (note < 0 || note > 24) { Validate.isTrue(note >= 0 && note <= 24, "The note value has to be between 0 and 24.");
throw new IllegalArgumentException("The note value has to be between 0 and 24.");
} this.note = (byte) note;
this.note = note;
} }
/** /**
* Creates a new note. * Creates a new note.
* *
* @param octave * @param octave The octave where the note is in. Has to be 0 - 2.
* 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 note * @param sharped Set it the tone is sharped (e.g. for F#).
* 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) { public Note(int octave, Tone tone, boolean sharped) {
if (sharped && !note.isSharpable()) { Validate.isTrue(!(sharped && !tone.isSharpable()), "This tone could not be sharped.");
throw new IllegalArgumentException("This tone could not be sharped."); if (octave < 0 || octave > 2 || (octave == 2 && !(tone == Tone.F && sharped))) {
}
if (octave < 0 || octave > 2 || (octave == 2 && !(note == Tone.F && sharped))) {
throw new IllegalArgumentException("Tone and octave have to be between F#0 and F#2"); 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. * @return the tone of this note.
*/ */
public Tone getTone() { public Tone getTone() {
return Tone.getToneById(getToneByte()); return Tone.getById(getToneByte());
} }
/** /**
@ -196,7 +188,28 @@ public class Note {
*/ */
public boolean isSharped() { public boolean isSharped() {
byte note = getToneByte(); 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;
}
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents a countable statistic, which is collected by the client * Represents a countable statistic, which is collected by the client
*/ */
@ -17,7 +18,7 @@ public enum Statistic {
USE_ITEM(6908288, false), USE_ITEM(6908288, false),
BREAK_ITEM(16973824, true); BREAK_ITEM(16973824, true);
private final static Map<Integer, Statistic> statistics = new HashMap<Integer, Statistic>(); private final static Map<Integer, Statistic> BY_ID = Maps.newHashMap();
private final int id; private final int id;
private final boolean isSubstat; private final boolean isSubstat;
private final boolean isBlock; private final boolean isBlock;
@ -70,14 +71,26 @@ public enum Statistic {
* *
* @param id ID of the statistic to return * @param id ID of the statistic to return
* @return statistic with the given ID * @return statistic with the given ID
* @deprecated See {@link #getById(int)}
*/ */
@Deprecated
public static Statistic getStatistic(int id) { 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 { static {
for (Statistic stat : values()) { for (Statistic statistic : values()) {
statistics.put(stat.getId(), stat); BY_ID.put(statistic.id, statistic);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents the different species of trees regardless of size. * Represents the different species of trees regardless of size.
*/ */
@ -11,21 +12,21 @@ public enum TreeSpecies {
/** /**
* Represents the common tree species. * Represents the common tree species.
*/ */
GENERIC((byte) 0x0), GENERIC(0x0),
/** /**
* Represents the darker barked/leaved tree species. * Represents the darker barked/leaved tree species.
*/ */
REDWOOD((byte) 0x1), REDWOOD(0x1),
/** /**
* Represents birches. * Represents birches.
*/ */
BIRCH((byte) 0x2); BIRCH(0x2);
private final byte data; private final byte data;
private final static Map<Byte, TreeSpecies> species = new HashMap<Byte, TreeSpecies>(); private final static Map<Byte, TreeSpecies> BY_DATA = Maps.newHashMap();
private TreeSpecies(final byte data) { private TreeSpecies(final int data) {
this.data = data; this.data = (byte) data;
} }
/** /**
@ -40,18 +41,17 @@ public enum TreeSpecies {
/** /**
* Gets the TreeSpecies with the given data value * Gets the TreeSpecies with the given data value
* *
* @param data * @param data Data value to fetch
* Data value to fetch
* @return The {@link TreeSpecies} representing the given value, or null if * @return The {@link TreeSpecies} representing the given value, or null if
* it doesn't exist * it doesn't exist
*/ */
public static TreeSpecies getByData(final byte data) { public static TreeSpecies getByData(final byte data) {
return species.get(data); return BY_DATA.get(data);
} }
static { static {
for (TreeSpecies s : TreeSpecies.values()) { for (TreeSpecies species : values()) {
species.put(s.getData(), s); BY_DATA.put(species.data, species);
} }
} }
} }

View file

@ -1,8 +1,9 @@
package org.bukkit; package org.bukkit;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
/** /**
* Represents various types of worlds that may exist * Represents various types of worlds that may exist
*/ */
@ -10,15 +11,9 @@ public enum WorldType {
NORMAL("DEFAULT"), NORMAL("DEFAULT"),
FLAT("FLAT"); FLAT("FLAT");
private final static Map<String, WorldType> lookup = new HashMap<String, WorldType>(); private final static Map<String, WorldType> BY_NAME = Maps.newHashMap();
private final String name; private final String name;
static {
for (WorldType type : values()) {
lookup.put(type.name, type);
}
}
private WorldType(String name) { private WorldType(String name) {
this.name = name; this.name = name;
} }
@ -39,6 +34,12 @@ public enum WorldType {
* @return Requested WorldType, or null if not found * @return Requested WorldType, or null if not found
*/ */
public static WorldType getByName(String name) { 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);
}
} }
} }

View file

@ -0,0 +1,28 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class AchievementTest {
@Test
public void getByDeprecated() {
for (Achievement achievement : Achievement.values()) {
assertThat(Achievement.getAchievement(achievement.getId()), is(achievement));
}
}
@Test
public void getById() {
for (Achievement achievement : Achievement.values()) {
assertThat(Achievement.getById(achievement.getId()), is(achievement));
}
}
@Test
public void getByOffset() {
assertThat(Achievement.getById(Achievement.STATISTIC_OFFSET), is(Achievement.values()[0]));
}
}

View file

@ -0,0 +1,45 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class ArtTest {
@Test(expected = IllegalArgumentException.class)
public void getByNullName() {
Art.getByName(null);
}
@Test
public void getById() {
for (Art art : Art.values()) {
assertThat(Art.getById(art.getId()), is(art));
}
}
@Test
public void getByName() {
for (Art art : Art.values()) {
assertThat(Art.getByName(art.toString()), is(art));
}
}
@Test
public void dimensionSanityCheck() {
for (Art art : Art.values()) {
assertThat(art.getBlockHeight(), is(greaterThan(0)));
assertThat(art.getBlockWidth(), is(greaterThan(0)));
}
}
@Test
public void getByNameWithMixedCase() {
Art subject = Art.values()[0];
String name = subject.toString().replace('E', 'e');
assertThat(Art.getByName(name), is(subject));
}
}

View file

@ -1,58 +1,71 @@
package org.bukkit; package org.bukkit;
import org.junit.AfterClass; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import static org.hamcrest.CoreMatchers.*;
public class ChatColorTest { public class ChatColorTest {
@Test @Test
public void testGetCode() { public void getByDeprecated() {
ChatColor color = ChatColor.DARK_RED;
assertThat(color.getCode(), equalTo(4));
}
@Test
public void testGetChar() {
ChatColor color = ChatColor.MAGIC;
assertThat(color.getChar(), equalTo('k'));
}
@Test
public void testToString() {
ChatColor color = ChatColor.LIGHT_PURPLE;
assertThat(color.toString(), equalTo("\u00A7d"));
}
@Test
public void testGetByCode() {
ChatColor color = ChatColor.AQUA;
assertThat(ChatColor.getByCode(color.getCode()), equalTo(color));
}
@Test
public void testGetByChar_char() {
ChatColor color = ChatColor.GOLD;
assertThat(ChatColor.getByChar(color.getChar()), equalTo(color));
}
@Test
public void testGetByChar_String() {
ChatColor color = ChatColor.BLUE;
assertThat(ChatColor.getByChar(((Character)color.getChar()).toString()), equalTo(color));
}
@Test
public void testStripColor() {
String string = "";
String expected = "";
for (ChatColor color : ChatColor.values()) { for (ChatColor color : ChatColor.values()) {
string += color + "test"; assertThat(ChatColor.getByCode(color.getCode()), is(color));
expected += "test"; }
}
@Test
public void getByChar() {
for (ChatColor color : ChatColor.values()) {
assertThat(ChatColor.getByChar(color.getChar()), is(color));
}
}
@Test(expected = IllegalArgumentException.class)
public void getByStringWithNull() {
ChatColor.getByChar((String) null);
}
@Test(expected = IllegalArgumentException.class)
public void getByStringWithEmpty() {
ChatColor.getByChar("");
}
@Test
public void getByNull() {
assertThat(ChatColor.stripColor(null), is(nullValue()));
}
@Test
public void getByString() {
for (ChatColor color : ChatColor.values()) {
assertThat(ChatColor.getByChar(String.valueOf(color.getChar())), is(color));
}
}
@Test
public void stripColorOnNullString() {
assertThat(ChatColor.stripColor(null), is(nullValue()));
}
@Test
public void stripColor() {
StringBuilder subject = new StringBuilder();
StringBuilder expected = new StringBuilder();
final String filler = "test";
for (ChatColor color : ChatColor.values()) {
subject.append(color).append(filler);
expected.append(filler);
} }
assertThat(ChatColor.stripColor(string), equalTo(expected)); assertThat(ChatColor.stripColor(subject.toString()), is(expected.toString()));
}
@Test
public void toStringWorks() {
for (ChatColor color : ChatColor.values()) {
assertThat(String.format("%c%c", ChatColor.COLOR_CHAR, color.getChar()), is(color.toString()));
}
} }
} }

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class CoalTypeTest {
@Test
public void getByData() {
for (CoalType coalType : CoalType.values()) {
assertThat(CoalType.getByData(coalType.getData()), is(coalType));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class CropStateTest {
@Test
public void getByData() {
for (CropState cropState : CropState.values()) {
assertThat(CropState.getByData(cropState.getData()), is(cropState));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class DifficultyTest {
@Test
public void getByValue() {
for (Difficulty difficulty : Difficulty.values()) {
assertThat(Difficulty.getByValue(difficulty.getValue()), is(difficulty));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class DyeColorTest {
@Test
public void getByData() {
for (DyeColor dyeColor : DyeColor.values()) {
assertThat(DyeColor.getByData(dyeColor.getData()), is(dyeColor));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class EffectTest {
@Test
public void getById() {
for (Effect effect : Effect.values()) {
assertThat(Effect.getById(effect.getId()), is(effect));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class EntityEffectTest {
@Test
public void getByData() {
for (EntityEffect entityEffect : EntityEffect.values()) {
assertThat(EntityEffect.getByData(entityEffect.getData()), is(entityEffect));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class GameModeTest {
@Test
public void getByValue() {
for (GameMode gameMode : GameMode.values()) {
assertThat(GameMode.getByValue(gameMode.getValue()), is(gameMode));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class GrassSpeciesTest {
@Test
public void getByData() {
for (GrassSpecies grassSpecies : GrassSpecies.values()) {
assertThat(GrassSpecies.getByData(grassSpecies.getData()), is(grassSpecies));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class InstrumentTest {
@Test
public void getByType() {
for (Instrument instrument : Instrument.values()) {
assertThat(Instrument.getByType(instrument.getType()), is(instrument));
}
}
}

View file

@ -0,0 +1,82 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MaterialTest {
@Test
public void getByName() {
for (Material material : Material.values()) {
assertThat(Material.getMaterial(material.toString()), is(material));
}
}
@Test
public void getById() {
for (Material material : Material.values()) {
assertThat(Material.getMaterial(material.getId()), is(material));
}
}
@Test
public void isBlock() {
for (Material material : Material.values()) {
if (material.getId() > 255) continue;
assertTrue(String.format("[%d] %s", material.getId(), material.toString()), material.isBlock());
}
}
@Test
public void getByOutOfRangeId() {
assertThat(Material.getMaterial(Integer.MAX_VALUE), is(nullValue()));
}
@Test
public void getByNameNull() {
assertThat(Material.getMaterial(null), is(nullValue()));
}
// [EB]: gawd we need better code >.>
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void getData() {
for (Material material : Material.values()) {
Class clazz = material.getData();
assertThat(material.getNewData((byte) 0), isA(clazz));
}
}
@Test(expected = IllegalArgumentException.class)
public void matchMaterialByNull() {
Material.matchMaterial(null);
}
@Test
public void matchMaterialById() {
for (Material material : Material.values()) {
assertThat(Material.matchMaterial(String.valueOf(material.getId())), is(material));
}
}
@Test
public void matchMaterialByName() {
for (Material material : Material.values()) {
assertThat(Material.matchMaterial(material.toString()), is(material));
}
}
@Test
public void matchMaterialByLowerCaseAndSpaces() {
for (Material material : Material.values()) {
String name = material.toString().replaceAll("_", " ").toLowerCase();
assertThat(Material.matchMaterial(name), is(material));
}
}
}

View file

@ -0,0 +1,107 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Collection;
import org.junit.Test;
import com.google.common.collect.Lists;
public class NoteTest {
@Test
public void getToneByDeprecated() {
for (Note.Tone tone : Note.Tone.values()) {
assertThat(Note.Tone.getToneById(tone.getId()), is(tone));
}
}
@Test
public void getToneByData() {
for (Note.Tone tone : Note.Tone.values()) {
assertThat(Note.Tone.getById(tone.getId()), is(tone));
}
}
@Test
public void verifySharpedData() {
for (Note.Tone tone : Note.Tone.values()) {
if (!tone.isSharpable()) return;
assertFalse(tone.isSharped(tone.getId(false)));
assertTrue(tone.isSharped(tone.getId(true)));
}
}
@Test
public void verifyUnknownToneData() {
Collection<Byte> tones = Lists.newArrayList();
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
tones.add((byte) i);
}
for (Note.Tone tone : Note.Tone.values()) {
if (tone.isSharpable()) tones.remove(tone.getId(true));
tones.remove(tone.getId());
}
for (Byte data : tones) {
assertThat(Note.Tone.getById(data), is(nullValue()));
for (Note.Tone tone : Note.Tone.values()) {
try {
tone.isSharped(data);
fail(data + " should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertNotNull(e);
}
}
}
}
@Test(expected = IllegalArgumentException.class)
public void createNoteBelowMin() {
new Note((byte) -1);
}
@Test(expected = IllegalArgumentException.class)
public void createNoteAboveMax() {
new Note((byte) 25);
}
@Test(expected = IllegalArgumentException.class)
public void createNoteOctaveBelowMax() {
new Note((byte) -1, Note.Tone.A, true);
}
@Test(expected = IllegalArgumentException.class)
public void createNoteOctaveAboveMax() {
new Note((byte) 3, Note.Tone.A, true);
}
@Test(expected = IllegalArgumentException.class)
public void createNoteOctaveNonSharpable() {
new Note((byte) 0, Note.Tone.B, true);
}
@Test
public void doo() {
for (int i = 1; i <= 24; i++) {
Note note = new Note((byte) i);
int octave = note.getOctave();
Note.Tone tone = note.getTone();
boolean sharped = note.isSharped();
Note newNote = new Note(octave, tone, sharped);
assertThat(newNote, is(note));
assertThat(newNote.getId(), is(note.getId()));
}
}
}

View file

@ -0,0 +1,22 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class StatisticTest {
@Test
public void getDeprecated() {
for (Statistic statistic : Statistic.values()) {
assertThat(Statistic.getStatistic(statistic.getId()), is(statistic));
}
}
@Test
public void getById() {
for (Statistic statistic : Statistic.values()) {
assertThat(Statistic.getById(statistic.getId()), is(statistic));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class TreeSpeciesTest {
@Test
public void getByData() {
for (TreeSpecies treeSpecies : TreeSpecies.values()) {
assertThat(TreeSpecies.getByData(treeSpecies.getData()), is(treeSpecies));
}
}
}

View file

@ -0,0 +1,15 @@
package org.bukkit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class WorldTypeTest {
@Test
public void getByName() {
for (WorldType worldType : WorldType.values()) {
assertThat(WorldType.getByName(worldType.getName()), is(worldType));
}
}
}