Register new account
Edit account
Search

Ancient Domains Of Mystery, forum overview / General / Help with some Java

Online users ( Unknown)
Application object not working properly at the moment, no clue who is online...

* Numbers in parentheses are the number of minutes since the user last loaded a page. Logged-in users time out after 40 minutes (unless they manually log out), lurkers and anonymous posters after 20.

Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Sunday, August 28, 2005 at 05:26 (GMT -5)

    public void onMessage(String channel, String sender, String login, String hostname, String message) {
	if (message.substring(0, 3).equalsIgnoreCase("!op") && hostname.equalsIgnoreCase(TrustedHost)) {
	    sendMessage(channel, "test /mode +o "+channel+" "+message.substring(4, message.length()));
	}

    }   

    protected void onPrivateMessage(String sender, String login, String hostname, String message) {
	if (message.equalsIgnoreCase("auth morio asdfg")) {
	    String TrustedHost = hostname;
	    sendMessage(sender, "auth succesful " + TrustedHost);
        }
    }

I'm trying to make a bot for IRC, and I want myself to be able to auth to it, but with this code the public void can't access the variable TrustedHost. How should I make it so TrustedHost can be accessed outside the Braces?
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

[Edited 1 time, last edit on 8/28/2005 at 05:27 (GMT -5) by Morio]
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Sunday, August 28, 2005 at 09:24 (GMT -5)

That's easy simply make it a global variable. That it define it directly in the class itself rather than inside any of the functions.

like for example:

public class IRCbot 
{
   Static String TrustedHost;

   protected void onPrivateMessage(String sender, String login, String hostname, String message) 
   {
if (message.equalsIgnoreCase("auth morio asdfg")) {
    TrustedHost = hostname;
    sendMessage(sender, "auth succesful " + TrustedHost);
        }
    }
}


You may or may not need the Static prefix depending on wether or not your functions are static or not...
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Sunday, August 28, 2005 at 11:34 (GMT -5)

Ok, where, exactly do I put those lines?
the whole code looks like this:

import org.jibble.pircbot.*;

public class MyBot extends PircBot {

    public MyBot() {
        this.setName("Moriobot");

    }
    
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
        if (message.equalsIgnoreCase("time")) {
            String time = new java.util.Date().toString();
            sendMessage(channel, sender + ": The time is now " + time);
        }
	if (message.equalsIgnoreCase("hello")) {
	    sendMessage(channel, "Hello " + sender);
	}
	if (message.substring(0, 3).equalsIgnoreCase("!op") && hostname.equalsIgnoreCase(TrustedHost)) {
	    sendMessage(channel, "test /mode +o "+channel+" "+message.substring(4, message.length()));
	}
    }
/*    protected void onPrivateMessage(String sender, String login, String hostname, String message) {
*	if (message.equalsIgnoreCase("auth morio asdfg")) {
*	    String TrustedHost = hostname;
*	    sendMessage(sender, "auth succesful " + TrustedHost);
*       }
    }*/
}


(sorry if this is a stupid question, but I started with Java yesterday, and didn't find any good ansvers in the tutorials i found)
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

[Edited 1 time, last edit on 8/28/2005 at 11:35 (GMT -5) by Morio]
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Sunday, August 28, 2005 at 15:33 (GMT -5)

Directly after

public class MyBot extends PircBot {

That way the variable's scope will be the entire class rather than just inside one of the functions

so:

import org.jibble.pircbot.*;

public class MyBot extends PircBot {

String TrustedHost;

public MyBot() {
this.setName("Moriobot");

}

public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (message.equalsIgnoreCase("time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
if (message.equalsIgnoreCase("hello")) {
sendMessage(channel, "Hello " + sender);
}
if (message.substring(0, 3).equalsIgnoreCase("!op") && hostname.equalsIgnoreCase(TrustedHost)) {
sendMessage(channel, "test /mode +o "+channel+" "+message.substring(4, message.length()));
}
}
protected void onPrivateMessage(String sender, String login, String hostname, String message) {
if (message.equalsIgnoreCase("auth morio asdfg")) {
TrustedHost = hostname;
sendMessage(sender, "auth succesful " + TrustedHost);
}
}
}

Of coarse with this setup each instance of the class can only hae one TrustedHost value at a time...
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Monday, August 29, 2005 at 06:57 (GMT -5)

Thanks a million
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Monday, August 29, 2005 at 08:37 (GMT -5)

another question

how can I make the it run two commands, one after to other, for some reason this doesn't work

	if (message.equalsIgnoreCase("!hop") && hostname.equalsIgnoreCase(TrustedHost)) {
	    partChannel(channel);
	    joinChannel(channel);
	}


"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Thursday, September 01, 2005 at 17:03 (GMT -5)

Don't see why that should not work... Guess it depends on what you actualy do in either of those functions. If they run as seperate threads for example they will execute simultaniously, wich might cause problems if one command depends on something the other should have done or something like that...
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Friday, September 02, 2005 at 02:57 (GMT -5)

the joinChannel-command should be executed after the partChannel-command
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Monday, September 05, 2005 at 06:59 (GMT -5)

    public void onMessage(String channel, String sender, String login, String hostname, String message) {
	if (message.substring(0, 5).equalsIgnoreCase("!join") && hostname.equalsIgnoreCase(TrustedHost)) {
	    joinChannel(message.substring(6, message.length()));
	}
    }


Jan, you seems to know much about Java, is there a way to make the program read the lines from an .ini-file. For an example in this code it would search the .ini-file for a line that starts with !join and then it would read joinChannel, from the file like this

    public void onMessage(String channel, String sender, String login, String hostname, String message) {
	if (message.substring(0, 5).equalsIgnoreCase("line_that_starts_with_!join") && hostname.equalsIgnoreCase(TrustedHost)) {
	    joinChannel(message.substring(6, message.length()));
	}
    }


In other words it should search for the words from a .ini-file, to make the code itself a bit cleaner.

The .ini-file would look something like this

!join joinChannel
!leave parChannel
!kick kick

you get the point

So I was wondering if this can be made in an relatively easy way?
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Monday, September 05, 2005 at 15:58 (GMT -5)

If it's a Java program then yes you can read files. Not so with applets (well you can, but you need to get them signed first and stuff and I'm a bit fuzzy on how that works myself).

I'll asume you use the latest version, so if you look at the documentation:
http://java.sun.com/j2se/1.5.0/docs/api/index.html

For what you describe it's best to wrap a java.io.BufferedReader around a java.io.FileReader like so:

BufferedReader iniFile = new BufferedReader(new FileReader("irc.ini"));


Then you can read the entire file one line at a time using some code like this:

String line;
while ( (line = iniFile.readLine()) != null )
{
  // some code to look at the content of "line" here.
}


You'll need to catch some exceptions and stuff too naturaly, but seeing as you work wit networking here I asume you already know how to do that.
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Tuesday, September 06, 2005 at 08:12 (GMT -5)

Thanks once again, but...
can I make it recognize the length of the first word so that is I type !join #channel it recognizes the first word as a 5(6 if you count the space) character word and then separates it from the rest of my message so that it joins the #channel. Something like this
    }
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
	if (message.substring(0, 5).equalsIgnoreCase("!join") && hostname.equalsIgnoreCase(TrustedHost)) {
	    joinChannel(message.substring(6, message.length()));
	}
    }


but it should seek for the first word of the message in the .ini-file, so it would put the "joinChannel" where it is now in the code, so the line in the .ini-file would look something like this:
!join joinChannel

(I hope that makes any sence, and sorry again if this is a stupid question)
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Tuesday, September 06, 2005 at 10:34 (GMT -5)

Not sure if I follow... Do you want to put the function in the ini file? That wouldn't realy work, what you read from the file would be a plain string value.

As for looking at the first word of a string there are a number of ways to do that.

In terms of least code (though quite a bit of memory overhead) you can use the split() function of the string like this:
String[] temp = message.split(" ");

System.out.println("The first word is " + temp[0] + " it's " + temp[0].length() + " characters long" );
if you have determined that the first word is a command you can then remove it from the message like this:
message = message.replaceFirst(temp[0], "").trim();
This replaces the first word (element 0 in the temp array), with a empty string and then I use trim() to remove any exess whitespace at the start and end of the string.
Jan Erik Mydland
HoF admin

[Edited 1 time, last edit on 9/6/2005 at 10:39 (GMT -5) by Jan Erik]
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Tuesday, September 06, 2005 at 12:49 (GMT -5)

    }
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (message.substring(0, 5).equalsIgnoreCase("!join") && hostname.equalsIgnoreCase(TrustedHost)) {
    joinChannel(message.substring(6, message.length()));
}
    }


I'll try to explain it a bit better.

Ok, here we go. It should recognize the first word I write like !join, then look for that word in the ini-file and where it in the code says "joinChannel" at this moment, it would put whats after !join in the ini-file. for example if I write !part, then it would replace the "joinChannel" with "partChannel"

(am I asking for something impossible now?:)
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Tuesday, September 06, 2005 at 13:55 (GMT -5)

Hmm, yeah you can't "edit" the code directly at runtime.

I'm afraid you are stuck with having one such if test for each "command" word you have. At least I can't think of any alternatives.
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Wednesday, September 07, 2005 at 02:12 (GMT -5)

Ok, I guess I'll check separately for each command, but thanks anyway. :)
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Wednesday, September 07, 2005 at 08:01 (GMT -5)

    public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (message.equalsIgnoreCase("Linenumber") && hostname.equalsIgnoreCase(TrustedHost)) {
    (linenumber+1)(message.substring(6, message.length()));
}
    }


I thought of something like this, would it be possible?
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Thursday, September 08, 2005 at 16:30 (GMT -5)

Depends on what it's supposed to do :P

If you are trying to "convert" linenumber to the name of a function to exeute then no, that is not possible.

What would be possible is to create one class file (or serialised object) for each of the commands, then have the onMessage function check if the first word of a message is a command that is listed in the ini file, and then load the class file that is asosiated with that command in the ini file, create an instance of that class and run it.
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Friday, September 09, 2005 at 05:38 (GMT -5)

So I put the
joinChannel(message.substring(6, message.length()));

in the class file?
But what else do I need to put in the file, and how do I "use" the file from the main file?
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Friday, September 09, 2005 at 09:53 (GMT -5)

Well no you have to make a fully fledged class, that is you start a new .java file, write all the code it needs to do it's task when created and to pass information to it. Then you compile it to a class file like you would your main program, except the "command" classes don't need main functions and such because they will just be plugins for your main program. Once you have created the classes (aka objects) you can load them with something like this:
ClassLoader loader = new ClassLoader();

Object MyCommand = loader.loadClass(ClassName).newInstance();

MyCommand.setMessage(message);
MyCommand.start();
A bit of explanation. "ClassName" abowe would be a variable that contain the name of the class to be loaded (found in your ini file for example), for ecxample "joinChannel" (without the .class extention, so it's the name of the class, not the file (although the file have to have the same name as the file).

The "setMessage" function is a public function you have to code into your joinChannel in order to get the message passed to it, also all other "command object classes" you make will need the same function (at least with the same name) in order to work with this scheme.

The start() function also need to be made and will carry out the actual "work" of the command. I choose start() because it's the default command to start a thread, but if you don't want to use threads you can call it anyting realy.
Jan Erik Mydland
HoF admin
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Friday, September 09, 2005 at 11:31 (GMT -5)

Am I understanding this correctly now?
should MyCommand be replaced with something?
Also, what should I write in the
(message)
or should I leave it like that, and shall I write the line
MyCommand.setMessage(message)
in for example the join.java file?
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Friday, September 09, 2005 at 12:07 (GMT -5)

No in the example abowe MyCommand is an Object wich will be an instance of whatever class is loaded into it.

"message" would be a string that contain the message.

In the join.java file you'll have to writer something like
public class join extends Object;
{
String TheMessage;

  public void setMessage(String messageText)
  {
    TheMessage = MessageText;
  }

  public void start()
  {
    //code that does whatever the command is supposed to do

  }
}
When you then make an instance of this class in the main program you can call on it's public functions. So if you make an object of the "join" type called "myLittleJoinObject" you could call on the functions like this:
myLittleJoinObject.setMessage("hello world");
myLittleJoinObject.start();


Normaly you create an instance of a class like this:

join MyObject = new join();

This is sort of the whole "point" of Object oriented languages like Java everyting is objects, For example String is just another class file like your new join class, only difference is that it's included with the language, but you can make your own objects for your own needs too.

But seeing as you want to make the code more dynamic you can use the classloader code I posted abowe instead, wich basicaly does the same thing, but is not hard coded.

Hope that made some sence...
Jan Erik Mydland
HoF admin

[Edited 1 time, last edit on 9/9/2005 at 12:24 (GMT -5) by Jan Erik]
Portrait
Morio
Registered user
Holy Champion of ADoM


Last page view:

3901 days, 23 hours, 52 minutes and 25 seconds ago.
Posted on Friday, September 09, 2005 at 13:56 (GMT -5)

the join.java looks like this:
public class join extends Object;
{
String TheMessage;

  public void setMessage(String messageText)
  {
    TheMessage = MessageText;
  }

  public void start()
  {
    joinChannel(message.substring(6, message.length()));
  }
}


and the file that uses the join.java looks like this:
import org.jibble.pircbot.*;

public class MyBot extends PircBot {
    ClassLoader loader = new ClassLoader();
    Object MyCommand = loader.loadClass(join).newInstance();    
    String TrustedHost;

    public MyBot() {
        this.setName("Moriobot");

    }
    
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
	if (message.substring(0, 5).equalsIgnoreCase("!join")) {
	    MyCommand.setMessage(message);
	    MyCommand.start();
	}
    }
}


I't won't work, and I have the feeling I'm making some stupid mistake
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein
Portrait
Jan Erik
Administrator

Last page view:

16 hours, 30 minutes and 14 seconds ago.
Posted on Friday, September 09, 2005 at 17:59 (GMT -5)

Well yeah I was only giving examples of syntax, not fully working code..

First of all if you are going to call the joinChannel function from within the "join" object you need to actualy have the code for it in there as well.

Where is that function defined? If it's part of the PircBot class you could simply inherit from it to make the function accessable. Replace "extends Object" with "extends PircBot" in the join class (and remove the trailing ; on that line, that's a typo, sorry. Will have to add "import org.jibble.pircbot.*;" at the start too so the program knows where to look for the "PircBot" class.

Also this line
Object MyCommand = loader.loadClass(join).newInstance();
Should be inside the onMessage functon after you have actualy checked that the join command has been issued.

Oh and it should be
Object MyCommand = loader.loadClass("join").newInstance();
unless you have defined a String somewhere called join. Don't get objects and values confused.

You might also want to cut the "!join" part from the message before you put it into the "MyCommand" object.


I would also suggest you Google up and read a couple of Java 101 torurials. It helps to have a solid understanding of the basics on how the language works. For example http://www.javaworld.com/javaworld/jw-03-2000/jw-0331-java101.html
Jan Erik Mydland
HoF admin

Color mixer:
Red: Green: Blue: HTML color code: result:      
Your Name: Check to login:

Your Message:


Read the
formating help
Are you a spambot? Yes No Maybe Huh?
Create poll? Yes No   What is this?
Poll question: