Skip to content

Commit

Permalink
Add config toggle & ignore tutorials
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Aug 21, 2023
1 parent fe2ee5f commit b017a79
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/tc/oc/pgm/PGMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public final class PGMConfig implements Config {
private final List<MapSourceFactory> mapSourceFactories;
private final Path mapPoolFile;
private final Path includesDirectory;
private final boolean showUnusedXml;

// countdown.*
private final Duration startTime;
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -475,6 +477,11 @@ public Path getMapPoolFile() {
return includesDirectory;
}

@Override
public boolean showUnusedXml() {
return showUnusedXml;
}

@Override
public Duration getStartTime() {
return startTime;
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
16 changes: 10 additions & 6 deletions core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MapModule<?>, MapModuleFactory<?>>
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
12 changes: 7 additions & 5 deletions util/src/main/java/tc/oc/pgm/util/xml/DocumentWrapper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +12,8 @@

public class DocumentWrapper extends Document {

private static final Set<String> IGNORED = Sets.newHashSet("variant", "tutorial");

private boolean visitingAllowed = true;

public DocumentWrapper() {
Expand Down Expand Up @@ -55,12 +59,10 @@ private void checkVisited(Element el, Consumer<Node> 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);
}
Expand Down
8 changes: 7 additions & 1 deletion util/src/test/java/tc/oc/pgm/util/xml/ParseRangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit b017a79

Please sign in to comment.