Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.rsbot.Configuration;
- import org.rsbot.script.Script;
- import org.rsbot.script.ScriptManifest;
- import org.rsbot.script.internal.event.PaintListener;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.util.ArrayList;
- import org.rsbot.script.methods.Game;
- import org.rsbot.script.methods.ui.Interfaces;
- import org.rsbot.service.IRC;
- import javax.imageio.ImageIO;
- import javax.sound.sampled.*;
- @ScriptManifest(name="Music Jacker", authors = {"Zalgo"})
- public class musicJacker extends Script implements PaintListener, MouseListener {
- File[] files;
- Clip currentSong;
- String currentName;
- String lastRSSong;
- int index = -1;
- SongList songList;
- @Override
- protected int loop() {
- if((Game.getCurrentTab().equals(Game.Tabs.MUSIC) && !Interfaces.getComponent(187, 4).getText().equals(lastRSSong))
- || index == -1) {
- lastRSSong = Interfaces.getComponent(187, 4).getText();
- try{
- playNextSong();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return 1000;
- }
- @Override
- protected void onStop() {
- if(currentSong != null) {
- currentSong.stop();
- currentSong.close();
- }
- }
- @Override
- protected boolean onRun() {
- File directory = new File(Configuration.Paths.getStorageDirectory() + File.separator + "Music Jacker");
- ArrayList<File> fileArrayList = new ArrayList<File>();
- if(!directory.exists()) {
- if(!directory.mkdir()) {
- return false;
- }
- }
- for(File file : directory.listFiles()) {
- if(file.getName().endsWith(".au") || file.getName().endsWith(".wav") || file.getName().endsWith(".aiff")) {
- fileArrayList.add(file);
- }
- }
- files = fileArrayList.toArray(new File[fileArrayList.size()]);
- Game.openTab(Game.Tabs.MUSIC);
- lastRSSong = Interfaces.getComponent(187, 4).getText();
- songList = new SongList(listStart.x, listStart.y, 150, 189, 10, files);
- return true;
- }
- private void playSong(File file) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- if(currentSong != null && currentSong.isOpen()) {
- currentSong.stop();
- currentSong.close();
- }
- currentName = file.getName().replace(".wav", "");
- currentName = currentName.replace(".au", "");
- currentName = currentName.replace(".aiff", "");
- currentSong = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, AudioSystem.getAudioFileFormat(file).getFormat()));
- currentSong.open(AudioSystem.getAudioInputStream(file));
- currentSong.loop(Clip.LOOP_CONTINUOUSLY);
- if(IRC.isConnected())
- IRC.dispatch.add("Now Playing: " + currentName);
- }
- private void playNextSong() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- index++;
- if(index >= files.length - 1) {
- index = 0;
- }
- playSong(files[index]);
- }
- private void playPriorSong() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
- if(index != 0) {
- index--;
- playSong(files[index]);
- }
- }
- private void pauseSong() {
- if(currentSong != null && currentSong.isRunning()) {
- currentSong.stop();
- }
- }
- private void resumeSong() {
- if(currentSong != null && !currentSong.isRunning()) {
- currentSong.loop(Clip.LOOP_CONTINUOUSLY);
- }
- }
- private final RenderingHints antialiasing = new RenderingHints(
- RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- private BufferedImage getImage(String url) {
- try {
- return ImageIO.read(new URL(url));
- } catch(Exception e) {
- return null;
- }
- }
- private final Color color1 = new Color(72, 64, 52);
- private final Color color2 = new Color(0, 0, 0);
- private final Color color3 = new Color(43, 38, 35);
- private final Color color4 = new Color(255, 255, 255);
- private final BasicStroke stroke1 = new BasicStroke(1);
- private final BasicStroke stroke2 = new BasicStroke(3);
- private final Font font1 = new Font("Arial", 1, 14);
- private final BufferedImage blueFF = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BlueFF.png");
- private final BufferedImage blueRewind = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BlueRewind.png");
- private final BufferedImage bluePlay = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BluePlay.png");
- private final BufferedImage bluePause = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/BluePause.png");
- private final BufferedImage greenUp = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/GreenUp.png");
- private final BufferedImage greenDown = getImage("http://scripters.powerbot.org/files/349221/Paint/MusicJacker/GreenDown.png");
- Point listStart = new Point(550, 275);
- public void onRepaint(Graphics g1) {
- if(Game.getCurrentTab().equals(Game.Tabs.MUSIC)){
- Graphics2D g = (Graphics2D)g1;
- g.setRenderingHints(antialiasing);
- g.setColor(color1);
- g.fillRect(547, 204, 189, 261);
- g.setColor(color2);
- g.setStroke(stroke1);
- g.drawRect(547, 204, 189, 261);
- g.drawLine(575, 252, 709, 252);
- g.setColor(color3);
- g.fillRect(549, 274, 186, 189);
- songList.draw(g);
- g.drawImage(greenUp, 704, 280, null);
- g.drawImage(greenDown, 704, 415, null);
- g.setColor(color2);
- g.setStroke(stroke2);
- g.drawRect(549, 274, 186, 189);
- g.drawImage(blueFF, 663, 222, null);
- g.drawImage(blueRewind, 593, 222, null);
- g.drawImage(currentSong.isRunning() ? bluePause : bluePlay, 623, 209, null);
- g.setFont(font1);
- g.setColor(color4);
- String shortened = getShortenedString(g, currentName, 185);
- g.drawString(shortened ,549 + (186 /2 - g.getFontMetrics().stringWidth(shortened) /2), 267);
- }
- }
- private String getShortenedString(Graphics g, String string, int pixels) {
- if(string != null) {
- int count = g.getFontMetrics().stringWidth(string);
- int shorten = 1;
- String toReturn = string;
- while(count >= pixels) {
- count = g.getFontMetrics().stringWidth(string.substring(0, string.length() -1 - shorten));
- if( count <= pixels) {
- toReturn = string.substring(0, string.length() -1 -shorten);
- break;
- } else {
- shorten++;
- }
- }
- if(!toReturn.equals(string)) {
- toReturn = toReturn.substring(0, toReturn.length() -4).concat("...");
- }
- return toReturn;
- }
- return "";
- }
- private class SongList {
- File[] files;
- int x;
- int y;
- int width;
- int maxHeight;
- int elementHeight;
- int rows;
- int index = 0;
- SongElement[] elements;
- public SongList(int x, int y, int width, int height, int rows, File... files) {
- this.files = files;
- this.x = x;
- this.y = y;
- this.width = width;
- this.maxHeight = height;
- this.elementHeight = height / rows;
- this.rows = rows;
- ArrayList<SongElement> elementsList = new ArrayList<SongElement>();
- for(File file : files) {
- elementsList.add(new SongElement(file));
- }
- this.elements = elementsList.toArray(new SongElement[elementsList.size()]);
- }
- public void draw(Graphics g) {
- int tempY = y;
- for(int iii = 0; iii < rows; iii++) {
- tempY += elementHeight;
- elements[index + iii].draw(g, x, tempY, width, elementHeight);
- }
- }
- public void scrollUp() {
- if(index < 0) {
- index--;
- }
- }
- public void scrollDown() {
- log(index + " + " + "1 < " + elements.length + " = " + (index + rows + 1 < elements.length));
- if(index + rows + 1 < elements.length && elements.length > rows) {
- index++;
- }
- }
- public boolean contains(Point p) {
- return new Rectangle(x, y, width, maxHeight).contains(p);
- }
- private class SongElement extends Component implements MouseListener{
- File file;
- String name;
- Rectangle bounds;
- Color color;
- public SongElement(File file) {
- this.file = file;
- name = file.getName().replace(".wav", "");
- name = name.replace(".au", "");
- name = name.replace(".aiff", "");
- bounds = null;
- color = Color.WHITE;
- }
- public void draw(Graphics g, int x, int y, int width, int height) {
- bounds = new Rectangle(x, y - height, width, height);
- if(isPlaying()) {
- color = Color.YELLOW;
- } else {
- color = Color.WHITE;
- }
- setBounds(bounds);
- g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 100));
- g.fillRect(x, y - height, width, height);
- g.setColor(color);
- g.drawRect(x, y - height, width, height);
- if(g.getFontMetrics().getMaxAscent() > height - 2) {
- while(g.getFontMetrics().getMaxAscent() > height - 2) {
- g.setFont(g.getFont().deriveFont(g.getFont().getSize() - 1));
- }
- }
- String toWrite = getShortenedString(g, name, width - 2);
- g.drawString(toWrite, x + 1, y - 1);
- }
- @Override
- public boolean contains(Point p) {
- return bounds != null && bounds.contains(p);
- }
- public boolean isPlaying() {
- return currentName.equals(name);
- }
- public void mouseClicked(MouseEvent e) {
- if(!currentName.equals(name)) {
- try{
- for(int iii = 0; iii < files.length; iii++) {
- if(files[iii].equals(file)) {
- musicJacker.this.index = iii;
- playSong(file);
- }
- }
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- }
- public void mousePressed(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- }
- }
- public void mouseClicked(MouseEvent e) {
- Polygon backPolygon = new Polygon(new int[]{593,595,600,607,616,620,620,617,615,610,600},
- new int[]{238,228,225,223,228,237,241,243,247,249,247}, 11);
- Polygon playPolygon = new Polygon(new int[]{641,627,625,627,638,652,660,655},
- new int[]{246,237,226,216,211,212,227,239}, 8);
- Polygon forwardPolygon = new Polygon(new int[]{689,690,680,669,664,672,684},
- new int[]{241,230,223,223,237,248,249}, 7);
- Rectangle upRect = new Rectangle(703, 276, 29, 37);
- Rectangle downRect = new Rectangle(703, 416, 29, 36);
- try {
- if(backPolygon.contains(e.getPoint())) {
- playPriorSong();
- } else if(playPolygon.contains(e.getPoint())) {
- if(currentSong.isRunning()) {
- pauseSong();
- } else {
- resumeSong();
- }
- } else if(forwardPolygon.contains(e.getPoint())) {
- playNextSong();
- } else if(upRect.contains(e.getPoint())) {
- songList.scrollUp();
- } else if(downRect.contains(e.getPoint())) {
- songList.scrollDown();
- } else if(songList.contains(e.getPoint())) {
- for(SongList.SongElement se : songList.elements) {
- if(se.contains(e.getPoint())) {
- se.mouseClicked(e);
- }
- }
- }
- } catch (Exception f) {
- f.printStackTrace();
- }
- }
- public void mousePressed(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement