Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean putHeadOnStake(Player p) {
- if(!getConfigManager().getConfig("main").getBoolean(getBaseConfig()+"onStake")){
- return false;
- }
- //head location
- Location head = p.getEyeLocation();
- //block the player's head is in
- Block headBlock = head.getBlock();
- //don't do anything if below the world
- if(headBlock.getY() < 0) {
- return false;
- }
- //get the closest non air block below the players feet
- Block ground = getClosestGround(headBlock.getRelative(BlockFace.DOWN, 2));
- if (ground == null) {
- return false;
- }
- //get the block 2 above the ground
- Block skullBlock = ground.getRelative(BlockFace.UP, 2);
- //if it's not empty we can't place the block
- if (skullBlock == null || !skullBlock.isEmpty()) {
- return false;
- }
- //teleport the player to the skull location
- p.teleport(skullBlock.getLocation());
- //check the player's permission for the stake here, used for position based permissions
- //TODO this doesn't appear to work as a valid way to check permissions for a coordinate
- if (!p.hasPermission(PLAYER_HEAD_STAKE)) {
- return false;
- }
- //set the skull block to an actual skull block
- setBlockAsHead(p, skullBlock);
- //get the space for a fence and set it if there's nothing there
- Block fenceBlock = ground.getRelative(BlockFace.UP);
- if (fenceBlock != null && fenceBlock.isEmpty()) {
- fenceBlock.setType(Material.NETHER_FENCE);
- }
- //made successfully
- return true;
- }
- /**
- * Sets the block given to head of the player
- * @param p the player
- * @param headBlock the block to set
- */
- private static void setBlockAsHead(Player p, Block headBlock) {
- //set the type to skull
- headBlock.setType(Material.SKULL);
- //noinspection deprecation
- headBlock.setData((byte) 1); //TODO depreacted but no alternative yet
- //get the state to be a player skull for the player and set its rotation based on where the player was looking
- Skull state = (Skull) headBlock.getState();
- state.setSkullType(SkullType.PLAYER);
- state.setOwner(p.getName());
- state.setRotation(ServerUtil.getCardinalDirection(p));
- state.update();
- }
- /**
- * Gets the closest non empty block under the block supplied or null if none found
- *
- * @param block Block
- * @return Block
- */
- private static Block getClosestGround(Block block) {
- Block loopBlock = block;
- //recurse until found
- while (true) {
- //if below the world return null
- if (loopBlock.getY() < 0) {
- return null;
- }
- //if it's not empty return this block
- if (!loopBlock.isEmpty()) {
- return block;
- }
- loopBlock = loopBlock.getRelative(BlockFace.DOWN);
- }
- }
- /**
- * Generates a player skull itemstack for the given name
- * @param name the player name
- * @return ItemStack
- */
- private static ItemStack playerSkullForName(String name) {
- //1 skull item
- ItemStack is = new ItemStack(Material.SKULL_ITEM, 1);
- //3 is a player skull
- is.setDurability((short) 3);
- //set the metadata for the owner
- SkullMeta meta = (SkullMeta) is.getItemMeta();
- meta.setOwner(name);
- is.setItemMeta(meta);
- return is;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement