PageRenderTime 339ms CodeModel.GetById 67ms RepoModel.GetById 28ms app.codeStats 1ms

/Essentials/src/com/earth2me/essentials/Teleport.java

https://github.com/krosk93/Essentials
Java | 271 lines | 234 code | 33 blank | 4 comment | 33 complexity | 6963f536158b194d0d921671dd74fe2f MD5 | raw file
  1. package com.earth2me.essentials;
  2. import com.earth2me.essentials.commands.NotEnoughArgumentsException;
  3. import java.util.Calendar;
  4. import java.util.GregorianCalendar;
  5. import java.util.logging.Logger;
  6. import org.bukkit.Location;
  7. import org.bukkit.entity.Entity;
  8. public class Teleport implements Runnable
  9. {
  10. private static final double MOVE_CONSTANT = 0.3;
  11. private static class Target
  12. {
  13. private final Location location;
  14. private final Entity entity;
  15. public Target(Location location)
  16. {
  17. this.location = location;
  18. this.entity = null;
  19. }
  20. public Target(Entity entity)
  21. {
  22. this.entity = entity;
  23. this.location = null;
  24. }
  25. public Location getLocation()
  26. {
  27. if (this.entity != null)
  28. {
  29. return this.entity.getLocation();
  30. }
  31. return location;
  32. }
  33. }
  34. private IUser user;
  35. private int teleTimer = -1;
  36. private long started; // time this task was initiated
  37. private long delay; // how long to delay the teleport
  38. private int health;
  39. // note that I initially stored a clone of the location for reference, but...
  40. // when comparing locations, I got incorrect mismatches (rounding errors, looked like)
  41. // so, the X/Y/Z values are stored instead and rounded off
  42. private long initX;
  43. private long initY;
  44. private long initZ;
  45. private Target teleportTarget;
  46. private Trade chargeFor;
  47. private final IEssentials ess;
  48. private static final Logger logger = Logger.getLogger("Minecraft");
  49. private void initTimer(long delay, Target target, Trade chargeFor)
  50. {
  51. this.started = System.currentTimeMillis();
  52. this.delay = delay;
  53. this.health = user.getHealth();
  54. this.initX = Math.round(user.getLocation().getX() * MOVE_CONSTANT);
  55. this.initY = Math.round(user.getLocation().getY() * MOVE_CONSTANT);
  56. this.initZ = Math.round(user.getLocation().getZ() * MOVE_CONSTANT);
  57. this.teleportTarget = target;
  58. this.chargeFor = chargeFor;
  59. }
  60. public void run()
  61. {
  62. if (user == null || !user.isOnline() || user.getLocation() == null)
  63. {
  64. cancel();
  65. return;
  66. }
  67. if (Math.round(user.getLocation().getX() * MOVE_CONSTANT) != initX
  68. || Math.round(user.getLocation().getY() * MOVE_CONSTANT) != initY
  69. || Math.round(user.getLocation().getZ() * MOVE_CONSTANT) != initZ
  70. || user.getHealth() < health)
  71. { // user moved, cancel teleport
  72. cancel(true);
  73. return;
  74. }
  75. health = user.getHealth(); // in case user healed, then later gets injured
  76. long now = System.currentTimeMillis();
  77. if (now > started + delay)
  78. {
  79. try
  80. {
  81. cooldown(false);
  82. user.sendMessage(Util.i18n("teleportationCommencing"));
  83. try
  84. {
  85. now(teleportTarget);
  86. if (chargeFor != null)
  87. {
  88. chargeFor.charge(user);
  89. }
  90. }
  91. catch (Throwable ex)
  92. {
  93. ess.showError(user.getBase(), ex, "teleport");
  94. }
  95. return;
  96. }
  97. catch (Exception ex)
  98. {
  99. user.sendMessage(Util.format("cooldownWithMessage", ex.getMessage()));
  100. }
  101. }
  102. }
  103. public Teleport(IUser user, IEssentials ess)
  104. {
  105. this.user = user;
  106. this.ess = ess;
  107. }
  108. public void respawn(Spawn spawn, Trade chargeFor) throws Exception
  109. {
  110. teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor);
  111. }
  112. public void warp(String warp, Trade chargeFor) throws Exception
  113. {
  114. Location loc = ess.getWarps().getWarp(warp);
  115. teleport(new Target(loc), chargeFor);
  116. user.sendMessage(Util.format("warpingTo", warp));
  117. }
  118. public void cooldown(boolean check) throws Exception
  119. {
  120. Calendar now = new GregorianCalendar();
  121. if (user.getLastTeleportTimestamp() > 0)
  122. {
  123. double cooldown = ess.getSettings().getTeleportCooldown();
  124. Calendar cooldownTime = new GregorianCalendar();
  125. cooldownTime.setTimeInMillis(user.getLastTeleportTimestamp());
  126. cooldownTime.add(Calendar.SECOND, (int)cooldown);
  127. cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0));
  128. if (cooldownTime.after(now) && !user.isAuthorized("essentials.teleport.cooldown.bypass"))
  129. {
  130. throw new Exception(Util.format("timeBeforeTeleport", Util.formatDateDiff(cooldownTime.getTimeInMillis())));
  131. }
  132. }
  133. // if justCheck is set, don't update lastTeleport; we're just checking
  134. if (!check)
  135. {
  136. user.setLastTeleportTimestamp(now.getTimeInMillis());
  137. }
  138. }
  139. public void cancel(boolean notifyUser)
  140. {
  141. if (teleTimer == -1)
  142. {
  143. return;
  144. }
  145. try
  146. {
  147. ess.getServer().getScheduler().cancelTask(teleTimer);
  148. if (notifyUser)
  149. {
  150. user.sendMessage(Util.i18n("pendingTeleportCancelled"));
  151. }
  152. }
  153. finally
  154. {
  155. teleTimer = -1;
  156. }
  157. }
  158. public void cancel()
  159. {
  160. cancel(false);
  161. }
  162. public void teleport(Location loc, Trade chargeFor) throws Exception
  163. {
  164. teleport(new Target(loc), chargeFor);
  165. }
  166. public void teleport(Entity entity, Trade chargeFor) throws Exception
  167. {
  168. teleport(new Target(entity), chargeFor);
  169. }
  170. private void teleport(Target target, Trade chargeFor) throws Exception
  171. {
  172. double delay = ess.getSettings().getTeleportDelay();
  173. if (chargeFor != null)
  174. {
  175. chargeFor.isAffordableFor(user);
  176. }
  177. cooldown(true);
  178. if (delay <= 0 || user.isAuthorized("essentials.teleport.timer.bypass"))
  179. {
  180. cooldown(false);
  181. now(target);
  182. if (chargeFor != null)
  183. {
  184. chargeFor.charge(user);
  185. }
  186. return;
  187. }
  188. cancel();
  189. Calendar c = new GregorianCalendar();
  190. c.add(Calendar.SECOND, (int)delay);
  191. c.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
  192. user.sendMessage(Util.format("dontMoveMessage", Util.formatDateDiff(c.getTimeInMillis())));
  193. initTimer((long)(delay * 1000.0), target, chargeFor);
  194. teleTimer = ess.scheduleSyncRepeatingTask(this, 10, 10);
  195. }
  196. private void now(Target target) throws Exception
  197. {
  198. cancel();
  199. user.setLastLocation();
  200. user.getBase().teleport(Util.getSafeDestination(target.getLocation()));
  201. }
  202. public void now(Location loc) throws Exception
  203. {
  204. cooldown(false);
  205. now(new Target(loc));
  206. }
  207. public void now(Location loc, Trade chargeFor) throws Exception
  208. {
  209. cooldown(false);
  210. chargeFor.charge(user);
  211. now(new Target(loc));
  212. }
  213. public void now(Entity entity, boolean cooldown) throws Exception
  214. {
  215. if (cooldown)
  216. {
  217. cooldown(false);
  218. }
  219. now(new Target(entity));
  220. }
  221. public void back(Trade chargeFor) throws Exception
  222. {
  223. teleport(new Target(user.getLastLocation()), chargeFor);
  224. }
  225. public void back() throws Exception
  226. {
  227. now(new Target(user.getLastLocation()));
  228. }
  229. public void home(IUser user, String home, Trade chargeFor) throws Exception
  230. {
  231. final Location loc = user.getHome(home);
  232. if (loc == null)
  233. {
  234. throw new NotEnoughArgumentsException();
  235. }
  236. teleport(new Target(loc), chargeFor);
  237. }
  238. }