Gridarta Editor
ArchetypeType.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.archetypetype;
21 
22 import java.util.Iterator;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
35 public class ArchetypeType implements Iterable<ArchetypeAttributeSection> {
36 
40  @NotNull
41  private final String typeName;
42 
46  private final int typeNo;
47 
51  @NotNull
52  private final String display;
53 
57  private final boolean map;
58 
63  @Nullable
64  private final int[] inv;
65 
70  private final boolean allowsAllInv;
71 
75  @Nullable
76  private final String description;
77 
81  @Nullable
82  private final String use;
83 
88  @NotNull
90 
95  @NotNull
97 
112  public ArchetypeType(@NotNull final String typeName, final int typeNo, @NotNull final String display, final boolean map, @Nullable final int[] inv, final boolean allowsAllInv, @Nullable final String description, @Nullable final String use, @NotNull final ArchetypeAttributesDefinition typeAttributes) {
113  this.typeName = typeName;
114  this.typeNo = typeNo;
115  this.display = display;
116  this.map = map;
117  this.inv = inv == null ? null : inv.clone();
118  this.allowsAllInv = allowsAllInv;
119  this.description = description;
120  this.use = use;
121  this.typeAttributes = typeAttributes;
122  }
123 
129  public void addAttributeSection(@NotNull final ArchetypeAttributeSection newArchetypeAttributeSection) {
130  archetypeAttributeSections.addSection(newArchetypeAttributeSection);
131  }
132 
133  @NotNull
134  @Override
135  public Iterator<ArchetypeAttributeSection> iterator() {
136  return archetypeAttributeSections.iterator();
137  }
138 
144  public boolean hasAttribute() {
145  return archetypeAttributeSections.hasAttribute();
146  }
147 
152  public int getTypeNo() {
153  return typeNo;
154  }
155 
160  @NotNull
161  public String getTypeName() {
162  return typeName;
163  }
164 
169  public boolean isMap() {
170  return map;
171  }
172 
179  @Nullable
180  public int[] getInv() {
181  return inv == null ? null : inv.clone();
182  }
183 
189  public boolean isAllowsAllInv() {
190  return allowsAllInv;
191  }
192 
197  @NotNull
198  @SuppressWarnings("NullableProblems")
199  public String getDescription() {
200  final String result = description;
201  return result == null ? "" : result.trim();
202  }
203 
208  @NotNull
209  @SuppressWarnings("NullableProblems")
210  public String getUse() {
211  final String result = use;
212  return result == null ? "" : result.trim();
213  }
214 
220  public boolean matches(@NotNull final BaseObject<?, ?, ?, ?> baseObject) {
221  return typeNo == baseObject.getTypeNo() && typeAttributes.matches(baseObject);
222  }
223 
228  public boolean hasTypeAttributes() {
229  return !typeAttributes.isEmpty();
230  }
231 
237  public boolean hasAttributeKey(@NotNull final Comparable<String> key) {
239  }
240 
241  @NotNull
242  @Override
243  public String toString() {
244  final StringBuilder sb = new StringBuilder();
245  toString(sb);
246  return sb.toString();
247  }
248 
254  public void toString(@NotNull final StringBuilder sb) {
255  sb.append("type=");
256  sb.append(typeNo);
257  sb.append(",name=");
258  sb.append(typeName);
259  sb.append("\n");
260  for (final ArchetypeAttributeDefinition typeAttribute : typeAttributes) {
261  sb.append(" type:");
262  sb.append(typeAttribute);
263  sb.append("\n");
264  }
265  for (final ArchetypeAttributeSection archetypeAttributeSection : archetypeAttributeSections) {
266  sb.append(" section:");
267  sb.append(archetypeAttributeSection.getSectionName());
268  sb.append('\n');
269  for (final ArchetypeAttribute archetypeAttribute : archetypeAttributeSection) {
270  sb.append(" attr:");
271  sb.append(archetypeAttribute);
272  sb.append('\n');
273  }
274  }
275  }
276 
282  @NotNull
283  public String getDisplayName(@NotNull final Attributes baseObject) {
284  final StringBuilder sb = new StringBuilder();
285  sb.append(typeName);
286  sb.append(" (");
287  sb.append(typeNo);
288  sb.append(')');
289 
290  if (!display.isEmpty()) {
291  sb.append(" [");
292  int start = 0;
293  while (start < display.length()) {
294  final int end = display.indexOf("${", start);
295  if (end == -1) {
296  break;
297  }
298 
299  final int end2 = display.indexOf('}', end + 2);
300  if (end2 == -1) {
301  // ignore syntax error
302  break;
303  }
304 
305  sb.append(display.substring(start, end));
306  final String spec = display.substring(end + 2, end2);
307  final int question = spec.indexOf('?');
308  final String value;
309  if (question == -1) {
310  value = baseObject.getAttributeString(spec);
311  } else {
312  final int colon = spec.indexOf(':', question + 1);
313  final String attributeValue = baseObject.getAttributeString(spec.substring(0, question));
314  final boolean attributeExists = !attributeValue.isEmpty() && !attributeValue.equals("0");
315  if (attributeExists) {
316  value = spec.substring(question + 1, colon == -1 ? spec.length() : colon);
317  } else {
318  value = colon == -1 ? "" : spec.substring(colon + 1);
319  }
320  }
321  sb.append(value);
322  start = end2 + 1;
323  }
324  sb.append(display.substring(start));
325  sb.append(']');
326  }
327 
328  return sb.toString();
329  }
330 
336  public void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute) {
337  archetypeAttributeSections.addArchetypeAttribute(sectionName, archetypeAttribute);
338  }
339 
340 }
final String typeName
The type name (artificial).
final String display
Additional description text.
int [] getInv()
Returns which archetype types allow this archetype type in their inventories.
boolean hasAttributeKey(@NotNull final Comparable< String > key)
Returns whether an attribute key is defined.
final boolean allowsAllInv
Whether this archetype type allows any inventory game objects, whether these types have "inv" specifi...
boolean hasAttribute()
Returns whether this archetype type defines at least one archetype attribute.
boolean hasAttribute()
Returns whether this archetype type defines at least one archetype attribute.
This Class contains the data of one archetype attribute.
void toString(@NotNull final StringBuilder sb)
Appends a string representation of this instance to a StringBuilder.
boolean hasTypeAttributes()
Returns whether any type attributes are defined.
String getDisplayName(@NotNull final Attributes baseObject)
Returns a description of this type.
boolean isMap()
Returns whether this archetype is allowed on maps.
Contains the data of one Gridarta Object-Type.
final boolean map
Whether this archetype type is allowed on maps.
boolean isEmpty()
Returns whether no attribute definitions exist.
ArchetypeType(@NotNull final String typeName, final int typeNo, @NotNull final String display, final boolean map, @Nullable final int[] inv, final boolean allowsAllInv, @Nullable final String description, @Nullable final String use, @NotNull final ArchetypeAttributesDefinition typeAttributes)
Creates a new instance.
final ArchetypeAttributesDefinition typeAttributes
The list of additional attributes that an object must have in order to be of this type...
Base package of all Gridarta classes.
void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute)
Adds an ArchetypeAttribute to this archetype type.
final int [] inv
Which archetype types allow this archetype type in their inventories or.
final ArchetypeAttributeSections archetypeAttributeSections
All ArchetypeAttributeSections in display order.
String getDescription()
Returns the description.
Holds the key/value pair of an archetype attribute definition.
Iterator< ArchetypeAttributeSection > iterator()
Iterator< ArchetypeAttributeSection > iterator()
Returns a read-only Iterator returning all sections in display order.
boolean hasAttributeKey(@NotNull final Comparable< String > key)
Returns whether an attribute key is defined.
final void addSection(@NotNull final ArchetypeAttributeSection section)
Adds an ArchetypeAttributeSection.
void addAttributeSection(@NotNull final ArchetypeAttributeSection newArchetypeAttributeSection)
Adds an ArchetypeAttributeSection.
boolean matches(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Checks whether a BaseObject matches all type attributes.
void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute)
Adds an ArchetypeAttribute to this archetype type.
boolean isAllowsAllInv()
Returns whether this archetype type allows any inventory game objects, regardless whether these types...
boolean matches(@NotNull final Attributes baseObject)
Checks whether an Attributes instance matches all type attributes.
String getTypeName()
Returns the type name (artificial).
String getUse()
Returns the usage notes.