下面列出了怎么用org.bukkit.TreeSpecies的API类实例代码及写法,或者点击链接到github查看源代码。
public static XMaterial getType(Block block) {
if (ISFLAT) return XMaterial.matchXMaterial(block.getType());
String type = block.getType().name();
BlockState state = block.getState();
MaterialData data = state.getData();
if (data instanceof Wood) {
TreeSpecies species = ((Wood) data).getSpecies();
return XMaterial.matchXMaterial(species.name() + block.getType().name())
.orElseThrow(() -> new IllegalArgumentException("Unsupported material from tree species " + species.name() + ": " + block.getType().name()));
}
if (data instanceof Colorable) {
Colorable color = (Colorable) data;
return XMaterial.matchXMaterial(color.getColor().name() + '_' + type).orElseThrow(() -> new IllegalArgumentException("Unsupported colored material"));
}
return XMaterial.matchXMaterial(block.getType());
}
/**
* Returns the item type of a wooden door for the given tree species.
*
* @param species The species of wood door required.
* @return The item type for the given species.
* @see Material#WOODEN_DOOR
* @see Material#SPRUCE_DOOR
* @see Material#BIRCH_DOOR
* @see Material#JUNGLE_DOOR
* @see Material#ACACIA_DOOR
* @see Material#DARK_OAK_DOOR
*/
public static Material getWoodDoorOfSpecies(TreeSpecies species) {
switch (species) {
default:
case GENERIC:
return Material.WOODEN_DOOR;
case BIRCH:
return Material.BIRCH_DOOR;
case REDWOOD:
return Material.SPRUCE_DOOR;
case JUNGLE:
return Material.JUNGLE_DOOR;
case ACACIA:
return Material.ACACIA_DOOR;
case DARK_OAK:
return Material.DARK_OAK_DOOR;
}
}
/**
* Gets the current species of this wood block
*
* @return TreeSpecies of this wood block
*/
public TreeSpecies getSpecies() {
switch (getItemType()) {
case WOOD:
case WOOD_DOUBLE_STEP:
return TreeSpecies.getByData((byte) getData());
case LOG:
case LEAVES:
return TreeSpecies.getByData((byte) (getData() & 0x3));
case LOG_2:
case LEAVES_2:
return TreeSpecies.getByData((byte) ((getData() & 0x3) | 0x4));
case SAPLING:
case WOOD_STEP:
return TreeSpecies.getByData((byte) (getData() & 0x7));
default:
throw new IllegalArgumentException("Invalid block type for tree species");
}
}
public static TreeSpecies getTreeSpecies(EntityBoat.Type boatType) {
switch (boatType) {
case SPRUCE:
return TreeSpecies.REDWOOD;
case BIRCH:
return TreeSpecies.BIRCH;
case JUNGLE:
return TreeSpecies.JUNGLE;
case ACACIA:
return TreeSpecies.ACACIA;
case DARK_OAK:
return TreeSpecies.DARK_OAK;
case OAK:
default:
return TreeSpecies.GENERIC;
}
}
public static EntityBoat.Type getBoatType(TreeSpecies species) {
switch (species) {
case REDWOOD:
return EntityBoat.Type.SPRUCE;
case BIRCH:
return EntityBoat.Type.BIRCH;
case JUNGLE:
return EntityBoat.Type.JUNGLE;
case ACACIA:
return EntityBoat.Type.ACACIA;
case DARK_OAK:
return EntityBoat.Type.DARK_OAK;
case GENERIC:
default:
return EntityBoat.Type.OAK;
}
}
public boolean isOfItemType(ItemType i){
if (i.getRandom() == null)
return false;
int ordinal = -1;
ItemStack stack = i.getRandom();
if (oakBoat.isOfType(stack))
ordinal = 0;
else if (spruceBoat.isOfType(stack))
ordinal = TreeSpecies.REDWOOD.ordinal();
else if (birchBoat.isOfType(stack))
ordinal = TreeSpecies.BIRCH.ordinal();
else if (jungleBoat.isOfType(stack))
ordinal = TreeSpecies.JUNGLE.ordinal();
else if (acaciaBoat.isOfType(stack))
ordinal = TreeSpecies.ACACIA.ordinal();
else if (darkOakBoat.isOfType(stack))
ordinal = TreeSpecies.DARK_OAK.ordinal();
return hashCode_i() == ordinal + 2 || (matchedPattern + ordinal == 0) || ordinal == 0;
}
public static boolean setWooden(Block block, XMaterial species) {
block.setType(species.parseMaterial());
if (XMaterial.ISFLAT) return true;
TreeSpecies type = species == XMaterial.SPRUCE_LOG ? TreeSpecies.REDWOOD :
TreeSpecies.valueOf(species.name().substring(0, species.name().indexOf('_')));
BlockState state = block.getState();
MaterialData data = state.getData();
((Wood) data).setSpecies(type);
state.update(true);
return true;
}
public static boolean setWooden(Block block, XMaterial species) {
block.setType(species.parseMaterial());
if (ISFLAT) return true;
TreeSpecies type = species == XMaterial.SPRUCE_LOG ? TreeSpecies.REDWOOD :
TreeSpecies.valueOf(species.name().substring(0, species.name().indexOf('_')));
BlockState state = block.getState();
MaterialData data = state.getData();
((Wood) data).setSpecies(type);
state.update(true);
return true;
}
/**
* Correct the block type for certain species-type combinations.
*
* @param type The desired type
* @param species The required species
* @return The actual type for this species given the desired type
*/
private static Material getSpeciesType(Material type, TreeSpecies species) {
switch (species) {
case GENERIC:
case REDWOOD:
case BIRCH:
case JUNGLE:
switch (type) {
case LOG_2:
return Material.LOG;
case LEAVES_2:
return Material.LEAVES;
default:
}
break;
case ACACIA:
case DARK_OAK:
switch (type) {
case LOG:
return Material.LOG_2;
case LEAVES:
return Material.LEAVES_2;
default:
}
break;
}
return type;
}
/**
* Sets the species of this wood block
*
* @param species New species of this wood block
*/
public void setSpecies(final TreeSpecies species) {
boolean firstType = false;
switch (getItemType()) {
case WOOD:
case WOOD_DOUBLE_STEP:
setData(species.getData());
break;
case LOG:
case LEAVES:
firstType = true;
// fall through to next switch statement below
case LOG_2:
case LEAVES_2:
switch (species) {
case GENERIC:
case REDWOOD:
case BIRCH:
case JUNGLE:
if (!firstType) {
throw new IllegalArgumentException("Invalid tree species for block type, use block type 2 instead");
}
break;
case ACACIA:
case DARK_OAK:
if (firstType) {
throw new IllegalArgumentException("Invalid tree species for block type 2, use block type instead");
}
break;
}
setData((byte) ((getData() & 0xC) | (species.getData() & 0x3)));
break;
case SAPLING:
case WOOD_STEP:
setData((byte) ((getData() & 0x8) | species.getData()));
break;
default:
throw new IllegalArgumentException("Invalid block type for tree species");
}
}
/**
* Get the material in the flower pot
*
* @return material MaterialData for the block currently in the flower pot
* or null if empty
*/
public MaterialData getContents() {
switch (getData()) {
case 1:
return new MaterialData(Material.RED_ROSE);
case 2:
return new MaterialData(Material.YELLOW_FLOWER);
case 3:
return new Tree(TreeSpecies.GENERIC);
case 4:
return new Tree(TreeSpecies.REDWOOD);
case 5:
return new Tree(TreeSpecies.BIRCH);
case 6:
return new Tree(TreeSpecies.JUNGLE);
case 7:
return new MaterialData(Material.RED_MUSHROOM);
case 8:
return new MaterialData(Material.BROWN_MUSHROOM);
case 9:
return new MaterialData(Material.CACTUS);
case 10:
return new MaterialData(Material.DEAD_BUSH);
case 11:
return new LongGrass(GrassSpecies.FERN_LIKE);
default:
return null;
}
}
@Override
public void set(Boat entity) {
if (matchedPattern == 1) // If the type is 'any boat'.
matchedPattern += new Random().nextInt(TreeSpecies.values().length); // It will spawn a random boat type in case is 'any boat'.
if (matchedPattern > 1) // 0 and 1 are excluded
entity.setWoodType(TreeSpecies.values()[matchedPattern - 2]); // Removes 2 to fix the index.
}
@Override
public TreeSpecies getWoodType() {
return getTreeSpecies(getHandle().getBoatType());
}
@Override
public void setWoodType(TreeSpecies species) {
getHandle().setBoatType(getBoatType(species));
}
public BoatData(@Nullable TreeSpecies type){
this(type != null ? type.ordinal() + 2 : 1);
}
/**
* Gets the wood type of the boat.
*
* @return the wood type
*/
TreeSpecies getWoodType();
/**
* Sets the wood type of the boat.
*
* @param species the new wood type
*/
void setWoodType(TreeSpecies species);
/**
* Constructs a leaf block of the given tree species.
*
* @param species the species of the wood block
*/
public Leaves(TreeSpecies species) {
this(DEFAULT_TYPE, species, DEFAULT_DECAYABLE);
}
/**
* Constructs a leaf block of the given tree species and flag for whether
* this leaf block will disappear when too far from a log.
*
* @param species the species of the wood block
* @param isDecayable whether the block is permanent or can disappear
*/
public Leaves(TreeSpecies species, boolean isDecayable) {
this(DEFAULT_TYPE, species, isDecayable);
}
/**
* Constructs a leaf block of the given type and tree species.
*
* @param type the type of leaf block
* @param species the species of the wood block
*/
public Leaves(final Material type, TreeSpecies species) {
this(type, species, DEFAULT_DECAYABLE);
}
/**
* Constructs a leaf block of the given type and tree species and flag for
* whether this leaf block will disappear when too far from a log.
*
* @param type the type of leaf block
* @param species the species of the wood block
* @param isDecayable whether the block is permanent or can disappear
*/
public Leaves(final Material type, TreeSpecies species, boolean isDecayable) {
super(type, species);
setDecayable(isDecayable);
}
/**
* Constructs a sapling of the given tree species.
*
* @param species the species of the sapling
*/
public Sapling(TreeSpecies species) {
this(species, false);
}
/**
* Constructs a sapling of the given tree species and if is it instant
* growable
*
* @param species the species of the tree block
* @param isInstantGrowable true if the Sapling should grow when next ticked with bonemeal
*/
public Sapling(TreeSpecies species, boolean isInstantGrowable) {
this(Material.SAPLING, species, isInstantGrowable);
}
/**
* Constructs a sapling of the given type and tree species.
*
* @param type the type of sapling
* @param species the species of the sapling
*/
public Sapling(final Material type, TreeSpecies species) {
this(type, species, false);
}
/**
* Constructs a sapling of the given type and tree species and if is it
* instant growable
*
* @param type the type of sapling
* @param species the species of the sapling
* @param isInstantGrowable true if the Sapling should grow when next ticked
* with bonemeal
*/
public Sapling(final Material type, TreeSpecies species, boolean isInstantGrowable) {
super(type, species);
setIsInstantGrowable(isInstantGrowable);
}
/**
* Constructs the bottom half of a wooden door of the given species, facing the specified direction and set to
* closed
*
* @param species The species this wooden door is made of. This must match the species of the block above.
* @param face The direction the door is facing.
* @see TreeSpecies
* @see BlockFace#WEST
* @see BlockFace#NORTH
* @see BlockFace#EAST
* @see BlockFace#SOUTH
*/
public Door(final TreeSpecies species, BlockFace face) {
this(getWoodDoorOfSpecies(species), face, false);
}
/**
* Constructs the bottom half of a wooden door of the given species, facing the specified direction and set to open
* or closed
*
* @param species The species this wooden door is made of. This must match the species of the block above.
* @param face The direction the door is facing.
* @param isOpen Whether the door is currently opened.
* @see TreeSpecies
* @see BlockFace#WEST
* @see BlockFace#NORTH
* @see BlockFace#EAST
* @see BlockFace#SOUTH
*/
public Door(final TreeSpecies species, BlockFace face, boolean isOpen) {
this(getWoodDoorOfSpecies(species), face, isOpen);
}
/**
* Constructs the top half of a wooden door of the given species and with the hinge on the left or right
*
* @param species The species this wooden door is made of. This must match the species of the block below.
* @param isHingeRight True if the hinge is on the right hand side, false if the hinge is on the left hand side.
* @see TreeSpecies
*/
public Door(final TreeSpecies species, boolean isHingeRight) {
this(getWoodDoorOfSpecies(species), isHingeRight);
}
/**
* Constructs a tree block of the given tree species.
*
* @param species the species of the tree block
*/
public Tree(TreeSpecies species) {
this(DEFAULT_TYPE, species, DEFAULT_DIRECTION);
}
/**
* Constructs a tree block of the given tree species, and facing the given
* direction.
*
* @param species the species of the tree block
* @param dir the direction the tree block is facing
*/
public Tree(TreeSpecies species, BlockFace dir) {
this(DEFAULT_TYPE, species, dir);
}