Advertisement
starboy103

Untitled

Sep 2nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. private boolean putHeadOnStake(Player p) {
  2. if(!getConfigManager().getConfig("main").getBoolean(getBaseConfig()+"onStake")){
  3. return false;
  4. }
  5. //head location
  6. Location head = p.getEyeLocation();
  7. //block the player's head is in
  8. Block headBlock = head.getBlock();
  9.  
  10. //don't do anything if below the world
  11. if(headBlock.getY() < 0) {
  12. return false;
  13. }
  14.  
  15. //get the closest non air block below the players feet
  16. Block ground = getClosestGround(headBlock.getRelative(BlockFace.DOWN, 2));
  17. if (ground == null) {
  18. return false;
  19. }
  20.  
  21. //get the block 2 above the ground
  22. Block skullBlock = ground.getRelative(BlockFace.UP, 2);
  23.  
  24. //if it's not empty we can't place the block
  25. if (skullBlock == null || !skullBlock.isEmpty()) {
  26. return false;
  27. }
  28.  
  29. //teleport the player to the skull location
  30. p.teleport(skullBlock.getLocation());
  31.  
  32. //check the player's permission for the stake here, used for position based permissions
  33. //TODO this doesn't appear to work as a valid way to check permissions for a coordinate
  34. if (!p.hasPermission(PLAYER_HEAD_STAKE)) {
  35. return false;
  36. }
  37.  
  38. //set the skull block to an actual skull block
  39. setBlockAsHead(p, skullBlock);
  40.  
  41. //get the space for a fence and set it if there's nothing there
  42. Block fenceBlock = ground.getRelative(BlockFace.UP);
  43. if (fenceBlock != null && fenceBlock.isEmpty()) {
  44. fenceBlock.setType(Material.NETHER_FENCE);
  45. }
  46. //made successfully
  47. return true;
  48. }
  49.  
  50. /**
  51. * Sets the block given to head of the player
  52. * @param p the player
  53. * @param headBlock the block to set
  54. */
  55. private static void setBlockAsHead(Player p, Block headBlock) {
  56. //set the type to skull
  57. headBlock.setType(Material.SKULL);
  58. //noinspection deprecation
  59. headBlock.setData((byte) 1); //TODO depreacted but no alternative yet
  60. //get the state to be a player skull for the player and set its rotation based on where the player was looking
  61. Skull state = (Skull) headBlock.getState();
  62. state.setSkullType(SkullType.PLAYER);
  63. state.setOwner(p.getName());
  64. state.setRotation(ServerUtil.getCardinalDirection(p));
  65. state.update();
  66. }
  67.  
  68. /**
  69. * Gets the closest non empty block under the block supplied or null if none found
  70. *
  71. * @param block Block
  72. * @return Block
  73. */
  74. private static Block getClosestGround(Block block) {
  75. Block loopBlock = block;
  76. //recurse until found
  77. while (true) {
  78. //if below the world return null
  79. if (loopBlock.getY() < 0) {
  80. return null;
  81. }
  82.  
  83. //if it's not empty return this block
  84. if (!loopBlock.isEmpty()) {
  85. return block;
  86. }
  87. loopBlock = loopBlock.getRelative(BlockFace.DOWN);
  88. }
  89. }
  90.  
  91. /**
  92. * Generates a player skull itemstack for the given name
  93. * @param name the player name
  94. * @return ItemStack
  95. */
  96. private static ItemStack playerSkullForName(String name) {
  97. //1 skull item
  98. ItemStack is = new ItemStack(Material.SKULL_ITEM, 1);
  99. //3 is a player skull
  100. is.setDurability((short) 3);
  101. //set the metadata for the owner
  102. SkullMeta meta = (SkullMeta) is.getItemMeta();
  103. meta.setOwner(name);
  104. is.setItemMeta(meta);
  105. return is;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement