From b017a798abc442fbddb58cad6175aea7cf29233e Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Mon, 21 Aug 2023 23:42:24 +0200 Subject: [PATCH] Add config toggle & ignore tutorials Signed-off-by: Pablo Herrera --- core/src/main/java/tc/oc/pgm/PGMConfig.java | 7 +++++++ core/src/main/java/tc/oc/pgm/api/Config.java | 3 +++ .../main/java/tc/oc/pgm/map/MapFactoryImpl.java | 16 ++++++++++------ core/src/main/resources/config.yml | 4 ++++ .../java/tc/oc/pgm/util/bukkit/Platform.java | 4 +++- .../java/tc/oc/pgm/util/xml/DocumentWrapper.java | 12 +++++++----- .../java/tc/oc/pgm/util/xml/ParseRangeTest.java | 8 +++++++- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/PGMConfig.java b/core/src/main/java/tc/oc/pgm/PGMConfig.java index 8cd8b94efe..11882d24b8 100644 --- a/core/src/main/java/tc/oc/pgm/PGMConfig.java +++ b/core/src/main/java/tc/oc/pgm/PGMConfig.java @@ -63,6 +63,7 @@ public final class PGMConfig implements Config { private final List mapSourceFactories; private final Path mapPoolFile; private final Path includesDirectory; + private final boolean showUnusedXml; // countdown.* private final Duration startTime; @@ -161,6 +162,7 @@ public final class PGMConfig implements Config { this.mapPoolFile = getPath(dataFolder.toPath(), config.getString("map.pools")); this.includesDirectory = getPath(dataFolder.toPath(), config.getString("map.includes")); + this.showUnusedXml = parseBoolean(config.getString("map.show-unused-xml", "true")); this.startTime = parseDuration(config.getString("countdown.start", "30s")); this.huddleTime = parseDuration(config.getString("countdown.huddle", "0s")); @@ -475,6 +477,11 @@ public Path getMapPoolFile() { return includesDirectory; } + @Override + public boolean showUnusedXml() { + return showUnusedXml; + } + @Override public Duration getStartTime() { return startTime; diff --git a/core/src/main/java/tc/oc/pgm/api/Config.java b/core/src/main/java/tc/oc/pgm/api/Config.java index 61af12f148..4768531174 100644 --- a/core/src/main/java/tc/oc/pgm/api/Config.java +++ b/core/src/main/java/tc/oc/pgm/api/Config.java @@ -69,6 +69,9 @@ public interface Config { @Nullable Path getIncludesDirectory(); + /** @return If unused XML tags should be reported or ignored */ + boolean showUnusedXml(); + /** * Gets a duration to wait before starting a match. * diff --git a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java index a70838d0ce..a1b592dbd3 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -12,6 +12,7 @@ import org.jdom2.Element; import org.jdom2.input.JDOMParseException; import tc.oc.pgm.api.Modules; +import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.map.MapContext; import tc.oc.pgm.api.map.MapModule; import tc.oc.pgm.api.map.MapProtos; @@ -36,6 +37,7 @@ import tc.oc.pgm.util.Version; import tc.oc.pgm.util.xml.DocumentWrapper; import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.XMLUtils; public class MapFactoryImpl extends ModuleGraph, MapModuleFactory> @@ -109,12 +111,14 @@ private void postLoad() throws InvalidXMLException { module.postParse(this, logger, document); } - ((DocumentWrapper) document) - .checkUnvisited( - node -> { - InvalidXMLException ex = new InvalidXMLException("Unused node, maybe a typo?", node); - logger.log(Level.WARNING, ex.getMessage(), ex); - }); + if (PGM.get().getConfiguration().showUnusedXml()) { + ((DocumentWrapper) document).checkUnvisited(this::printUnvisitedNode); + } + } + + private void printUnvisitedNode(Node node) { + InvalidXMLException ex = new InvalidXMLException("Unused node, maybe a typo?", node); + logger.log(Level.WARNING, ex.getMessage(), ex); } @Override diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index 8b0b6f1b05..a4da0ce181 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -35,6 +35,10 @@ map: # A path to the includes folder, or empty to disable map includes. includes: "includes" + # Show unused XML nodes as map errors. Helps with finding typos in xml. + show-unused-xml: true + + # Sets the duration of various countdowns. # # "30s" = 30 seconds diff --git a/util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java b/util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java index 5774f56fd4..db03ef28de 100644 --- a/util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java +++ b/util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java @@ -3,6 +3,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.bukkit.Bukkit; +import org.bukkit.Server; import tc.oc.pgm.util.ClassLogger; import tc.oc.pgm.util.nms.NMSHacksNoOp; import tc.oc.pgm.util.nms.NMSHacksPlatform; @@ -32,7 +33,8 @@ public enum Platform { public static Platform SERVER_PLATFORM = computeServerPlatform(); private static Platform computeServerPlatform() { - String versionString = Bukkit.getServer().getVersion(); + Server sv = Bukkit.getServer(); + String versionString = sv == null ? "" : sv.getVersion(); for (Platform platform : Platform.values()) { if (versionString.contains(platform.variant) && versionString.contains("MC: " + platform.majorVersion)) { diff --git a/util/src/main/java/tc/oc/pgm/util/xml/DocumentWrapper.java b/util/src/main/java/tc/oc/pgm/util/xml/DocumentWrapper.java index 1f70512b8d..cc749b0b39 100644 --- a/util/src/main/java/tc/oc/pgm/util/xml/DocumentWrapper.java +++ b/util/src/main/java/tc/oc/pgm/util/xml/DocumentWrapper.java @@ -1,5 +1,7 @@ package tc.oc.pgm.util.xml; +import com.google.common.collect.Sets; +import java.util.Set; import java.util.function.Consumer; import org.jdom2.Attribute; import org.jdom2.Content; @@ -10,6 +12,8 @@ public class DocumentWrapper extends Document { + private static final Set IGNORED = Sets.newHashSet("variant", "tutorial"); + private boolean visitingAllowed = true; public DocumentWrapper() { @@ -55,12 +59,10 @@ private void checkVisited(Element el, Consumer unvisited) { for (int i = 0; i < el.getContentSize(); i++) { Content c = el.getContent(i); - if (c instanceof InheritingElement) { - InheritingElement child = (InheritingElement) c; - - // Only main map reads all variants, others read just their own. Skip the check. - if ("variant".equals(child.getName())) continue; + if (!(c instanceof InheritingElement)) continue; + InheritingElement child = (InheritingElement) c; + if (child.getNamespace() == Namespace.NO_NAMESPACE && !IGNORED.contains(child.getName())) { if (!child.wasVisited()) unvisited.accept(Node.fromNullable(child)); else checkVisited(child, unvisited); } diff --git a/util/src/test/java/tc/oc/pgm/util/xml/ParseRangeTest.java b/util/src/test/java/tc/oc/pgm/util/xml/ParseRangeTest.java index 0ca18540a7..54e3dde93b 100644 --- a/util/src/test/java/tc/oc/pgm/util/xml/ParseRangeTest.java +++ b/util/src/test/java/tc/oc/pgm/util/xml/ParseRangeTest.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.jdom2.Attribute; +import org.jdom2.Document; import org.jdom2.Namespace; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -13,7 +14,12 @@ public final class ParseRangeTest { private static Node dummyNode(String value) { - return new Node(new Attribute("range", value, Namespace.NO_NAMESPACE)); + Document doc = new Document(); + InheritingElement el = new InheritingElement("a"); + Attribute attr = new Attribute("range", value, Namespace.NO_NAMESPACE); + el.setAttribute(attr); + doc.setRootElement(el); + return new Node(attr); } @Test