|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ItemData.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Common - Virtual Reality Object Oriented MUD Common Code
|
|
4 |
Copyright (C) 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.common.data;
|
|
28 |
|
|
29 |
import org.vrmoo.common.exception.VRMooException;
|
|
30 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Abstract class that contains the common functionality for dealing with a
|
|
34 |
* single item that will be placed into an array of items.
|
|
35 |
*
|
|
36 |
* @author Jeff Weston
|
|
37 |
*/
|
|
38 |
public abstract class ItemData |
|
39 |
{ |
|
40 |
/**
|
|
41 |
* Contains the vertices the make up this item.
|
|
42 |
*/
|
|
43 |
private final VertexData[ ] vertices = new VertexData[ getVertexCount( ) ]; |
|
44 |
|
|
45 |
/**
|
|
46 |
* Contains a reference to the appearance object for this item.
|
|
47 |
*/
|
|
48 |
private String appearanceReference;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Construct the item using an array of vertices and a reference to the
|
|
52 |
* appearance object for the item.
|
|
53 |
*
|
|
54 |
* @param vertices the vertices of the item
|
|
55 |
* @param appearanceReference a reference to the appearance object for the
|
|
56 |
* item
|
|
57 |
*
|
|
58 |
* @throws VRMooException if the correct number of vertices aren't passed in
|
|
59 |
*/
|
|
60 | 0 |
public ItemData( VertexData[ ] vertices, String appearanceReference )
|
61 |
throws VRMooException
|
|
62 |
{ |
|
63 | 0 |
if ( vertices.length != getVertexCount( ) )
|
64 |
{ |
|
65 | 0 |
throw new VRMooException( "Invalid vertex count. Expected " + |
66 |
getVertexCount( ) + ", but got " +
|
|
67 |
vertices.length + "." );
|
|
68 |
} |
|
69 |
|
|
70 | 0 |
for ( int i = 0; i < getVertexCount( ); i++ ) |
71 |
{ |
|
72 | 0 |
this.vertices[ i ] = vertices[ i ];
|
73 |
} |
|
74 |
|
|
75 | 0 |
this.appearanceReference = appearanceReference;
|
76 |
} |
|
77 |
|
|
78 |
/**
|
|
79 |
* Construct the item using text representing the encoded vertices.
|
|
80 |
*
|
|
81 |
* @param data the encoded vertices
|
|
82 |
*
|
|
83 |
* @throws VRMooParseException for any parsing errors that occur
|
|
84 |
*/
|
|
85 | 0 |
public ItemData( String data ) throws VRMooParseException |
86 |
{ |
|
87 | 0 |
int idStart = data.indexOf( "\"" ); |
88 | 0 |
int idEnd = data.indexOf( "\"", idStart + 1 ); |
89 |
|
|
90 | 0 |
if ( ( idStart != -1 ) &&
|
91 |
( idEnd != -1 ) ) |
|
92 |
{ |
|
93 | 0 |
appearanceReference = data.substring( idStart + 1, idEnd ); |
94 |
} |
|
95 |
else
|
|
96 |
{ |
|
97 | 0 |
throw new VRMooParseException( "Unable to parse appearance ID." ); |
98 |
} |
|
99 |
|
|
100 | 0 |
data = data.trim( ); |
101 | 0 |
int vertexCount = 0;
|
102 | 0 |
int vertexEnd = data.indexOf( ")" ); |
103 | 0 |
while ( ( data.indexOf( "(" ) == 0 ) && ( vertexEnd != -1 ) ) |
104 |
{ |
|
105 | 0 |
if ( vertexCount > getVertexCount( ) )
|
106 |
{ |
|
107 | 0 |
throw new VRMooParseException( "Too many vertices." ); |
108 |
} |
|
109 |
|
|
110 | 0 |
String vertex = data.substring( 1, vertexEnd ); |
111 | 0 |
int nextIndex = data.indexOf( "(", vertexEnd ); |
112 | 0 |
if ( nextIndex != -1 )
|
113 |
{ |
|
114 | 0 |
data = data.substring( nextIndex ).trim( ); |
115 |
} |
|
116 |
else
|
|
117 |
{ |
|
118 | 0 |
data = "";
|
119 |
} |
|
120 | 0 |
vertexEnd = data.indexOf( ")" );
|
121 |
|
|
122 | 0 |
vertices[ vertexCount++ ] = new VertexData( vertex );
|
123 |
} |
|
124 | 0 |
if ( vertexCount < getVertexCount( ) )
|
125 |
{ |
|
126 | 0 |
throw new VRMooParseException( "Too few vertices." ); |
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/**
|
|
131 |
* Get the vertices that define this item.
|
|
132 |
*
|
|
133 |
* @return the vertices that define this item
|
|
134 |
*/
|
|
135 | 0 |
public VertexData[ ] getVertices( )
|
136 |
{ |
|
137 | 0 |
return vertices;
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Get the reference to the appearance object for this item.
|
|
142 |
*
|
|
143 |
* @return the reference to the appearance object for this item
|
|
144 |
*/
|
|
145 | 0 |
public String getAppearanceReference( )
|
146 |
{ |
|
147 | 0 |
return appearanceReference;
|
148 |
} |
|
149 |
|
|
150 |
/**
|
|
151 |
* Convert this item to its encoded <code>String</code> representation.
|
|
152 |
*
|
|
153 |
* @return the encoded <code>String</code>
|
|
154 |
*/
|
|
155 | 0 |
public String toEncodedString( )
|
156 |
{ |
|
157 | 0 |
StringBuffer result = new StringBuffer( );
|
158 |
|
|
159 | 0 |
for ( int i = 0; i < getVertexCount( ); i++ ) |
160 |
{ |
|
161 | 0 |
VertexData vertexData = vertices[ i ]; |
162 | 0 |
if ( i != 0 )
|
163 |
{ |
|
164 | 0 |
result.append( ", " );
|
165 |
} |
|
166 | 0 |
result.append( vertexData.toEncodedString( ) ); |
167 |
} |
|
168 | 0 |
result.append( ", \"" );
|
169 | 0 |
result.append( appearanceReference ); |
170 | 0 |
result.append( "\"" );
|
171 | 0 |
return result.toString( );
|
172 |
} |
|
173 |
|
|
174 |
/**
|
|
175 |
* Abstract method that subclasses must implement telling us how many
|
|
176 |
* vertices form this item.
|
|
177 |
*
|
|
178 |
* @return the number of vertices that form this item
|
|
179 |
*/
|
|
180 |
protected abstract int getVertexCount( ); |
|
181 |
} |
|
182 |
|
|