PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/edoxile/bettermechanics/mechanics/Door.java

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