Gridarta Editor
CustomTypeChecker.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.validation.checks;
21 
22 import java.util.HashMap;
23 import java.util.Map;
33 import org.jetbrains.annotations.NotNull;
34 
39 public class CustomTypeChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValidator<G, A, R> implements GameObjectValidator<G, A, R> {
40 
46  @NotNull
47  private final Map<Integer, Map<Integer, Integer>> ignoreEntries = new HashMap<>();
48 
54  super(validatorPreferences);
55  }
56 
57  @Override
58  public void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector<G, A, R> errorCollector) {
59  final int archetypeTypeNo = gameObject.getArchetype().getTypeNo();
60  final int gameObjectTypeNo = gameObject.getTypeNo();
61  if (gameObjectTypeNo != archetypeTypeNo && !isAllowedTypeChange(gameObject, archetypeTypeNo, gameObjectTypeNo)) {
62  errorCollector.collect(new CustomTypeError<>(gameObject, archetypeTypeNo, gameObjectTypeNo));
63  }
64  }
65 
73  private boolean isAllowedTypeChange(@NotNull final GameObject<G, A, R> gameObject, final int archetypeTypeNo, final int gameObjectTypeNo) {
74  final Map<Integer, Integer> entries = ignoreEntries.get(archetypeTypeNo);
75  if (entries == null) {
76  return false;
77  }
78 
79  final Integer allowedEnvType = entries.get(gameObjectTypeNo);
80  if (allowedEnvType == null) {
81  return entries.containsKey(gameObjectTypeNo);
82  }
83 
84  final BaseObject<G, A, R, ?> envGameObject = gameObject.getContainerGameObject();
85  if (envGameObject == null) {
86  return false;
87  }
88 
89  final int envTypeNo = envGameObject.getTypeNo();
90  return envTypeNo == allowedEnvType;
91  }
92 
98  public void addIgnore(final int fromType, final int toType) {
99  getIgnoreSet(fromType).put(toType, null);
100  }
101 
108  public void addIgnore(final int fromType, final int toType, final int envType) {
109  getIgnoreSet(fromType).put(toType, envType);
110  }
111 
117  @NotNull
118  private Map<Integer, Integer> getIgnoreSet(final int type) {
119  final Map<Integer, Integer> ignoreEntrySet = ignoreEntries.get(type);
120  if (ignoreEntrySet != null) {
121  return ignoreEntrySet;
122  }
123 
124  final Map<Integer, Integer> newIgnoreEntrySet = new HashMap<>();
125  ignoreEntries.put(type, newIgnoreEntrySet);
126  return newIgnoreEntrySet;
127  }
128 
129 }
net.sf.gridarta.model.validation.checks.CustomTypeChecker.isAllowedTypeChange
boolean isAllowedTypeChange(@NotNull final GameObject< G, A, R > gameObject, final int archetypeTypeNo, final int gameObjectTypeNo)
Definition: CustomTypeChecker.java:73
net.sf.gridarta
net.sf.gridarta.model.validation.checks.CustomTypeChecker.addIgnore
void addIgnore(final int fromType, final int toType)
Definition: CustomTypeChecker.java:98
net.sf
net.sf.gridarta.model.validation.checks.CustomTypeChecker.CustomTypeChecker
CustomTypeChecker(@NotNull final ValidatorPreferences validatorPreferences)
Definition: CustomTypeChecker.java:53
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Definition: GameObject.java:36
net.sf.gridarta.model.validation.checks.CustomTypeChecker.validateGameObject
void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Definition: CustomTypeChecker.java:58
net.sf.gridarta.model.validation.AbstractValidator.validatorPreferences
final ValidatorPreferences validatorPreferences
Definition: AbstractValidator.java:55
net.sf.gridarta.model.validation.ValidatorPreferences
Definition: ValidatorPreferences.java:28
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maparchobject.MapArchObject
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.ErrorCollector
Definition: ErrorCollector.java:33
net.sf.gridarta.model.validation.AbstractValidator
Definition: AbstractValidator.java:37
net.sf.gridarta.model.validation
Definition: AbstractValidator.java:20
net.sf.gridarta.model.validation.errors.CustomTypeError
Definition: CustomTypeError.java:32
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model.validation.checks.CustomTypeChecker.ignoreEntries
final Map< Integer, Map< Integer, Integer > > ignoreEntries
Definition: CustomTypeChecker.java:47
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.model.validation.checks.CustomTypeChecker.getIgnoreSet
Map< Integer, Integer > getIgnoreSet(final int type)
Definition: CustomTypeChecker.java:118
net.sf.gridarta.model.validation.GameObjectValidator
Definition: GameObjectValidator.java:32
net.sf.gridarta.model.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.baseobject.BaseObject.getTypeNo
int getTypeNo()
net.sf.gridarta.model.validation.checks.CustomTypeChecker.addIgnore
void addIgnore(final int fromType, final int toType, final int envType)
Definition: CustomTypeChecker.java:108
net.sf.gridarta.model.validation.checks.CustomTypeChecker
Definition: CustomTypeChecker.java:39