📄 mudclient.java
字号:
package mud;import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;import java.io.*;import java.util.*;import mud.Mud.*;public class MudClient { public static void main(String[] args){ try{ String hostname = args[0]; String mudname = args[1]; String placename = null; if(args.length>2) placename = args[2]; RemoteMudServer server = (RemoteMudServer)Naming.lookup( "rmi://"+hostname+"/"+Mud.mudPrefix+mudname ); RemoteMudPlace location =null; if(placename ==null) location = server.getEntrance(); else location = (RemoteMudPlace)server.getNamedPlace(placename); System.out.println("welcome to "+mudname); String name = getLine("Enter your name : "); String description = getMultiLine("Please describe what" + "People see when they look at you : "); PrintWriter myout = new PrintWriter(System.out); MudPerson me = new MudPerson(name,description,myout);// int pri=Thread.currentThread().getPriority();// Thread.currentThread().setPriority(-1); runMud(location ,me); } catch(Exception e){ System.out.println(e); System.out.println("Usage: java MudClient <host> <mud> [<place>] "); System.exit(1); } } public static void runMud(RemoteMudPlace entrance,MudPerson me) throws RemoteException { RemoteMudPlace location = entrance; String myname = me.getName(); String placename = null; String mudname = null; try{ location.enter(me,myname,myname+" has entered the MUD."); mudname = location.getServer().getMudName(); placename = location.getPlaceName(); look(location); } catch(Exception e){ System.out.println(e); System.exit(1); } for(;;){ try{ try{ Thread.sleep(200); } catch( InterruptedException e ){} String line = getLine(mudname + "."+placename+">"); String cmd,arg; int i = line.indexOf( ' '); if(i==-1) {cmd=line; arg=null;} else{ cmd=line.substring(0,i).toLowerCase(); arg = line.substring(i+1); } if (arg == null) arg = ""; if(cmd.equals("look")) look(location); else if(cmd.equals("examine")) System.out.println(location.examineThing(arg)); else if(cmd.equals("describe")){ try{ RemoteMudPerson p = location.getPerson(arg); System.out.println(p.getDescription()); } catch(RemoteException e){ System.out.println(arg + " is having technical difficulties.No description is available."); } } else if(cmd.equals("go")){ location = location.go(me,arg); mudname = location.getServer().getMudName(); placename = location.getPlaceName(); look(location); } else if(cmd.equals("say")){ location.speak(me,arg); } else if(cmd.equals("do")){ location.act(me,arg); } else if(cmd.equals("talk")){ try{ RemoteMudPerson p = location.getPerson(arg); String msg = getLine("What do you want to Sya?:"); p.tell(myname + "Says \""+msg+"\""); }catch(RemoteException e){ System.out.println(arg + " is having technical difficuties .Can't talk to them"); } } else if(cmd.equals("change")){ me.setDescription(getMultiLine("Describe yourself for others:")); } else if(cmd.equals("create")){ if(arg.length() == 0) throw new IllegalArgumentException("name excepted"); String desc = getMultiLine("Please describe the " + arg +":"); location.createThing(me,arg,desc); } else if(cmd.equals("destroy")){ location.destoryThing(me,arg); } else if(cmd.equals("open")){ if(arg.length() == 0) throw new IllegalArgumentException("direction excepted"); String name = getLine("what is the name of place there?: "); String back = getLine("What is the direction from " + "there back to here?: "); String desc = getMultiLine("Please describe " +name + ":"); location.createPlace(me,arg,back,name,desc); } else if(cmd.equals("close")){ if(arg.length() == 0) throw new IllegalArgumentException("derection excepted"); location.close(me,arg); } else if(cmd.equals("link")){ if(arg.length() == 0) throw new IllegalArgumentException("derection excepted"); String host = getLine("What host are you linking to?: "); String mud = getLine("What is the name of the MUD ont that host?: "); String place = getLine("What is the place name int that MUD?: "); location.linkTo(me,arg,host,mud,place); System.out.println("Don't forget to make a link from there back to here"); } else if(cmd.equals("dump")){ if(arg.length() == 0) throw new IllegalArgumentException("filename excepted"); String password = getLine("Password:"); location.getServer().dump(password,arg); } else if(cmd.equals("quit")){ try{location.exit(me,myname+ " has quit.");}catch(Exception e){} System.out.println("Bye"); System.out.flush(); System .exit(0); } else if(cmd.equals("help")){ System.out.println(help); } else { System.out.println("Unknown command. try help."); } } catch(MudException e){ if( e instanceof NoSuchThing) System.out.println("There isn't any such thing here." ); else if(e instanceof NoSuchPerson) System.out.println("There isn't anyon by that name here."); else if(e instanceof NoSuchExit) System.out.println("There isn't an exit in that direction."); else if(e instanceof NoSuchPlace) System.out.println("There isn't any such place."); else if(e instanceof ExitAlreadyExists) System.out.println("There is already an exit in that direction"); else if(e instanceof PlaceAlreadyExists) System.out.println("There is already a place with that name"); else if(e instanceof LinkFailed) System.out.println("That exit is not functioning..."); else if(e instanceof BadPassword) System.out.println("Invalid Password"); else if(e instanceof NotThere) System.out.println("You can't do that when you're not there"); else if(e instanceof AlreadyThere) System.out.println("You can't go there; you're already there."); } catch(RemoteException e){ System.out.println("The MUD is having technical difficulties"); System.out.println("Perhaps the server has crashed."); System.out.println("Try using the help command."); } catch(Exception e){ System.out.println("Syntax or other error:"); System.out.println(e); System.out.println("Try using the help command."); } } } public static void look(RemoteMudPlace location) throws RemoteException,MudException{ String mudname = location.getServer().getMudName(); String placename = location.getPlaceName(); String description = location.getDescription(); Vector things = location.getThings(); Vector names = location.getNames(); Vector exits = location.getExits(); System.out.println("you are in: " + placename + " of the Mud : " +mudname); System.out.println(description); System.out.print("Things here: "); for(int i=0;i<things.size();i++){ if(i>0) System.out.print(","); System.out.print(things.elementAt(i)); } System.out.println("\nPeople here: "); for(int i=0;i<names.size();i++){ if(i>0) System.out.print(","); System.out.print(names.elementAt(i)); } System.out.println("\nExits here: "); for(int i=0;i<exits.size();i++){ if(i>0) System.out.print(","); System.out.print(exits.elementAt(i)); } System.out.println(); System.out.flush(); } static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static String getLine(String prompt){ String line = null; do{ try{ System.out.print(prompt); System.out.flush(); line = in.readLine(); if(line != null) line = line.trim(); } catch(Exception e) {} } while ((line == null) || (line.length() == 0 )); return line; } public static String getMultiLine(String prompt){ String text = ""; for(;;){ try{ BufferedReader br = in; System.out.println(prompt); System.out.println("You can enter multiple lines. " + "End with a '.' on a line by itself.\n" + "Or enter a '<<' followed by a filename"); System.out.flush(); String line; while ((line = br.readLine()) != null) { if(line.equals(".")) break; if(line.trim().startsWith("<<")) { String filename = line.trim().substring(2).trim(); br = new BufferedReader(new FileReader(filename)); continue; } else { text += line + "\n"; } if(text != null) return text; else System.out.println("Please enter at least one line."); } } catch(Exception e){} } }static final String help = "Commands are:\n" + "look: Look around\n" + "examine <thing>: examine the named thing in more detail\n" + "describe <person>: describe the named person\n" + "go <direction>: go in the named direction (i.e. a named exit)\n" + "say <message>: say something to everyone\n" + "do <message>: tell everyone that you are doing something\n" + "talk <person>: talk to one person. Will prompt for message\n" + "change: change how you are described. Will prompt for input\n" + "create <thing>: create a new thing. Prompts for description \n" + "destroy <thing>: destroy a thing.\n" + "open <direction>: create an adjoining place. Prompts for input\n"+ "close <direction>: close an exit from this place.\n" + "link <direction>: create an exit to an existing place,\n" + " perhaps on another server. Will prompt for input.\n" + "dump <filename>: save server state. Prompts for password\n" + "quit: leave the Mud\n" + "help: display this message";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -