Chat Tutorial in NetBeans/Text Client

From Arianne
Revision as of 17:46, 8 January 2012 by imported>SimonSmall (Added building the Text Client)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

These instructions are a continuation of the Chat Tutorial. This is a text based client that runs in a command / terminal window.

NetBeans Project

In the NetBeans IDE, click New Project, choose Java and Java Application. Click Next, then give the Project Name as client. Change the Project Location to DEVPATH\DevChat (DEVPATH, see above); the Project Folder is shown as DEVPATH\DevChat\client with DEVPATH being your chosen location. Check that Create Main Class is ticked, and the class is client.Test before clicking Finish.

The empty Project is created in NetBeans. Under the Files tab you will see the directory structure it has also generated; check this using your file explorer.

Right click the Libraries branch of the server Project tree, and select Add Project. Make sure you browse to the correct file location and select the marauroa project that you created earlier. This will include your marauroa library that you built.

Client Files

The following files need to be created, as they are not present in the marauroa source file. It does not matter in which order you create them.

Client.java

Right-click on the client package and add a new Class. Give the class name as Client. Add the following code:

<source lang="Java"> package client;

import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.ArrayList;

import marauroa.client.ClientFramework; import marauroa.client.net.IPerceptionListener; import marauroa.client.net.PerceptionHandler; import marauroa.common.game.RPAction; import marauroa.common.game.RPObject; import marauroa.common.net.message.MessageS2CPerception; import marauroa.common.net.message.TransferContent;

public class Client extends ClientFramework {

 private PerceptionHandler handler;
 protected static Client client;
 private Map<RPObject.ID, RPObject> world_objects;
 private String[] available_characters;
 private List<String> quotes = new ArrayList<String>();

 public static Client get() {
   if (client == null) {
     client = new Client();
   }
   return client;
 }

 protected Client() {
   super("log4j.properties");
   world_objects = new HashMap<RPObject.ID, RPObject>();
   handler = new PerceptionHandler(new PerceptionListener());
 }

 public String[] GetAvailableCharacters() {
   return available_characters;
 }

 String popQuote() {
   if (quotes.isEmpty()) {
     return null;
   }
   String result = quotes.get(0);
   quotes.remove(0);
   return result;
 }

 public void SendMessage(String text) {
   RPAction action;
   action = new RPAction();
   action.put("type", "chat");
   action.put("text", text);
   send(action);
 }

 protected void onPerception(MessageS2CPerception message) {
   try {
     handler.apply(message, world_objects);
   } catch (java.lang.Exception e) {
     // Something weird happened while applying perception
   }
 }

 protected List<TransferContent> onTransferREQ(List<TransferContent> items) {
   return items;
 }

 protected void onTransfer(List<TransferContent> items) {
 }

 protected void onAvailableCharacters(String[] characters) {
   available_characters = characters;
 }

 protected void onServerInfo(String[] info) {
   for (String s : info) {
     quotes.add(s);
   }
 }

 protected String getGameName() {
   return "Chat";
 }

 protected String getVersionNumber() {
   return "0.5";
 }

 protected void onPreviousLogins(List<String> previousLogins) {
 }

 class PerceptionListener implements IPerceptionListener {
   public boolean onAdded(RPObject object) {
     if (object.has("text")) {
       quotes.add("*" + object.get("from") + "* : " + object.get("text"));
     }
     return false;
   }

   public boolean onModifiedAdded(RPObject object, RPObject changes) {
     return false;
   }

   public boolean onModifiedDeleted(RPObject object, RPObject changes) {
     return false;
   }

   public boolean onDeleted(RPObject object) {
     return false;
   }

   public boolean onMyRPObject(RPObject added, RPObject deleted) {
     return false;
   }

   public void onSynced() {
   }

   public void onUnsynced() {
   }

   public void onException(Exception e, marauroa.common.net.message.MessageS2CPerception perception) {
     e.printStackTrace();
     System.exit(-1);
   }

   public boolean onClear() {
     return false;
   }

   public void onPerceptionBegin(byte type, int timestamp) {
   }

   public void onPerceptionEnd(byte type, int timestamp) {
   }
 }

} </source>

Close and Save the file.

Test.java

This file was created when you created the new Project. Add the following (or replace the existing) code:

<source lang="Java"> package client;

import marauroa.common.game.RPObject;

public class Test {

 @SuppressWarnings("SleepWhileInLoop")
 public static void main(String[] args) {
   boolean cond = true;
   Client my = Client.get();
   try {
     my.connect("localhost", 5555);
     if (args.length == 3) {
       my.createAccount(args[0], args[1], args[2]);
     }
     my.login(args[0], args[1]);
     if (my.GetAvailableCharacters().length == 0) {
       RPObject character = new RPObject();
       my.createCharacter(args[0], character);
     }
     my.chooseCharacter(args[0]);
   } catch (Exception e) {
     cond = false;
   }
   int i = 0;
   while (cond) {
     ++i;
     my.loop(0);
     if (i % 100 == 50) {
       my.SendMessage("test" + i);
     }
     String s = my.popQuote();
     while (s != null) {
       System.out.println(s);
       s = my.popQuote();
     }
     try {
       Thread.sleep(100);
     } catch (InterruptedException e) {
       cond = false;
     }
   }
 }

} </source>

Close and Save the file.

Build the Project

If there are no errors in the package, select Run, Clean and Build Project from the NetBeans menu bar. This will create client.jar in the dist directory.

Completion

The files in the dist directory are the ones you will need for your game.