|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
WorldManager.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Server - Virtual Reality Object Oriented MUD Server
|
|
4 |
Copyright (C) 2001 - 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.server.data;
|
|
28 |
|
|
29 |
import java.io.File;
|
|
30 |
import java.util.HashMap;
|
|
31 |
import java.util.Iterator;
|
|
32 |
import java.util.Map;
|
|
33 |
import java.util.TreeSet;
|
|
34 |
|
|
35 |
import org.vrmoo.common.util.FileUtility;
|
|
36 |
|
|
37 |
import org.vrmoo.server.world.World;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Provides functionality for managing the set of worlds that the server is
|
|
41 |
* currently dealing with.
|
|
42 |
*
|
|
43 |
* @author Jeff Weston
|
|
44 |
*/
|
|
45 |
public class WorldManager |
|
46 |
{ |
|
47 |
/**
|
|
48 |
* Hold a record of all worlds managed by this server.
|
|
49 |
*/
|
|
50 |
private static Map worlds = new HashMap( ); |
|
51 |
|
|
52 |
/**
|
|
53 |
* This class is not meant to be instantiated, so keep the default
|
|
54 |
* constructor private.
|
|
55 |
*/
|
|
56 | 0 |
private WorldManager( )
|
57 |
{ |
|
58 |
} |
|
59 |
|
|
60 |
/**
|
|
61 |
* Called by the <code>World</code> constructor when a new world has been
|
|
62 |
* created on the server.
|
|
63 |
*
|
|
64 |
* @param world the newly constructed <code>World</code>
|
|
65 |
*/
|
|
66 | 0 |
public static synchronized void addWorld( World world ) |
67 |
{ |
|
68 | 0 |
if ( ! worlds.containsKey( world.getName( ) ) )
|
69 |
{ |
|
70 | 0 |
worlds.put( world.getName( ), world ); |
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
/**
|
|
75 |
* Get the <code>World</code> associated with the passed in world name.
|
|
76 |
*
|
|
77 |
* @param worldName the name of the <code>World</code> to get
|
|
78 |
*
|
|
79 |
* @return the <code>World</code> matching the given world name
|
|
80 |
*/
|
|
81 | 0 |
public static synchronized World getWorld( String worldName ) |
82 |
{ |
|
83 | 0 |
return ( World ) worlds.get( worldName );
|
84 |
} |
|
85 |
|
|
86 |
/**
|
|
87 |
* Get the name of all of the worlds available from this class.
|
|
88 |
*
|
|
89 |
* @return the array of world names
|
|
90 |
*/
|
|
91 | 0 |
public static synchronized String[ ] getWorldNames( ) |
92 |
{ |
|
93 | 0 |
String[ ] names = new String[ worlds.size( ) ];
|
94 | 0 |
Iterator iter = new TreeSet( worlds.keySet( ) ).iterator( );
|
95 |
|
|
96 | 0 |
int i = 0;
|
97 | 0 |
while ( iter.hasNext( ) )
|
98 |
{ |
|
99 | 0 |
names[ i++ ] = ( String ) iter.next( ); |
100 |
} |
|
101 | 0 |
return names;
|
102 |
} |
|
103 |
|
|
104 |
/**
|
|
105 |
* Iterate through all of the worlds and have them save their data.
|
|
106 |
*/
|
|
107 | 0 |
static synchronized void saveData( ) |
108 |
{ |
|
109 | 0 |
String[ ] worldNames = getWorldNames( ); |
110 |
|
|
111 | 0 |
for ( int i = 0; i < worldNames.length; i++ ) |
112 |
{ |
|
113 | 0 |
getWorld( worldNames[ i ] ).saveWorldData( ); |
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Initialize the world manager. Look for all worlds that are within the
|
|
119 |
* data directory and load them.
|
|
120 |
*/
|
|
121 | 0 |
static synchronized void init( ) |
122 |
{ |
|
123 | 0 |
System.out.println( "Initializing World Manager" );
|
124 | 0 |
try
|
125 |
{ |
|
126 | 0 |
File worldsDir = new File( "data/worlds" ); |
127 | 0 |
FileUtility.ensureDirectoryExists( worldsDir ); |
128 |
|
|
129 | 0 |
File[ ] files = worldsDir.listFiles( ); |
130 | 0 |
for ( int i = 0; i < files.length; i++ ) |
131 |
{ |
|
132 | 0 |
if ( files[ i ].isDirectory( ) )
|
133 |
{ |
|
134 | 0 |
addWorld( new World( files[ i ].getName( ) ) );
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
catch ( Exception e )
|
|
139 |
{ |
|
140 | 0 |
e.printStackTrace( ); |
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|