-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRegionModule.java
More file actions
249 lines (238 loc) · 12.3 KB
/
RegionModule.java
File metadata and controls
249 lines (238 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2016, Kevin Phoenix
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package in.twizmwaz.cardinal.module.region;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.AbstractModule;
import in.twizmwaz.cardinal.module.ModuleEntry;
import in.twizmwaz.cardinal.module.ModuleError;
import in.twizmwaz.cardinal.module.region.exception.RegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.RegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.attribute.InvalidRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.attribute.MissingRegionAttributeException;
import in.twizmwaz.cardinal.module.region.exception.property.InvalidRegionPropertyException;
import in.twizmwaz.cardinal.module.region.exception.property.MissingRegionPropertyException;
import in.twizmwaz.cardinal.module.region.parser.AboveRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BelowRegionParser;
import in.twizmwaz.cardinal.module.region.parser.BlockRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CircleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CuboidRegionParser;
import in.twizmwaz.cardinal.module.region.parser.CylinderRegionParser;
import in.twizmwaz.cardinal.module.region.parser.HalfRegionParser;
import in.twizmwaz.cardinal.module.region.parser.PointRegionParser;
import in.twizmwaz.cardinal.module.region.parser.RectangleRegionParser;
import in.twizmwaz.cardinal.module.region.parser.SphereRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.ComplementRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.IntersectRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.MirroredRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.NegativeRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.TranslatedRegionParser;
import in.twizmwaz.cardinal.module.region.parser.modifications.UnionRegionParser;
import in.twizmwaz.cardinal.module.region.type.AboveRegion;
import in.twizmwaz.cardinal.module.region.type.BelowRegion;
import in.twizmwaz.cardinal.module.region.type.BlockRegion;
import in.twizmwaz.cardinal.module.region.type.CircleRegion;
import in.twizmwaz.cardinal.module.region.type.CuboidRegion;
import in.twizmwaz.cardinal.module.region.type.CylinderRegion;
import in.twizmwaz.cardinal.module.region.type.EmptyRegion;
import in.twizmwaz.cardinal.module.region.type.EverywhereRegion;
import in.twizmwaz.cardinal.module.region.type.HalfRegion;
import in.twizmwaz.cardinal.module.region.type.NowhereRegion;
import in.twizmwaz.cardinal.module.region.type.RectangleRegion;
import in.twizmwaz.cardinal.module.region.type.SphereRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.ComplementRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.IntersectRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.MirroredRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.NegativeRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.TranslatedRegion;
import in.twizmwaz.cardinal.module.region.type.modifications.UnionRegion;
import in.twizmwaz.cardinal.util.ParseUtil;
import in.twizmwaz.cardinal.util.Proto;
import lombok.NonNull;
import org.jdom2.Element;
import org.jdom2.located.Located;
import java.util.List;
import java.util.Map;
@ModuleEntry
public class RegionModule extends AbstractModule {
private Map<Match, Map<String, Region>> regions = Maps.newHashMap();
@Override
public boolean loadMatch(@NonNull Match match) {
Map<String, Region> matchRegions = Maps.newHashMap();
matchRegions.put("everywhere", new EverywhereRegion(match));
matchRegions.put("nowhere", new NowhereRegion(match));
regions.put(match, matchRegions);
for (Element regionsElement : match.getMap().getDocument().getRootElement().getChildren("regions")) {
for (Element regionElement : regionsElement.getChildren()) {
if (regionElement.getName().equals("apply")) {
continue;
}
try {
getRegion(match, regionElement);
} catch (RegionException e) {
errors.add(new ModuleError(this, match.getMap(), new String[]{getRegionError(e, "region", null)}, false));
}
}
}
return true;
}
/**
* @param id The ID of the region that is returned.
* @return The region that has the given ID.
*/
public Region getRegionById(@NonNull Match match, @NonNull String id) {
return regions.get(match).get(id);
}
/**
* Parses a region from an element.
*
* @param element The element.
* @param alternateAttributes Any alternative attributes to look for regions from.
* @return The parsed region.
* @throws RegionAttributeException Thrown if there are missing or invalid attributes for a region.
*/
public Region getRegion(Match match, Element element, String... alternateAttributes) throws RegionException {
List<String> attributes = Lists.newArrayList(alternateAttributes);
attributes.add("id");
attributes.add("name");
Map.Entry<String, String> value = ParseUtil.getFirstNonNullAttributeValue(element, attributes);
String id = null;
if (value != null) {
String attr = value.getKey();
Proto proto = match.getMap().getProto();
if (attr.equals("id") && proto.isBefore("1.4.0")) {
errors.add(new ModuleError(this, match.getMap(),
new String[]{"Attribute \"id\" should be \"name\" prior to proto 1.4.0"}, false));
} else if (attr.equals("name") && proto.isAfterOrAt("1.4.0")) {
errors.add(new ModuleError(this, match.getMap(),
new String[]{"Attribute \"name\" should be \"id\" in proto 1.4.0 or later"}, false));
}
id = value.getValue();
}
switch (element.getName()) {
case "cuboid":
return checkRegion(match, id, new CuboidRegion(match, new CuboidRegionParser(element)));
case "cylinder":
return checkRegion(match, id, new CylinderRegion(match, new CylinderRegionParser(element)));
case "block":
return checkRegion(match, id, new BlockRegion(match, new BlockRegionParser(element)));
case "sphere":
return checkRegion(match, id, new SphereRegion(match, new SphereRegionParser(element)));
case "rectangle":
return checkRegion(match, id, new RectangleRegion(match, new RectangleRegionParser(element)));
case "circle":
return checkRegion(match, id, new CircleRegion(match, new CircleRegionParser(element)));
case "half":
return checkRegion(match, id, new HalfRegion(match, new HalfRegionParser(element)));
case "below":
return checkRegion(match, id, new BelowRegion(match, new BelowRegionParser(element)));
case "above":
return checkRegion(match, id, new AboveRegion(match, new AboveRegionParser(element)));
case "empty":
return checkRegion(match, id, new EmptyRegion(match));
case "nowhere":
return checkRegion(match, id, new NowhereRegion(match));
case "everywhere":
return checkRegion(match, id, new EverywhereRegion(match));
case "negative":
return checkRegion(match, id, new NegativeRegion(match, new NegativeRegionParser(match, element)));
case "union":
return checkRegion(match, id, new UnionRegion(match, new UnionRegionParser(match, element)));
case "complement":
return checkRegion(match, id, new ComplementRegion(new ComplementRegionParser(match, element)));
case "intersect":
return checkRegion(match, id, new IntersectRegion(match, new IntersectRegionParser(match, element)));
case "translate":
return checkRegion(match, id, new TranslatedRegion(new TranslatedRegionParser(match, element)));
case "mirror":
return checkRegion(match, id, new MirroredRegion(new MirroredRegionParser(match, element)));
case "point":
return checkRegion(match, id, PointRegionParser.generateRegion(match, element));
default:
if (id != null) {
Region region = getRegionById(match, id);
if (region != null) {
return region;
}
}
List<Region> regions = Lists.newArrayList();
for (Element child : element.getChildren()) {
Region childRegion = getRegion(match, child);
if (childRegion != null) {
regions.add(childRegion);
}
}
if (regions.isEmpty()) {
return null;
}
return checkRegion(match, id, new UnionRegion(match, regions));
}
}
private Region checkRegion(Match match, String id, Region region) {
if (id != null) {
if (!regions.get(match).containsKey(id)) {
regions.get(match).put(id, region);
} else {
errors.add(new ModuleError(this, match.getMap(),
new String[] {"Cannot have two regions assigned to the same id."}, false));
}
}
return region;
}
/**
* Gets an appropriate error message for a failed region parsing.
*
* @param e The exception thrown when parsing.
* @param name The name of the expected region.
* @param parent The module that attempted to retrieve the region.
* @return The error message.
*/
public static String getRegionError(RegionException e, String name, String parent) {
if (e instanceof RegionAttributeException) {
RegionAttributeException exception = (RegionAttributeException) e;
Located located = (Located) exception.getElement();
if (exception instanceof MissingRegionAttributeException) {
return "Missing attribute \"" + exception.getAttribute() + "\"" + (name != null ? " for " + name : "")
+ (parent != null ? " for " + parent : "") + " at " + located.getLine() + ", " + located.getColumn();
} else if (exception instanceof InvalidRegionAttributeException) {
return "Invalid attribute \"" + exception.getAttribute() + "\"" + (name != null ? " for " + name : "")
+ (parent != null ? " for " + parent : "") + " at " + located.getLine() + ", " + located.getColumn();
}
} else if (e instanceof RegionPropertyException) {
RegionPropertyException exception = (RegionPropertyException) e;
Located located = (Located) exception.getElement();
if (exception instanceof MissingRegionPropertyException) {
return "Missing property \"" + exception.getProperty() + "\"" + (name != null ? " for " + name : "")
+ (parent != null ? " for " + parent : "") + " at " + located.getLine() + ", " + located.getColumn();
} else if (exception instanceof InvalidRegionPropertyException) {
return "Invalid property \"" + exception.getProperty() + "\"" + (name != null ? " for " + name : "")
+ (parent != null ? " for " + parent : "") + " at " + located.getLine() + ", " + located.getColumn();
}
}
return "Could not parse " + (name != null ? name : "region") + (parent != null ? " for " + parent : "")
+ (e.getMessage() != null ? ": " + e.getMessage() : "");
}
}