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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import org.cybuch.jbot.gui.JBot;
import org.cybuch.jbot.utils.Global;
import org.cybuch.jbot.utils.Keyboard;
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;
public class FisherController {
private Properties prop;
private int fishX0;
private int fishDelay;
private int fishY0;
@FXML
private TextField delay;
@FXML
private TextField times;
private int fishX1;
private int fishY1;
private int timez;
private int rodX;
private int rodY;
@FXML
private void initialize() throws FileNotFoundException, IOException {
prop = new Properties();
if (!new File("config.properties").exists()) {
return;
}
prop.load(new FileInputStream("config.properties"));
if (prop.getProperty("fishX0") == null) {
return;
}
fishX1 = Integer.parseInt(prop.getProperty("fishX1"));
fishY1 = Integer.parseInt(prop.getProperty("fishY1"));
fishX0 = Integer.parseInt(prop.getProperty("fishX0"));
fishY0 = Integer.parseInt(prop.getProperty("fishY0"));
fishDelay = Integer.parseInt(prop.getProperty("fishDelay"));
rodX = Integer.parseInt(prop.getProperty("rodX"));
rodY = Integer.parseInt(prop.getProperty("rodY"));
timez = Integer.parseInt(prop.getProperty("times"));
delay.setText(prop.getProperty("fishDelay"));
times.setText(prop.getProperty("times"));
}
public void selectRod() 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) {
rodX = (int) me.getScreenX();
rodY = (int) me.getScreenY();
stage.close();
}
});
}
public void selectWater() throws IOException {
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) {
fishX0 = (int) me.getScreenX();
fishY0 = (int) me.getScreenY();
}
});
scene.setOnMouseReleased(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
fishX1 = (int) me.getScreenX();
fishY1 = (int) me.getScreenY();
fishDelay = Integer.parseInt(delay.getText());
timez = Integer.parseInt(times.getText());
prop.setProperty("fishX0", Integer.toString(fishX0));
prop.setProperty("fishY0", Integer.toString(fishY0));
prop.setProperty("rodX", Integer.toString(rodX));
prop.setProperty("rodY", Integer.toString(rodY));
if (delay.getText() != null || delay.getText().equalsIgnoreCase("")); {
prop.setProperty("fishDelay", delay.getText());
}
if (times.getText() != null || times.getText().equalsIgnoreCase("")) {
prop.setProperty("times", times.getText());
}
prop.setProperty("fishX1", Integer.toString(fishX1));
prop.setProperty("fishY1", Integer.toString(fishY1));
try {
prop.store(new FileOutputStream("config.properties"), null);
stage.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void fish() throws AWTException {
t.start();
}
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
for (int j = 0; j < Integer.MAX_VALUE; j++) {
for (int i = 0; i < timez; i++) {
if (Global.LOCKED) {
i--;
Thread.sleep(5000);
}
else {
Global.LOCKED = true;
Robot robot = new Robot();
robot.setAutoDelay(500);
robot.mouseMove(rodX, rodY);
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
robot.mouseMove(randInt(fishX0, fishX1), randInt(fishY0, fishY1));
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Global.LOCKED = false;
}
}
Thread.sleep(fishDelay * 1000);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}