Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How can i make my Player attack another Player?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can i make my Player attack another Player?

    All it does is just Spam click my Avatar. Since Players don't have ids i have been having a lot of trouble figuring out the best method for it to find and interact with another Player. Basically if my player can attack someone in the wild i want him to Attack. Also how would i go about making my Player continue attacking the same person if another attackable person comes on Screen? Heres the Javadocs if you want to take a look. Please Help this has been very difficult for me to figure out.

    Powerbot API JavaDocs 4012

    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.LookAndFeel;
    import org.powerbot.concurrent.Task;
    import org.powerbot.concurrent.strategy.Condition;
    import org.powerbot.concurrent.strategy.Strategy;
    import org.powerbot.game.api.ActiveScript;
    import org.powerbot.game.api.Manifest;
    import org.powerbot.game.api.methods.Calculations;
    import org.powerbot.game.api.methods.Game;
    import org.powerbot.game.api.methods.Widgets;
    import org.powerbot.game.api.methods.interactive.Players;
    import org.powerbot.game.api.util.Filter;
    import org.powerbot.game.api.util.Time;
    import org.powerbot.game.api.wrappers.Tile;
    import org.powerbot.game.api.wrappers.interactive.Player;
    import org.powerbot.game.bot.event.listener.PaintListener;
     
    @Manifest(name = "GrasersPker",authors = {"Graser"},description = "The Worlds Very First Pking Script.",version = 1.0D,website = "")
    public class GrasersPker extends ActiveScript implements Task, Condition, PaintListener {
     
    	 private Player[] players = null;
    	 boolean nonCombat = false;
    	 public boolean Activated = false;
    	    public boolean enableSpecialAttack = false;
    	    public boolean PlayersEnemy = false;
    	    public double lastSpecial = -1;
    	    public double currentSpecial = -1;
    	    public double specialDamage = -1;
    	    public String ActionString = "";
    	    int wildernessLevel = 0;
    	    int tick = 0;
    	    PKAidGUI GUI = null;
    	    PKAidGUI_Strategy GUI_Strategy = null;
     
    	    enum GUI_SETTINGS {
     
    	        BOUNDING_BOX,
    	        TILE,
    	        NAME_LEVEL;
    	        private boolean enabled = false;
     
    	        GUI_SETTINGS() {
    	        }
     
    	        public void set(boolean enabled) {
    	            this.enabled = enabled;
    	        }
     
    	        public boolean enabled() {
    	            return enabled;
    	        }
    	    }
     
    	    protected void setup() {
    	        provide(new Strategy(this, this));
    	        GUI_Strategy = new PKAidGUI_Strategy();
    	        provide(GUI_Strategy);
    	    }
     
    	    public boolean validate() {
    	        return (GUI != null && !GUI.isVisible())
    	                || (GUI_Strategy != null && !GUI_Strategy.validate());
    	    }
     
    	    public void run() {
    	        if (tick == 0) {
    	            GUI_Strategy.start();
    	        }
    	        if (Game.isLoggedIn()) {
    	        	if(CanAttack()) {
    	        		UseSpecial();
    	        	}
    	            final Player botter = Players.getLocal();
    	            players = Players.getLoaded(new Filter<Player>() {
     
    	                public boolean accept(Player p) {
    	                    return (p != null)
    	                            && (Calculations.distanceTo(p.getLocation()) < 19.0D)
    	                            && (botter.getLevel() - getWildernessLevel() <= p.getLevel())
    	                            && (botter.getLevel() + getWildernessLevel() >= p.getLevel())
    	                            && (!p.equals(botter));
     
    	                }
    	            });
    	        }
    	        tick++;
    	        Time.sleep(50);
    	    }
     
    	    private void UseSpecial() {
    			// TODO Auto-generated method stub
     
    		}
     
    		private boolean CanAttack() {
    			ActionString = "Attack players.";
                	if(Players.getLocal().interact("Attack"))
                return false;
    			return true;
    		}
     
    		public void onRepaint(Graphics grphcs) {
    	        if (Game.isLoggedIn()) {
    	            Graphics2D g = (Graphics2D) grphcs;
    	            g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
    	                    RenderingHints.VALUE_ANTIALIAS_ON));
     
    	            Player botter = Players.getLocal();
    	            if ((players != null)
    	                    && (inWilderness())) {
    	                for (Player p : players) {
    	                    if ((p != null) && (!p.equals(botter))) {
    	                        if (GUI_SETTINGS.TILE.enabled() && p.getLocation().isOnScreen()) {
    	                            drawTile(g, p.getLocation(), colorForLevel(p));
    	                        }
    	                        if (GUI_SETTINGS.BOUNDING_BOX.enabled() && p.isOnScreen()) {
    	                            drawPlayer(g, p, colorForLevel(p));
    	                        }
    	                        if (GUI_SETTINGS.NAME_LEVEL.enabled() && p.isOnScreen()) {
    	                            drawPlayerInformation(g, p, colorForLevel(p));
    	                        }
    	                    }
    	                }
    	            }
    	        }
    	    }
     
    	    public void drawTile(Graphics2D g, Tile tile, Color col) {
    	        for (Polygon poly : tile.getBounds()) {
    	            boolean drawThisOne = true;
    	            for (int i = 0; i < poly.npoints; i++) {
    	                Point p = new Point(poly.xpoints[i], poly.ypoints[i]);
    	                if (!Calculations.isOnScreen(p)) {
    	                    drawThisOne = false;
    	                }
    	            }
    	            if (drawThisOne) {
    	                Color col2 = new Color(col.getRed(), col.getGreen(), col.getBlue(), 80);
    	                g.setColor(col2);
    	                g.fillPolygon(poly);
    	                g.setColor(col);
    	                g.drawPolygon(poly);
    	            }
    	        }
    	    }
     
    	    public void drawPlayer(Graphics2D g, Player player, Color col) {
    	        if (player.isOnScreen()) {
    	            Rectangle rect = getBoundingBox(player.getBounds());
    	            if ((Calculations.isOnScreen(new Point(rect.x, rect.y))) && (Calculations.isOnScreen(new Point(rect.x + rect.width, rect.y + rect.height)))) {
    	                g.setColor(col);
    	            }
    	            g.drawRect((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect.getHeight());
    	        }
    	    }
     
    	    public void drawPlayerInformation(Graphics2D g, Player player, Color col) {
    	        Rectangle boundingBox = getBoundingBox(player.getBounds());
    	        String playerName = player.getName();
    	        int level = player.getLevel();
    	        int centerBoundPointX = (int) (boundingBox.getX() + boundingBox.getWidth() / 2);
    	        double fullWidth = g.getFont().getStringBounds(playerName + "(" + level + ")",
    	                g.getFontRenderContext()).getWidth(); //Left space out to look neater in-game.
    	        double nameWidth = g.getFont().getStringBounds(playerName + " (",
    	                g.getFontRenderContext()).getWidth();
    	        double levelWidth = g.getFont().getStringBounds(level + "",
    	                g.getFontRenderContext()).getWidth();
    	        int stringStartX = centerBoundPointX - (int) (fullWidth / 2);
    	        g.setColor(Color.WHITE);
    	        g.drawString(playerName + " (", stringStartX,
    	                (int) boundingBox.getY());
    	        g.setColor(col);
    	        g.drawString("" + level, stringStartX + (int) nameWidth, (int) boundingBox.getY());
    	        g.setColor(Color.WHITE);
    	        g.drawString(")", stringStartX + (int) nameWidth + (int) levelWidth, (int) boundingBox.getY());
    	    }
     
    	    public Color colorForLevel(Player p) {
    	        Player botter = Players.getLocal();
    	        float delta = getScale(p.getLevel() - botter.getLevel(), getWildernessLevel());
     
    	        return Color.getHSBColor((60.0F - 60.0F * delta) / 360.0F, 1F, 1F);
    	    }
     
    	    public Rectangle getBoundingBox(Polygon[] polys) {
    	        double minX = Double.MAX_VALUE;
    	        double minY = Double.MAX_VALUE;
    	        double maxX = Double.MIN_VALUE;
    	        double maxY = Double.MIN_VALUE;
    	        for (Polygon poly : polys) {
    	            Rectangle rect = poly.getBounds();
    	            if (minX > rect.getX()) {
    	                minX = rect.getX();
    	            }
    	            if (minY > rect.getY()) {
    	                minY = rect.getY();
    	            }
    	            if (maxX < rect.getX()) {
    	                maxX = rect.getX();
    	            }
    	            if (maxY < rect.getY()) {
    	                maxY = rect.getY();
    	            }
    	        }
    	        return new Rectangle((int) minX - 5, (int) minY - 5, (int) (maxX - minX) + 5, (int) (maxY - minY) + 5);
    	    }
     
    	    public boolean inWilderness() {
    	        return Widgets.get(381, 2).visible() ? true : false;
    	    }
     
    	    public int getWildernessLevel() {
    	        return inWilderness() ? Integer.parseInt(
    	                Widgets.get(381, 2).getText().replaceAll("Level: ", "")) : 0;
    	    }
     
    	    public float getScale(int difference, int max) {
    	        if (difference == 0) {
    	            return 0F;
    	        }
    	        float val = (float) Math.max(difference, -difference) / (float) max;
    	        return difference > 0 ? val : -val;
    	    }
     
    	    class PKAidGUI_Strategy extends Strategy implements Task {
     
    	        public void start() {
    	            if (GUI != null) {
    	                GUI.setVisible(true);
    	            } else {
    	                GUI = new PKAidGUI();
    	                GUI.setVisible(true);
    	                GUI.toFront();
    	            }
    	        }
     
    	        public void run() {
    	            Time.sleep(250);
    	        }
     
    	        @Override
    	        public boolean validate() {
    	            return (GUI != null && GUI.isVisible());
    	        }
    	    }
     
    	    class PKAidGUI extends JFrame {
     
    	        /**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;
    			public PKAidGUI() {
    	            LookAndFeel old = javax.swing.UIManager.getLookAndFeel();
    	            try {
    	                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    	                    if ("Nimbus".equals(info.getName())) {
    	                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
    	                        break;
    	                    }
    	                }
    	            } catch (ClassNotFoundException ex) {
    	                java.util.logging.Logger.getLogger(PKAidGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    	            } catch (InstantiationException ex) {
    	                java.util.logging.Logger.getLogger(PKAidGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    	            } catch (IllegalAccessException ex) {
    	                java.util.logging.Logger.getLogger(PKAidGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    	            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    	                java.util.logging.Logger.getLogger(PKAidGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    	            }
    	            initComponents();
    	            setTitle("GrasersPker Config");
    	            setLocationRelativeTo(getRootPane());
    	            setVisible(true);
    	            try {
    	                javax.swing.UIManager.setLookAndFeel(old);
    	            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    	                java.util.logging.Logger.getLogger(PKAidGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    	            }
    	        }
     
    	        private void initComponents() {
    	            jPanel2 = new javax.swing.JPanel();
    	            jCkbx_boundingBox = new javax.swing.JCheckBox();
    	            jCkbx_tile = new javax.swing.JCheckBox();
    	            jPanel3 = new javax.swing.JPanel();
    	            jCkbx_levelName = new javax.swing.JCheckBox();
    	            jBtn_start = new javax.swing.JButton();
     
    	            jBtn_start.addActionListener(new java.awt.event.ActionListener() {
     
    	                public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                    setVisible(false);
    	                }
    	            });
     
    	            jCkbx_boundingBox.addActionListener(new java.awt.event.ActionListener() {
     
    	                public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                    GUI_SETTINGS.BOUNDING_BOX.set(jCkbx_boundingBox.isSelected());
    	                }
    	            });
     
    	            jCkbx_tile.addActionListener(new java.awt.event.ActionListener() {
     
    	                public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                    GUI_SETTINGS.TILE.set(jCkbx_tile.isSelected());
    	                }
    	            });
     
    	            jCkbx_levelName.addActionListener(new java.awt.event.ActionListener() {
     
    	                public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                    GUI_SETTINGS.NAME_LEVEL.set(jCkbx_levelName.isSelected());
    	                }
    	            });
     
    	            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    	            setResizable(false);
     
    	            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Player Colouring"));
     
    	            jCkbx_boundingBox.setText("Bounding Box");
     
    	            jCkbx_tile.setText("Tile");
     
    	            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    	            jPanel2.setLayout(jPanel2Layout);
    	            jPanel2Layout.setHorizontalGroup(
    	                    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jCkbx_boundingBox).addComponent(jCkbx_tile)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    	            jPanel2Layout.setVerticalGroup(
    	                    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addComponent(jCkbx_boundingBox).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jCkbx_tile)));
     
    	            jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Player Information"));
     
    	            jCkbx_levelName.setText("Level & Name");
    	            jCkbx_levelName.setText("Eating");
    	            jCkbx_levelName.setText("Looting");
    	            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    	            jPanel3.setLayout(jPanel3Layout);
    	            jPanel3Layout.setHorizontalGroup(
    	                    jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jCkbx_levelName).addContainerGap(24, Short.MAX_VALUE)));
    	            jPanel3Layout.setVerticalGroup(
    	                    jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jCkbx_levelName));
     
    	            jBtn_start.setText("Start");
     
    	            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    	            getContentPane().setLayout(layout);
    	            layout.setHorizontalGroup(
    	                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jBtn_start, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE).addContainerGap()));
    	            layout.setVerticalGroup(
    	                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jBtn_start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));
     
    	            pack();
    	        }
    	        public javax.swing.JCheckBox getjCkbx_Eating() {
    				return jCkbx_Eating;
    			}
     
    			public void setjCkbx_Eating(javax.swing.JCheckBox jCkbx_Eating) {
    				this.jCkbx_Eating = jCkbx_Eating;
    			}
    			public javax.swing.JCheckBox getjCkbx_Looting() {
    				return jCkbx_Looting;
    			}
     
    			public void setjCkbx_Looting(javax.swing.JCheckBox jCkbx_Looting) {
    				this.jCkbx_Looting = jCkbx_Looting;
    			}
    			private javax.swing.JButton jBtn_start;
    	        private javax.swing.JCheckBox jCkbx_boundingBox;
    	        private javax.swing.JCheckBox jCkbx_levelName;
    	        private javax.swing.JCheckBox jCkbx_Eating;
    	        private javax.swing.JCheckBox jCkbx_Looting;
    	        private javax.swing.JCheckBox jCkbx_tile;
    	        private javax.swing.JPanel jPanel2;
    	        private javax.swing.JPanel jPanel3;
    	    }
    	}


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How can i make my Player attack another Player?

    Powerbot has a development forum, which would be much faster in responding and assisting you in their API; available here

    However, I'll make a few notes that are non-specific to the API, in the order I noticed them:

    • Do not initialize class variables the same place they are declared. Initialize in a constructor [Or in this case the setup method]
    • GraserPker.GUI_SETTINGS#set(boolean) should be renamed
    • Enums should be named just like a class (They ARE a class) with the members capitalized. Further Explanation
    • According to the Java Naming Conventions all methods should start with a lowercase letter, f.e. canAttack()
    • Make sure your scope makes sense. Don't have a higher scope than is necessary
    • While the powerbot community doesn't usually, break up classes into seperate files.
    • Use imports rather than excessive use of java.awt.event.ActionListener() constructors.
    • Hypocritically I suggest instead of having a million action listeners, consider having one
      large multi-use actionlistener and set the action commands.
    • Again, imports over calling its name. This helps readers of your code know what you use
    • Consider refactoring your layout into something more managable. Have a look at this for help.


    Edit:
    On further looking, consider reading over the previously linked naming conventions in its entirety.
    Last edited by Tjstretch; July 31st, 2012 at 01:07 PM.

  3. #3
    Junior Member awinston's Avatar
    Join Date
    Jul 2012
    Location
    United States
    Posts
    10
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can i make my Player attack another Player?

    Quote Originally Posted by Tjstretch View Post
    • Do not initialize class variables the same place they are declared. Initialize in a constructor [Or in this case the setup method]
    I think you mean instance variables, right?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Java mp3 Player
    By aNGaJe in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2011, 09:49 AM
  2. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  3. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM
  4. Java Mp3 player.
    By alex901 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 18th, 2010, 09:12 AM
  5. [ASK] JMF Class Player
    By bocahTuaNakalzz in forum Java SE APIs
    Replies: 2
    Last Post: December 8th, 2009, 03:40 AM

Tags for this Thread