Code: Show/Hide package bots.TutorialBot; import hybrid.core.*; import hybrid.core.consts.*; import hybrid.core.events.*; import hybrid.core.events.ss.*; import frontend.alphaspawn.*; import frontend.alphaspawn.tools.command.*; public class Lesson1 extends AlphaSpawn { /** * The ASConnection object will be used throughout most of the tutorials. This is the AlphaSpawn * extension of the SSConnection class, which provides a number of support functions and interfaces * for player tracking and command handling. If you play on using the AlphaSpawn frontend, you * should get used to working with the ASConnection object and the connection framework in general. */ private ASConnection objConnection; public Lesson1() { // Set this bots author and contact info... super("Cerium", "Cerium@gmail.com"); CommandManager objCommands = CommandManager.getInstance(); objCommands.registerCommand("!test", new TutorialCommand()); // Attempt to open a connection... this.objConnection = (ASConnection) ASConnection.open(); if(this.objConnection != null) { /* The connection was opened successfully. At this point we need to allocate a username and * password for the connection to use. The SpawnConfiguration class provides a few functions * for managing the names provided in spawn.ini. * * The allocateLoginInfo() assigns a single username for use with this bot. */ LoginInfo objLoginInfo = SpawnConfiguration.allocateLoginInfo(); if(objLoginInfo != null) { /* Once we have a username, we can set the info in the connection. This is optional, but * is easier than handling than handling the Connected event manually. * * Once the info is set, we tell our ASConnection object to connect. Calling connect() * with no parameters tells the connection to use the zone IP and port specified in * spawn.ini. */ this.objConnection.setLoginInfo(objLoginInfo); this.objConnection.connect(); /* Now we setup the timer task. the AlphaSpawn class provides a Timer for us, so we just * need to create the TimerTask (See the ChatTask class below) / super.objTimer.schedule(new ChatTask(), 1800000, 1800000); } else { // A username could not be allocated. This bot relies on a connection that is able to // login. Without a username, we need to shutdown. BotSpawn.shutdown("Unable to allocate a username!"); } } else { // We were unable to allocate a connection, so were shutting down. BotSpawn.shutdown("Unable to allocate a connection!"); } } /** * The ChatTask class is our TimerTask to send a message at a set interval. Its not very extensive, * so the run function is all we need to take care of. */ private class ChatTask extends TimerTask { public void run() { // Make sure the connection isnt null, and check that its online... if(objConnection != null && objConnection.isOnline()) { /* Send our message. This sends the message to the first chat channel the bot is joined to. * This is identical to typing ';Hello internet!' in game. * * Before this will work, we need to either set the channels in spawn.ini, or send the * public message ?chat=channel where 'channel' is the name of the channel to join. */ objConnection.sendChannelMessage(1, "Hello internet!"); } } } protected void processEvent(HybridEvent objEvent) { // Ignore null or dead events... if(objEvent == null || !objEvent.isAlive()) { return; } System.out.println("Received event: " + objEvent.getEventName()); } } |