Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Last active October 6, 2015 03:27
Show Gist options
  • Save TheBatScripts/2927716 to your computer and use it in GitHub Desktop.
Save TheBatScripts/2927716 to your computer and use it in GitHub Desktop.
BatAlerter
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.runedream.api.Script;
import org.runedream.api.ScriptManifest;
import org.runedream.api.methods.OCR;
import org.runedream.api.util.Log;
import org.runedream.api.util.Time;
@ScriptManifest(
authors = { "TheBat"},
name = "BatAlert",
version = 1.0,
description = "Makes an Alert when a given string is detected.",
keywords = {},
language = { true, true, true, true })
public class BatAlert extends Script{
private GUI gui;
private ALERT pop;
private String [] tests;
AudioClip sound;
String current = "~@#",test = "";
private boolean useSound = false;
public boolean onStart(){
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
gui = new GUI();
gui.setVisible(true);
}
});
} catch (InterruptedException ex) {
} catch (InvocationTargetException ex) {
}
while(gui.OPEN){
Time.sleep(100);
}
try{
sound = Applet.newAudioClip(new URL("http://dl.dropbox.com/u/8033155/you_were_poked.wav")); // Load the Sound
}catch(Exception e2){
Log.log("The audio file wasn't loaded correctly!");
} // Satisfy the catch
return true;
}
@Override
public int loop() {
test = OCR.findString(new Rectangle(4,435,510,33), null, true);
if(!current.contains(test)){
current = test;
for(int i = 0; i < tests.length; i++){
String cur2 = current.toLowerCase();
if(cur2.contains(tests[i])){
alarm();
break;
}
}
}
return 100;
}
public void onStop(){
sound.stop();
}
private void alarm(){
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
pop = new ALERT();
pop.setVisible(true);
}
});
} catch (InterruptedException ex) {
} catch (InvocationTargetException ex) {
}
while(pop.OPEN){
if(useSound){
sound.loop();
Time.sleep(1500);
}
Time.sleep(100);
}
sound.stop();
}
public void onRepaint(Graphics g){
g.setColor(Color.RED);
g.drawString("Alerts are activated!", 4, 336);
}
@SuppressWarnings("serial")
class GUI extends JFrame implements ActionListener{
GridBagConstraints c;
Container pane;
public boolean OPEN = true;
private JButton startB = new JButton("Start");
private JLabel stringL = new JLabel("Strings (Seperate by Commas):", JLabel.LEFT);
private JCheckBox soundCKB = new JCheckBox("Use sound notification");
private JTextField stringT = new JTextField();
public GUI(){
super("BatAlert");
this.setLocationRelativeTo(getOwner());
pane = new Container();
pane.setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = c.ipadx = 3;
c.insets = new Insets(5, 10, 5, 5);
init();
build();
}
private void build(){
addToGrid(stringL,0,0,1,.5);
addToGrid(stringT,0,1,2,.5);
addToGrid(soundCKB,0,2,1,1);
addToGrid(startB,0,3,2,1);
getContentPane().add(pane);
pack();
setVisible(true);
}
private void init(){
startB.addActionListener(this);
}
private void addToGrid(Component comp, int gridx, int gridy, int gridwidth,
double weightx) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.weightx = weightx;
pane.add(comp, c);
}
public void actionPerformed(ActionEvent e) {
tests = stringT.getText().split(",");
for(int i = 0; i < tests.length; i++){
tests[i] = tests[i].toLowerCase();
}
useSound = soundCKB.isSelected();
OPEN = false;
dispose();
}
}
@SuppressWarnings("serial")
class ALERT extends JFrame implements ActionListener{
GridBagConstraints c;
JPanel pane;
public boolean OPEN = true;
private JButton dissmissB = new JButton("Dismiss");
private JLabel aL = new JLabel("Phrase detected!", JLabel.CENTER);
public ALERT(){
super("ALERT!");
pane = new JPanel();
pane.setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = c.ipadx = 3;
c.insets = new Insets(5, 10, 5, 5);
init();
build();
pane.setBackground(Color.RED.brighter());
this.setSize(500,500);
this.setLocationRelativeTo(getOwner());
}
private void build(){
addToGrid(aL,0,0,1,.5);
addToGrid(dissmissB,0,1,1,1);
getContentPane().add(pane);
pack();
setVisible(true);
}
private void init(){
dissmissB.addActionListener(this);
}
private void addToGrid(Component comp, int gridx, int gridy, int gridwidth,
double weightx) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.weightx = weightx;
pane.add(comp, c);
}
public void actionPerformed(ActionEvent e) {
OPEN = false;
dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment