Skip to content

Commit a06b69a

Browse files
Pablete1234twizmwazin
authored andcommitted
Better parsing than looking thru parents
1 parent b232904 commit a06b69a

File tree

3 files changed

+120
-95
lines changed

3 files changed

+120
-95
lines changed

src/main/java/in/twizmwaz/cardinal/module/objective/core/CoreModule.java

+84-90
Original file line numberDiff line numberDiff line change
@@ -74,116 +74,110 @@ public class CoreModule extends AbstractListenerModule {
7474
@Override
7575
public boolean loadMatch(Match match) {
7676
Document document = match.getMap().getDocument();
77-
for (Element coresElement : document.getRootElement().getChildren("cores")) {
78-
for (Element coreElement : coresElement.getChildren("core")) {
79-
Located located = (Located) coreElement;
77+
for (Element coreElement : ParseUtil.getElementsIn(document.getRootElement(), "cores", "core")) {
78+
Located located = (Located) coreElement;
8079

81-
String id = ParseUtil.getFirstAttribute("id", coreElement, coresElement);
80+
String id = coreElement.getAttributeValue("id");
81+
String name = coreElement.getAttributeValue("name", "Core");
8282

83-
String nameValue = ParseUtil.getFirstAttribute("name", coreElement, coresElement);
84-
String name = nameValue == null ? "Core" : nameValue;
83+
boolean required = Numbers.parseBoolean(coreElement.getAttributeValue("required"), true);
8584

86-
String requiredValue = ParseUtil.getFirstAttribute("required", coreElement, coresElement);
87-
boolean required = requiredValue == null || Numbers.parseBoolean(requiredValue);
85+
RegionModule regionModule = Cardinal.getModule(RegionModule.class);
86+
Region region;
87+
try {
88+
region = regionModule.getRegion(match, coreElement);
89+
} catch (RegionException e) {
90+
errors.add(new ModuleError(this, match.getMap(),
91+
new String[]{RegionModule.getRegionError(e, "region", "core"),
92+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
93+
continue;
94+
}
95+
if (region == null && coreElement.getAttribute("region") != null) {
96+
region = regionModule.getRegionById(match, coreElement.getAttributeValue("region"));
97+
}
98+
if (region == null) {
99+
errors.add(new ModuleError(this, match.getMap(), new String[]{"No region specified for core",
100+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
101+
continue;
102+
}
103+
if (!region.isBounded()) {
104+
errors.add(new ModuleError(this, match.getMap(),
105+
new String[]{"Region specified for core must be a bounded region",
106+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
107+
continue;
108+
}
88109

89-
RegionModule regionModule = Cardinal.getModule(RegionModule.class);
90-
Region region;
110+
String leakValue = coreElement.getAttributeValue("leak");
111+
int leak = 5;
112+
if (leakValue != null) {
91113
try {
92-
region = regionModule.getRegion(match, coreElement);
93-
} catch (RegionException e) {
114+
leak = Numbers.parseInteger(leakValue);
115+
} catch (NumberFormatException e) {
94116
errors.add(new ModuleError(this, match.getMap(),
95-
new String[]{RegionModule.getRegionError(e, "region", "core"),
117+
new String[]{"Invalid leak distance specified for core",
96118
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
97119
continue;
98120
}
99-
if (region == null && coresElement.getAttribute("region") != null) {
100-
region = regionModule.getRegionById(match, coresElement.getAttributeValue("region"));
101-
}
102-
if (region == null) {
103-
errors.add(new ModuleError(this, match.getMap(), new String[]{"No region specified for core",
104-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
105-
continue;
106-
}
107-
if (!region.isBounded()) {
121+
}
122+
123+
MaterialPattern material = new MaterialPattern(new AbstractMap.SimpleEntry<>(Material.OBSIDIAN,
124+
MaterialPattern.ANY_DATA_VALUE));
125+
String materialValue = coreElement.getAttributeValue("material");
126+
if (materialValue != null) {
127+
try {
128+
material = MaterialPattern.getSingleMaterialPattern(materialValue);
129+
} catch (NumberFormatException e) {
108130
errors.add(new ModuleError(this, match.getMap(),
109-
new String[]{"Region specified for core must be a bounded region",
131+
new String[]{"Invalid data value of material specified for core",
110132
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
111133
continue;
112134
}
135+
}
136+
String teamValue = coreElement.getAttributeValue("team");
137+
if (teamValue == null) {
138+
errors.add(new ModuleError(this, match.getMap(), new String[]{"No team specified for wool",
139+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
140+
continue;
141+
}
142+
Team team = Cardinal.getModule(TeamModule.class).getTeamById(match, teamValue);
143+
if (team == null) {
144+
errors.add(new ModuleError(this, match.getMap(), new String[]{"Invalid team specified for core",
145+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
146+
continue;
147+
}
113148

114-
String leakValue = ParseUtil.getFirstAttribute("leak", coreElement, coresElement);
115-
int leak = 5;
116-
if (leakValue != null) {
117-
try {
118-
leak = Numbers.parseInteger(leakValue);
119-
} catch (NumberFormatException e) {
120-
errors.add(new ModuleError(this, match.getMap(),
121-
new String[]{"Invalid leak distance specified for core",
122-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
123-
continue;
124-
}
125-
}
126-
127-
MaterialPattern material = new MaterialPattern(new AbstractMap.SimpleEntry<>(Material.OBSIDIAN,
128-
MaterialPattern.ANY_DATA_VALUE));
129-
String materialValue = ParseUtil.getFirstAttribute("material", coreElement, coresElement);
130-
if (materialValue != null) {
131-
try {
132-
material = MaterialPattern.getSingleMaterialPattern(materialValue);
133-
} catch (NumberFormatException e) {
134-
errors.add(new ModuleError(this, match.getMap(),
135-
new String[]{"Invalid data value of material specified for core",
136-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
137-
continue;
138-
}
139-
}
140-
String teamValue = ParseUtil.getFirstAttribute("team", coreElement, coresElement);
141-
if (teamValue == null) {
142-
errors.add(new ModuleError(this, match.getMap(), new String[]{"No team specified for core",
143-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
144-
continue;
145-
}
146-
Team team = Cardinal.getModule(TeamModule.class).getTeamById(match, teamValue);
147-
if (team == null) {
148-
errors.add(new ModuleError(this, match.getMap(), new String[]{"Invalid team specified for core",
149-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
150-
continue;
151-
}
152-
153-
String modeChangesValue = ParseUtil.getFirstAttribute("mode-changes", coreElement, coresElement);
154-
boolean modeChanges = modeChangesValue == null || Numbers.parseBoolean(modeChangesValue);
155-
156-
String showValue = ParseUtil.getFirstAttribute("show", coreElement, coresElement);
157-
boolean show = showValue == null || Numbers.parseBoolean(showValue);
158-
159-
ProximityMetric proximityMetric = ProximityMetric.CLOSEST_PLAYER;
160-
String woolProximityMetricValue = ParseUtil.getFirstAttribute("proximity-metric", coreElement, coresElement);
161-
if (woolProximityMetricValue != null) {
162-
try {
163-
proximityMetric =
164-
ProximityMetric.valueOf(Strings.getTechnicalName(woolProximityMetricValue));
165-
} catch (IllegalArgumentException e) {
166-
errors.add(new ModuleError(this, match.getMap(),
167-
new String[]{"Invalid proximity metric specified for core",
168-
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
169-
continue;
170-
}
171-
}
149+
String modeChangesValue = coreElement.getAttributeValue("mode-changes");
150+
boolean modeChanges = modeChangesValue == null || Numbers.parseBoolean(modeChangesValue);
172151

173-
String proximityHorizontalValue = ParseUtil.getFirstAttribute("proximity-horizontal", coreElement,
174-
coresElement);
175-
boolean proximityHorizontal = proximityHorizontalValue != null
176-
&& Numbers.parseBoolean(proximityHorizontalValue);
152+
String showValue = coreElement.getAttributeValue("show");
153+
boolean show = showValue == null || Numbers.parseBoolean(showValue);
177154

178-
Core core = new Core(match, id, name, required, region, leak, material, team, modeChanges, show,
179-
proximityMetric, proximityHorizontal);
180-
if (!IdModule.get().add(match, id, core)) {
155+
ProximityMetric proximityMetric = ProximityMetric.CLOSEST_PLAYER;
156+
String proximityMetricValue = coreElement.getAttributeValue("proximity-metric");
157+
if (proximityMetricValue != null) {
158+
try {
159+
proximityMetric =
160+
ProximityMetric.valueOf(Strings.getTechnicalName(proximityMetricValue));
161+
} catch (IllegalArgumentException e) {
181162
errors.add(new ModuleError(this, match.getMap(),
182-
new String[]{"Core id is not valid or already in use",
163+
new String[]{"Invalid proximity metric specified for core",
183164
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
184-
IdModule.get().add(match, null, core, true);
165+
continue;
185166
}
186167
}
168+
169+
String proximityHorizontalValue = coreElement.getAttributeValue("proximity-horizontal");
170+
boolean proximityHorizontal = proximityHorizontalValue != null
171+
&& Numbers.parseBoolean(proximityHorizontalValue);
172+
173+
Core core = new Core(match, id, name, required, region, leak, material, team, modeChanges, show,
174+
proximityMetric, proximityHorizontal);
175+
if (!IdModule.get().add(match, id, core)) {
176+
errors.add(new ModuleError(this, match.getMap(),
177+
new String[]{"Core id is not valid or already in use",
178+
"Element at " + located.getLine() + ", " + located.getColumn()}, false));
179+
IdModule.get().add(match, null, core, true);
180+
}
187181
}
188182
return true;
189183
}

src/main/java/in/twizmwaz/cardinal/module/objective/wool/WoolModule.java

-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@
6464
import org.bukkit.entity.Player;
6565
import org.bukkit.event.EventHandler;
6666
import org.bukkit.event.EventPriority;
67-
import org.bukkit.event.block.BlockBreakEvent;
68-
import org.bukkit.event.block.BlockFormEvent;
69-
import org.bukkit.event.block.BlockPistonExtendEvent;
70-
import org.bukkit.event.block.BlockPistonRetractEvent;
7167
import org.bukkit.event.block.BlockPlaceEvent;
7268
import org.bukkit.event.entity.PlayerDeathEvent;
7369
import org.bukkit.event.inventory.CraftItemEvent;

src/main/java/in/twizmwaz/cardinal/util/ParseUtil.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.jdom2.Element;
3030

3131
import java.util.AbstractMap;
32+
import java.util.ArrayList;
3233
import java.util.List;
3334
import java.util.Map;
3435

@@ -84,7 +85,41 @@ public static Element[] addElement(Element element, Element... elements) {
8485
* @return Returns value as long as it isn't null, if it's null, it returns fallback.
8586
*/
8687
public static <T> T fallback(T value, @NonNull T fallback) {
87-
return value == null ? fallback : value;
88+
return value == null ? fallback : value;
89+
}
90+
91+
/** Utility to get a list of pre-processed elements, with the parent's attributes. Very useful in any module that has
92+
* a parent element, with sub elements.
93+
* @param parentElement The parent element to search on. Usually the document root element.
94+
* @param parent The parent element name. Ex: cores
95+
* @param child The child element name. Ex: core
96+
* @return A list of child elements, containing all attributes from their parents, in importance order.
97+
*/
98+
public static List<Element> getElementsIn(Element parentElement, String parent, String child) {
99+
List<Element> results = new ArrayList<>();
100+
for (Element element : parentElement.getChildren(parent)) {
101+
results.addAll(getElementsIn(parent, child, element));
102+
}
103+
return results;
104+
}
105+
106+
private static List<Element> getElementsIn(String parent, String child, Element... elements) {
107+
List<Element> results = new ArrayList<>();
108+
109+
elements[0].getChildren(child).forEach(childEl -> results.add(getJoinedElements(addElement(childEl, elements))));
110+
elements[0].getChildren(parent)
111+
.forEach(parentEl -> results.addAll(getElementsIn(parent, child, addElement(parentEl, elements))));
112+
113+
return results;
114+
}
115+
116+
private static Element getJoinedElements(Element... elements) {
117+
Element result = elements[0].clone();
118+
for (Element element : elements) {
119+
element.getAttributes().stream()
120+
.filter(attribute -> result.getAttribute(attribute.getName()) != null).map(result::setAttribute);
121+
}
122+
return result;
88123
}
89124

90125
}

0 commit comments

Comments
 (0)