⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mudplace.java

📁 Java实现简易Mud
💻 JAVA
字号:
package mud;import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;import java.io.*;import java.util.*;import mud.Mud.*;import java.rmi.server.UnicastRemoteObject;public class MudPlace extends UnicastRemoteObject  implements RemoteMudPlace,Serializable{  String placename,description ;                 //位置  Vector Exits = new Vector();  Vector destinations = new Vector();  Vector things = new Vector();  Vector descriptions = new Vector();  transient Vector names = new Vector();  transient Vector people =  new Vector();  MudServer server;  public MudPlace() throws RemoteException {    super();  }  public MudPlace(MudServer server,String placename,String desciription)      throws RemoteException,PlaceAlreadyExists{    this.server = server;    this.placename = placename;    this.description = desciription;    server.setPlaceName(this,placename);           //注册位置  }  public String getPlaceName() throws RemoteException{    return placename;  }  public String getDescription() throws RemoteException {    return description;  }  public Vector getNames() throws RemoteException{    return names;  }  public Vector getThings() throws RemoteException{    return things;  }  public Vector getExits() throws RemoteException{    return Exits;  }  public RemoteMudPerson getPerson(String name) throws RemoteException,NoSuchPerson{    synchronized(names){      int i= names.indexOf(name);      if(i==-1) throw new NoSuchPerson();      return (RemoteMudPerson)people.elementAt(i);    }  }  public String examineThing(String name)throws RemoteException,NoSuchThing{    synchronized(things){      int i= things.indexOf(name);      if(i==-1) throw new NoSuchThing();      return (String) descriptions.elementAt(i);    }  }  public RemoteMudPlace go(RemoteMudPerson who,String direction)      throws RemoteException,NotThere,AlreadyThere,NoSuchExit,LinkFailed{    Object destination;    synchronized(direction){      int i= Exits.indexOf(direction);      if(i==-1) throw new NoSuchExit();      destination = destinations.elementAt(i);    }    RemoteMudPlace newplace;    if(destination instanceof String){      try{        String t = (String)destination;        int pos = t.indexOf('@');        String url=t.substring(0, pos);        String placename = t.substring(pos+1);        RemoteMudServer s = (RemoteMudServer)Naming.lookup(url);        newplace = s.getNamedPlace(placename);      }      catch(Exception e){ throw new LinkFailed();}    } else      newplace = (RemoteMudPlace) destination;    String name = verifyPresence(who);    this.exit(who,name+" has gone" +direction);    String fromwhere;    if(newplace instanceof MudPlace)      fromwhere = placename;    else      fromwhere = server.getMudName()+"."+placename;    newplace.enter(who,name,name+" has arrived from "+fromwhere);    return newplace;  }  public void speak(RemoteMudPerson speaker,String msg) throws RemoteException,NotThere{    String name = verifyPresence(speaker);    tellEveryone(name+":"+msg);  }  public void act(RemoteMudPerson speaker,String msg) throws RemoteException,NotThere{    String name = verifyPresence(speaker);    tellEveryone(name+":"+msg);  }  public void createThing(RemoteMudPerson who,String name,String description)      throws RemoteException,NotThere,AlreadyThere{    String creatorname = verifyPresence(who);    synchronized(things){      if(things.indexOf(name)!=-1){        throw new AlreadyThere();      }      things.addElement(name);      descriptions.addElement(description);    }    tellEveryone(creatorname+ "has created a "+name);  }  public void destoryThing(RemoteMudPerson who,String thing)      throws RemoteException,NotThere,NoSuchThing{    String creatorname = verifyPresence(who);    synchronized(things){      if(things.indexOf(thing)!=-1){        throw new NoSuchThing();      }      things.removeElement(thing);      descriptions.removeElementAt(things.indexOf(thing));    }    tellEveryone(creatorname+ "has created a "+thing);  }  public void createPlace(RemoteMudPerson creator,String exit,String entrance,String name,String description)      throws RemoteException,NotThere,ExitAlreadyExists,PlaceAlreadyExists{    String creatorname = verifyPresence(creator);    synchronized(Exits){      if(Exits.indexOf(exit) != -1) throw new ExitAlreadyExists();      MudPlace destination = new MudPlace(server,name,description);      destination.Exits.addElement(entrance);      destination.destinations.addElement(destination);    }    tellEveryone(creatorname+" has created a new place:"+exit);  }  public void linkTo(RemoteMudPerson who,String exit,String hostname,String mudname,                     String placename)      throws RemoteException,NotThere,ExitAlreadyExists,NoSuchPlace{    String name = verifyPresence(who);    String url = "rmi://"+hostname+"/"+Mud.mudPrefix+mudname;    try{      RemoteMudServer s = (RemoteMudServer) Naming.lookup(url);      RemoteMudPlace destination = s.getNamedPlace(placename);    }    catch(Exception e){      throw new NoSuchPlace();    }    synchronized(Exits){      if(Exits.indexOf(exit)!=-1){        throw new ExitAlreadyExists();      }      Exits.addElement(exit);      destinations.addElement(url+"@"+placename);      tellEveryone(name+" has linked " +exit+" to ' " +placename+" ' in MUD"+mudname+"' host "+hostname);    }  }  public void close(RemoteMudPerson who,String exit)      throws RemoteException,NotThere,NoSuchExit{    String name = verifyPresence(who);    synchronized(Exits){      int i = Exits.indexOf(exit);      if(i==-1) throw new NoSuchExit();      Exits.removeElementAt(i);      destinations.removeElementAt(i);    }    tellEveryone(name+" has closed exit " + exit);  }  public void exit(RemoteMudPerson who,String message)      throws RemoteException,NotThere{    String name;    synchronized(names){      int i = people.indexOf(who);      if(i==-1) return ;      names.removeElementAt(i);      people.removeElementAt(i);    }    if(message!=null) tellEveryone(message);  }  public void enter(RemoteMudPerson who,String name,String message)      throws RemoteException,AlreadyThere{    if(message!=null) tellEveryone(message);    synchronized(names){      if(people.indexOf(who)!=-1) throw new AlreadyThere();      names.addElement(name);      people.addElement(who);    }  }  public RemoteMudServer getServer() throws RemoteException{    return server;  }  public String verifyPresence(RemoteMudPerson who) throws NotThere{    int i = people.indexOf(who);    if(i==-1) throw new NotThere();    else return (String )names.elementAt(i);  }  public void tellEveryone(final String message){    if(people.size() ==0) return ;    final Vector recipients = (Vector) people.clone();    new Thread(){      public void run(){        for(int i=0;i<recipients.size(); i++){          RemoteMudPerson person = (RemoteMudPerson)recipients.elementAt(i);          try{            person.tell(message);          } catch(RemoteException e){            try{              MudPlace.this.exit(person,null);            } catch(Exception ex) {}          }        }      }    }.start();  }  private void readObject(ObjectInputStream in)      throws IOException,ClassNotFoundException{   in.defaultReadObject();   names =new Vector();   people = new Vector();  }  static final long serialVersionUID = 5090967989223703026L;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -