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);
}
}
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!
Related
I want to drag and drop TextButtons to designated targets. Searching this site, DragAndDropTest.java on libgdx's testing github is cited as an example to to start from. Implementing the flow described in the test, I received this error:
Exception in thread "main" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop$1.drag(DragAndDrop.java:111)
at com.badlogic.gdx.scenes.scene2d.utils.DragListener.touchDragged(DragListener.java:61)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:62)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDragged(Stage.java:315)
at com.badlogic.gdx.InputEventQueue.drain(InputEventQueue.java:89)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Input.update(Lwjgl3Input.java:205)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:390)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:137)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:111)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.createApplication(Lwjgl3Launcher.java:17)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.main(Lwjgl3Launcher.java:12)
Eventually I just tried to run the DragAndDropTest.java directly and received the same error. Anything from a fixed test, pointing out where I may have gone wrong in trying to run the test, or a different working example of drag and drop to study would all be helpful in moving forward.
Note that I also tried a different path when I struck out here in implementing Lee Stemkoski's DragAndDropActor class. While that did work, the flow I learned from doing so is only good for his BaseActor family, and the work of re-implementing Buttons and such on top of it doesn't seem like the right solution. I'll take from that effort confirmation that drag and drop itself is not broken, just the test.
I've ripped out all of the dragging methods and tracking variables from Stemkoski's DragAndDropActor, and placed them in .addListener() for each item I want to drag within my screen. Now that I've seen my draggables moving around the screen without crashing, I think I'll make a new DragListener extending from InputListener, as that seems like the way to encapsulate the logic (open to feedback). Anyway, it occurs to me that the DragAndDrop class found in the DragAndDropText is never referenced in Stemkoski's work, so he must have implemented his own parallel drag and drop functionality. So I take back what I said about knowing the drag and drop that comes with native libgdx works, however unlikely it would seem that it not.
While this is technically an example of drag and drop that still works compared to the oft-cited test class, I think any answer provided that uses the vanilla libgdx packages should be considered a superior one.
I've done something like that :
public class DragAndDropActorListener extends DragListener {
private Actor actor;
public DragAndDropActorListener(Actor actor) {
this.actor = actor;
}
#Override
public void drag(InputEvent event, float x, float y, int pointer) {
this.actor.setY(event.getStageY());
this.actor.setX(event.getStageX());
super.drag(event, x, y, pointer);
}
}
With actor the object to move. you could also a little Offset for center the object on the mouse
I'm sorry this is probably way too basic to be on here, but it's a subject I've been struggling with for about a month now and I don't know where else to go (as far as I know there is no "noob overflow", lol).
I'm trying to create a class that would:
1. put an image on a window (a JFrame, JPanel or other container)
2. be able to support keyboard and mouse listeners
3. could have multiple instances in the same container
So anyway I've tried all the usual places - Google, YouTube, the official Java site (sorry forgot the URL) and of course here on Stack Overflow - but haven't been able to find anything even remotely similar to what I'm trying to do.
Of course, I've also considered the possiblity that maybe it can't be done at all. There doesn't seem to be any kind of standard "JImage" or "JGraphic" that works like JButton or JLabel, and for whatever reason graphics requires a completely different list of (extremely involved) processes and procedures. As an example, in this post: How to "really" draw images in a Java app - it took me 60+ lines of code and 2 classes to just come close. That project didn't work in the end because for some reason it would only let me create one instance (even it you created 2-4 in the main method, it would only display the last one you told it to add).
But anyway, assuming that I'm not trying to "re-invent the wheel" here and it is actually possible (in Java), does anyone have an idea as to how (or at least know of a better site to study it)? Unfortunately most of the sites I've visited tend to assume you know all the inner workings of images (I know what a pixel is but that's about it - Buffers, Rastars etc. are still beyond me). It would be absolutely outstanding if there were a site that would explain it in layman's terms, if such a site exists. Thanks in advance.
Just use a plain old JLabel.
Regarding your requirements:
put an image on a window (a JFrame, JPanel or other container).
You can give a JLabel an ImageIcon of the image of interest and it will display it. This can then be easily placed in any other container such as a JPanel or JFrame.
be able to support keyboard and mouse listeners
Any component that extends JComponent, such as a JLabel allows for use of MouseListener, MouseMotionListener and can listen for keyboard input via Key Bindings.
could have multiple instances in the same container
You can add as many as you'd like to any container. Just be cognizant and respectful of the layout managers in use.
Typically, when I create a class, for example Customer, I give it some data fields, i.e. public int IdNumber; and some methods, i.e. public String getName(){...}. But that's pretty much it. I can't go beyond that and start playing with graphics - I can only manipulate and organize data as far as the class allows.
I can't get my head around what is happening inside JFrame. Whoever wrote the class JFrame, how did they write a class that can make a box appear on screen? What is happening internally that causes this to happen? Is there anyway to emulate it?
The same question applies to all graphics-based Java classes. I'm really curious to know how it works, as it bothers me each time I use one of them.
Java started with awt (Abstract Windowing Toolkit) and later introduced swing.
In AWT the platform event handling loop is hooked into, and events are packed in own java classes and one single (non-parallel) event handling queue/thread handles them, one after another. Swing inherits this.
In AWT every GUI component, like radio button or menu item, has a native code "peer" control, the platform provided component. There is a parallel set of java classes and their C counterpart. Especially interesting is the java Graphics class which allows custom drawing of lines, rectangles and such. It is peered under Windows with a CDC (Device Context) - presumably.
In Swing most of the platform components are emulated, that is, recreated oneself: the drawing, the mouse handling, and so on. So the native part is simpler, say maybe a CWnd (Window component) with custom drawing.
Swing can achieve a more consistent and more feature rich functionality. You can imagine that setting a backgroud color on an AWT radio button might not be possible, or using HTML on a label or tool tip. Also Swing can do skinning, themes, LookAndFeels. The System look and feel being a close imitation of the platform components. Especially Swing components are more light weight, as not every component has a native peer control to be handled in C.
Now SWT was a later initiative of IBM realized in eclipse for AWT reloaded. Not as customizable as Swing but intended to be platform near.
You should forget using AWT components, and if not programming for eclipse RCP also SWT.
So: global platform events like mouse click, repaint request are translated to Java events. There is a container hierarchy of JFrame, JPanels, JScrollPanes, JComponents. An event is dispatched to the handling components, on which for example paintComponent is called:
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // A later introduced class that can do more.
g2.draw...
}
With JavaFX there comes a new player, which is IMHO not yet fully mature, but usable in non-production code. It enables effects/animations, rotations, transformations, light. So a 2D - 4D rendering, based on like platform rendering. Also it is property based, so a check box would not necessarily be bound to a boolean, but a boolean property observing and notifying changes. I need still some practical experience, to conceive an optimal architecture with it.
If you are curious about how java is implemented you should take a look at the source code. http://openjdk.java.net/projects/jdk7/ would be a start.
Of course this would only give you insight into that particular implementation and doesn't mean that your java is implemented the same way.
How does a box appear on a screen? This functionality is offered by the operating system to the JVM (by the X Window System on Linux).
On the Java level, JFrame inherits from java.awt.Window, which has the "native peers" provided by the native windowing system.
If you really want to understand it, it is better if you try to create some windows using C only.
I'm looking to create an interface similar to that of the iPhone's SMS screen. More specifically I'm looking to replicate the "bubbles" coming from each side of the page which contain the messages, as shown here http://www.bidslammer.com/images/iphone_shot1.png .
I do also want to recreate the date and time above the bubbles like you can see in that image. I need to be able to do this by code because its use will be to display the messages that it receives over my socket connection, and show the messages I send over the socket.
I'm really new to Java, and even newer to Swing, so I'm looking for some pointers on how I should go about this.
Can anyone offer my any suggestions about how I would go about doing this? I'm not looking for someone to do the work for me, just a few pointers, perhaps some things I should learn how to use/do and perhaps a helpful tutorial or two.
Google for "swing tutorial" gives a lot of tutorial links, so just pick one. When I was learning Swing I used original Java Swing tutorial.
As for some pointers, I think good idea is to use images to represent the bubbles - that will be the easiest. Inherit some basic component like JLabel or JPanel and override a drawing method - do a custom drawing. First draw the bubble image and then the text over it. This may help with image drawing.
Generally with custom component drawing you use Graphics class, which provides a lot of useful drawing methods.
The sticky notes demo might get you started in the right direction.
Even though that demo is a NetBeans platform module, the sticky note itself is a pure Swing component and should be usable without the platform.
I need to create a drag and drop system in swing where an image of the thing being dragged is attached to the cursor during the drag. In theory this is achieveable with
public Icon TransferHandler.getVisualRepresentation(Transferable t)
but there appears to be a long standing bug (here) that means this method is never called. I know I can do it by implementing my own DnD system with DragSource etc., but does anyone know of an easier workround that will get me what I need?
The method TransferHandler.getVisualRepresentation wasn't supported in java 1.4, I'm not sure if or when it was fixed. To test whether it works in a current version you could adapt this example
In the end I used the old style drag-and-drop to implement what I wanted. However I have no reason to think abrightwell's solution wouldn't work just as well - this was just the best way at the time.
You could Try putting the image on a Jlabel (in the draggesture recognizer)and set its bounds, in the droptargetListener dragover method. Alternately, hows about implementing a Mouse listener (I've not tested this latter method).
I have used the "work around" suggested towards the bottom of the bug report you have listed. It worked well enough for me. Granted I was using this with Mac OS X so I have no idea whether Winderz will support it. It would be nice if they would at least fix it to work like they intended and simply document where it will and won't work... oh well. Good Luck.