package org.cybuch.jbot.controler;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.cybuch.jbot.gui.JBot;
import org.cybuch.jbot.utils.Global;
import org.cybuch.jbot.utils.Keyboard;
public class RuneController {
@FXML
private TextField spell;
@FXML
private TextField delay;
@FXML
private TextField bps;
int leftHandX;
int leftHandY;
int blanksX;
int blanksY;
int bpss;
int runeDelay;
ArrayList<Integer> blankX;
private int iteration = 0;
ArrayList<Integer> blankY;
Properties prop;
private String spellText;
private String delayText;
private int j;
@FXML
private void initialize() {
try {
prop = new Properties();
blankX = new ArrayList<Integer>();
blankY = new ArrayList<Integer>();
if (!new File("config.properties").exists()) {
return;
}
prop.load(new FileInputStream("config.properties"));
if (prop.getProperty("blanksX0") == null) {
return;
}
leftHandX = Integer.parseInt(prop.getProperty("leftHandX"));
leftHandY = Integer.parseInt(prop.getProperty("leftHandY"));
runeDelay = Integer.parseInt(prop.getProperty("runeDelay"));
delay.setText(Integer.toString(runeDelay));
bpss = Integer.parseInt(prop.getProperty("bps"));
bps.setText(prop.getProperty("bps"));
spell.setText(prop.getProperty("spell"));
for (int i = 0; i < bpss; i++) {
blankX.add(i, Integer.parseInt(prop.getProperty("blanksX" + Integer.toString(i))));
blankY.add(i, Integer.parseInt(prop.getProperty("blanksY" + Integer.toString(i))));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void selectLeftHand() throws IOException, AWTException {
FXMLLoader loader = new FXMLLoader(
JBot.class.getResource("Select.fxml"));
AnchorPane selectDialog = (AnchorPane) loader.load();
final Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(selectDialog);
stage.setScene(scene);
stage.setOpacity(0.1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
stage.setWidth(screenSize.getWidth());
stage.setHeight(screenSize.getHeight());
stage.show();
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
leftHandX = (int) me.getScreenX();
leftHandY = (int) me.getScreenY();
prop.setProperty("leftHandX", Integer.toString(leftHandX));
prop.setProperty("leftHandY", Integer.toString(leftHandY));
try {
prop.store(new FileOutputStream("config.properties"), null);
stage.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void selectBlanks() throws IOException, AWTException {
FXMLLoader loader = new FXMLLoader(
JBot.class.getResource("Select.fxml"));
AnchorPane selectDialog = (AnchorPane) loader.load();
final Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(selectDialog);
stage.setScene(scene);
stage.setOpacity(0.1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
stage.setWidth(screenSize.getWidth());
stage.setHeight(screenSize.getHeight());
stage.show();
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
try {
blankX.add(iteration, (int) me.getScreenX());
blankY.add(iteration, (int) me.getScreenY());
prop.setProperty("blanksX" + Integer.toString(iteration), Integer.toString(blankX.get(iteration)));
prop.setProperty("blanksY" + Integer.toString(iteration), Integer.toString(blankY.get(iteration)));
prop.setProperty("bps", bps.getText());
prop.setProperty("spell", spell.getText());
prop.setProperty("runeDelay", delay.getText());
bpss = Integer.parseInt(bps.getText());
prop.store(new FileOutputStream("config.properties"), null);
iteration++;
if (iteration > Integer.parseInt(bps.getText()) - 1) {
iteration = 0;
stage.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void makeRunes() throws AWTException {
spellText = spell.getText();
delayText = delay.getText();
t.start();
}
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
for (j = 0; j < bpss; j++) {
for (int i = 0; i < 20; i++) {
if (Global.LOCKED) {
i--;
Thread.sleep(5000);
}
else {
Global.LOCKED = true;
Robot robot = new Robot();
robot.setAutoDelay(500);
robot.mouseMove(blankX.get(j), blankY.get(j));
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(leftHandX, leftHandY);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Keyboard keyboard = new Keyboard();
keyboard.type(spellText);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.mouseMove(leftHandX, leftHandY);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(blankX.get(j), blankY.get(j));
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Global.LOCKED = false;
Thread.sleep(Integer.parseInt(delayText) * 1000);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}