Guest User

Untitled

a guest
Dec 12th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.awt.Polygon;
  3. import java.awt.geom.Rectangle2D;
  4.  
  5. import org.powerbot.game.api.methods.Calculations;
  6. import org.powerbot.game.api.methods.node.Menu;
  7. import org.powerbot.game.api.util.Filter;
  8. import org.powerbot.game.api.wrappers.Entity;
  9. import org.powerbot.game.api.wrappers.ViewportEntity;
  10. import org.powerbot.game.api.wrappers.graphics.CapturedModel;
  11. import org.powerbot.game.bot.Context;
  12. import org.powerbot.game.bot.handler.input.a;
  13. import org.powerbot.game.bot.handler.input.util.MouseNode;
  14.  
  15. /**
  16. * @author Bradsta
  17. *
  18. * A clicker that changes destinations as its moving to provide an accurate click.
  19. */
  20. public class DynamicInteract {
  21.  
  22. /**
  23. * Interacts with an entity. Grabs a point from the {@link CapturedModel}.
  24. * Uses a slightly modified version of the built in mouse method,
  25. * changes the mouse destination as it is moving toward the target {@link Entity}.
  26. * <p>
  27. *
  28. * @param entity Entity to interact with
  29. * @return <tt>true</tt> if click was successful.
  30. */
  31. public static boolean interact(final Entity entity, final String action) {
  32. if (entity.isOnScreen()) {
  33. Point destination = getCenterPoint(entity);
  34.  
  35. if (destination != null) {
  36. return interact(entity, action, destination.x, destination.y);
  37. }
  38. }
  39.  
  40. return false;
  41. }
  42.  
  43. /**
  44. * Returns <tt>true</tt> if the destination point is within the target entity bounds.
  45. * <p>
  46. * Uses Rectangle2D as a means of a more general area. But not by too much.
  47. *
  48. * @param target Target entity to interact with
  49. * @param destination Destination point calculated previously
  50. * @return Returns <tt>true</tt> if the target pounds contains destination
  51. */
  52. private static boolean containsPoint(final Entity target, final Point destination) {
  53. final Polygon p = target.getBounds()[0];
  54.  
  55. if (p != null) {
  56. final Rectangle2D bounds = p.getBounds2D();
  57.  
  58. if (bounds != null) {
  59. return bounds.contains(destination);
  60. }
  61. }
  62.  
  63. return false;
  64. }
  65.  
  66. //Copied from the API (All credits to Timer)
  67. //Removed randomX/Y
  68. private static MouseNode create(final Entity target, final int x, final int y) {
  69. return new MouseNode(
  70. new ViewportEntity() {
  71. public Point getCentralPoint() {
  72. return getCenterPoint(target);
  73. }
  74.  
  75. public Point getNextViewportPoint() {
  76. return getCenterPoint(target);
  77. }
  78.  
  79. public boolean contains(final Point point) {
  80. return containsPoint(target, point);
  81. }
  82.  
  83. public boolean validate() {
  84. return Calculations.isOnScreen(x, y);
  85. }
  86. },
  87. new Filter<Point>() {
  88. public boolean accept(final Point point) {
  89. return true;
  90. }
  91. }
  92. );
  93. }
  94.  
  95. /**
  96. * Returns a center point within the target's bounds used to interact.
  97. * <p>
  98. * Only a target point, not necessarily the point that it will click.
  99. *
  100. * @param target The target entity to interact with.
  101. * @return A random point within the target's bounds.
  102. */
  103. private static Point getCenterPoint(final Entity target) {
  104. final Polygon p = target.getBounds()[0];
  105.  
  106. if (p != null) {
  107. final Rectangle2D bounds = p.getBounds2D();
  108.  
  109. if (bounds != null) {
  110. return new Point((int) bounds.getCenterX(), (int)bounds.getCenterY());
  111. }
  112. }
  113.  
  114. return null;
  115. }
  116.  
  117. //Copied from the API (Credits: Timer)
  118. //A few minor modifications by Bradsta to change destinations as the mouse is moving.
  119. private static boolean interact(final Entity target, final String action, final int x, final int y) {
  120. final a executor = Context.get().getExecutor();
  121.  
  122. MouseNode node = create(target, x, y);
  123. //Point destination = new Point(x, y);
  124.  
  125. while (node.a().isRunning() && node.l()) { //processable
  126. //if (containsPoint(target, destination)
  127. //&& Menu.select(action)) return true; //No need to move mouse to center if mouse is already within polygon and action is available.
  128.  
  129. executor.a(node); //step
  130.  
  131. // if (!containsPoint(target, destination)
  132. // && (destination = getCenterPoint(target)) != null) {
  133. // node = create(destination.x, destination.y);
  134. // }
  135. }
  136.  
  137. //node.f() completed()
  138. return Menu.select(action); //Called after executor should be completed
  139. }
  140.  
  141. }
Add Comment
Please, Sign In to add comment