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

https://github.com/GuntherDW/BetterMechanics · Java · 167 lines · 151 code · 11 blank · 5 comment · 42 complexity · 67ffbb71b03a369a58c1739f87551d76 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 Bridge {
  23. private Logger log = Logger.getLogger("Minecraft");
  24. private Sign sign;
  25. private Player player;
  26. private Chest chest;
  27. private MechanicsConfig.BridgeConfig config;
  28. private Set<Block> blockSet;
  29. private MaterialData bridgeMaterial;
  30. public Bridge(MechanicsConfig c, Sign s, Player p) {
  31. sign = s;
  32. player = p;
  33. config = c.getBridgeConfig();
  34. }
  35. public boolean map() throws NonCardinalDirectionException, BlockNotFoundException, InvalidMaterialException, ChestNotFoundException {
  36. if (!config.enabled)
  37. return false;
  38. BlockFace bf;
  39. if (config.canUseBlock(sign.getBlock().getRelative(BlockFace.UP).getType())) {
  40. bf = BlockFace.UP;
  41. bridgeMaterial = new MaterialData(sign.getBlock().getRelative(BlockFace.UP).getType(), sign.getBlock().getRelative(BlockFace.UP).getData());
  42. } else if (config.canUseBlock(sign.getBlock().getRelative(BlockFace.DOWN).getType())) {
  43. bf = BlockFace.DOWN;
  44. bridgeMaterial = new MaterialData(sign.getBlock().getRelative(BlockFace.DOWN).getType(), sign.getBlock().getRelative(BlockFace.DOWN).getData());
  45. } else {
  46. throw new InvalidMaterialException();
  47. }
  48. MechanicsType bridgeType = SignUtil.getMechanicsType(sign);
  49. Sign endSign = BlockMapper.findMechanicsSign(sign.getBlock(), SignUtil.getBackBlockFace(sign), bridgeType, config.maxLength);
  50. Block startBlock = sign.getBlock().getRelative(SignUtil.getBackBlockFace(sign)).getRelative(bf);
  51. Block endBlock = endSign.getBlock().getRelative(bf);
  52. try {
  53. blockSet = BlockMapper.mapHorizontal(SignUtil.getBackBlockFace(sign), startBlock, endBlock, bridgeType == MechanicsType.SMALL_BRIDGE);
  54. if (!blockSet.isEmpty()) {
  55. Block chestBlock = BlockMapper.mapCuboidRegion(sign.getBlock(), 3, Material.CHEST);
  56. if (chestBlock == null) {
  57. //Check other sign
  58. chestBlock = BlockMapper.mapCuboidRegion(endSign.getBlock(), 3, Material.CHEST);
  59. if (chestBlock == null) {
  60. throw new ChestNotFoundException();
  61. }
  62. }
  63. chest = BlockbagUtil.getChest(chestBlock);
  64. if (chest == null) {
  65. throw new ChestNotFoundException();
  66. }
  67. return true;
  68. } else {
  69. log.info("[BetterMechanics] Empty blockSet?");
  70. return false;
  71. }
  72. } catch (InvalidDirectionException ex) {
  73. log.info("[BetterMechanics] Our mapper is acting weird!");
  74. return false;
  75. }
  76. }
  77. public void toggleOpen() {
  78. int amount = 0;
  79. try {
  80. for (Block b : blockSet) {
  81. if (b.getType() == bridgeMaterial.getItemType()) {
  82. b.setType(Material.AIR);
  83. amount++;
  84. }
  85. }
  86. BlockbagUtil.safeAddItems(chest, bridgeMaterial.toItemStack(amount));
  87. if (player != null) {
  88. player.sendMessage(ChatColor.GOLD + "Bridge opened!");
  89. }
  90. } catch (OutOfSpaceException ex) {
  91. for (Block b : blockSet) {
  92. if (b.getType() == Material.AIR) {
  93. b.setType(bridgeMaterial.getItemType());
  94. b.setData(bridgeMaterial.getData());
  95. amount--;
  96. if (amount == 0) {
  97. if (player != null) {
  98. player.sendMessage(ChatColor.RED + "Not enough space in chest!");
  99. }
  100. return;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. public void toggleClosed() {
  107. int amount = 0;
  108. try {
  109. for (Block b : blockSet) {
  110. if (canPassThrough(b.getType())) {
  111. b.setType(bridgeMaterial.getItemType());
  112. b.setData(bridgeMaterial.getData());
  113. amount++;
  114. }
  115. }
  116. BlockbagUtil.safeRemoveItems(chest, bridgeMaterial.toItemStack(amount));
  117. if (player != null) {
  118. player.sendMessage(ChatColor.GOLD + "Bridge closed!");
  119. }
  120. } catch (OutOfMaterialException ex) {
  121. for (Block b : blockSet) {
  122. if (b.getType() == bridgeMaterial.getItemType()) {
  123. b.setType(Material.AIR);
  124. amount--;
  125. if (amount == 0) {
  126. if (player != null) {
  127. player.sendMessage(ChatColor.RED + "Not enough items in chest! Still need: " + Integer.toString(ex.getAmount()) + " of type: " + bridgeMaterial.getItemType().name());
  128. }
  129. return;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. private boolean canPassThrough(Material m) {
  136. switch (m) {
  137. case AIR:
  138. case WATER:
  139. case STATIONARY_WATER:
  140. case LAVA:
  141. case STATIONARY_LAVA:
  142. case SNOW:
  143. return true;
  144. default:
  145. return false;
  146. }
  147. }
  148. public boolean isClosed() {
  149. for (Block b : blockSet) {
  150. if (b.getType() == bridgeMaterial.getItemType() || canPassThrough(b.getType())) {
  151. return (!canPassThrough(b.getType()));
  152. }
  153. }
  154. return false;
  155. }
  156. }