Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 185 lines 10 kB view raw
1package org.bukkit; 2 3import org.bukkit.material.*; 4 5import java.lang.reflect.Constructor; 6import java.lang.reflect.InvocationTargetException; 7import java.util.HashMap; 8import java.util.Map; 9import java.util.logging.Level; 10import java.util.logging.Logger; 11 12/** 13 * An enum of all material ids accepted by the official server + client 14 */ 15public enum Material { 16 AIR(0), STONE(1), GRASS(2), DIRT(3), COBBLESTONE(4), WOOD(5), SAPLING(6, Tree.class), BEDROCK(7), WATER(8, MaterialData.class), STATIONARY_WATER(9, MaterialData.class), LAVA(10, MaterialData.class), STATIONARY_LAVA(11, MaterialData.class), SAND(12), GRAVEL(13), GOLD_ORE(14), IRON_ORE(15), COAL_ORE(16), LOG(17, Tree.class), LEAVES(18, Tree.class), SPONGE(19), GLASS(20), LAPIS_ORE(21), LAPIS_BLOCK(22), DISPENSER(23, Dispenser.class), SANDSTONE(24), NOTE_BLOCK(25), BED_BLOCK(26, Bed.class), POWERED_RAIL(27, PoweredRail.class), DETECTOR_RAIL(28, DetectorRail.class), PISTON_STICKY_BASE(29, PistonBaseMaterial.class), WEB(30), LONG_GRASS(31, LongGrass.class), DEAD_BUSH(32), PISTON_BASE(33, PistonBaseMaterial.class), PISTON_EXTENSION(34, PistonExtensionMaterial.class), WOOL(35, Wool.class), PISTON_MOVING_PIECE(36), YELLOW_FLOWER(37), RED_ROSE(38), BROWN_MUSHROOM(39), RED_MUSHROOM(40), GOLD_BLOCK(41), IRON_BLOCK(42), DOUBLE_STEP(43, Step.class), STEP(44, Step.class), BRICK(45), TNT(46), BOOKSHELF(47), MOSSY_COBBLESTONE(48), OBSIDIAN(49), TORCH(50, Torch.class), FIRE(51), MOB_SPAWNER(52), WOOD_STAIRS(53, Stairs.class), CHEST(54), REDSTONE_WIRE(55, RedstoneWire.class), DIAMOND_ORE(56), DIAMOND_BLOCK(57), WORKBENCH(58), CROPS(59, Crops.class), SOIL(60, MaterialData.class), FURNACE(61, Furnace.class), BURNING_FURNACE(62, Furnace.class), SIGN_POST(63, 1, 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), LEVER(69, Lever.class), STONE_PLATE(70, PressurePlate.class), IRON_DOOR_BLOCK(71, Door.class), WOOD_PLATE(72, PressurePlate.class), REDSTONE_ORE(73), GLOWING_REDSTONE_ORE(74), REDSTONE_TORCH_OFF(75, RedstoneTorch.class), REDSTONE_TORCH_ON(76, RedstoneTorch.class), STONE_BUTTON(77, Button.class), SNOW(78), ICE(79), SNOW_BLOCK(80), CACTUS(81, MaterialData.class), CLAY(82), SUGAR_CANE_BLOCK(83, MaterialData.class), JUKEBOX(84, Jukebox.class), FENCE(85), PUMPKIN(86, Pumpkin.class), NETHERRACK(87), SOUL_SAND(88), GLOWSTONE(89), PORTAL(90), JACK_O_LANTERN(91, Pumpkin.class), CAKE_BLOCK(92, 1, Cake.class), DIODE_BLOCK_OFF(93, Diode.class), DIODE_BLOCK_ON(94, Diode.class), LOCKED_CHEST(95), TRAP_DOOR(96, TrapDoor.class), // ----- Item Separator ----- 17 IRON_SPADE(256, 1, 250), IRON_PICKAXE(257, 1, 250), IRON_AXE(258, 1, 250), FLINT_AND_STEEL(259, 1, 64), APPLE(260, 1), BOW(261, 1), ARROW(262), COAL(263, Coal.class), DIAMOND(264), IRON_INGOT(265), GOLD_INGOT(266), IRON_SWORD(267, 1, 250), WOOD_SWORD(268, 1, 59), WOOD_SPADE(269, 1, 59), WOOD_PICKAXE(270, 1, 59), WOOD_AXE(271, 1, 59), STONE_SWORD(272, 1, 131), STONE_SPADE(273, 1, 131), STONE_PICKAXE(274, 1, 131), STONE_AXE(275, 1, 131), DIAMOND_SWORD(276, 1, 1561), DIAMOND_SPADE(277, 1, 1561), DIAMOND_PICKAXE(278, 1, 1561), DIAMOND_AXE(279, 1, 1561), STICK(280), BOWL(281), MUSHROOM_SOUP(282, 1), GOLD_SWORD(283, 1, 32), GOLD_SPADE(284, 1, 32), GOLD_PICKAXE(285, 1, 32), GOLD_AXE(286, 1, 32), STRING(287), FEATHER(288), SULPHUR(289), WOOD_HOE(290, 1, 59), STONE_HOE(291, 1, 131), IRON_HOE(292, 1, 250), DIAMOND_HOE(293, 1, 1561), GOLD_HOE(294, 1, 32), SEEDS(295), WHEAT(296), BREAD(297, 1), LEATHER_HELMET(298, 1, 33), LEATHER_CHESTPLATE(299, 1, 47), LEATHER_LEGGINGS(300, 1, 45), LEATHER_BOOTS(301, 1, 39), CHAINMAIL_HELMET(302, 1, 66), CHAINMAIL_CHESTPLATE(303, 1, 95), CHAINMAIL_LEGGINGS(304, 1, 91), CHAINMAIL_BOOTS(305, 1, 78), IRON_HELMET(306, 1, 135), IRON_CHESTPLATE(307, 1, 191), IRON_LEGGINGS(308, 1, 183), IRON_BOOTS(309, 1, 159), DIAMOND_HELMET(310, 1, 271), DIAMOND_CHESTPLATE(311, 1, 383), DIAMOND_LEGGINGS(312, 1, 367), DIAMOND_BOOTS(313, 1, 319), GOLD_HELMET(314, 1, 67), GOLD_CHESTPLATE(315, 1, 95), GOLD_LEGGINGS(316, 1, 91), GOLD_BOOTS(317, 1, 79), FLINT(318), PORK(319, 1), GRILLED_PORK(320, 1), PAINTING(321), GOLDEN_APPLE(322, 1), SIGN(323, 1), WOOD_DOOR(324, 1), BUCKET(325, 1), WATER_BUCKET(326, 1), LAVA_BUCKET(327, 1), MINECART(328, 1), SADDLE(329, 1), IRON_DOOR(330, 1), REDSTONE(331), SNOW_BALL(332, 16), BOAT(333, 1), LEATHER(334), MILK_BUCKET(335, 1), CLAY_BRICK(336), CLAY_BALL(337), SUGAR_CANE(338), PAPER(339), BOOK(340), SLIME_BALL(341), STORAGE_MINECART(342, 1), POWERED_MINECART(343, 1), EGG(344, 16), COMPASS(345), FISHING_ROD(346, 1, 64), WATCH(347), GLOWSTONE_DUST(348), RAW_FISH(349, 1), COOKED_FISH(350, 1), INK_SACK(351, Dye.class), BONE(352), SUGAR(353), CAKE(354, 1), BED(355, 1), DIODE(356), COOKIE(357, 8), MAP(358, 1, MaterialData.class), SHEARS(359, 1, 238), GOLD_RECORD(2256, 1), GREEN_RECORD(2257, 1); 18 19 private final int id; 20 private final Class<? extends MaterialData> data; 21 private static final Map<Integer, Material> lookupId = new HashMap<Integer, Material>(); 22 private static final Map<String, Material> lookupName = new HashMap<String, Material>(); 23 private final int maxStack; 24 private final short durability; 25 26 private Material(final int id) { 27 this(id, 64); 28 } 29 30 private Material(final int id, final int stack) { 31 this(id, stack, null); 32 } 33 34 private Material(final int id, final int stack, final int durability) { 35 this(id, stack, durability, null); 36 } 37 38 private Material(final int id, final Class<? extends MaterialData> data) { 39 this(id, 64, data); 40 } 41 42 private Material(final int id, final int stack, final Class<? extends MaterialData> data) { 43 this(id, stack, -1, data); 44 } 45 46 private Material(final int id, final int stack, final int durability, final Class<? extends MaterialData> data) { 47 this.id = id; 48 this.durability = (short) durability; 49 this.maxStack = stack; 50 this.data = data; 51 } 52 53 /** 54 * Gets the item ID or block ID of this Material 55 * 56 * @return ID of this material 57 */ 58 public int getId() { 59 return id; 60 } 61 62 /** 63 * Gets the maximum amount of this material that can be held in a stack 64 * 65 * @return Maximum stack size for this material 66 */ 67 public int getMaxStackSize() { 68 return maxStack; 69 } 70 71 /** 72 * Gets the maximum durability of this material 73 * 74 * @return Maximum durability for this material 75 */ 76 public short getMaxDurability() { 77 return durability; 78 } 79 80 /** 81 * Gets the MaterialData class associated with this Material 82 * 83 * @return MaterialData associated with this Material 84 */ 85 public Class<? extends MaterialData> getData() { 86 return data; 87 } 88 89 /** 90 * Constructs a new MaterialData relevant for this Material, with the given 91 * initial data 92 * 93 * @param raw Initial data to construct the MaterialData with 94 * @return New MaterialData with the given data 95 */ 96 public MaterialData getNewData(final byte raw) { 97 if (data == null) { 98 return null; 99 } 100 101 try { 102 Constructor<? extends MaterialData> ctor = data.getConstructor(int.class, byte.class); 103 104 return ctor.newInstance(id, raw); 105 } catch (InstantiationException ex) { 106 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 107 } catch (IllegalAccessException ex) { 108 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 109 } catch (IllegalArgumentException ex) { 110 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 111 } catch (InvocationTargetException ex) { 112 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 113 } catch (NoSuchMethodException ex) { 114 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 115 } catch (SecurityException ex) { 116 Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex); 117 } 118 119 return null; 120 } 121 122 /** 123 * Checks if this Material is a placable block 124 * 125 * @return true if this material is a block 126 */ 127 public boolean isBlock() { 128 return id < 256; 129 } 130 131 /** 132 * Attempts to get the Material with the given ID 133 * 134 * @param id ID of the material to get 135 * @return Material if found, or null 136 */ 137 public static Material getMaterial(final int id) { 138 return lookupId.get(id); 139 } 140 141 /** 142 * Attempts to get the Material with the given name. 143 * This is a normal lookup, names must be the precise name they are given 144 * in the enum. 145 * 146 * @param name Name of the material to get 147 * @return Material if found, or null 148 */ 149 public static Material getMaterial(final String name) { 150 return lookupName.get(name); 151 } 152 153 /** 154 * Attempts to match the Material with the given name. 155 * This is a match lookup; names will be converted to uppercase, then stripped 156 * of special characters in an attempt to format it like the enum 157 * 158 * @param name Name of the material to get 159 * @return Material if found, or null 160 */ 161 public static Material matchMaterial(final String name) { 162 Material result = null; 163 164 try { 165 result = getMaterial(Integer.parseInt(name)); 166 } catch (NumberFormatException ex) { 167 } 168 169 if (result == null) { 170 String filtered = name.toUpperCase(); 171 172 filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); 173 result = lookupName.get(filtered); 174 } 175 176 return result; 177 } 178 179 static { 180 for (Material material : values()) { 181 lookupId.put(material.getId(), material); 182 lookupName.put(material.name(), material); 183 } 184 } 185}