diff --git a/core/src/main/java/tc/oc/pgm/PGMConfig.java b/core/src/main/java/tc/oc/pgm/PGMConfig.java index 786330c781..bfed54c0ba 100644 --- a/core/src/main/java/tc/oc/pgm/PGMConfig.java +++ b/core/src/main/java/tc/oc/pgm/PGMConfig.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.TreeSet; import java.util.logging.Level; +import javax.annotation.Nullable; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; @@ -473,7 +474,7 @@ public String getMapPoolFile() { } @Override - public String getIncludesDirectory() { + public @Nullable String getIncludesDirectory() { return includesDirectory; } diff --git a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java index a3e583e791..4a96615342 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/MapSource.java +++ b/core/src/main/java/tc/oc/pgm/api/map/MapSource.java @@ -3,7 +3,7 @@ import java.io.File; import java.io.InputStream; import tc.oc.pgm.api.map.exception.MapMissingException; -import tc.oc.pgm.api.map.includes.StoredMapInclude; +import tc.oc.pgm.api.map.includes.MapInclude; /** A source where {@link MapInfo} documents and files are downloaded. */ public interface MapSource { @@ -41,12 +41,12 @@ public interface MapSource { boolean checkForUpdates() throws MapMissingException; /** - * Adds a {@link StoredMapInclude} which holds information about a {@link MapInclude} + * Adds an associated {@link MapInclude} * - * @param include The {@link StoredMapInclude} + * @param include The {@link MapInclude} */ - void addMapInclude(StoredMapInclude include); + void addMapInclude(MapInclude include); - /** Remove all associated {@link StoredMapInclude}, used when reloading document. */ + /** Remove all associated {@link MapInclude}, used when reloading document. */ void clearIncludes(); } diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java index 4447f71285..e9a3df7e82 100644 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java +++ b/core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java @@ -20,6 +20,14 @@ public interface MapInclude { */ long getLastModified(); + /** + * Gets whether the associated {@link MapInclude} files have changed since last loading. + * + * @param time The current system time + * @return True if given time is newer than last modified time + */ + boolean hasBeenModified(long time); + /** * Get a collection of {@link Content} which can be merged into an existing {@link Document} * diff --git a/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java b/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java deleted file mode 100644 index d020446059..0000000000 --- a/core/src/main/java/tc/oc/pgm/api/map/includes/StoredMapInclude.java +++ /dev/null @@ -1,22 +0,0 @@ -package tc.oc.pgm.api.map.includes; - -/** - * A snapshot of {@link MapInclude} info, used to determine when include files have been updated. * - */ -public interface StoredMapInclude { - - /** - * Gets the unique include id, used to reference a {@link MapInclude}. - * - * @return A unique include id - */ - String getIncludeId(); - - /** - * Gets whether the associated {@link MapInclude} files have changed since last loading. - * - * @param time The current system time - * @return True if given time is newer than last modified time - */ - boolean hasBeenModified(long time); -} 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 e34d17ab9b..efcce812df 100644 --- a/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java @@ -31,7 +31,6 @@ import tc.oc.pgm.kits.FeatureKitParser; import tc.oc.pgm.kits.KitParser; import tc.oc.pgm.kits.LegacyKitParser; -import tc.oc.pgm.map.includes.StoredMapIncludeImpl; import tc.oc.pgm.regions.FeatureRegionParser; import tc.oc.pgm.regions.LegacyRegionParser; import tc.oc.pgm.regions.RegionParser; @@ -78,7 +77,7 @@ protected MapModule createModule(MapModuleFactory factory) throws ModuleLoadExce } private void storeInclude(MapInclude include) { - this.source.addMapInclude(new StoredMapIncludeImpl(include.getId(), include.getLastModified())); + this.source.addMapInclude(include); } private void preLoad() diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java index 7896f56dc4..d608e47f4a 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java @@ -51,4 +51,9 @@ public boolean equals(Object other) { public long getLastModified() { return lastModified.get(); } + + @Override + public boolean hasBeenModified(long time) { + return time > lastModified.get(); + } } diff --git a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java index 6cd973a634..269bbaf6c7 100644 --- a/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java +++ b/core/src/main/java/tc/oc/pgm/map/includes/MapIncludeProcessorImpl.java @@ -1,10 +1,12 @@ package tc.oc.pgm.map.includes; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Logger; import org.jdom2.Document; @@ -16,13 +18,14 @@ import tc.oc.pgm.api.map.includes.MapInclude; import tc.oc.pgm.api.map.includes.MapIncludeProcessor; import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.Node; import tc.oc.pgm.util.xml.SAXHandler; import tc.oc.pgm.util.xml.XMLUtils; public class MapIncludeProcessorImpl implements MapIncludeProcessor { private final Logger logger; - private final Set includes; + private final Map includes; protected static final ThreadLocal DOCUMENT_FACTORY = ThreadLocal.withInitial( @@ -34,7 +37,7 @@ public class MapIncludeProcessorImpl implements MapIncludeProcessor { public MapIncludeProcessorImpl(Logger logger) { this.logger = logger; - this.includes = Sets.newHashSet(); + this.includes = Maps.newHashMap(); } public MapInclude getGlobalInclude() { @@ -43,10 +46,7 @@ public MapInclude getGlobalInclude() { @Override public MapInclude getMapIncludeById(String includeId) { - return includes.stream() - .filter(include -> include.getId().equalsIgnoreCase(includeId)) - .findAny() - .orElse(null); + return includes.get(includeId); } @Override @@ -61,15 +61,14 @@ public Collection getMapIncludes(Document document) throws InvalidXM List elements = document.getRootElement().getChildren("include"); for (Element element : elements) { - String legacy = XMLUtils.getNullableAttribute(element, "src"); - if (legacy != null) { + if (Node.fromAttr(element, "src") != null) { // Send a warning to legacy include statements without preventing them from loading logger.warning( "[" + document.getBaseURI() + "] " + "Legacy include statements are no longer supported, please upgrade to the format."); - return Sets.newHashSet(); + continue; } String id = XMLUtils.getRequiredAttribute(element, "id").getValue(); @@ -97,7 +96,8 @@ public void reload(Config config) { File[] files = includeFiles.listFiles(); for (File file : files) { try { - this.includes.add(new MapIncludeImpl(file)); + MapIncludeImpl include = new MapIncludeImpl(file); + this.includes.put(include.getId(), include); } catch (MapMissingException | JDOMException | IOException error) { logger.info("Unable to load " + file.getName() + " include document"); error.printStackTrace(); diff --git a/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java b/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java deleted file mode 100644 index 42a824e460..0000000000 --- a/core/src/main/java/tc/oc/pgm/map/includes/StoredMapIncludeImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package tc.oc.pgm.map.includes; - -import java.util.concurrent.atomic.AtomicLong; -import tc.oc.pgm.api.map.includes.StoredMapInclude; - -public class StoredMapIncludeImpl implements StoredMapInclude { - - private final String includeId; - private final AtomicLong lastModified; - - public StoredMapIncludeImpl(String includeId, long lastModified) { - this.includeId = includeId; - this.lastModified = new AtomicLong(lastModified); - } - - @Override - public String getIncludeId() { - return includeId; - } - - @Override - public boolean hasBeenModified(long time) { - return time > lastModified.get(); - } -} diff --git a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java index 18182139c0..cd4f5da1e0 100644 --- a/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java +++ b/core/src/main/java/tc/oc/pgm/map/source/SystemMapSourceFactory.java @@ -15,12 +15,9 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Stream; import org.apache.commons.lang3.builder.ToStringBuilder; -import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.map.MapSource; import tc.oc.pgm.api.map.exception.MapMissingException; import tc.oc.pgm.api.map.includes.MapInclude; -import tc.oc.pgm.api.map.includes.MapIncludeProcessor; -import tc.oc.pgm.api.map.includes.StoredMapInclude; import tc.oc.pgm.util.FileUtils; public class SystemMapSourceFactory extends PathMapSourceFactory { @@ -50,14 +47,12 @@ protected static class SystemMapSource implements MapSource { private final String dir; private final AtomicLong modified; - private final Set storedIncludes; - private final MapIncludeProcessor includes; + private final Set storedIncludes; private SystemMapSource(String dir) { this.dir = checkNotNull(dir); this.modified = new AtomicLong(-1); this.storedIncludes = Sets.newHashSet(); - this.includes = PGM.get().getMapLibrary().getIncludeProcessor(); } private File getDirectory() throws MapMissingException { @@ -151,7 +146,7 @@ public String toString() { } @Override - public void addMapInclude(StoredMapInclude include) { + public void addMapInclude(MapInclude include) { this.storedIncludes.add(include); } @@ -161,9 +156,8 @@ public void clearIncludes() { } private boolean checkForIncludeUpdates() { - for (StoredMapInclude stored : storedIncludes) { - MapInclude include = includes.getMapIncludeById(stored.getIncludeId()); - if (stored.hasBeenModified(include.getLastModified())) { + for (MapInclude include : storedIncludes) { + if (include.hasBeenModified(include.getLastModified())) { return true; } }