上传备份

wjdr
王兵 1 year ago
parent 8e426d8744
commit 4865efd9fb

@ -2,7 +2,6 @@
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -8,7 +8,7 @@
<packaging>jar</packaging>
<!--<packaging>war</packaging>--><!--需要打包成war时放开-->
<name>starter-jmacro-wjdr</name>
<description>project for Spring Boot</description>
<description>project for jmacro</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@ -1,13 +0,0 @@
package com.example.jmacro.wjdr;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
public abstract class JGameThread implements Runnable {
protected JMacro jMacro;
public JGameThread(JMacro jMacro) {
this.jMacro = jMacro;
}
}

@ -5,7 +5,6 @@ import com.example.jmacro.wjdr.base.ScreenPoint;
import com.example.jmacro.wjdr.base.ScreenRect;
import com.example.jmacro.wjdr.util.ColorUtil;
import com.example.jmacro.wjdr.util.Imager;
import com.example.jmacro.wjdr.util.Logger;
import com.example.jmacro.wjdr.util.TaskUtil;
import java.awt.*;
@ -15,47 +14,62 @@ import java.io.File;
import java.util.concurrent.TimeUnit;
/**
* Java
* Java
*/
public class JMacro {
public static void main(String[] args) throws AWTException {
JMacro jMacro = new JMacro();
jMacro.mouseMove(new ScreenPoint(100, 100));
}
/**
*
*
*/
private Robot robot;
private JGameThread thread;
private final Robot robot;
public JMacro() throws AWTException {
this.robot = new Robot();
}
public JGameThread getThread() {
return thread;
}
public void setThread(JGameThread thread) {
this.thread = thread;
/**
*
*
* @param point
*/
public void mouseMove(ScreenPoint point) {
mouseMove(point, false);
}
/**
*
*
* @param point
* @param point
* @param smooth
*/
public void mouseMove(ScreenPoint point) {
// 获取当前鼠标位置
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
int startX = mousePoint.x;
int startY = mousePoint.y;
// 分10次移动到指定点
for (int i = 1; i <= 10; i++) {
float d = i / 10f;
int dx = (int) (startX + (point.getX() - startX) * d);
int dy = (int) (startY + (point.getY() - startY) * d);
robot.mouseMove(dx, dy);
robot.delay(RandomUtil.randomInt(30, 70));
public void mouseMove(ScreenPoint point, boolean smooth) {
if (smooth) {
// 获取当前鼠标位置
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
int startX = mousePoint.x;
int startY = mousePoint.y;
// 求两点距离
double absX = Math.abs(startX - point.getX());
double absY = Math.abs(startY - point.getY());
double absZ = Math.sqrt(Math.pow(absX, 2) + Math.pow(absY, 2));
int times = (int) (absZ / 30 + (absZ % 30 > 0 ? 1 : 0));
// 分times次移动到指定点
for (int i = 1; i <= times; i++) {
float d = i * 1.0f / times;
int dx = (int) (startX + (point.getX() - startX) * d);
int dy = (int) (startY + (point.getY() - startY) * d);
robot.mouseMove(dx, dy);
robot.delay(RandomUtil.randomInt(5, 20));
}
} else {
robot.mouseMove(point.getX(), point.getY());
}
}
@ -126,7 +140,6 @@ public class JMacro {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
/**
*
*/
@ -136,8 +149,6 @@ public class JMacro {
/**
*
*
* @return
*/
public ScreenRect getScreenRect() {
Toolkit tk = Toolkit.getDefaultToolkit();
@ -149,7 +160,7 @@ public class JMacro {
*
* @param pic
* @param minSimilar
* @return
* @return
*/
public ScreenRect findPic(File pic, double minSimilar) {
return findPic(getScreenRect(), Imager.load(pic), minSimilar);
@ -160,7 +171,7 @@ public class JMacro {
*
* @param pic
* @param minSimilar
* @return
* @return
*/
public ScreenRect findPic(BufferedImage pic, double minSimilar) {
return findPic(getScreenRect(), pic, minSimilar);
@ -172,7 +183,7 @@ public class JMacro {
* @param pic
* @param screenRect
* @param minSimilar
* @return
* @return
*/
public ScreenRect findPic(ScreenRect screenRect, BufferedImage pic, double minSimilar) {
// 当查找区域比图片还小时,直接返回失败
@ -220,26 +231,41 @@ public class JMacro {
public void waitTap() {
int i = RandomUtil.randomInt(100, 200);
// Logger.info("随机等待{}ms", i);
robot.delay(i);
}
public void waitNormal() {
int i = RandomUtil.randomInt(500, 1500);
// Logger.info("随机等待{}ms", i);
robot.delay(i);
}
public void waitLong() {
int i = RandomUtil.randomInt(2000, 5000);
// Logger.info("随机等待{}ms", i);
robot.delay(i);
}
/**
*
*
* @param rect
* @param file
* @param minSimilar
* @return
*/
public ScreenRect waitAndFindPic(ScreenRect rect, File file, double minSimilar) {
return waitAndFindPic(rect, file, minSimilar, 10, TimeUnit.SECONDS);
}
/**
*
*
* @param rect
* @param file
* @param minSimilar
* @param time
* @param unit
* @return
*/
public ScreenRect waitAndFindPic(ScreenRect rect, File file, double minSimilar, long time, TimeUnit unit) {
return TaskUtil.timeTask(() -> {
while (true) {

@ -0,0 +1,13 @@
package com.example.jmacro.wjdr;
/**
* 线
*/
public abstract class JMacroThread implements Runnable {
protected JMacro jMacro;
public JMacroThread(JMacro jMacro) {
this.jMacro = jMacro;
}
}

@ -13,65 +13,86 @@ import com.example.jmacro.wjdr.util.TaskUtil;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class Main {
public class MainTask {
public static void main(String[] args) throws AWTException {
Logger.info("启动脚本");
/**
* 线
*/
private static ScheduledFuture<?> schedule;
/**
*
*/
private final String workStart = "08:00:00";
/**
*
*/
private final String workEnd = "24:00:00";
private boolean noDelay = true;
public void start() throws AWTException {
Logger.setDebug(true);
boolean first = true;
Logger.info("初始化脚本...");
Logger.info("初始化脚本");
JMacro jMacro = new JMacro();
String workStart = "08:00:00";
String workEnd = "24:00:00";
while (true) {
ThreadUtil.sleep(1000);
// 首次启动直接执行游戏线程
if (first) {
first = false;
jMacro.setThread(new GameThread(jMacro));
TaskUtil.schedule(jMacro.getThread(), 0, TimeUnit.SECONDS);
// 立即执行
if (noDelay) {
if (schedule != null) {
schedule.cancel(true);
}
noDelay = false;
Logger.info("启动线程");
schedule = TaskUtil.schedule(new MacroThread(jMacro), 0, TimeUnit.SECONDS);
continue;
}
if (jMacro.getThread() != null) {
// Logger.debug("待机中...");
if (schedule != null) {
continue;
}
if (DateUtil.isIn(DateUtil.date(), DateUtil.parse(workStart), DateUtil.parse(workEnd))) {
int anInt = RandomUtil.randomInt(5, 20);
Logger.info("等待{}分钟后,重新启动游戏线程", anInt);
jMacro.setThread(new GameThread(jMacro));
TaskUtil.schedule(jMacro.getThread(), anInt, TimeUnit.MINUTES);
int delay = RandomUtil.randomInt(5, 20);
Logger.info("等待{}分钟后,重新启动线程", delay);
schedule = TaskUtil.schedule(new MacroThread(jMacro), delay, TimeUnit.MINUTES);
}
}
}
public static class GameThread extends JGameThread {
/**
* 线
*/
public static class MacroThread extends JMacroThread {
/**
*
*/
private ScreenRect gameScreen;
public GameThread(JMacro jMacro) {
public MacroThread(JMacro jMacro) {
super(jMacro);
// 定位mumu bar
ScreenRect mumu = locationMuMu(jMacro);
if (mumu == null) {
throw new IllegalStateException("未检测到MuMu请开启MuMu模拟器");
}
Logger.info("游戏线程启动成功");
// 获取游戏窗口返回
Logger.info("线程启动成功");
// 获取窗口返回
gameScreen = new ScreenRect();
gameScreen.setLeft(mumu.getLeft() - 428);
gameScreen.setTop(mumu.getTop() - 8);
gameScreen.setRight(mumu.getRight());
gameScreen.setBottom(mumu.getBottom() + 951);
Logger.info("游戏窗口位置:" + gameScreen.toString());
Logger.info("游戏窗口大小:{}x{}", gameScreen.getRight()- gameScreen.getLeft(),gameScreen.getBottom()- gameScreen.getTop());
Logger.info("窗口位置:" + gameScreen.toString());
Logger.info("窗口大小:{}x{}", gameScreen.getRight() - gameScreen.getLeft(), gameScreen.getBottom() - gameScreen.getTop());
}
@Override
@ -84,9 +105,9 @@ public class Main {
Logger.info("启动程序");
jMacro.mouseLeftClick(launch);
} else {
Logger.info("启动图标失败,继续定位游戏主界面");
Logger.info("启动图标失败,继续定位主界面");
}
Logger.info("定位游戏主界面");
Logger.info("定位主界面");
ScreenRect = TaskUtil.timeTask(() -> {
while (true) {
ScreenRect screenRect1 = locationHome(jMacro, gameScreen);
@ -98,12 +119,13 @@ public class Main {
}, 30 * 1000, TimeUnit.MILLISECONDS);
if ( == null) {
jMacro.setThread(null);
Logger.info("未扫描到游戏主界:退出线程");
schedule.cancel(true);
schedule = null;
Logger.info("未扫描到主界面:退出线程");
return;
}
Logger.info("进入游戏主界面");
Logger.info("进入主界面");
Logger.info("进入任务线程");
TaskUtil.timeTask((Runnable) () -> {
@ -165,4 +187,11 @@ public class Main {
public static void taskMineAttack(JMacro jMacro, ScreenRect screenRect) {
}
/**
*
*/
public static void main(String[] args) throws AWTException {
new MainTask().start();
}
}

@ -34,11 +34,11 @@ public class TaskUtil extends CronUtil {
* @param runnable Runnable
* @param delay (ms)
*/
public static void schedule(Runnable runnable, long delay, TimeUnit unit) {
public static ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit unit) {
if (service == null) {
service = new ScheduledThreadPoolExecutor(1);
}
service.schedule(runnable, delay, unit);
return service.schedule(runnable, delay, unit);
}
/**

Loading…
Cancel
Save

Powered by TurnKey Linux.