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  private final int @Nullable [] inv;
64 
69  private final boolean allowsAllInv;
70 
74  @Nullable
75  private final String description;
76 
80  @Nullable
81  private final String use;
82 
87  @NotNull
89 
94  @NotNull
96 
111  public ArchetypeType(@NotNull final String typeName, final int typeNo, @NotNull final String display, final boolean map, final int @Nullable [] inv, final boolean allowsAllInv, @Nullable final String description, @Nullable final String use, @NotNull final ArchetypeAttributesDefinition typeAttributes) {
112  this.typeName = typeName;
113  this.typeNo = typeNo;
114  this.display = display;
115  this.map = map;
116  this.inv = inv == null ? null : inv.clone();
117  this.allowsAllInv = allowsAllInv;
118  this.description = description;
119  this.use = use;
120  this.typeAttributes = typeAttributes;
121  }
122 
128  public void addAttributeSection(@NotNull final ArchetypeAttributeSection newArchetypeAttributeSection) {
129  archetypeAttributeSections.addSection(newArchetypeAttributeSection);
130  }
131 
132  @NotNull
133  @Override
134  public Iterator<ArchetypeAttributeSection> iterator() {
136  }
137 
143  public boolean hasAttribute() {
145  }
146 
151  public int getTypeNo() {
152  return typeNo;
153  }
154 
159  @NotNull
160  public String getTypeName() {
161  return typeName;
162  }
163 
168  public boolean isMap() {
169  return map;
170  }
171 
178  public int @Nullable [] getInv() {
179  return inv == null ? null : inv.clone();
180  }
181 
187  public boolean isAllowsAllInv() {
188  return allowsAllInv;
189  }
190 
195  @NotNull
196  public String getDescription() {
197  final String result = description;
198  return result == null ? "" : result.trim();
199  }
200 
205  @NotNull
206  public String getUse() {
207  final String result = use;
208  return result == null ? "" : result.trim();
209  }
210 
216  public boolean matches(@NotNull final BaseObject<?, ?, ?, ?> baseObject) {
217  return typeNo == baseObject.getTypeNo() && typeAttributes.matches(baseObject);
218  }
219 
224  public boolean hasTypeAttributes() {
225  return !typeAttributes.isEmpty();
226  }
227 
233  public boolean hasAttributeKey(@NotNull final Comparable<String> key) {
235  }
236 
237  @NotNull
238  @Override
239  public String toString() {
240  final StringBuilder sb = new StringBuilder();
241  toString(sb);
242  return sb.toString();
243  }
244 
250  public void toString(@NotNull final StringBuilder sb) {
251  sb.append("type=");
252  sb.append(typeNo);
253  sb.append(",name=");
254  sb.append(typeName);
255  sb.append("\n");
256  for (final ArchetypeAttributeDefinition typeAttribute : typeAttributes) {
257  sb.append(" type:");
258  sb.append(typeAttribute);
259  sb.append("\n");
260  }
261  for (final ArchetypeAttributeSection archetypeAttributeSection : archetypeAttributeSections) {
262  sb.append(" section:");
263  sb.append(archetypeAttributeSection.getSectionName());
264  sb.append('\n');
265  for (final ArchetypeAttribute archetypeAttribute : archetypeAttributeSection) {
266  sb.append(" attr:");
267  sb.append(archetypeAttribute);
268  sb.append('\n');
269  }
270  }
271  }
272 
278  @NotNull
279  public String getDisplayName(@NotNull final Attributes baseObject) {
280  final StringBuilder sb = new StringBuilder();
281  sb.append(typeName);
282  sb.append(" (");
283  sb.append(typeNo);
284  sb.append(')');
285 
286  if (!display.isEmpty()) {
287  sb.append(" [");
288  int start = 0;
289  while (start < display.length()) {
290  final int end = display.indexOf("${", start);
291  if (end == -1) {
292  break;
293  }
294 
295  final int end2 = display.indexOf('}', end + 2);
296  if (end2 == -1) {
297  // ignore syntax error
298  break;
299  }
300 
301  sb.append(display, start, end);
302  final String spec = display.substring(end + 2, end2);
303  final int question = spec.indexOf('?');
304  final String value;
305  if (question == -1) {
306  value = baseObject.getAttributeString(spec);
307  } else {
308  final int colon = spec.indexOf(':', question + 1);
309  final String attributeValue = baseObject.getAttributeString(spec.substring(0, question));
310  final boolean attributeExists = !attributeValue.isEmpty() && !attributeValue.equals("0");
311  if (attributeExists) {
312  value = spec.substring(question + 1, colon == -1 ? spec.length() : colon);
313  } else {
314  value = colon == -1 ? "" : spec.substring(colon + 1);
315  }
316  }
317  sb.append(value);
318  start = end2 + 1;
319  }
320  sb.append(display.substring(start));
321  sb.append(']');
322  }
323 
324  return sb.toString();
325  }
326 
332  public void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute) {
333  archetypeAttributeSections.addArchetypeAttribute(sectionName, archetypeAttribute);
334  }
335 
336 }
net.sf.gridarta.model.archetypetype.ArchetypeType.getInv
int[] getInv()
Definition: ArchetypeType.java:178
net.sf.gridarta.model.archetypetype.ArchetypeType.matches
boolean matches(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Definition: ArchetypeType.java:216
net.sf.gridarta.model.archetypetype.ArchetypeType.archetypeAttributeSections
final ArchetypeAttributeSections archetypeAttributeSections
Definition: ArchetypeType.java:88
net.sf.gridarta.model.archetypetype.ArchetypeType.typeAttributes
final ArchetypeAttributesDefinition typeAttributes
Definition: ArchetypeType.java:95
net.sf.gridarta.model.archetypetype.ArchetypeType.hasAttribute
boolean hasAttribute()
Definition: ArchetypeType.java:143
net.sf.gridarta
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections.iterator
Iterator< ArchetypeAttributeSection > iterator()
Definition: ArchetypeAttributeSections.java:73
net.sf.gridarta.model.archetypetype.ArchetypeType.toString
void toString(@NotNull final StringBuilder sb)
Definition: ArchetypeType.java:250
net.sf.gridarta.model.archetypetype.ArchetypeType.getTypeName
String getTypeName()
Definition: ArchetypeType.java:160
net.sf.gridarta.model.archetypetype.ArchetypeType.getUse
String getUse()
Definition: ArchetypeType.java:206
net.sf
net.sf.gridarta.model.archetypetype.ArchetypeType.map
final boolean map
Definition: ArchetypeType.java:57
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections
Definition: ArchetypeAttributeSections.java:32
net.sf.gridarta.model.archetypetype.ArchetypeType.hasTypeAttributes
boolean hasTypeAttributes()
Definition: ArchetypeType.java:224
net.sf.gridarta.model.archetypetype.ArchetypeType.iterator
Iterator< ArchetypeAttributeSection > iterator()
Definition: ArchetypeType.java:134
net.sf.gridarta.model.archetypetype.ArchetypeType.isMap
boolean isMap()
Definition: ArchetypeType.java:168
net.sf.gridarta.model.archetypetype.ArchetypeType.allowsAllInv
final boolean allowsAllInv
Definition: ArchetypeType.java:69
net.sf.gridarta.model.archetypetype.ArchetypeType.display
final String display
Definition: ArchetypeType.java:52
net.sf.gridarta.model.baseobject.Attributes
Definition: Attributes.java:28
net
net.sf.gridarta.model.archetypetype.ArchetypeAttributesDefinition.matches
boolean matches(@NotNull final Attributes baseObject)
Definition: ArchetypeAttributesDefinition.java:56
net.sf.gridarta.model.archetypetype.ArchetypeType.hasAttributeKey
boolean hasAttributeKey(@NotNull final Comparable< String > key)
Definition: ArchetypeType.java:233
net.sf.gridarta.model.archetypetype.ArchetypeType.description
final String description
Definition: ArchetypeType.java:75
net.sf.gridarta.model.archetypetype.ArchetypeAttribute
Definition: ArchetypeAttribute.java:28
net.sf.gridarta.model.archetypetype.ArchetypeAttributesDefinition
Definition: ArchetypeAttributesDefinition.java:34
net.sf.gridarta.model.archetypetype.ArchetypeType.getDescription
String getDescription()
Definition: ArchetypeType.java:196
net.sf.gridarta.model.archetypetype.ArchetypeType.getDisplayName
String getDisplayName(@NotNull final Attributes baseObject)
Definition: ArchetypeType.java:279
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections.addSection
final void addSection(@NotNull final ArchetypeAttributeSection section)
Definition: ArchetypeAttributeSections.java:55
net.sf.gridarta.model.archetypetype.ArchetypeType.addAttributeSection
void addAttributeSection(@NotNull final ArchetypeAttributeSection newArchetypeAttributeSection)
Definition: ArchetypeType.java:128
net.sf.gridarta.model.archetypetype.ArchetypeType.toString
String toString()
Definition: ArchetypeType.java:239
net.sf.gridarta.model.archetypetype.ArchetypeType.addArchetypeAttribute
void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute)
Definition: ArchetypeType.java:332
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model.archetypetype.ArchetypeType.isAllowsAllInv
boolean isAllowsAllInv()
Definition: ArchetypeType.java:187
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSection
Definition: ArchetypeAttributeSection.java:32
net.sf.gridarta.model.archetypetype.ArchetypeType.use
final String use
Definition: ArchetypeType.java:81
net.sf.gridarta.model.archetypetype.ArchetypeType.inv
final int[] inv
Definition: ArchetypeType.java:63
net.sf.gridarta.model
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections.addArchetypeAttribute
void addArchetypeAttribute(@NotNull final String sectionName, @NotNull final ArchetypeAttribute archetypeAttribute)
Definition: ArchetypeAttributeSections.java:110
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.archetypetype.ArchetypeType.ArchetypeType
ArchetypeType(@NotNull final String typeName, final int typeNo, @NotNull final String display, final boolean map, final int @Nullable[] inv, final boolean allowsAllInv, @Nullable final String description, @Nullable final String use, @NotNull final ArchetypeAttributesDefinition typeAttributes)
Definition: ArchetypeType.java:111
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections.hasAttribute
boolean hasAttribute()
Definition: ArchetypeAttributeSections.java:82
net.sf.gridarta.model.archetypetype.ArchetypeType
Definition: ArchetypeType.java:35
net.sf.gridarta.model.archetypetype.ArchetypeAttributeSections.hasAttributeKey
boolean hasAttributeKey(@NotNull final Comparable< String > key)
Definition: ArchetypeAttributeSections.java:96
net.sf.gridarta.model.archetypetype.ArchetypeType.getTypeNo
int getTypeNo()
Definition: ArchetypeType.java:151
net.sf.gridarta.model.archetypetype.ArchetypeType.typeNo
final int typeNo
Definition: ArchetypeType.java:46
net.sf.gridarta.model.archetypetype.ArchetypeAttributesDefinition.isEmpty
boolean isEmpty()
Definition: ArchetypeAttributesDefinition.java:75
net.sf.gridarta.model.archetypetype.ArchetypeType.typeName
final String typeName
Definition: ArchetypeType.java:41
net.sf.gridarta.model.archetypetype.ArchetypeAttributeDefinition
Definition: ArchetypeAttributeDefinition.java:30