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

https://github.com/GuntherDW/BetterMechanics · Java · 96 lines · 85 code · 7 blank · 4 comment · 17 complexity · 8e23540ade7bec8e5354635678c7ba37 MD5 · raw file

  1. package net.edoxile.bettermechanics.mechanics;
  2. import net.edoxile.bettermechanics.MechanicsType;
  3. import net.edoxile.bettermechanics.exceptions.BlockNotFoundException;
  4. import net.edoxile.bettermechanics.utils.BlockMapper;
  5. import net.edoxile.bettermechanics.utils.MechanicsConfig;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.block.BlockFace;
  11. import org.bukkit.block.Sign;
  12. import org.bukkit.entity.Player;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import java.util.logging.Logger;
  16. /**
  17. * Created by IntelliJ IDEA.
  18. * User: Edoxile
  19. */
  20. public class Lift {
  21. private static final Logger log = Logger.getLogger("Minecraft");
  22. private Sign sign;
  23. private Player player;
  24. private MechanicsConfig.LiftConfig config;
  25. private static List<Integer> nonSolidBlockList = Arrays.asList(0, 6, 8, 9, 10, 11, 27, 28, 31, 32, 37, 38, 39, 40, 50, 51, 55, 59, 63, 65, 66, 68, 69, 70, 72, 75, 76, 78, 83, 90, 93, 94);
  26. private Location destination;
  27. private String floorName;
  28. private BlockFace direction;
  29. public Lift(MechanicsConfig c, Sign s, Player p) {
  30. sign = s;
  31. player = p;
  32. config = c.getLiftConfig();
  33. }
  34. public boolean map() throws BlockNotFoundException {
  35. if (!config.enabled)
  36. return false;
  37. Sign endSign;
  38. if (sign.getLine(1).equalsIgnoreCase("[Lift Down]")) {
  39. direction = BlockFace.DOWN;
  40. } else {
  41. direction = BlockFace.UP;
  42. }
  43. endSign = BlockMapper.findMechanicsSign(sign.getBlock(), direction, MechanicsType.LIFT, config.maxSearchHeight);
  44. int x = player.getLocation().getBlockX();
  45. int z = player.getLocation().getBlockZ();
  46. int y = endSign.getBlock().getLocation().getBlockY();
  47. Block dest1 = sign.getWorld().getBlockAt(x, y, z);
  48. Block dest2 = dest1.getRelative(BlockFace.UP);
  49. Block dest3 = dest1.getRelative(BlockFace.DOWN);
  50. floorName = endSign.getLine(0);
  51. if (canPassThrough(dest1.getType()) && canPassThrough(dest2.getType())) {
  52. if (canPassThrough(dest3.getType()) && canPassThrough(dest3.getRelative(BlockFace.DOWN).getType())) {
  53. player.sendMessage(ChatColor.RED + "You have no place to stand on!");
  54. return false;
  55. }
  56. destination = player.getLocation();
  57. destination.setY(((double) dest1.getY()));
  58. return true;
  59. } else if (canPassThrough(dest1.getType()) && canPassThrough(dest3.getType())) {
  60. if (canPassThrough(dest3.getRelative(BlockFace.DOWN).getType()) && canPassThrough(dest3.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN).getType())) {
  61. player.sendMessage(ChatColor.RED + "You have no place to stand on!");
  62. return false;
  63. }
  64. destination = player.getLocation();
  65. destination.setY(((double) dest3.getY()));
  66. return true;
  67. } else {
  68. player.sendMessage(ChatColor.RED + "You would be obscured!");
  69. return false;
  70. }
  71. }
  72. public boolean movePlayer() {
  73. if (player.teleport(destination)) {
  74. if (floorName.equals("")) {
  75. player.sendMessage(ChatColor.GOLD + "You went " + direction.name().toLowerCase() + " a floor!");
  76. } else {
  77. player.sendMessage(ChatColor.GOLD + "You went to floor: " + floorName);
  78. }
  79. return true;
  80. } else {
  81. player.sendMessage(ChatColor.RED + "Something went wrong teleporting you!");
  82. log.warning("Couldn't teleport player '" + player.getName() + "' via lift.");
  83. return false;
  84. }
  85. }
  86. private boolean canPassThrough(Material m) {
  87. return nonSolidBlockList.contains(m.getId());
  88. }
  89. }