Skip to content

Commit 3946f43

Browse files
committed
Improve module errors, and add ModuleReporter
1 parent b232904 commit 3946f43

File tree

6 files changed

+153
-5
lines changed

6 files changed

+153
-5
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
compile 'ee.ellytr.command:EllyCommand:2.0.4'
3838
compile 'ee.ellytr.chat:EllyChat:1.2.6'
3939
compile 'org.jdom:jdom2:2.0.6'
40+
compile 'org.jdom:jdom2-contrib:2.0.5'
4041
compile 'org.ow2.asm:asm-all:5.0.4'
4142

4243
testCompile('junit:junit:4.12')
@@ -57,6 +58,7 @@ shadowJar {
5758
append('LICENSE')
5859
dependencies {
5960
include(dependency('org.jdom:jdom2:2.0.6'))
61+
include(dependency('org.jdom:jdom2-contrib:2.0.5'))
6062
relocate 'org.jdom2', 'in.twizmwaz.cardinal.libs.jdom2'
6163
include(dependency('org.ow2.asm:asm-all:5.0.4'))
6264
relocate 'org.objectweb.asm', 'in.twizmwaz.cardinal.libs.asm'

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Tue Apr 26 23:37:45 EDT 2016
1+
#Thu Sep 22 02:07:06 CEST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME

src/main/java/in/twizmwaz/cardinal/module/ModuleError.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,46 @@
2626
package in.twizmwaz.cardinal.module;
2727

2828
import in.twizmwaz.cardinal.module.repository.LoadedMap;
29+
import lombok.AllArgsConstructor;
2930
import lombok.Data;
31+
import org.jdom2.Attribute;
32+
import org.jdom2.Element;
33+
import org.jdom2.contrib.input.LineNumberElement;
3034

3135
@Data
36+
@AllArgsConstructor
3237
public final class ModuleError {
3338

3439
private final Module module;
3540
private final LoadedMap map;
3641
private final String[] message;
3742
private final boolean critical;
3843

44+
public ModuleError(Module module, LoadedMap map, boolean critical, String... message) {
45+
this(module, map, message, critical);
46+
}
47+
48+
public ModuleError(Module module, LoadedMap map, Element element, String message, boolean critical) {
49+
this(module, map, critical, getErrorLocation(element), message);
50+
}
51+
52+
public ModuleError(Module module, LoadedMap map, Attribute attr, String message, boolean critical) {
53+
this(module, map, critical, getErrorLocation(attr), message);
54+
}
55+
56+
private static String getErrorLocation(Element element) {
57+
String result = "'" + element.getName() + "' element";
58+
if (element instanceof LineNumberElement) {
59+
LineNumberElement lineElement = (LineNumberElement) element;
60+
result += lineElement.getStartLine() == lineElement.getEndLine()
61+
? " on line " + lineElement.getStartLine()
62+
: " from line " + lineElement.getStartLine() + " to " + lineElement.getEndLine();
63+
}
64+
return result;
65+
}
66+
67+
private static String getErrorLocation(Attribute attribute) {
68+
return "'" + attribute.getName() + "' attribute in " + getErrorLocation(attribute.getParent());
69+
}
70+
3971
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package in.twizmwaz.cardinal.module;
2+
3+
import in.twizmwaz.cardinal.module.repository.LoadedMap;
4+
import in.twizmwaz.cardinal.util.document.XML;
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
import org.jdom2.Attribute;
8+
import org.jdom2.Element;
9+
10+
@RequiredArgsConstructor
11+
public class ModuleReporter {
12+
13+
private final Module module;
14+
private final LoadedMap map;
15+
16+
@Getter
17+
private boolean canLoad = false;
18+
19+
public void reset() {
20+
canLoad = true;
21+
}
22+
23+
/**
24+
* Gets an attribute value from an element.
25+
* @param element The element to get the attribute from.
26+
* @param attr The attribute to get.
27+
* @param type The class of the type to convert the string to.
28+
* @param required If this is required for the module to load, if true, null is not a valid return, and an error will
29+
* be thrown.
30+
* @param <T> The type to convert the attribute value to.
31+
* @return An object of the correct type, or null if argument is missing or invalid.
32+
*/
33+
public <T> T getAttr(Element element, String attr, Class<T> type, boolean required) {
34+
Attribute attribute = element.getAttribute(attr);
35+
if (required && attribute == null) {
36+
module.getErrors().add(new ModuleError(module, map, element, "Missing required attribute:'" + attr + "'", false));
37+
return null;
38+
}
39+
T result = getAttribute(attribute, type);
40+
if (required && result == null) {
41+
canLoad = false;
42+
}
43+
return result;
44+
}
45+
46+
private <T> T getAttribute(Attribute attr, Class<T> type) {
47+
try {
48+
return XML.getAttr(attr, type);
49+
} catch (Exception e) {
50+
module.getErrors().add(new ModuleError(module, map, attr, e.getMessage(), false));
51+
}
52+
return null;
53+
}
54+
55+
}

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

Lines changed: 0 additions & 4 deletions
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;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package in.twizmwaz.cardinal.util.document;
2+
3+
import com.google.common.collect.Maps;
4+
import com.google.common.collect.ObjectArrays;
5+
import in.twizmwaz.cardinal.util.Numbers;
6+
import in.twizmwaz.cardinal.util.Strings;
7+
import lombok.NonNull;
8+
import org.bukkit.DyeColor;
9+
import org.jdom2.Attribute;
10+
import org.jdom2.Element;
11+
12+
import java.util.Map;
13+
import java.util.function.Function;
14+
15+
public class XML {
16+
17+
private static Map<Class, Function<String, ?>> stringProviders = Maps.newHashMap();
18+
private static Map<Class, Function<Element, ?>> elementProviders = Maps.newHashMap();
19+
20+
static {
21+
stringProviders.put(String.class, (String in) -> in);
22+
stringProviders.put(Integer.class, Numbers::parseInteger);
23+
stringProviders.put(Double.class, Numbers::parseDouble);
24+
stringProviders.put(DyeColor.class, (String in) -> DyeColor.valueOf(Strings.getTechnicalName(in)));
25+
26+
elementProviders.put(String.class, Element::getText);
27+
}
28+
29+
30+
/**
31+
* Gets an object from an attribute.
32+
* @param attr The attribute name.
33+
* @param type The class you want.
34+
* @param <T> The type.
35+
* @return An object of that type if possible, null otherwise.
36+
*/
37+
public static <T> T getAttr(@NonNull Attribute attr, Class<T> type) throws Exception {
38+
return getFromString(attr.getValue(), type);
39+
}
40+
41+
/**
42+
* Gets an object from an attribute.
43+
* @param el The Element name.
44+
* @param type The class you want.
45+
* @param <T> The type.
46+
* @return An object of that type if possible, null otherwise.
47+
*/
48+
public static <T> T getElement(@NonNull Element el, Class<T> type) throws Exception {
49+
return getFromString(el.getText(), type);
50+
}
51+
52+
@SuppressWarnings("unchecked")
53+
public static <T> T getFromString(String in, Class<T> type) {
54+
if (in == null) {
55+
return null;
56+
}
57+
if (XML.stringProviders.containsKey(type)) {
58+
return (T) XML.stringProviders.get(type).apply(in);
59+
}
60+
throw new IllegalStateException("Missing ObjectProvider for class: " + type.getName());
61+
}
62+
63+
}

0 commit comments

Comments
 (0)