Gridarta Editor
AbstractArchetypeBuilder.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.archetype;
21 
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
40 public abstract class AbstractArchetypeBuilder<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
41 
46  @Nullable
47  private R prototype;
48 
52  @Nullable
54 
59  @Nullable
60  private R archetype;
61 
65  @NotNull
66  private final Map<String, String> attributes = new LinkedHashMap<>();
67 
72  @NotNull
74 
81  this.gameObjectFactory = gameObjectFactory;
82  }
83 
90  public void init(@Nullable final R prototype, @NotNull final ErrorViewCollector errorViewCollector) {
91  this.prototype = prototype;
92  this.errorViewCollector = errorViewCollector;
93  // start with new clean GameObject instance
94  if (prototype == null) {
95  archetype = gameObjectFactory.newArchetype(""); // XXX: undefined archetype name
96  } else {
97  archetype = prototype.clone();
98  }
99  attributes.clear();
100  }
101 
102  public void reInit(@NotNull final String archetypeName) {
103  if (archetype == null) {
104  archetype = gameObjectFactory.newArchetype(archetypeName);
105  } else {
106  archetype.setArchetypeName(archetypeName);
107  }
108  attributes.clear();
109  }
110 
111  public void setMsgText(@NotNull final String msgText) {
112  if (archetype == null) {
113  throw new IllegalStateException("archetype is unset");
114  }
115  archetype.setMsgText(msgText);
116  }
117 
118  @NotNull
119  public String getArchetypeName() {
120  if (archetype == null) {
121  throw new IllegalStateException("archetype is unset");
122  }
123  return archetype.getArchetypeName();
124  }
125 
126  public void addObjectText(@NotNull final String line) {
127  if (archetype == null) {
128  throw new IllegalStateException("archetype is unset");
129  }
130  if (errorViewCollector == null) {
131  throw new IllegalStateException("error view collector is unset");
132  }
133 
134  if (line.indexOf('\n') != -1) {
135  throw new IllegalArgumentException("line contains a NL character: '" + line + "'");
136  }
137 
138  final String[] tmp = StringUtils.PATTERN_SPACES.split(line, 2);
139  final String key = tmp[0];
140  if (tmp.length < 2) {
141  assert archetype != null;
142  final String archetypeName = archetype.getArchetypeName();
143  assert errorViewCollector != null;
144  errorViewCollector.addWarning(ErrorViewCategory.ARCHETYPE_INVALID, archetypeName + ": invalid attribute definition: " + line);
145  return;
146  }
147  final String value = tmp[1];
148  final String previousValue = attributes.put(key, value);
149  if (previousValue != null) {
150  assert archetype != null;
151  final String archetypeName = archetype.getArchetypeName();
152  assert errorViewCollector != null;
153  errorViewCollector.addWarning(ErrorViewCategory.ARCHETYPE_INVALID, archetypeName + ": duplicate attribute definition: " + line + ", previous value was " + previousValue);
154  }
155  }
156 
157  public void addLast(@NotNull final G invObject) {
158  if (archetype == null) {
159  throw new IllegalStateException("archetype is unset");
160  }
161  archetype.addLast(invObject);
162  }
163 
168  @NotNull
169  public R finish() {
170  if (prototype != null && prototype.getAttributeString(BaseObject.NAME).isEmpty() && !attributes.containsKey(BaseObject.NAME)) {
171  assert prototype != null;
172  addObjectText(BaseObject.NAME + " " + prototype.getArchetypeName());
173  }
174 
175  if (archetype == null) {
176  throw new IllegalStateException("archetype is unset");
177  }
178  final R result = archetype;
179  archetype = null;
180  final StringBuilder sb = new StringBuilder();
181  for (final String attribute : StringUtils.PATTERN_END_OF_LINE.split(result.getObjectText())) {
182  final String[] tmp = StringUtils.PATTERN_SPACES.split(attribute, 2);
183  if (tmp.length == 2 && !attributes.containsKey(tmp[0])) {
184  sb.append(attribute).append('\n');
185  }
186  }
187  for (final Entry<String, String> attribute : attributes.entrySet()) {
188  sb.append(attribute.getKey()).append(' ').append(attribute.getValue()).append('\n');
189  }
190  result.setObjectText(sb.toString());
191  attributes.clear();
192  return result;
193  }
194 
195  public void setMultiX(final int multiX) {
196  if (archetype == null) {
197  throw new IllegalStateException("archetype is unset");
198  }
199  archetype.setMultiX(multiX);
200  }
201 
202  public void setMultiY(final int multiY) {
203  if (archetype == null) {
204  throw new IllegalStateException("archetype is unset");
205  }
206  archetype.setMultiY(multiY);
207  }
208 
209  @NotNull
210  protected R getArchetype() {
211  final R result = archetype;
212  if (result == null) {
213  throw new IllegalStateException("archetype is unset");
214  }
215  return result;
216  }
217 
218 }
net.sf.gridarta.model.errorview.ErrorViewCollector.addWarning
void addWarning(@NotNull final ErrorViewCategory category)
Definition: ErrorViewCollector.java:68
net.sf.gridarta.model.gameobject.GameObjectFactory
Definition: GameObjectFactory.java:31
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.attributes
final Map< String, String > attributes
Definition: AbstractArchetypeBuilder.java:66
net.sf.gridarta
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.setMsgText
void setMsgText(@NotNull final String msgText)
Definition: AbstractArchetypeBuilder.java:111
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.init
void init(@Nullable final R prototype, @NotNull final ErrorViewCollector errorViewCollector)
Definition: AbstractArchetypeBuilder.java:90
net.sf
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.reInit
void reInit(@NotNull final String archetypeName)
Definition: AbstractArchetypeBuilder.java:102
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.gameObjectFactory
final GameObjectFactory< G, A, R > gameObjectFactory
Definition: AbstractArchetypeBuilder.java:73
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.AbstractArchetypeBuilder
AbstractArchetypeBuilder(@NotNull final GameObjectFactory< G, A, R > gameObjectFactory)
Definition: AbstractArchetypeBuilder.java:80
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.setMultiY
void setMultiY(final int multiY)
Definition: AbstractArchetypeBuilder.java:202
net.sf.gridarta.model.gameobject.GameObject
Definition: GameObject.java:36
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.prototype
R prototype
Definition: AbstractArchetypeBuilder.java:47
net.sf.gridarta.model.baseobject.BaseObject.NAME
String NAME
Definition: BaseObject.java:60
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.getArchetypeName
String getArchetypeName()
Definition: AbstractArchetypeBuilder.java:119
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.archetype
R archetype
Definition: AbstractArchetypeBuilder.java:60
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.utils.StringUtils.PATTERN_END_OF_LINE
static final Pattern PATTERN_END_OF_LINE
Definition: StringUtils.java:61
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.gameobject.GameObjectFactory.newArchetype
R newArchetype(@NotNull String archetypeName)
net.sf.gridarta.model.errorview.ErrorViewCollector
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.errorview.ErrorViewCategory
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.errorViewCollector
ErrorViewCollector errorViewCollector
Definition: AbstractArchetypeBuilder.java:53
net.sf.gridarta.model.maparchobject.MapArchObject
Definition: MapArchObject.java:40
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.finish
R finish()
Definition: AbstractArchetypeBuilder.java:169
net.sf.gridarta.model.errorview.ErrorViewCategory.ARCHETYPE_INVALID
ARCHETYPE_INVALID
Definition: ErrorViewCategory.java:50
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.utils.StringUtils
Definition: StringUtils.java:31
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder
Definition: AbstractArchetypeBuilder.java:40
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.utils.StringUtils.PATTERN_SPACES
static final Pattern PATTERN_SPACES
Definition: StringUtils.java:73
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.getArchetype
R getArchetype()
Definition: AbstractArchetypeBuilder.java:210
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.addLast
void addLast(@NotNull final G invObject)
Definition: AbstractArchetypeBuilder.java:157
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.addObjectText
void addObjectText(@NotNull final String line)
Definition: AbstractArchetypeBuilder.java:126
net.sf.gridarta.model.archetype.AbstractArchetypeBuilder.setMultiX
void setMultiX(final int multiX)
Definition: AbstractArchetypeBuilder.java:195
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20