Skip to content

Commit

Permalink
feat(plugins): Try to identify Sponge plugins vs Forge mods.
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
Marco Crespi committed May 10, 2018
1 parent ea908f1 commit fdf67b9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
@ApiModel("PluginContainer")
public interface ICachedPluginContainer extends ICachedObject<PluginContainer> {

enum PluginType {
Unknown, Sponge, Forge, Minecraft,
}

enum PluginState {
Loaded, Unloaded, WillBeLoaded, WillBeUnloaded,
}
Expand All @@ -36,6 +40,9 @@ enum PluginState {
@ApiModelProperty(value = "Other plugins that this plugin depends on", required = true)
Set<ICachedPluginDependency> getDependencies();

@ApiModelProperty(value = "The type of the plugin", required = true)
PluginType getType();

@ApiModelProperty("The file source where the plugin was loaded from.")
String getSource();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import valandur.webapi.api.cache.CachedObject;
import valandur.webapi.api.cache.plugin.ICachedPluginContainer;
Expand Down Expand Up @@ -63,6 +65,12 @@ public Set<ICachedPluginDependency> getDependencies() {
return new HashSet<>(dependencies);
}

private PluginType type;
@Override
public PluginType getType() {
return type;
}

private String source;
@Override
public String getSource() {
Expand All @@ -85,9 +93,11 @@ public CachedPluginContainer(PluginContainer plugin) {
this.version = plugin.getVersion().orElse(null);
this.url = plugin.getUrl().orElse(null);
this.authors = new ArrayList<>(plugin.getAuthors());
plugin.getDependencies().forEach(d -> dependencies.add(new CachedPluginDependency(d)));
plugin.getDependencies().forEach(d -> this.dependencies.add(new CachedPluginDependency(d)));
this.source = plugin.getSource().map(p -> p.normalize().toString()).orElse(null);
this.state = PluginState.Loaded;

this.checkType();
}
public CachedPluginContainer(JsonNode node, Path source) {
super(null);
Expand All @@ -97,10 +107,44 @@ public CachedPluginContainer(JsonNode node, Path source) {
this.description = node.path("description").asText(null);
this.version = node.path("version").asText(null);
this.url = node.path("url").asText(null);
this.authors = new ArrayList<>();
this.dependencies = new HashSet<>();
List<String> authors = new ArrayList<>();
for (JsonNode authorNode : node.path("authorList")) {
authors.add(authorNode.asText());
}
this.authors = authors;
Set<ICachedPluginDependency> deps = new HashSet<>();
for (JsonNode depNode : node.path("requiredMods")) {
deps.add(new CachedPluginDependency(depNode.asText(), true));
}
for (JsonNode depNode : node.path("dependencies")) {
deps.add(new CachedPluginDependency(depNode.asText(), false));
}
this.dependencies = deps;
this.source = source.normalize().toString();
this.state = source.toString().endsWith(".jar") ? PluginState.WillBeLoaded : PluginState.Unloaded;

this.checkType();
}

private void checkType() {
if (this.id.equalsIgnoreCase("minecraft") || this.id.equalsIgnoreCase("mcp")) {
this.type = PluginType.Minecraft;
} else if (this.id.equalsIgnoreCase("sponge") || this.id.equalsIgnoreCase("spongeapi")) {
this.type = PluginType.Sponge;
} else if (this.id.equalsIgnoreCase("fml") || this.id.equalsIgnoreCase("forge")
|| this.id.equalsIgnoreCase("mercurius_updater")) {
this.type = PluginType.Forge;
} else if (this.dependencies.stream().anyMatch(d -> d.getId().contains("sponge"))) {
this.type = PluginType.Sponge;
} else if (this.dependencies.stream().anyMatch(d -> d.getId().contains("forge"))) {
this.type = PluginType.Forge;
} else if (this.dependencies.stream().anyMatch(d -> d.getId().contains("fml"))) {
this.type = PluginType.Forge;
} else if (this.source.contains("plugins")) {
this.type = PluginType.Sponge;
} else {
this.type = PluginType.Unknown;
}
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CachedPluginDependency extends CachedObject<PluginDependency> imple
private String id;
@Override
public String getId() {
return null;
return id;
}

private LoadOrder loadOrder;
Expand Down Expand Up @@ -45,6 +45,12 @@ public CachedPluginDependency(PluginDependency dependency) {
this.loadOrder = dependency.getLoadOrder();
this.optional = dependency.isOptional();
}
public CachedPluginDependency(String id, boolean optional) {
super(null);

this.id = id;
this.optional = optional;
}

@Override
public Optional<PluginDependency> getLive() {
Expand Down

0 comments on commit fdf67b9

Please sign in to comment.