Gridarta Editor
AttributeRangeChecker.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.IdentityHashMap;
24 import java.util.Map;
33 import org.jetbrains.annotations.NotNull;
34 
39 public class AttributeRangeChecker<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 
44  @NotNull
45  private final Map<Integer, Type<G, A, R>> types = new HashMap<>();
46 
50  @NotNull
51  private final Map<GameObjectMatcher, Type<G, A, R>> matchers = new IdentityHashMap<>();
52 
58  super(validatorPreferences);
59  }
60 
71  public void add(final int type, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue) throws InvalidCheckException {
72  if (minValue == Integer.MIN_VALUE && maxValue == Integer.MAX_VALUE) {
73  return;
74  }
75 
76  getType(type).add(name, displayName, new Range(minValue, maxValue));
77  }
78 
89  public void add(@NotNull final GameObjectMatcher matcher, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue) throws InvalidCheckException {
90  if (minValue == Integer.MIN_VALUE && maxValue == Integer.MAX_VALUE) {
91  return;
92  }
93 
94  getMatcher(matcher).add(name, displayName, new Range(minValue, maxValue));
95  }
96 
102  @NotNull
103  private Type<G, A, R> getType(final int type) {
104  final Type<G, A, R> oldType = types.get(type);
105  if (oldType != null) {
106  return oldType;
107  }
108 
109  final Type<G, A, R> newType = new Type<>();
110  types.put(type, newType);
111  return newType;
112  }
113 
119  @NotNull
120  private Type<G, A, R> getMatcher(@NotNull final GameObjectMatcher matcher) {
121  final Type<G, A, R> oldType = matchers.get(matcher);
122  if (oldType != null) {
123  return oldType;
124  }
125 
126  final Type<G, A, R> newType = new Type<>();
127  matchers.put(matcher, newType);
128  return newType;
129  }
130 
131  @Override
132  public void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector<G, A, R> errorCollector) {
133  if (!gameObject.isHead()) {
134  return;
135  }
136 
137  for (final Map.Entry<GameObjectMatcher, Type<G, A, R>> e : matchers.entrySet()) {
138  if (e.getKey().isMatching(gameObject)) {
139  e.getValue().validate(gameObject, errorCollector);
140  }
141  }
142 
143  final Type<G, A, R> type = types.get(gameObject.getTypeNo());
144  if (type == null) {
145  return;
146  }
147 
148  type.validate(gameObject, errorCollector);
149  }
150 
151 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.validation.checks.Type
Manages all checks for one game object type.
Definition: Type.java:34
net.sf.gridarta.model.validation.checks.InvalidCheckException
An Exception indicating that an attribute check is invalid.
Definition: InvalidCheckException.java:28
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.getType
Type< G, A, R > getType(final int type)
Returns the Type instance for a given object type.
Definition: AttributeRangeChecker.java:103
net.sf
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.types
final Map< Integer, Type< G, A, R > > types
Maps object type to Type instance.
Definition: AttributeRangeChecker.java:45
net.sf.gridarta.model.match.GameObjectMatcher
Interface for classes that match GameObjects.
Definition: GameObjectMatcher.java:30
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.validation.AbstractValidator.validatorPreferences
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
Definition: AbstractValidator.java:55
net.sf.gridarta.model.validation.ValidatorPreferences
Configuration parameters for Validators.
Definition: ValidatorPreferences.java:28
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.AttributeRangeChecker
AttributeRangeChecker(@NotNull final ValidatorPreferences validatorPreferences)
Creates a new instance.
Definition: AttributeRangeChecker.java:57
net.sf.gridarta.model.match
Classes related to matching {GameObjects}, so called { net.sf.gridarta.model.match....
Definition: AndGameObjectMatcher.java:20
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.checks.Type.validate
void validate(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a game object.
Definition: Type.java:63
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.model.validation.AbstractValidator
This is the base class for validators.
Definition: AbstractValidator.java:37
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.add
void add(@NotNull final GameObjectMatcher matcher, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue)
Adds an attribute to check.
Definition: AttributeRangeChecker.java:89
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.validation.checks.Range
Represents a range of attribute values.
Definition: Range.java:26
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.validateGameObject
void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validates a game object.
Definition: AttributeRangeChecker.java:132
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.add
void add(final int type, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue)
Adds an attribute to check.
Definition: AttributeRangeChecker.java:71
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.validation.GameObjectValidator
Interface for GameObject Validators.
Definition: GameObjectValidator.java:32
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.validation.checks.AttributeRangeChecker
A validator that checks for suspicious attribute values.
Definition: AttributeRangeChecker.java:39
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.getMatcher
Type< G, A, R > getMatcher(@NotNull final GameObjectMatcher matcher)
Returns the Type instance for a given GameObjectMatcher.
Definition: AttributeRangeChecker.java:120
net.sf.gridarta.model.validation.checks.AttributeRangeChecker.matchers
final Map< GameObjectMatcher, Type< G, A, R > > matchers
Maps matcher to Type instance.
Definition: AttributeRangeChecker.java:51