Package org.cybuch.jbot.controler

Source Code of org.cybuch.jbot.controler.EaterController

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 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 EaterController {

  private Properties prop;
  private int foodX;
  private int foodY;
  private int foodDelay;
  @FXML
  private TextField delay;

  @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("foodX") == null) {
      return;
    }
    foodX = Integer.parseInt(prop.getProperty("foodX"));
    foodY = Integer.parseInt(prop.getProperty("foodY"));
    foodDelay = Integer.parseInt(prop.getProperty("foodDelay"));
    delay.setText(prop.getProperty("foodDelay"));
  }
 
  public void select() 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) {
        foodX = (int) me.getScreenX();
        foodY = (int) me.getScreenY();
        foodDelay = Integer.parseInt(delay.getText());
        prop.setProperty("foodX", Integer.toString(foodX));
        prop.setProperty("foodY", Integer.toString(foodY));
        if (delay.getText() != null || delay.getText().equalsIgnoreCase("")); {
        prop.setProperty("foodDelay", delay.getText());
        }
        try {
          prop.store(new FileOutputStream("config.properties"), null);
          stage.close();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    })
  }
 
  public void eat() throws AWTException {
    t.start();
  }

  Thread t = new Thread(new Runnable() {
    public void run() {
      try {
        Thread.sleep(2000);
          for (int i = 0; i < Integer.MAX_VALUE; i++) {
            if (Global.LOCKED) {
              i--;
              Thread.sleep(5000);
            }
            else {
            Global.LOCKED = true;
            Robot robot = new Robot();
            robot.setAutoDelay(500);
            robot.mouseMove(foodX, foodY);
            robot.mousePress(InputEvent.BUTTON3_MASK);
            robot.mouseRelease(InputEvent.BUTTON3_MASK);
            Global.LOCKED = false;
            Thread.sleep(foodDelay * 1000);
            }
          }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  });
 
}
TOP

Related Classes of org.cybuch.jbot.controler.EaterController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.