Client.java
Client.java · 4.8 KB
From the archive palrichclient.zip
Download the archive « Back to the archive contents
/*
Java Mud Client -- Client module
by Michael Barton <red@smileyface.com>
Originally written for Divine Blood, telnet://shell.tetradine.com:4000
Permission is granted to modify and distribute freely.
Credit/links are appreciated, but not necessary.
Bugs and other issues can be noted to red@smileyface.com.
*/
import java.util.StringTokenizer;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Client extends Frame implements Runnable, ActionListener,
KeyListener, WindowListener, FocusListener
{
private Thread runner = null;
TextField chatLine = null;
StringArray history = null;
MudConn connection = null;
Screen myScreen = null;
String host = "";
int port = 0;
String ansiToken = "";
ClientMenu mBar = null;
boolean stacking = true;
boolean clear = true;
public Client(String h, int p)
{
setTitle("Java Mud Client");
history = new StringArray(100);
host = h;
port = p;
connection = new MudConn(host, port);
setLayout(new BorderLayout());
addFocusListener(this);
myScreen = new Screen();
myScreen.setFont(new Font("Monospaced", Font.PLAIN, 14));
myScreen.addFocusListener(this);
chatLine = new TextField("", 20);
chatLine.setFont(new Font("Monospaced", Font.PLAIN, 14));
chatLine.addActionListener(this);
chatLine.addKeyListener(this);
addWindowListener(this);
mBar = new ClientMenu(this);
setMenuBar(mBar);
add("Center", myScreen);
add("South", chatLine);
}
public void start()
{
mBar.connect.setEnabled(false);
mBar.disconnect.setEnabled(true);
runner = new Thread(this);
runner.start();
}
public void stop()
{
mBar.connect.setEnabled(true);
mBar.disconnect.setEnabled(false);
print("\n%% Disconnected from host %%\n");
connection.close();
if (runner != null) runner.stop();
runner = null;
}
public void run()
{
print("\033[1;40m\033[0;31mPalrich's\033[0;37m Java MUD client.\033[0m\n");
print("Connecting to "+host+", port "+port+".\n");
String tempstr = "";
try
{
connection.connect();
while((tempstr = connection.read()) != null)
processInput(tempstr);
}
catch(Exception e) {}
finally {stop();}
}
public final void processInput(String str)
{
int i = 0, j = 0;
byte characters[] = str.getBytes();
for (i = 0; i < characters.length; i++)
{
if (characters[i] == (byte)0xff)
i+=2;
else
{
characters[j] = characters[i];
j++;
}
}
print(new String(characters, 0, j));
}
public final void print(String str)
{
myScreen.concat(str);
}
public void actionPerformed(ActionEvent evt)
{
String tempstr = "";
history.reset();
tempstr = chatLine.getText();
if (tempstr.length() > 0)
history.push(tempstr);
if (connection != null)
{
try
{
if (stacking)
{
StringTokenizer commands = new StringTokenizer(tempstr, ";");
if (commands.countTokens() == 0)
{
connection.send("");
print("\n");
}
while (commands.hasMoreTokens())
{
tempstr = commands.nextToken();
connection.send(tempstr);
print(tempstr+"\n");
}
}
else
{
connection.send(tempstr);
print(tempstr+"\n");
}
}
catch(Exception e)
{
stop();
}
}
chatLine.setSelectionStart(0);
chatLine.setSelectionEnd(chatLine.getText().length());
if (clear)
chatLine.setText("");
}
public final void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_DOWN:
chatLine.setText(history.last());
break;
case KeyEvent.VK_UP:
chatLine.setText(history.next());
break;
case KeyEvent.VK_PAGE_UP:
myScreen.pageUp();
break;
case KeyEvent.VK_PAGE_DOWN:
myScreen.pageDown();
break;
}
}
public final void windowClosing(WindowEvent e)
{
connection.close();
setVisible(false);
stop();
}
public final void focusGained(FocusEvent e)
{
chatLine.requestFocus();
}
public final void focusLost(FocusEvent e) {}
public final void windowActivated(WindowEvent e) {}
public final void windowDeactivated(WindowEvent e) {}
public final void windowDeiconified(WindowEvent e) {}
public final void windowOpened(WindowEvent e) {}
public final void windowClosed(WindowEvent e) {}
public final void windowIconified(WindowEvent e) {}
public final void keyTyped(KeyEvent e){}
public final void keyReleased(KeyEvent e) {}
}