Java Swing在Runelite插件中模拟鼠标。

huangapple go评论90阅读模式
英文:

Java swing fake mouse in Runelite for plugin

问题

package net.runelite.client.plugins.clicktest;

import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;

import javax.inject.Inject;
import java.awt.event.MouseEvent;

@Slf4j
@PluginDescriptor(
    name = "ClickTestPlugin",
    description = "Plugin for testing MouseEvents in Runelite"
)
public class clicktest extends Plugin {

    @Inject
    public Client client;

    @Inject
    public MouseEvent mouseEvent;

    public clicktest() {
        mouseEvent = new MouseEvent(this, MouseEvent.MOUSE_CLICKED,
            System.currentTimeMillis(), 0, 10, 10, 1, false);
    }
}
英文:

Heyy, i've been searching the internet for making a plugin for Runelite a client for Oldschool Runescape. I want to make a plugin where i can fight some monsters, basically its a bot. I know there are other clients out there that make it easier to write bot scripts.

My question is how do i make a "Fake" mouse that fires MouseEvent to a specific location on the screen and implement this inside a plugin. I've seen that i can create a new MouseEvent and pass the X and Y value's but i also need to pass a source for that. The source needs to be a component. I Tried using this snippet but "this" doesn't work in a plugin because its not a Component i guess.

I know i could use the class Robot from java.awt to create mouse movement etc but that hijacks my mouse on the pc. Also ofc this is for educational purposes. I just would really like to know how to create a bot, was always facinated by the thought of creating one.

ps: i found this video. i want to create something similar, this is the "fake" mouse i mean. the cross on the window.

this is the code i have at the moment:

package net.runelite.client.plugins.clicktest;

import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;

import javax.inject.Inject;
import java.awt.event.MouseEvent;


@Slf4j
@PluginDescriptor(
        name = "ClickTestPlugin",
        description = "Plugin for testing MouseEvents in Runelite"
)
public class clicktest extends Plugin {

    @Inject
    public Client client;


    @Inject
    public MouseEvent mouseEvent;

    public clicktest() {

        mouseEvent = new MouseEvent(this, MouseEvent.MOUSE_CLICKED,
                System.currentTimeMillis(), 0, 10, 10, 1, false);
    }
}

答案1

得分: 3

鉴于没有其他人回复,虽然我也不确定是否有一个库或方法可以在不劫持鼠标的情况下实现,但我将尝试指导您正确的方向。除了机器人之外,还有其他方法,比如计划执行器,但它仍然会劫持您的鼠标。有很多免费的存储库使用runelite的API来创建机器人并与游戏交互,这样您就不必担心在特定坐标处点击。使用这些作为参考将帮助您达到目标!

有些方法不同,我见过一些不会劫持您的鼠标。但是,这些方法都不是开源的(至少根据我所找到的)。我将在这里粘贴一些示例。正如我在开头所说,我对如何创建一个可以在不依赖于鼠标的情况下覆盖鼠标移动并进行点击的机器人不是特别自信,但是这里有一些其他人机器人的示例。

下面显示的第一个插件使用了这种方法:

executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(this::simLeftClick, delay, TimeUnit.MILLISECONDS);

AutoPrayFlick - https://github.com/Ganom/ExternalPlugins/blob/master/AutoPrayFlick/src/main/java/net/runelite/client/plugins/externals/autoprayflick/AutoPrayFlickPlugin.java

ItemDropper -
https://github.com/Failed4life/ExternalPlugins/blob/master/ItemDropper/src/main/java/net/runelite/client/plugins/externals/itemdropper/ItemDropper.java

OneClick - 这个插件只使用API,不需要任何鼠标模拟!
https://github.com/Ganom/ExternalPlugins/blob/master/OneClick/src/main/java/net/runelite/client/plugins/externals/oneclick/OneClickPlugin.java

如果使用机器人或类似方法可能需要考虑检测,使用类似这样的东西会使您的移动更加人性化。

https://github.com/JoonasVali/NaturalMouseMotion

附注:我建议在GitHub上查看所有开源存储库,以了解人们是如何做事情的,这是我熟悉API及其用法的方法!

英文:

Seeing as no one else has gotten back, I'll try to point you in the right direction even though I'm also not sure of a library or way to do it without hijacking the mouse. There are other methods than robot such as a scheduled executor, but it will still hijack your mouse. There are a lot of free repos that use runelite's API to cerate bots and interact with the game, allowing you to not have to worry about clicking at specific coordinates. And using those as reference will help you get where you wanna go!

Some have different methods, and I've seen some that do not hijack your mouse. However, none of those are open source (at least from what I've found). I'll paste some examples here. Like I said at the beginning I'm not super confident in how to create a bot that can override your mouse movements and click independent of your mouse but here are some examples of other's bots.

The first plugin shown below uses this method for example:

executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(this::simLeftClick, delay, TimeUnit.MILLISECONDS);

AutoPrayFlick - https://github.com/Ganom/ExternalPlugins/blob/master/AutoPrayFlick/src/main/java/net/runelite/client/plugins/externals/autoprayflick/AutoPrayFlickPlugin.java

ItemDropper -
https://github.com/Failed4life/ExternalPlugins/blob/master/ItemDropper/src/main/java/net/runelite/client/plugins/externals/itemdropper/ItemDropper.java

OneClick - This one uses only the API and doesn't require any mouse simulation!
https://github.com/Ganom/ExternalPlugins/blob/master/OneClick/src/main/java/net/runelite/client/plugins/externals/oneclick/OneClickPlugin.java

It may be smart to consider detection if using robot or anything like that, using something like this will help humanize your movements

https://github.com/JoonasVali/NaturalMouseMotion

P.S I recommend looking around github at all the opensource repos to get a feel for how people are doing things, this is how I familiarized myself with the API and it' usage!

答案2

得分: 0

你的 mouseEvent 组件有问题。应将 'this' 替换为 client.getCanvas()。

  mouseEvent = new MouseEvent(client.getCanvas(), MouseEvent.MOUSE_CLICKED,
                 System.currentTimeMillis(), 0, 10, 10, 1, false);

这样就可以使虚拟鼠标点击正常工作。

英文:

Your mouseEvent component is wrong. 'this' should be replaced with client.getCanvas().

  mouseEvent = new MouseEvent(client.getCanvas(), MouseEvent.MOUSE_CLICKED,
                 System.currentTimeMillis(), 0, 10, 10, 1, false);

That's all to make virtual mouse click work.

huangapple
  • 本文由 发表于 2020年8月24日 19:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63560039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定