0

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);
    }
}

Teun
  • 1
  • 1
  • 1
  • 1
    I'm guessing that you want to send an OS message that tells a window that a mouse click occurred, and if so, this would require code that makes OS calls, something that Java is very poor at doing (by design). – Hovercraft Full Of Eels Aug 24 '20 at 11:25
  • @HovercraftFullOfEels Thanks for responding so quickly! isn't this just what you can do with a new MouseEvent? because you pass an X and Y coordinate? – Teun Aug 24 '20 at 11:36
  • 1
    No, not if you're trying to control another program that is not a Swing program that you created – Hovercraft Full Of Eels Aug 24 '20 at 12:50

3 Answers3

3

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!

  • If making bots using Python it's worth checking out the pyclick library, as it creates human like mouse movements and adds some variation to the angles the mouse moves in, and is emulating human like curves when moving from one point to another. – Lana Del Slay Mar 22 '22 at 03:44
0

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.

0

Typically when you need to simulate an click you'd do something like use the Windows API - however RuneLite is a 3rd party client for RuneScape, which is a Java application. The game window is just a Canvas and RuneLite, which is open source software, exposes this canvas in their Client class getCanvas().

Automating the game is as simple as dispatching events to this canvas, for example:

 public void sendMoveEvent(int x, int y) {
        Point point = new Point(x, y);
        SwingUtilities.convertPointToScreen(point, canvas);
        canvas.dispatchEvent(new MouseEvent(canvas, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, x, y, point.x, point.y, 0, false, 0));
    }

You can find more info about dispatching events online, however, given the intended use, I would recommend you set up a Plugin using the RuneLite API. In the plugin you can register a MouseListener. Log the data from the events and you can study, and than simulate, what events should look like.