Added FileOrLink class

This commit is contained in:
Timo Ley 2020-07-27 23:14:19 +02:00
parent 123e1f49cb
commit 30c7ee114a
2 changed files with 55 additions and 7 deletions

View File

@ -9,7 +9,6 @@ import ley.anvil.addonscript.curse.CurseMeta;
import ley.anvil.addonscript.forge.ForgeMeta;
import ley.anvil.addonscript.installer.IInstaller;
import ley.anvil.addonscript.installer.InternalDirInstaller;
import ley.anvil.addonscript.util.HTTPRequest;
import ley.anvil.addonscript.util.Utils;
import ley.anvil.addonscript.v1.AddonscriptJSON;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
@ -196,14 +195,10 @@ public class ASWrapper {
return Utils.notEmpty(link) && link.startsWith("file://");
}
public File asFile(String jsonDir) {
if (isFile()) {
return new File(Utils.slashEnd(jsonDir) + link.replace("file://", ""));
}
throw new RuntimeException("This is not a local file, try to check it before calling it.");
public FileOrLink get() {
return new FileOrLink(getLink());
}
@HTTPRequest
public String getLink() {
if (!Utils.notEmpty(link) && Utils.notEmpty(file.artifact)) {
String l = getArtifact().getPath();

View File

@ -0,0 +1,53 @@
package ley.anvil.addonscript.wrapper;
import ley.anvil.addonscript.util.Utils;
import javax.annotation.Nonnull;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class FileOrLink {
String link;
public FileOrLink(String link) {
this.link = link;
}
public boolean isFile() {
return Utils.notEmpty(link) && link.startsWith("file://");
}
public boolean isURL() {
return Utils.notEmpty(link) && (link.startsWith("http://") || link.startsWith("https://"));
}
public String getLink() {
return link;
}
/**
* In Addonscript file:// links are relative paths from the Addonscript.json,
* so you have to specify the path of the directory in which it is placed.
* @param path The path to the directory, in which the Addonscript.json is placed
* @return A File
*/
public File getFile(@Nonnull String path) {
if (isFile())
return new File(Utils.slashEnd(path) + link.replace("file://", ""));
throw new RuntimeException("This is no file");
}
public URL getURL() {
if (isURL()) {
try {
return new URL(link);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
throw new RuntimeException("This is no URL");
}
}