Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Issue with GUI selection


    Tweeboy

    Recommended Posts

    Hey so I'm trying to make a WCer, GUI has the user select the type of tree:

    		final JComboBox<String> treeTypeBox = new JComboBox<>(new String[] {
    				"Tree", "Oak", "Willow", "Maple", "Yew", "Magic", "Teak",
    				"Mahogany" });
    		settingPanel.add(treeTypeBox);
    

    logType string is set to the treeTypeBox selection in a button's actionListener (this could be giving me the issues?):

    		JButton button = new JButton();
    		button.setText("Start Chopping");
    		button.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				logType = treeTypeBox.getSelectedItem().toString();
    				dropMode = powerChopBox.isSelected();
    				isRunning = true;
    				frame.dispose();
    			}
    		});
    

    logType String is fed to TreeFilter

    	Filter<GameObject> TreeFilter = new Filter<GameObject>() {
    		public boolean match(GameObject twe) {
    			return twe != null && twe.toString().contains(logType)
    					&& twe.getTile().distance() < 8;
    		}
    	};
    

    and then Chop Loop looks for Tree:

                                    Tree = getGameObjects().closest(TreeFilter);
    
    				if (Tree != null && !getLocalPlayer().isAnimating()) {
    					if (Tree.getTile().distance(getLocalPlayer().getTile()) > 6) {
    						getWalking().walk(
    								Tree.getSurroundingArea(2).getRandomTile());
    					} else if (!Tree.isOnScreen()) {
    						getCamera().mouseRotateToTile(Tree.getTile());
    					} else if (Tree.interact("Chop down")) {
    						sleep(Calculations.random(700, 1100));
    					}
    				}
    

    But any tree I select, doesn't work. Script just sits there running, doing nothing. I don't think logType is being assigned properly.
     
    Help pls wat do
     
    EDIT: Source=

     

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    import org.dreambot.dinh.framework.price.PriceCheck;
    import org.dreambot.dinh.framework.price.PriceType;
    import org.dreambot.api.utilities.Timer;
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.listener.InventoryListener;
    import org.dreambot.api.wrappers.interactive.GameObject;
    import org.dreambot.api.wrappers.items.Item;
    import org.dreambot.api.methods.filter.Filter;
    import org.dreambot.api.methods.map.Area;
    import org.dreambot.api.methods.skills.Skill;
    
    @ScriptManifest(author = "Roflme", name = "RoflmeCuttR", version = 0.4, description = "Cuts (almost) anything. Banks (almost) anywhere", category = Category.WOODCUTTING)
    public class Main extends AbstractScript implements InventoryListener {
    
    	private Area treeArea;
    	private Area bankArea;
    	private Point p;
    	private Rectangle b;
    
    	private String logType;
    	private boolean dropMode;
    	private boolean isRunning;
    
    	private long GP;
    	private long logCut = 0;
    	private long XP;
    	private Timer timer = new Timer();
    
    	GameObject Tree;
    
    	@Override
    	public void onStart() {
    		createGUI();
    		while (!isRunning || !getClient().isLoggedIn()) {
    			sleep(1200, 1600);
    		}
    		getSkillTracker().start(Skill.WOODCUTTING, true);
    		treeArea = Area.generateArea(10, getLocalPlayer().getTile());
    		bankArea = getBank().getClosestBankLocation().getArea(8);
    
    	}
    
    	@Override
    	public void onPaint(Graphics g) {
    		g.setColor(Color.RED);
    		g.setFont(new Font("Arial", Font.BOLD, 15));
    		g.drawString("Time Running: " + timer.formatTime(), 10, 20);
    		g.drawString(
    				"Logs Cut: " + logCut + " ("
    						+ (timer.getHourlyRate(Math.toIntExact(logCut))) + ")",
    				10, 40);
    		g.drawString(
    				"XP Gained: " + XP + " ("
    						+ (timer.getHourlyRate(Math.toIntExact(XP))) + ")", 10,
    				60);
    		}
    
    	private enum State {
    		CHOP, BANK, WAIT, RETURN
    	};
    
    	private State getState() {
    		if (getInventory().isFull()) {
    			return State.BANK;
    		} else if (!treeArea.contains(getLocalPlayer().getTile())) {
    			return State.RETURN;
    		} else if (!getLocalPlayer().isAnimating()) {
    			return State.CHOP;
    		} else {
    			return State.WAIT;
    		}
    	}
    
    	public void onExit() {
    
    	}
    
    	@Override
    	public int onLoop() {
    
    		XP = getSkillTracker().getGainedExperience(Skill.WOODCUTTING);
    
    		if (getDialogues().canContinue()) {
    			getDialogues().spaceToContinue();
    		}
    
    		switch (getState()) {
    		case CHOP:
    			while (getLocalPlayer().isMoving()) {
    				sleep(Calculations.random(300, 400));
    			}
    			sleep(Calculations.random(700, 1100));
    			Tree = getGameObjects().closest(
    					(obj) -> obj != null && !obj.getName().contains("stump")
    							&& obj.getName().contains(logType)
    							&& obj.distance(getLocalPlayer()) < 8);
    
    			if (Tree != null && !getLocalPlayer().isAnimating()) {
    				if (!Tree.isOnScreen()) {
    					getCamera().mouseRotateToTile(Tree.getTile());
    				} else if (Tree.interact("Chop down")) {
    					sleep(Calculations.random(700, 1100));
    				}
    			}
    			break;
    		case BANK:
    			if (bankArea.contains(getLocalPlayer().getTile())) {
    				if (!getBank().isOpen()) {
    					getBank().openClosest();
    				} else {
    					for (int i = 1; i <= 28; i++) {
    						if (!getInventory().getNameForSlot(i).endsWith("axe")) {
    							getBank().depositAll(i);
    						}
    					}
    				}
    			} else {
    				getWalking().walk(bankArea.getRandomTile());
    			}
    			sleep(Calculations.random(500, 1800));
    			break;
    		case RETURN:
    			getWalking().walk(treeArea.getRandomTile());
    			int returnrand = (Calculations.random(1, 100));
    			if (returnrand <= 98) {
    				sleep(Calculations.random(1500, 1800));
    			} else {
    				int yaw = (getCamera().getYaw());
    				if (yaw <= 1200) {
    					getCamera().mouseRotateToYaw(
    							getCamera().getYaw()
    									+ Calculations.random(300, 800));
    				} else {
    					getCamera().mouseRotateToYaw(
    							getCamera().getYaw()
    									- Calculations.random(300, 800));
    				}
    			}
    
    		case WAIT:
    			p = getLocalPlayer().getCenterPoint().getLocation();
    			b = Tree.getBoundingBox();
    			if (p != null && b != null && b.contains(p)) {
    				int yaw = (getCamera().getYaw());
    				if (yaw <= 1200) {
    					getCamera().mouseRotateToYaw(
    							getCamera().getYaw()
    									+ Calculations.random(300, 800));
    				} else {
    					getCamera().mouseRotateToYaw(
    							getCamera().getYaw()
    									- Calculations.random(300, 800));
    				}
    			}
    			if (getCamera().getPitch() < 300) {
    				getCamera().mouseRotateToPitch(Calculations.random(300, 380));
    			}
    			if (getMouse().isMouseInScreen()) {
    				getMouse().moveMouseOutsideScreen();
    			}
    			sleep(Calculations.random(3000, 5000));
    			break;
    		}
    		return Calculations.random(420, 666);
    	}
    
    	private void createGUI() {
    		final JFrame frame = new JFrame();
    		frame.setTitle("");
    		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frame.setLocationRelativeTo(getClient().getInstance().getCanvas());
    		frame.setPreferredSize(new Dimension(300, 170));
    		frame.getContentPane().setLayout(new BorderLayout());
    
    		JPanel settingPanel = new JPanel();
    		settingPanel.setLayout(new GridLayout(0, 1));
    
    		JLabel label = new JLabel();
    		label.setText("Log Type:");
    		settingPanel.add(label);
    
    		final JComboBox<String> treeTypeBox = new JComboBox<>(new String[] {
    				"Tree", "Oak", "Willow", "Maple", "Yew", "Magic", "Teak",
    				"Mahogany" });
    		settingPanel.add(treeTypeBox);
    
    		final JCheckBox powerChopBox = new JCheckBox();
    		powerChopBox.setText("Powerchop?");
    		settingPanel.add(powerChopBox);
    
    		frame.getContentPane().add(settingPanel, BorderLayout.CENTER);
    
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new FlowLayout());
    
    		JButton button = new JButton();
    		button.setText("Start Chopping");
    		button.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				logType = treeTypeBox.getSelectedItem().toString();
    				dropMode = powerChopBox.isSelected();
    				isRunning = true;
    				frame.dispose();
    			}
    		});
    
    		buttonPanel.add(button);
    
    		frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    
    		frame.pack();
    		frame.setVisible(true);
    
    	}
    
    	@Override
    	public void onItemChange(Item[] items) {
    		for (Item item : items) {
    			if (item != null && item.getName().contains("ogs")
    					&& item.getAmount() > 0) {
    				logCut += item.getAmount();
    			}
    
    		}
    
    	}
    
    }
    
     

     

     

    Link to comment
    Share on other sites

    I think the problem might be that you are calling "toString()" on the game object, and then checking that string for the logType name. What you should do instead is something like this.

    twe.getName().contains(logType);
    
    You also don't need to get the tile to compare distance for game objects. You can just do this.

    twe.distance(getLocalPlayer()) < whateverNumber
    
    One other thing you can do to make your code simpler is replace the way you're doing your filter with a lambda expression. So in your chop state you could do this to get the tree.

    GameObject tree = getGameObjects().closest((obj) -> obj != null && obj.getName().contains(logType) && obj.distance(getLocalPlayer()) < 8);
    Link to comment
    Share on other sites

    I think the problem might be that you are calling "toString()" on the game object, and then checking that string for the logType name. What you should do instead is something like this.

    twe.getName().contains(logType);
    
    You also don't need to get the tile to compare distance for game objects. You can just do this.

    twe.distance(getLocalPlayer()) < whateverNumber
    
    One other thing you can do to make your code simpler is replace the way you're doing your filter with a lambda expression. So in your chop state you could do this to get the tree.

    GameObject tree = getGameObjects().closest((obj) -> obj != null && obj.getName().contains(logType) && obj.distance(getLocalPlayer()) < 8);

    OP updated with source, changes were made and nothing has changed. It is important to note that the script wasn't doing ANYTHING. I walked out of TreeArea and it did NOT try to return. Camera was pointed down and it did NOT change. Script isn't entering any loops?

    Link to comment
    Share on other sites

    OP updated with source, changes were made and nothing has changed. It is important to note that the script wasn't doing ANYTHING. I walked out of TreeArea and it did NOT try to return. Camera was pointed down and it did NOT change. Script isn't entering any loops?

    It sounds like you need to do some debugging. One easy method is to use "log(String)" to see what the script is doing, and work based on that. Try adding something like log("State was statename") within each case block of your switch statement. You can also log the logType to see if its being set properly. To view the logged information while the script is running, just open the debug console by clicking tools->debugging->debug console. 

     

    If you haven't already, you might also want to just open the debug console when you run your script to see if any errors are happening. Its possible that its not working due to some error happening that you aren't aware of.

    Link to comment
    Share on other sites

    It sounds like you need to do some debugging. One easy method is to use "log(String)" to see what the script is doing, and work based on that. Try adding something like log("State was statename") within each case block of your switch statement. You can also log the logType to see if its being set properly. To view the logged information while the script is running, just open the debug console by clicking tools->debugging->debug console. 

     

    If you haven't already, you might also want to just open the debug console when you run your script to see if any errors are happening. Its possible that its not working due to some error happening that you aren't aware of.

    Found the issue. IDLog wouldn't grab the log's ID, so when I tried PriceChecking IDLog I was getting errors:

     

    • [ERROR]17:10:32: Exception has occurred while running! Please report error to developer if problem persists: java.lang.ArrayIndexOutOfBoundsException: 28 at java.util.concurrent.CopyOnWriteArrayList.get(Unknown Source) at java.util.concurrent.CopyOnWriteArrayList.get(Unknown Source) at org.dreambot.api.methods.container.AbstractItemContainer.getNameForSlot(AbstractItemContainer.java:291) at Main.onLoop(Main.java:139) at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:251) at java.lang.Thread.run(Unknown Source)   

     

    Just gonna have it search specific IDs based on logType

    Link to comment
    Share on other sites

    Found the issue. IDLog wouldn't grab the log's ID, so when I tried PriceChecking IDLog I was getting errors:

     

    • [ERROR]17:10:32: Exception has occurred while running! Please report error to developer if problem persists: java.lang.ArrayIndexOutOfBoundsException: 28 at java.util.concurrent.CopyOnWriteArrayList.get(Unknown Source) at java.util.concurrent.CopyOnWriteArrayList.get(Unknown Source) at org.dreambot.api.methods.container.AbstractItemContainer.getNameForSlot(AbstractItemContainer.java:291) at Main.onLoop(Main.java:139) at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:251) at java.lang.Thread.run(Unknown Source)   

     

    Just gonna have it search specific IDs based on logType

    Nice job! I'm glad you were able to get it figured out.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.