Jump to content

Help with basic to intermediate coding. (using 2 classes in a script, getting message from clanchat etc)


sarthycool

Recommended Posts

I am trying to make a basic dice rolling script for personal use. 

 

The script should:

1)Join a clan chat that is input. This includes leaving current cc , then joining the new cc.

2)Taking in commands from host/owner.

  These commands are: The host would only execute these 2 commands.

   

   !sethost *player* and !removehost *player*: It should only be taken and executed from the owner of cc. It      will check for the player in cc, if the player is present, make him host(able to execute next listed      commands). If not present, send a popup/log message saying hes not present.

 

3) Commands to be done by host: Host can execute only these commands.

   

   !roll: This command will roll the dice.

 

    !rulesgame *game name*: This command will list the rules for the named game. The games i want to play     and their rules can be input in the script.

 

    !hosts: will list all the hosts irrespective weather they are online in the cc or not. Only This command  could be executed by anyone.

 

  These commands are very basic for a dice bot.

 

3) These commands are to be read from the clan chat messages.

 

 

 

 

Now for this bot, I have created a rough logic structure.

 

In onStart() function:
1)At start, it will take the input from user of the clan chat it wants to join. Creating a popup message for this input string.
2)checks for the cc. If the account is already in a cc, leaves it and enters the user input cc name.
3)Sets the owner of the cc as the admin of the bot. This player can execute !sethost & !removehost command.

In onLoop() function:
1)Reads the messages entered in the cc. If the message read is from player A and player A == host, it would identify the command.
2)A command message would be identified by breaking in 2 parts: first part '!' for labelling it as command, second part 'x' (roll,sethost,hosts etc) for executing the corresponding case.
3)If the message is !sethost/!removehost, identify that the player who sent this message is owner. If yes, it would make the player name entered a host i.e save his name as a host. He can roll now.
4)If the command is !roll, it would check that the player who executed is a host? if yes, then it will generate a random number as a message.

 

 

This is what I am trying to do. How do i convert this blueprint into a code? I have very basic knowledge of coding.

 

As for my question in the title of using 2 classes in a script: I was seeing the getmessage class in the bot and it has only 3 states: Player, Game, Trade. How to get message from clan chat?

 

For this i saw a class made by Explv in a separate thread:

 

import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.script.Script;

import java.util.Random;

public class ClanChatter{

private Script script;

public ClanChatter(Script script){
this.script = script;
}

public boolean notInClanChat(){
RS2Widget notInChat = script.getWidgets().getWidgetContainingText("Not in chat");
return (notInChat != null && notInChat.isVisible());
}

public void openClanChatTab(){
script.getTabs().open(Tab.CLANCHAT);
}

public boolean isClanChatTabOpen(){
return Tab.CLANCHAT.isOpen(script.getBot());
}

public boolean isInClanChat(){
RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");
return (talkingIn != null && talkingIn.isVisible() && !talkingIn.getMessage().contains("Not in chat"));
}

public void joinClanChat(String name){

//RS2Widget enterName = getWidgets().getWidgetContainingText("Enter the player name whose channel you wish to join:");
RS2Widget enterName = script.getWidgets().get(162, 32);
RS2Widget joinChat = script.getWidgets().getWidgetContainingText("Join Chat");

if (enterName != null && enterName.isVisible()){
script.getKeyboard().typeString(name);
sleep(4000, 5000);
}
else if (joinChat != null && joinChat.isVisible()) clickOnWidget(joinChat);
}

public void leaveClanChat(){
RS2Widget leaveWidget = script.getWidgets().getWidgetContainingText("Leave Chat");
if (leaveWidget != null && leaveWidget.isVisible()) clickOnWidget(leaveWidget);
}

public boolean isInClan(String name){
RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");
return talkingIn.getMessage().contains(name);
}

public boolean isOwner(String name){
RS2Widget owner = script.getWidgets().getWidgetContainingText("Owner:");
return owner.getMessage().contains(name);
}

public void talkInClanChat(String output){
script.getKeyboard().typeString(String.format("/%s", output));
sleep(1500, 2000);
}

private void clickOnWidget(RS2Widget widget){
widget.hover();
script.getMouse().click(false);
sleep(1500, 2000);
}

private void sleep(int min, int max){

try{
script.sleep(new Random().nextInt(max-min) + min);
} catch(Exception e){
script.log(e);
}
}
}

 

He uses this class as a separate class for taking in messages from clan chat. How do i integrate the two?

 

 

Any help will be fabulous. I even do not know how to start this code. So please help as much as you can. Thanks.

 

Edited by sarthycool
Link to comment
Share on other sites

Create a separate class and insert Explv's code in it. Then you can either import it and or extend it in your main class. That allows you to use all the methods.

 

Edit: As for all the other code, if you don't know how to write it then you should just hire a scripter and give them your layout.

Edited by oTroll
Link to comment
Share on other sites

Right thanks.

I want to learn coding myself so I can't hire anyone.

http://osbot.org/forum/topic/58775-a-beginners-guide-to-writing-osbot-scripts-where-to-get-started-by-apaec/

 

Read his tutorial, he's a great scripter and that will provide you the skeleton for your script. I also recommend downloading some scripts in the downloadable section and decompile them to learn the API and what not.

  • Like 2
Link to comment
Share on other sites

 

Any help will be fabulous. I even do not know how to start this code. So please help as much as you can. Thanks.

 

I wrote that class a long time ago and it needs some improvement, sorry I do not have too much time to fix it up for you:

 

import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.utility.ConditionalSleep;

public final class ClanChatter {

    private Script script;

    public ClanChatter(final Script script){
        this.script = script;
    }

    public final void openClanChatTab(){
        script.getTabs().open(Tab.CLANCHAT);
    }

    public final boolean isClanChatTabOpen(){
        return script.getTabs().getOpen() == Tab.CLANCHAT;
    }

    public final boolean isInClanChat(){
        RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");
        return (talkingIn != null && !talkingIn.getMessage().contains("Not in chat"));
    }

    public final void joinClanChat(final String name){

        RS2Widget enterName = script.getWidgets().get(162, 32);
        RS2Widget joinChat = script.getWidgets().getWidgetContainingText("Join Chat");

        if (enterName != null && script.getKeyboard().typeString(name)) {
            new ConditionalSleep(2500) {
                @Override
                public boolean condition() throws InterruptedException {
                    return isInClan(name);
                }
            }.sleep();
        } else if (joinChat != null && joinChat.interact()) {
            new ConditionalSleep(2500) {
                @Override
                public boolean condition() throws InterruptedException {
                    return script.getDialogues().inDialogue();
                }
            }.sleep();
        }
    }

    public final void leaveClanChat(){
        RS2Widget leaveWidget = script.getWidgets().getWidgetContainingText("Leave Chat");
        if(leaveWidget != null && leaveWidget.interact()) {
            new ConditionalSleep(2500) {
                @Override
                public boolean condition() throws InterruptedException {
                    return !isInClanChat();
                }
            }.sleep();
        }
    }

    public final boolean isInClan(String name){
        RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");
        return talkingIn != null && talkingIn.getMessage().contains(name);
    }

    public final boolean isOwner(String name){
        RS2Widget owner = script.getWidgets().getWidgetContainingText("Owner:");
        return owner != null && owner.getMessage().contains(name);
    }

    public final boolean talkInClanChat(final String output){
        return script.getKeyboard().typeString(String.format("/%s", output));
    }
}

 

In terms of using it, I recommend you follow some Java tutorials if you do not understand how classes work.

Link to comment
Share on other sites

Make your cc class implement MessageListener then in your main class do addMessageListener(new CC()); and then u can override onmessage as well which will fire when a message is received, then just check that its a CC message:

 

this is useful for parsing the message text and username from a cc mesage:

	private String parseUserName(String messageText) {
		String temp = messageText.replaceAll("\\[(.*?)\\][\\S]", "");
		String temp2 = temp.replace(":", "");
		return temp2;

	}

	private String parseMessageText(String message) {
		return message.replaceAll("<[^>]*>", "");
	}
Edited by LoudPacks
Link to comment
Share on other sites

Make your cc class implement MessageListener then in your main class do addMessageListener(new CC()); and then u can override onmessage as well which will fire when a message is received, then just check that its a CC message:

this is useful for parsing the message text and username from a cc mesage:

private String parseUserName(String messageText) {		String temp = messageText.replaceAll("\\[(.*?)\\][\\S]", "");		String temp2 = temp.replace(":", "");		return temp2;	}	private String parseMessageText(String message) {		return message.replaceAll("<[^>]*>", "");	}

elaborate more please.

Link to comment
Share on other sites

I wrote that class a long time ago and it needs some improvement, sorry I do not have too much time to fix it up for you:

import org.osbot.rs07.api.ui.RS2Widget;import org.osbot.rs07.api.ui.Tab;import org.osbot.rs07.script.Script;import org.osbot.rs07.utility.ConditionalSleep;public final class ClanChatter {    private Script script;    public ClanChatter(final Script script){        this.script = script;    }    public final void openClanChatTab(){        script.getTabs().open(Tab.CLANCHAT);    }    public final boolean isClanChatTabOpen(){        return script.getTabs().getOpen() == Tab.CLANCHAT;    }    public final boolean isInClanChat(){        RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");        return (talkingIn != null && !talkingIn.getMessage().contains("Not in chat"));    }    public final void joinClanChat(final String name){        RS2Widget enterName = script.getWidgets().get(162, 32);        RS2Widget joinChat = script.getWidgets().getWidgetContainingText("Join Chat");        if (enterName != null && script.getKeyboard().typeString(name)) {            new ConditionalSleep(2500) {                @Override                public boolean condition() throws InterruptedException {                    return isInClan(name);                }            }.sleep();        } else if (joinChat != null && joinChat.interact()) {            new ConditionalSleep(2500) {                @Override                public boolean condition() throws InterruptedException {                    return script.getDialogues().inDialogue();                }            }.sleep();        }    }    public final void leaveClanChat(){        RS2Widget leaveWidget = script.getWidgets().getWidgetContainingText("Leave Chat");        if(leaveWidget != null && leaveWidget.interact()) {            new ConditionalSleep(2500) {                @Override                public boolean condition() throws InterruptedException {                    return !isInClanChat();                }            }.sleep();        }    }    public final boolean isInClan(String name){        RS2Widget talkingIn = script.getWidgets().getWidgetContainingText("Talking in:");        return talkingIn != null && talkingIn.getMessage().contains(name);    }    public final boolean isOwner(String name){        RS2Widget owner = script.getWidgets().getWidgetContainingText("Owner:");        return owner != null && owner.getMessage().contains(name);    }    public final boolean talkInClanChat(final String output){        return script.getKeyboard().typeString(String.format("/%s", output));    }}

In terms of using it, I recommend you follow some Java tutorials if you do not understand how classes work.

Is the logic correct in this class? If yes then I can see what to update for current osbot version.

Link to comment
Share on other sites

Is the logic correct in this class? If yes then I can see what to update for current osbot version.

 

Yes.

 

By the way, you say you want to learn how to program, if that is true then you should make some attempt to write the code yourself. This is the scripting help section not the scripting service section. Try and learn Java, look at the OSBot API, follow some tutorials, and then if you are still stuck request help here. Saying "I don't even know how to start this" is an indicator that you have not spent enough time trying to learn Java / scripting by yourself.

Link to comment
Share on other sites

Yes.

 

By the way, you say you want to learn how to program, if that is true then you should make some attempt to write the code yourself. This is the scripting help section not the scripting service section. Try and learn Java, look at the OSBot API, follow some tutorials, and then if you are still stuck request help here. Saying "I don't even know how to start this" is an indicator that you have not spent enough time trying to learn Java / scripting by yourself.

 

Yes of-course. I am learning from your code, not copying it. Using it as reference.

 

I am just looking for a head start. How to get things started. Right now I am just trying to make simple scripts. Making parts of this script i want to make and then get it together. In need for help, not service.

 

Thanks.

Edited by sarthycool
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...