Advertisement
Zalgo2462

Music Jacker

Aug 30th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.11 KB | None | 0 0
  1. import org.rsbot.Configuration;
  2. import org.rsbot.script.Script;
  3. import org.rsbot.script.ScriptManifest;
  4.  
  5. import org.rsbot.script.internal.event.PaintListener;
  6.  
  7.  
  8. import java.awt.*;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16.  
  17. import org.rsbot.script.methods.Game;
  18. import org.rsbot.script.methods.ui.Interfaces;
  19. import org.rsbot.service.IRC;
  20.  
  21.  
  22. import javax.imageio.ImageIO;
  23. import javax.sound.sampled.*;
  24.  
  25.  
  26. @ScriptManifest(name="Music Jacker", authors = {"Zalgo"})
  27. public class musicJacker extends Script implements PaintListener, MouseListener {
  28. File[] files;
  29. Clip currentSong;
  30. String currentName;
  31. String lastRSSong;
  32. int index = -1;
  33.  
  34. SongList songList;
  35.  
  36. @Override
  37. protected int loop() {
  38. if((Game.getCurrentTab().equals(Game.Tabs.MUSIC) && !Interfaces.getComponent(187, 4).getText().equals(lastRSSong))
  39. || index == -1) {
  40. lastRSSong = Interfaces.getComponent(187, 4).getText();
  41. try{
  42. playNextSong();
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. return 1000;
  48. }
  49.  
  50. @Override
  51. protected void onStop() {
  52. if(currentSong != null) {
  53. currentSong.stop();
  54. currentSong.close();
  55. }
  56. }
  57.  
  58. @Override
  59. protected boolean onRun() {
  60. File directory = new File(Configuration.Paths.getStorageDirectory() + File.separator + "Music Jacker");
  61. ArrayList<File> fileArrayList = new ArrayList<File>();
  62. if(!directory.exists()) {
  63. if(!directory.mkdir()) {
  64. return false;
  65. }
  66. }
  67. for(File file : directory.listFiles()) {
  68. if(file.getName().endsWith(".au") || file.getName().endsWith(".wav") || file.getName().endsWith(".aiff")) {
  69. fileArrayList.add(file);
  70. }
  71. }
  72. files = fileArrayList.toArray(new File[fileArrayList.size()]);
  73.  
  74. Game.openTab(Game.Tabs.MUSIC);
  75. lastRSSong = Interfaces.getComponent(187, 4).getText();
  76. songList = new SongList(listStart.x, listStart.y, 150, 189, 10, files);
  77. return true;
  78. }
  79.  
  80. private void playSong(File file) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  81. if(currentSong != null && currentSong.isOpen()) {
  82. currentSong.stop();
  83. currentSong.close();
  84. }
  85. currentName = file.getName().replace(".wav", "");
  86. currentName = currentName.replace(".au", "");
  87. currentName = currentName.replace(".aiff", "");
  88. currentSong = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, AudioSystem.getAudioFileFormat(file).getFormat()));
  89. currentSong.open(AudioSystem.getAudioInputStream(file));
  90. currentSong.loop(Clip.LOOP_CONTINUOUSLY);
  91. if(IRC.isConnected())
  92. IRC.dispatch.add("Now Playing: " + currentName);
  93. }
  94.  
  95. private void playNextSong() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  96. index++;
  97. if(index >= files.length - 1) {
  98. index = 0;
  99. }
  100. playSong(files[index]);
  101. }
  102.  
  103. private void playPriorSong() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  104. if(index != 0) {
  105. index--;
  106. playSong(files[index]);
  107. }
  108. }
  109.  
  110. private void pauseSong() {
  111. if(currentSong != null && currentSong.isRunning()) {
  112. currentSong.stop();
  113. }
  114. }
  115.  
  116. private void resumeSong() {
  117. if(currentSong != null && !currentSong.isRunning()) {
  118. currentSong.loop(Clip.LOOP_CONTINUOUSLY);
  119. }
  120. }
  121.  
  122. private final RenderingHints antialiasing = new RenderingHints(
  123. RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  124.  
  125. private BufferedImage getImage(String url) {
  126. try {
  127. return ImageIO.read(new URL(url));
  128. } catch(Exception e) {
  129. return null;
  130. }
  131. }
  132.  
  133. private final Color color1 = new Color(72, 64, 52);
  134. private final Color color2 = new Color(0, 0, 0);
  135. private final Color color3 = new Color(43, 38, 35);
  136. private final Color color4 = new Color(255, 255, 255);
  137.  
  138. private final BasicStroke stroke1 = new BasicStroke(1);
  139. private final BasicStroke stroke2 = new BasicStroke(3);
  140.  
  141. private final Font font1 = new Font("Arial", 1, 14);
  142.  
  143. private final BufferedImage blueFF = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BlueFF.png");
  144. private final BufferedImage blueRewind = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BlueRewind.png");
  145. private final BufferedImage bluePlay = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BluePlay.png");
  146. private final BufferedImage bluePause = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BluePause.png");
  147. private final BufferedImage greenUp = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/GreenUp.png");
  148. private final BufferedImage greenDown = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/GreenDown.png");
  149.  
  150. Point listStart = new Point(550, 275);
  151.  
  152. public void onRepaint(Graphics g1) {
  153. if(Game.getCurrentTab().equals(Game.Tabs.MUSIC)){
  154. Graphics2D g = (Graphics2D)g1;
  155. g.setRenderingHints(antialiasing);
  156.  
  157. g.setColor(color1);
  158. g.fillRect(547, 204, 189, 261);
  159. g.setColor(color2);
  160. g.setStroke(stroke1);
  161. g.drawRect(547, 204, 189, 261);
  162. g.drawLine(575, 252, 709, 252);
  163. g.setColor(color3);
  164. g.fillRect(549, 274, 186, 189);
  165. songList.draw(g);
  166. g.drawImage(greenUp, 704, 280, null);
  167. g.drawImage(greenDown, 704, 415, null);
  168. g.setColor(color2);
  169. g.setStroke(stroke2);
  170. g.drawRect(549, 274, 186, 189);
  171. g.drawImage(blueFF, 663, 222, null);
  172. g.drawImage(blueRewind, 593, 222, null);
  173. g.drawImage(currentSong.isRunning() ? bluePause : bluePlay, 623, 209, null);
  174. g.setFont(font1);
  175. g.setColor(color4);
  176. String shortened = getShortenedString(g, currentName, 185);
  177. g.drawString(shortened ,549 + (186 /2 - g.getFontMetrics().stringWidth(shortened) /2), 267);
  178. }
  179. }
  180.  
  181. private String getShortenedString(Graphics g, String string, int pixels) {
  182. if(string != null) {
  183. int count = g.getFontMetrics().stringWidth(string);
  184. int shorten = 1;
  185. String toReturn = string;
  186. while(count >= pixels) {
  187. count = g.getFontMetrics().stringWidth(string.substring(0, string.length() -1 - shorten));
  188. if( count <= pixels) {
  189. toReturn = string.substring(0, string.length() -1 -shorten);
  190. break;
  191. } else {
  192. shorten++;
  193. }
  194. }
  195. if(!toReturn.equals(string)) {
  196. toReturn = toReturn.substring(0, toReturn.length() -4).concat("...");
  197. }
  198. return toReturn;
  199. }
  200. return "";
  201. }
  202.  
  203. private class SongList {
  204. File[] files;
  205. int x;
  206. int y;
  207. int width;
  208. int maxHeight;
  209. int elementHeight;
  210. int rows;
  211.  
  212. int index = 0;
  213.  
  214. SongElement[] elements;
  215.  
  216. public SongList(int x, int y, int width, int height, int rows, File... files) {
  217. this.files = files;
  218. this.x = x;
  219. this.y = y;
  220. this.width = width;
  221. this.maxHeight = height;
  222. this.elementHeight = height / rows;
  223. this.rows = rows;
  224. ArrayList<SongElement> elementsList = new ArrayList<SongElement>();
  225. for(File file : files) {
  226. elementsList.add(new SongElement(file));
  227. }
  228.  
  229. this.elements = elementsList.toArray(new SongElement[elementsList.size()]);
  230. }
  231.  
  232. public void draw(Graphics g) {
  233. int tempY = y;
  234. for(int iii = 0; iii < rows; iii++) {
  235. tempY += elementHeight;
  236. elements[index + iii].draw(g, x, tempY, width, elementHeight);
  237. }
  238. }
  239.  
  240. public void scrollUp() {
  241. if(index < 0) {
  242. index--;
  243. }
  244. }
  245.  
  246. public void scrollDown() {
  247. log(index + " + " + "1 < " + elements.length + " = " + (index + rows + 1 < elements.length));
  248. if(index + rows + 1 < elements.length && elements.length > rows) {
  249. index++;
  250. }
  251. }
  252.  
  253. public boolean contains(Point p) {
  254. return new Rectangle(x, y, width, maxHeight).contains(p);
  255. }
  256.  
  257. private class SongElement extends Component implements MouseListener{
  258. File file;
  259. String name;
  260. Rectangle bounds;
  261. Color color;
  262.  
  263. public SongElement(File file) {
  264. this.file = file;
  265. name = file.getName().replace(".wav", "");
  266. name = name.replace(".au", "");
  267. name = name.replace(".aiff", "");
  268. bounds = null;
  269. color = Color.WHITE;
  270. }
  271.  
  272. public void draw(Graphics g, int x, int y, int width, int height) {
  273. bounds = new Rectangle(x, y - height, width, height);
  274. if(isPlaying()) {
  275. color = Color.YELLOW;
  276. } else {
  277. color = Color.WHITE;
  278. }
  279.  
  280. setBounds(bounds);
  281. g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 100));
  282. g.fillRect(x, y - height, width, height);
  283. g.setColor(color);
  284. g.drawRect(x, y - height, width, height);
  285. if(g.getFontMetrics().getMaxAscent() > height - 2) {
  286. while(g.getFontMetrics().getMaxAscent() > height - 2) {
  287. g.setFont(g.getFont().deriveFont(g.getFont().getSize() - 1));
  288. }
  289. }
  290. String toWrite = getShortenedString(g, name, width - 2);
  291. g.drawString(toWrite, x + 1, y - 1);
  292. }
  293.  
  294. @Override
  295. public boolean contains(Point p) {
  296. return bounds != null && bounds.contains(p);
  297. }
  298.  
  299. public boolean isPlaying() {
  300. return currentName.equals(name);
  301. }
  302.  
  303. public void mouseClicked(MouseEvent e) {
  304. if(!currentName.equals(name)) {
  305. try{
  306. for(int iii = 0; iii < files.length; iii++) {
  307. if(files[iii].equals(file)) {
  308. musicJacker.this.index = iii;
  309. playSong(file);
  310. }
  311. }
  312. } catch (Exception exc) {
  313. exc.printStackTrace();
  314. }
  315. }
  316. }
  317.  
  318. public void mousePressed(MouseEvent e) {}
  319.  
  320. public void mouseReleased(MouseEvent e) {}
  321.  
  322. public void mouseEntered(MouseEvent e) {}
  323.  
  324. public void mouseExited(MouseEvent e) {}
  325. }
  326. }
  327.  
  328. public void mouseClicked(MouseEvent e) {
  329. Polygon backPolygon = new Polygon(new int[]{593,595,600,607,616,620,620,617,615,610,600},
  330. new int[]{238,228,225,223,228,237,241,243,247,249,247}, 11);
  331.  
  332. Polygon playPolygon = new Polygon(new int[]{641,627,625,627,638,652,660,655},
  333. new int[]{246,237,226,216,211,212,227,239}, 8);
  334.  
  335. Polygon forwardPolygon = new Polygon(new int[]{689,690,680,669,664,672,684},
  336. new int[]{241,230,223,223,237,248,249}, 7);
  337. Rectangle upRect = new Rectangle(703, 276, 29, 37);
  338.  
  339. Rectangle downRect = new Rectangle(703, 416, 29, 36);
  340.  
  341. try {
  342. if(backPolygon.contains(e.getPoint())) {
  343. playPriorSong();
  344. } else if(playPolygon.contains(e.getPoint())) {
  345. if(currentSong.isRunning()) {
  346. pauseSong();
  347. } else {
  348. resumeSong();
  349. }
  350. } else if(forwardPolygon.contains(e.getPoint())) {
  351. playNextSong();
  352. } else if(upRect.contains(e.getPoint())) {
  353. songList.scrollUp();
  354. } else if(downRect.contains(e.getPoint())) {
  355. songList.scrollDown();
  356. } else if(songList.contains(e.getPoint())) {
  357. for(SongList.SongElement se : songList.elements) {
  358. if(se.contains(e.getPoint())) {
  359. se.mouseClicked(e);
  360. }
  361. }
  362. }
  363. } catch (Exception f) {
  364. f.printStackTrace();
  365. }
  366. }
  367.  
  368. public void mousePressed(MouseEvent e) {}
  369.  
  370. public void mouseReleased(MouseEvent e) {}
  371.  
  372. public void mouseEntered(MouseEvent e) {}
  373.  
  374. public void mouseExited(MouseEvent e) {}
  375.  
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement