📄 mudclient.java
字号:
name + ":"); location.createPlace(me, arg, back, name, desc); } // CLOSE: Close a named exit. Note: only closes an exit // uni-directionally, and does not destroy a place. else if (cmd.equals("close")) { if (arg.length() == 0) throw new IllegalArgumentException("direction expected"); location.close(me, arg); } // LINK: Create a new exit that connects to an existing place // that may be in another MUD running on another host else if (cmd.equals("link")) { if (arg.length() == 0) throw new IllegalArgumentException("direction expected"); String host = getLine("What host are you linking to?: "); String mud = getLine("What is the name of the MUD on that host?: "); String place = getLine("What is the place name in that MUD?: "); location.linkTo(me, arg, host, mud, place); System.out.println("Don't forget to make a link from " + "there back to here!"); } // DUMP: Save the state of this MUD into the named file, // if the password is correct else if (cmd.equals("dump")) { if (arg.length() == 0) throw new IllegalArgumentException("filename expected"); String password = getLine("Password: "); location.getServer().dump(password, arg); } // QUIT: Quit the game 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); } // HELP: Print out a big help message else if (cmd.equals("help")) System.out.println(help); // Otherwise, this is an unrecognized command. else System.out.println("Unknown command. Try 'help'."); } // Handle the many possible types of MudException 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 anyone 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) // Shouldn't happen System.out.println("You can't do that when " + "you're not there."); else if (e instanceof AlreadyThere) // Shouldn't happen System.out.println("You can't go there; " + "you're already there."); } // Handle RMI exceptions catch (RemoteException e) { System.out.println("The MUD is having technical difficulties."); System.out.println("Perhaps the server has crashed:"); System.out.println(e); } // Handle everything else that could go wrong. catch (Exception e) { System.out.println("Syntax or other error:"); System.out.println(e); System.out.println("Try using the 'help' command."); } } } /** * This convenience method is used in several places in the * runMud() method above. It displays the name and description of * the current place (including the name of the mud the place is in), * and also displays the list of things, people, and exits in * the current place. **/ public static void look(RemoteMudPlace p) throws RemoteException, MudException { String mudname = p.getServer().getMudName(); // Mud name String placename = p.getPlaceName(); // Place name String description = p.getDescription(); // Place description Vector things = p.getThings(); // List of things here Vector names = p.getNames(); // List of people here Vector exits = p.getExits(); // List of exits from here // Print it all out 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++) { // Display list of things if (i > 0) System.out.print(", "); System.out.print(things.elementAt(i)); } System.out.print("\nPeople here: "); for(int i = 0; i < names.size(); i++) { // Display list of people if (i > 0) System.out.print(", "); System.out.print(names.elementAt(i)); } System.out.print("\nExits are: "); for(int i = 0; i < exits.size(); i++) { // Display list of exits if (i > 0) System.out.print(", "); System.out.print(exits.elementAt(i)); } System.out.println(); // Blank line System.out.flush(); // Make it appear now! } /** This static input stream reads lines from the console */ static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); /** * A convenience method for prompting the user and getting a line of * input. It guarantees that the line is not empty and strips off * whitespace at the beginning and end of the line. **/ public static String getLine(String prompt) { String line = null; do { // Loop until a non-empty line is entered try { System.out.print(prompt); // Display prompt System.out.flush(); // Display it right away line = in.readLine(); // Get a line of input if (line != null) line = line.trim(); // Strip off whitespace } catch (Exception e) {} // Ignore any errors } while((line == null) || (line.length() == 0)); return line; } /** * A convenience method for getting multi-line input from the user. * It prompts for the input, displays instructions, and guarantees that * the input is not empty. It also allows the user to enter the name of * a file from which text will be read. **/ public static String getMultiLine(String prompt) { String text = ""; for(;;) { // We'll break out of this loop when we get non-empty input try { BufferedReader br = in; // The stream to read from System.out.println(prompt); // Display the prompt // Display some instructions System.out.println("You can enter multiple lines. " + "End with a '.' on a line by itself.\n" + "Or enter a '<<' followed by a filename"); // Make the prompt and instructions appear now. System.out.flush(); // Read lines String line; while((line = br.readLine()) != null) { // Until EOF if (line.equals(".")) break; // Or until a dot by itself // Or, if a file is specified, start reading from it // instead of from the console. if (line.trim().startsWith("<<")) { String filename = line.trim().substring(2).trim(); br = new BufferedReader(new FileReader(filename)); continue; // Don't count the << as part of the input } // Add the line to the collected input else text += line + "\n"; } // If we got at least one line, return it. Otherwise, chastise // the user and go back to the prompt and the instructions. if (text.length() > 0) return text; else System.out.println("Please enter at least one line."); } // If there were errors, for example an IO error reading a file, // display the error and loop again, displaying prompt and // instructions catch(Exception e) { System.out.println(e); } } } /** This is the usage string that explains the available commands */ 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 + -