Created
December 13, 2011 02:35
-
-
Save Dinnerbone/1470198 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.bukkit.types.block; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Represents the various types of blocks in a world. | |
*/ | |
public abstract class BlockType { | |
public static final BlockType AIR = new BlockTypeWrapper(0); | |
public static final BlockType STONE = new BlockTypeWrapper(1); | |
// .... | |
private static final int MAX_BLOCK_TYPES = 256; | |
private static final BlockType[] types = new BlockType[MAX_BLOCK_TYPES]; | |
private static final Map<String, BlockType> aliases = new HashMap<String, BlockType>(MAX_BLOCK_TYPES); | |
private static boolean acceptingRegs = true; | |
private final int id; | |
public BlockType(int id) { | |
this.id = id; | |
} | |
/** | |
* Gets the ID of this BlockType. | |
* | |
* @return ID of this BlockType. | |
*/ | |
public final int getId() { | |
return id; | |
} | |
/** | |
* Gets a human readable name of this block. | |
* | |
* @return Human readable block name. | |
*/ | |
public abstract String getName(); | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (obj instanceof BlockType) { | |
BlockType other = (BlockType)obj; | |
if (this.getId() == other.getId()) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 7; | |
hash = 31 * hash + this.id; | |
return hash; | |
} | |
@Override | |
public String toString() { | |
return "BlockType[" + id + ", " + getName() + "]"; | |
} | |
// Statics after yonder! | |
/** | |
* Attempts to register a given BlockType. | |
* | |
* @param type Type to register. | |
* @throws IllegalArgumentException Thrown if type is null. | |
* @throws IllegalArgumentException Thrown is a BlockType is already registered with the same ID as the passed type. | |
*/ | |
public static void registerBlock(BlockType type) { | |
if (type == null) { | |
throw new IllegalArgumentException("Cannot register a null type"); | |
} else if (types[type.getId()] != null) { | |
throw new IllegalArgumentException("A BlockType with the id " + type.getId() + " already exists!"); | |
} else if (!acceptingRegs) { | |
throw new IllegalStateException("Block type registrations are not being accepted at this time"); | |
} | |
types[type.getId()] = type; | |
} | |
/** | |
* Stops this class from accepting any further BlockType registrations. | |
*/ | |
public static void stopAcceptingRegistrations() { | |
acceptingRegs = false; | |
} | |
/** | |
* Checks if this class is accepting new BlockType registrations. | |
* | |
* @return True if you can call {@link #registerBlock(org.bukkit.types.block.BlockType)}. | |
*/ | |
public static boolean isAcceptingRegistrations() { | |
return acceptingRegs; | |
} | |
/** | |
* Gets a BlockType with the given ID. | |
* <p> | |
* This may return null if no BlockType is registered with the given ID. | |
* | |
* @param id ID to retrieve. | |
* @return BlockType with the given ID, or null. | |
*/ | |
public static BlockType get(int id) { | |
return (id < 0 || id >= MAX_BLOCK_TYPES) ? null : types[id]; | |
} | |
/** | |
* Gets a BlockType with the given name or alias. | |
* <p> | |
* This may return null if no BlockType is registered with the given name. | |
* | |
* @param name Name of the block to retrieve. | |
* @return BlockType with the given ID, or null. | |
*/ | |
public static BlockType get(String name) { | |
return aliases.get(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.bukkit.types; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Represents the various type of Items in the game, that may be held by players | |
*/ | |
public abstract class ItemType { | |
public static final BlockItem STONE = new BlockItem.BlockItemWrapper(1); | |
public static final BlockItem GRASS = new BlockItem.BlockItemWrapper(2); | |
// .... | |
public static final ItemType IRON_SHOVEL = new ItemWrapper(256); | |
public static final ItemType IRON_PICKAXE = new ItemWrapper(257); | |
// .... | |
private static final int MAX_ITEM_TYPES = 32000; | |
private static final ItemType[] types = new ItemType[MAX_ITEM_TYPES]; | |
private static final Map<String, ItemType> aliases = new HashMap<String, ItemType>(MAX_ITEM_TYPES); | |
private static boolean acceptingRegs = true; | |
private final int id; | |
public ItemType(int id) { | |
this.id = id; | |
} | |
/** | |
* Gets the ID of this ItemType. | |
* | |
* @return ID of this ItemType. | |
*/ | |
public final int getId() { | |
return id; | |
} | |
/** | |
* Gets a human readable name of this block. | |
* | |
* @return Human readable block name. | |
*/ | |
public abstract String getName(); | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (obj instanceof ItemType) { | |
ItemType other = (ItemType)obj; | |
if (this.getId() == other.getId()) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 7; | |
hash = 31 * hash + this.id; | |
return hash; | |
} | |
@Override | |
public String toString() { | |
return getClass().getSimpleName() + "[" + id + ", " + getName() + "]"; | |
} | |
// Statics after yonder! | |
/** | |
* Attempts to register a given ItemType. | |
* | |
* @param type Type to register. | |
* @throws IllegalArgumentException Thrown if type is null. | |
* @throws IllegalArgumentException Thrown is a ItemType is already registered with the same ID as the passed type. | |
*/ | |
public static void registerItem(ItemType type) { | |
if (type == null) { | |
throw new IllegalArgumentException("Cannot register a null type"); | |
} else if (types[type.getId()] != null) { | |
throw new IllegalArgumentException("An ItemType with the id " + type.getId() + " already exists!"); | |
} else if (!acceptingRegs) { | |
throw new IllegalStateException("Item type registrations are not being accepted at this time"); | |
} | |
types[type.getId()] = type; | |
} | |
/** | |
* Stops this class from accepting any further ItemType registrations. | |
*/ | |
public static void stopAcceptingRegistrations() { | |
acceptingRegs = false; | |
} | |
/** | |
* Checks if this class is accepting new ItemType registrations. | |
* | |
* @return True if you can call {@link #registerBlock(org.bukkit.types.item.ItemType)}. | |
*/ | |
public static boolean isAcceptingRegistrations() { | |
return acceptingRegs; | |
} | |
/** | |
* Gets an ItemType with the given ID. | |
* <p> | |
* This may return null if no ItemType is registered with the given ID. | |
* | |
* @param id ID to retrieve. | |
* @return ItemType with the given ID, or null. | |
*/ | |
public static ItemType get(int id) { | |
return (id < 0 || id >= MAX_ITEM_TYPES) ? null : types[id]; | |
} | |
/** | |
* Gets a ItemType with the given name or alias. | |
* <p> | |
* This may return null if no ItemType is registered with the given name. | |
* | |
* @param name Name of the item to retrieve. | |
* @return ItemType with the given ID, or null. | |
*/ | |
public static ItemType get(String name) { | |
return aliases.get(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment