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

/src/com/firestar/animate/Animate.java

https://github.com/firestar/animation
Java | 296 lines | 256 code | 12 blank | 28 comment | 97 complexity | e627cc82bfe33c7b1bd6599c39bbb1fd MD5 | raw file
  1. package com.firestar.animate;
  2. import java.util.Hashtable;
  3. import java.util.Map;
  4. import java.util.logging.Logger;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.command.Command;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.Event;
  12. import org.bukkit.plugin.PluginDescriptionFile;
  13. import org.bukkit.plugin.PluginManager;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15. public class Animate extends JavaPlugin {
  16. public Logger log = Logger.getLogger("Minecraft");
  17. private final AnimateBlockListener blockListener = new AnimateBlockListener(this);
  18. private Map<String, Animation> animations = new Hashtable<String, Animation>();
  19. private Map<String, Animator> animators = new Hashtable<String, Animator>();
  20. public void onEnable() {
  21. PluginManager pm = getServer().getPluginManager();
  22. pm.registerEvents(new AnimateBlockListener(this), this);
  23. PluginDescriptionFile pdfFile = this.getDescription();
  24. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  25. }
  26. public void onDisable() {
  27. }
  28. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
  29. Player player = null;
  30. String commandName = command.getName();
  31. if (sender instanceof Player) {
  32. player = (Player) sender;
  33. } else {
  34. log.info("Cannot execute any commands from console for this plugin :(");
  35. return false;
  36. }
  37. Animator animator = getAnimator(player.getName());
  38. if (commandName.equalsIgnoreCase("cra")) {
  39. if (animator.hasOpenAnimation()) {
  40. player.sendMessage("Already have animation open!");
  41. } else {
  42. if (args.length == 1) {
  43. Animation animation = getAnimation(args[0]);
  44. if (animation == null) {
  45. createNewAnimation(args[0], player);
  46. player.sendMessage("Animation created!");
  47. } else {
  48. player.sendMessage("Animation with that name already exists!");
  49. }
  50. } else {
  51. player.sendMessage("Incorrect Animation Name!");
  52. }
  53. }
  54. return true;
  55. } else if (commandName.equalsIgnoreCase("cla")) {
  56. if (!animator.hasOpenAnimation()) {
  57. player.sendMessage("Animation is not open!");
  58. } else {
  59. animator.closeAnimation();
  60. player.sendMessage("Animation closed!");
  61. }
  62. return true;
  63. } else if (commandName.equalsIgnoreCase("opa")) {
  64. if (animator.hasOpenAnimation()) {
  65. player.sendMessage("Animation already open, please close it!");
  66. } else {
  67. if (args.length == 1) {
  68. Animation animation = getAnimation(args[0]);
  69. if (animation != null) {
  70. animator.openAnimation(animation);
  71. player.sendMessage("Animation opened!");
  72. } else {
  73. player.sendMessage("Animation with that name does not exist!");
  74. }
  75. } else {
  76. player.sendMessage("Incorrect Animation Name!");
  77. }
  78. }
  79. return true;
  80. } else if (commandName.equalsIgnoreCase("sea")) {
  81. if (animator.hasOpenAnimation()) {
  82. Animation open_anime = animator.getOpenAnimation();
  83. if (!open_anime.isAreaSet()) {
  84. if (animator.locationsSet()) {
  85. Area area = new Area(this, player.getWorld(), animator.getLoc1(), animator.getLoc2());
  86. open_anime.setArea(area);
  87. player.sendMessage("Animation area is now set, use saf to set frame! " + area.get_blocks().size());
  88. /*
  89. Hashtable<Integer, Location> jsu = player_pos.get(Sender_Name);
  90. if (jsu.containsKey(0) && jsu.containsKey(1)) {
  91. area j = new area(this, player.getWorld(), jsu.get(0), jsu.get(1));
  92. animations_save_locations.put(open_anime, j.get_blocks());
  93. setAnimation(open_anime);
  94. player.sendMessage("Animation is now set, use saf to set frame!" + j.get_blocks().size());
  95. } else {
  96. player.sendMessage("select the positions!");
  97. }
  98. */
  99. } else {
  100. player.sendMessage("select the positions!");
  101. }
  102. } else {
  103. player.sendMessage("Animation already set!");
  104. }
  105. } else {
  106. player.sendMessage("No open animation!");
  107. }
  108. return true;
  109. } else if (commandName.equalsIgnoreCase("saf")) {
  110. if (animator.hasOpenAnimation()) {
  111. Animation open_anime = animator.getOpenAnimation();
  112. if (open_anime.isAreaSet()) {
  113. Frameset this_frameset = open_anime.getFrames();
  114. if (this_frameset.frames.size() == 0) {
  115. Hashtable<Integer, Block> blocks = new Hashtable<Integer, Block>();
  116. for (Location location : open_anime.getArea().get_blocks()) {
  117. blocks.put(blocks.size(), this_frameset.this_world.getBlockAt(location));
  118. }
  119. this_frameset.add_frame(blocks);
  120. player.sendMessage("Initial frame saved: " + blocks.size() + " Saved Frame: " + this_frameset.frames.size());
  121. } else {
  122. Hashtable<Integer, Block> blocks = new Hashtable<Integer, Block>();
  123. Map<Location, Material> jprevtype = this_frameset.frames.get((this_frameset.frames.size() - 1)).frame_blocks_type;
  124. Map<Location, Byte> jprevbyte = this_frameset.frames.get((this_frameset.frames.size() - 1)).frame_blocks_data;
  125. for (Location location : open_anime.getArea().get_blocks()) {
  126. if (this_frameset.this_world.getBlockAt(location).getType() != jprevtype.get(location)) {
  127. blocks.put(blocks.size(), this_frameset.this_world.getBlockAt(location));
  128. } else if (this_frameset.this_world.getBlockAt(location).getData() != jprevbyte.get(location)) {
  129. blocks.put(blocks.size(), this_frameset.this_world.getBlockAt(location));
  130. }
  131. }
  132. this_frameset.add_frame(blocks);
  133. player.sendMessage("blocks set to frame! changes: " + blocks.size() + " Saved Frame: " + this_frameset.frames.size());
  134. }
  135. //animation_frame_sets.put(open_anime, this_frameset); // unnecessary because the frame set should be modified itself - doesn't need to be readded to the map
  136. } else {
  137. player.sendMessage("Animation not set!");
  138. }
  139. } else {
  140. player.sendMessage("No open animation!");
  141. }
  142. return true;
  143. } else if (commandName.equalsIgnoreCase("play")) {
  144. if (animator.hasOpenAnimation()) {
  145. Animation open_anime = animator.getOpenAnimation();
  146. if (open_anime.isAreaSet()) {
  147. if (!open_anime.isPlaying()) {
  148. Thread animation_player = null;
  149. if (args.length == 1) {
  150. if (args[0].equalsIgnoreCase("t")) {
  151. open_anime.setRepeat(true);
  152. animation_player = new Play(this, open_anime);
  153. } else {
  154. open_anime.setRepeat(false);
  155. animation_player = new Play(this, open_anime, Integer.valueOf(args[0]));
  156. }
  157. } else if (args.length == 2) {
  158. if (args[1].equalsIgnoreCase("t")) {
  159. open_anime.setRepeat(true);
  160. }
  161. animation_player = new Play(this, open_anime, Integer.valueOf(args[0]));
  162. } else {
  163. open_anime.setRepeat(false);
  164. animation_player = new Play(this, open_anime);
  165. }
  166. animation_player.start();
  167. player.sendMessage("Now Playing!");
  168. } else {
  169. player.sendMessage("Already playing!");
  170. }
  171. } else {
  172. player.sendMessage("Animation not set!");
  173. }
  174. } else {
  175. player.sendMessage("No open animation!");
  176. }
  177. return true;
  178. } else if (commandName.equalsIgnoreCase("stp")) {
  179. if (animator.hasOpenAnimation()) {
  180. Animation open_anime = animator.getOpenAnimation();
  181. if (open_anime.isAreaSet()) {
  182. if (open_anime.isPlaying()) {
  183. if (open_anime.isRepeat()) {
  184. player.sendMessage("Repeat turned off!");
  185. open_anime.setRepeat(false);
  186. } else {
  187. player.sendMessage("Repeat already off!");
  188. }
  189. } else {
  190. player.sendMessage("Not running!");
  191. }
  192. } else {
  193. player.sendMessage("Animation not set!");
  194. }
  195. } else {
  196. player.sendMessage("No open animation!");
  197. }
  198. return true;
  199. } else if (commandName.equalsIgnoreCase("gtf")) {
  200. if (animator.hasOpenAnimation()) {
  201. Animation open_anime = animator.getOpenAnimation();
  202. if (open_anime.isAreaSet()) {
  203. if (args.length == 1) {
  204. if (args[0].equalsIgnoreCase("lt")) {
  205. Frameset g = open_anime.getFrames();
  206. Integer hi = 0;
  207. while (hi < g.frames.size()) {
  208. g.gt(hi);
  209. hi++;
  210. }
  211. player.sendMessage("frame " + g.frames.size() + "!");
  212. } else if (args[0].equalsIgnoreCase("ft")) {
  213. Frameset g = open_anime.getFrames();
  214. g.gt(0);
  215. player.sendMessage("frame 1!");
  216. } else if (is_integer(args[0])) {
  217. Frameset g = open_anime.getFrames();
  218. if (g.frames.size() >= Integer.valueOf(args[0])) {
  219. Integer hi = 0;
  220. while (hi < Integer.valueOf(args[0])) {
  221. g.gt(hi);
  222. hi++;
  223. }
  224. player.sendMessage("frame " + (Integer.valueOf(args[0]) + 1) + "!");
  225. } else {
  226. player.sendMessage("No such frame!");
  227. }
  228. } else {
  229. player.sendMessage("Incorrect Input!");
  230. }
  231. } else {
  232. player.sendMessage("Specify a frame!");
  233. }
  234. } else {
  235. player.sendMessage("Animation not set!");
  236. }
  237. } else {
  238. player.sendMessage("No open animation!");
  239. }
  240. return true;
  241. }
  242. return false;
  243. }
  244. /**
  245. * The animation with this name.
  246. * @param name The name of the animation.
  247. * @return null if the animation does not yet exist.
  248. */
  249. public Animation getAnimation(String name) {
  250. return animations.get(name);
  251. }
  252. /**
  253. * The animator with this name.
  254. * @param name The name of the animator.
  255. * @return The animator, or a new animator if it does not yet exist.
  256. */
  257. public Animator getAnimator(String name) {
  258. Animator animator = animators.get(name);
  259. if (animator == null) {
  260. animator = new Animator();
  261. animators.put(name, animator);
  262. }
  263. return animator;
  264. }
  265. /**
  266. *
  267. * WARNING: currently will override any existing animation of the same name.
  268. * @param name
  269. * @param player
  270. */
  271. private void createNewAnimation(String name, Player player) {
  272. Animation animation = new Animation(new Frameset(name, this, player.getWorld()));
  273. getAnimator(player.getName()).openAnimation(animation);
  274. animations.put(name, animation);
  275. }
  276. public boolean is_integer(String input) {
  277. try {
  278. Integer.parseInt(input);
  279. return true;
  280. } catch (Exception e) {
  281. }
  282. return false;
  283. }
  284. }