Skip to content

Commit

Permalink
Remove StoredMapInclude and other small fixes
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <[email protected]>
  • Loading branch information
applenick committed Aug 12, 2022
1 parent 42d9abd commit 4fa2ed4
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 75 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/tc/oc/pgm/PGMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -473,7 +474,7 @@ public String getMapPoolFile() {
}

@Override
public String getIncludesDirectory() {
public @Nullable String getIncludesDirectory() {
return includesDirectory;
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/tc/oc/pgm/api/map/MapSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
8 changes: 8 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<MapInclude> includes;
private final Map<String, MapInclude> includes;

protected static final ThreadLocal<SAXBuilder> DOCUMENT_FACTORY =
ThreadLocal.withInitial(
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -61,15 +61,14 @@ public Collection<MapInclude> getMapIncludes(Document document) throws InvalidXM
List<Element> 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 <include id='name'/> format.");
return Sets.newHashSet();
continue;
}

String id = XMLUtils.getRequiredAttribute(element, "id").getValue();
Expand Down Expand Up @@ -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();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -50,14 +47,12 @@ protected static class SystemMapSource implements MapSource {

private final String dir;
private final AtomicLong modified;
private final Set<StoredMapInclude> storedIncludes;
private final MapIncludeProcessor includes;
private final Set<MapInclude> 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 {
Expand Down Expand Up @@ -151,7 +146,7 @@ public String toString() {
}

@Override
public void addMapInclude(StoredMapInclude include) {
public void addMapInclude(MapInclude include) {
this.storedIncludes.add(include);
}

Expand All @@ -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;
}
}
Expand Down

0 comments on commit 4fa2ed4

Please sign in to comment.