PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/edoxile/bukkit/bettermechanics/mechanics/Door.java

https://github.com/Retinue/BetterMechanics
Java | 203 lines | 187 code | 12 blank | 4 comment | 46 complexity | ce2ef64c9ae6c4a8574f6781714e0add MD5 | raw file
  1. package com.edoxile.bukkit.bettermechanics.mechanics;
  2. import java.util.Set;
  3. import java.util.logging.Logger;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.block.BlockFace;
  8. import org.bukkit.block.Chest;
  9. import org.bukkit.block.Sign;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.material.MaterialData;
  12. import com.edoxile.bukkit.bettermechanics.MechanicsType;
  13. import com.edoxile.bukkit.bettermechanics.exceptions.BlockNotFoundException;
  14. import com.edoxile.bukkit.bettermechanics.exceptions.ChestNotFoundException;
  15. import com.edoxile.bukkit.bettermechanics.exceptions.InvalidDirectionException;
  16. import com.edoxile.bukkit.bettermechanics.exceptions.InvalidMaterialException;
  17. import com.edoxile.bukkit.bettermechanics.exceptions.NonCardinalDirectionException;
  18. import com.edoxile.bukkit.bettermechanics.exceptions.OutOfMaterialException;
  19. import com.edoxile.bukkit.bettermechanics.exceptions.OutOfSpaceException;
  20. import com.edoxile.bukkit.bettermechanics.utils.BlockMapper;
  21. import com.edoxile.bukkit.bettermechanics.utils.BlockbagUtil;
  22. import com.edoxile.bukkit.bettermechanics.utils.MechanicsConfig;
  23. import com.edoxile.bukkit.bettermechanics.utils.SignUtil;
  24. /**
  25. * Created by IntelliJ IDEA. User: Edoxile
  26. */
  27. public class Door {
  28. private static final Logger log = Logger.getLogger("Minecraft");
  29. private Sign sign;
  30. private Player player;
  31. private Chest chest;
  32. private MechanicsConfig.DoorConfig config;
  33. private Set<Block> blockSet;
  34. private MaterialData doorMaterial;
  35. public Door(MechanicsConfig c, Sign s, Player p) {
  36. sign = s;
  37. player = p;
  38. config = c.getDoorConfig();
  39. }
  40. public boolean map() throws InvalidMaterialException,
  41. BlockNotFoundException, NonCardinalDirectionException,
  42. ChestNotFoundException {
  43. if (!config.enabled)
  44. return false;
  45. BlockFace direction;
  46. BlockFace orientation = SignUtil.getBlockFace(sign);
  47. MechanicsType doorType = SignUtil.getMechanicsType(sign);
  48. if (sign.getLine(1).equalsIgnoreCase("[Door Down]")
  49. || sign.getLine(1).equalsIgnoreCase("[sDoor Down]")) {
  50. direction = BlockFace.DOWN;
  51. } else {
  52. direction = BlockFace.UP;
  53. }
  54. if (config
  55. .canUseBlock(sign.getBlock().getRelative(direction).getType())) {
  56. doorMaterial = new MaterialData(sign.getBlock()
  57. .getRelative(direction).getType(), sign.getBlock()
  58. .getRelative(direction).getData());
  59. } else {
  60. throw new InvalidMaterialException();
  61. }
  62. Sign endSign = BlockMapper.findMechanicsSign(sign.getBlock(),
  63. direction, doorType, config.maxHeight);
  64. Block startBlock = sign.getBlock().getRelative(direction)
  65. .getRelative(direction);
  66. Block endBlock = null;
  67. switch (direction) {
  68. case UP:
  69. endBlock = endSign.getBlock().getRelative(BlockFace.DOWN);
  70. break;
  71. case DOWN:
  72. endBlock = endSign.getBlock().getRelative(BlockFace.UP);
  73. break;
  74. }
  75. try {
  76. blockSet = BlockMapper.mapVertical(direction, orientation,
  77. startBlock, endBlock,
  78. (doorType == MechanicsType.SMALL_DOOR));
  79. if (!blockSet.isEmpty()) {
  80. Block chestBlock = BlockMapper.mapCuboidRegion(sign.getBlock(),
  81. 3, Material.CHEST);
  82. if (chestBlock == null) {
  83. // Check other sign
  84. chestBlock = BlockMapper.mapCuboidRegion(
  85. endSign.getBlock(), 3, Material.CHEST);
  86. if (chestBlock == null) {
  87. throw new ChestNotFoundException();
  88. }
  89. }
  90. chest = BlockbagUtil.getChest(chestBlock);
  91. if (chest == null) {
  92. throw new ChestNotFoundException();
  93. }
  94. return true;
  95. } else {
  96. log.info("[BetterMechanics] Empty blockSet?");
  97. return false;
  98. }
  99. } catch (InvalidDirectionException e) {
  100. log.info("[BetterMechanics] Our mapper is acting weird!");
  101. return false;
  102. }
  103. }
  104. public void toggleOpen() {
  105. int amount = 0;
  106. try {
  107. for (Block b : blockSet) {
  108. if (b.getType() == doorMaterial.getItemType()
  109. && b.getData() == doorMaterial.getData()) {
  110. b.setType(Material.AIR);
  111. amount++;
  112. }
  113. }
  114. BlockbagUtil.safeAddItems(chest, doorMaterial.toItemStack(amount));
  115. if (player != null) {
  116. player.sendMessage(ChatColor.GOLD + "Door opened!");
  117. }
  118. } catch (OutOfSpaceException ex) {
  119. for (Block b : blockSet) {
  120. if (b.getType() == Material.AIR) {
  121. b.setType(doorMaterial.getItemType());
  122. b.setData(doorMaterial.getData());
  123. amount--;
  124. if (amount == 0) {
  125. if (player != null) {
  126. player.sendMessage(ChatColor.RED
  127. + "Not enough space in chest!");
  128. }
  129. return;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. public void toggleClosed() {
  136. int amount = 0;
  137. try {
  138. for (Block b : blockSet) {
  139. if (canPassThrough(b.getType())) {
  140. b.setType(doorMaterial.getItemType());
  141. b.setData(doorMaterial.getData());
  142. amount++;
  143. }
  144. }
  145. BlockbagUtil.safeRemoveItems(chest,
  146. doorMaterial.toItemStack(amount));
  147. if (player != null) {
  148. player.sendMessage(ChatColor.GOLD + "Door closed!");
  149. }
  150. } catch (OutOfMaterialException ex) {
  151. for (Block b : blockSet) {
  152. if (b.getType() == doorMaterial.getItemType()) {
  153. b.setType(Material.AIR);
  154. amount--;
  155. if (amount == 0) {
  156. if (player != null) {
  157. player.sendMessage(ChatColor.RED
  158. + "Not enough items in chest! Still need: "
  159. + Integer.toString(ex.getAmount())
  160. + " of type: "
  161. + doorMaterial.getItemType().name());
  162. }
  163. return;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. private boolean canPassThrough(Material m) {
  170. switch (m) {
  171. case AIR:
  172. case WATER:
  173. case STATIONARY_WATER:
  174. case LAVA:
  175. case STATIONARY_LAVA:
  176. case SNOW:
  177. return true;
  178. default:
  179. return false;
  180. }
  181. }
  182. public boolean isClosed() {
  183. for (Block b : blockSet) {
  184. if (b.getType() == doorMaterial.getItemType()
  185. || canPassThrough(b.getType())) {
  186. return (!canPassThrough(b.getType()));
  187. }
  188. }
  189. return false;
  190. }
  191. }