Skip to content

Commit ab2f9c6

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 887159d + 19cf9b7 commit ab2f9c6

File tree

12 files changed

+439
-69
lines changed

12 files changed

+439
-69
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package myessentials.curse;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
11+
public final class Curse {
12+
private Curse() {
13+
}
14+
15+
/**
16+
* Gets CurseModInfo using the mcf widget api. http://widget.mcf.li/
17+
* @param projectid The CurseForge project ID
18+
* @return The CurseModInfo object
19+
* @throws IOException
20+
*/
21+
public static CurseModInfo getModInfo(String projectid) throws IOException {
22+
URL url = new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectid + ".json");
23+
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
24+
return (new Gson()).fromJson(reader, CurseModInfo.class);
25+
}
26+
27+
/**
28+
* Returns a direct download link of the file for the modid
29+
* @param projectid The CurseForge
30+
* @param downloadid The download id
31+
* @return The download URL
32+
* @throws MalformedURLException
33+
*/
34+
public static URL getDownloadURL(String projectid, String downloadid) throws MalformedURLException {
35+
return new URL("http://minecraft.curseforge.com/mc-mods/" + projectid + "/files/" + downloadid + "/download");
36+
}
37+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package myessentials.curse;
2+
3+
import java.util.Map;
4+
5+
public class CurseModInfo {
6+
private String title;
7+
private String game;
8+
private String category;
9+
private String url;
10+
private String thumbnail;
11+
private String[] authors;
12+
// TODO Downloads Counters
13+
private int favorites;
14+
private int likes;
15+
private String updated_at;
16+
private String created_at;
17+
private String project_url;
18+
private String release_type;
19+
private String license;
20+
private VersionInfo download;
21+
private Map<String, VersionInfo[]> versions;
22+
23+
public VersionInfo[] getVersions(String mcVersion) {
24+
return versions.get(mcVersion);
25+
}
26+
27+
public VersionInfo getNewestVersion(String mcVersion) {
28+
VersionInfo newest = null;
29+
for (VersionInfo versionInfo : getVersions(mcVersion)) {
30+
if (newest == null || versionInfo.getId() > newest.getId()) {
31+
newest = versionInfo;
32+
}
33+
}
34+
return newest;
35+
}
36+
37+
public String getTitle() {
38+
return title;
39+
}
40+
41+
public String getGame() {
42+
return game;
43+
}
44+
45+
public String getCategory() {
46+
return category;
47+
}
48+
49+
public String getUrl() {
50+
return url;
51+
}
52+
53+
public String getThumbnail() {
54+
return thumbnail;
55+
}
56+
57+
public String[] getAuthors() {
58+
return authors;
59+
}
60+
61+
public int getFavorites() {
62+
return favorites;
63+
}
64+
65+
public int getLikes() {
66+
return likes;
67+
}
68+
69+
public String getUpdated_at() {
70+
return updated_at;
71+
}
72+
73+
public String getCreated_at() {
74+
return created_at;
75+
}
76+
77+
public String getProject_url() {
78+
return project_url;
79+
}
80+
81+
public String getRelease_type() {
82+
return release_type;
83+
}
84+
85+
public String getLicense() {
86+
return license;
87+
}
88+
89+
public VersionInfo getDownload() {
90+
return download;
91+
}
92+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package myessentials.curse;
2+
3+
public class VersionInfo {
4+
private int id;
5+
private String url;
6+
private String name;
7+
private String type;
8+
private String version;
9+
private int downloads;
10+
private String created_at;
11+
12+
public int getId() {
13+
return id;
14+
}
15+
16+
public String getUrl() {
17+
return url;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public String getType() {
25+
return type;
26+
}
27+
28+
public String getVersion() {
29+
return version;
30+
}
31+
32+
public int getDownloads() {
33+
return downloads;
34+
}
35+
36+
public String getCreated_at() {
37+
return created_at;
38+
}
39+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package myessentials.new_config;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Marks a class as a Config.
10+
*
11+
* Not currently used, but there are plans!
12+
*/
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target(ElementType.TYPE)
15+
public @interface Config {
16+
/**
17+
* The filename of the config class
18+
* @return
19+
*/
20+
String value() default "";
21+
22+
/**
23+
* The configuration backend to load (forge or json)
24+
* @return
25+
*/
26+
String backend() default "forge";
27+
28+
/**
29+
* Marks a field to hold the ConfigData
30+
*/
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target(ElementType.FIELD)
33+
@interface Instance {
34+
}
35+
36+
/**
37+
* Marks a class as a config group
38+
*/
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Target(ElementType.TYPE)
41+
@interface Group {
42+
/**
43+
* Returns the name of the category.
44+
* If set to empty string or null, name will be name of the class its attached to.
45+
*
46+
* @return
47+
*/
48+
String name() default "";
49+
50+
/**
51+
* Returns the comment of the category.
52+
*
53+
* @return Comment of the category
54+
*/
55+
String comment() default "";
56+
57+
Class<?>[] classes() default {};
58+
}
59+
60+
/**
61+
* Marks a field as a config property
62+
*/
63+
@Retention(RetentionPolicy.RUNTIME)
64+
@Target(ElementType.FIELD)
65+
@interface Property {
66+
/**
67+
* Returns the name of the property.
68+
* If set to empty string or null, name will be name of the field its attached to.
69+
*
70+
* @return Name of property
71+
*/
72+
String name() default "";
73+
74+
/**
75+
* Returns the comment of the property.
76+
*
77+
* @return Comment of the property
78+
*/
79+
String comment() default "";
80+
81+
/**
82+
* Returns if this property can be changed by commands.
83+
*
84+
* @return Can be changed by commands
85+
*/
86+
boolean command() default true;
87+
}
88+
}

src/main/java/myessentials/new_config/ConfigProcessor.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package myessentials.new_config;
22

3-
import myessentials.new_config.annotations.ConfigGroup;
4-
import myessentials.new_config.annotations.ConfigProperty;
53
import myessentials.new_config.backends.ForgeConfigBackend;
64
import myessentials.new_config.backends.JsonConfigBackend;
75
import myessentials.new_config.data.ConfigGroupData;
@@ -27,13 +25,22 @@ public static ConfigData load(Class<?> clazz, IConfigBackend backend) {
2725
Map<String, ConfigGroupData> groups = genGroups(clazz, null, backend);
2826
ConfigData cfgData = new ConfigData(groups, backend);
2927
cfgData.load();
28+
for (Field f : clazz.getFields()) {
29+
Config.Instance instAnnot = clazz.getAnnotation(Config.Instance.class);
30+
if (instAnnot == null || !ConfigData.class.isAssignableFrom(f.getClass())) continue;
31+
try {
32+
f.set(null, cfgData);
33+
} catch (IllegalAccessException e) {
34+
// TODO Handle this exception nicely?
35+
}
36+
}
3037
return cfgData;
3138
}
3239

3340
private static Map<String, ConfigGroupData> genGroups(Class<?> clazz, String parentFullName, IConfigBackend backend) {
3441
Map<String, ConfigGroupData> groupsMap = new HashMap<String, ConfigGroupData>();
3542
for (Class<?> c : clazz.getClasses()) {
36-
ConfigGroup groupAnnot = getConfigGroupAnnotation(c);
43+
Config.Group groupAnnot = getConfigGroupAnnotation(c);
3744
if (groupAnnot == null) continue;
3845
String groupName = (groupAnnot.name() == null || groupAnnot.name().trim().isEmpty()) ? c.getSimpleName() : groupAnnot.name();
3946
String groupFullName = (parentFullName != null ? (parentFullName + ".") : "") + groupName;
@@ -59,7 +66,7 @@ private static Map<String, ConfigGroupData> genGroups(Class<?> clazz, String par
5966
private static Map<String, ConfigPropertyData> genProps(Class<?> clazz, String groupFullName, IConfigBackend backend) {
6067
Map<String, ConfigPropertyData> propertiesMap = new HashMap<String, ConfigPropertyData>();
6168
for (Field field : clazz.getFields()) {
62-
ConfigProperty propAnnot = field.getAnnotation(ConfigProperty.class);
69+
Config.Property propAnnot = field.getAnnotation(Config.Property.class);
6370
if (propAnnot == null) continue;
6471
String propName = (propAnnot.name() == null || propAnnot.name().trim().isEmpty()) ? field.getName() : propAnnot.name();
6572
String propFullName = groupFullName + "." + propName;
@@ -70,13 +77,13 @@ private static Map<String, ConfigPropertyData> genProps(Class<?> clazz, String g
7077
return propertiesMap;
7178
}
7279

73-
private static ConfigGroup getConfigGroupAnnotation(Class<?> clazz) {
74-
ConfigGroup annot = null;
75-
if (clazz.isAnnotationPresent(ConfigGroup.class)) {
76-
annot = clazz.getAnnotation(ConfigGroup.class);
77-
} else if (ConfigGroup.class.isAssignableFrom(clazz)) {
80+
private static Config.Group getConfigGroupAnnotation(Class<?> clazz) {
81+
Config.Group annot = null;
82+
if (clazz.isAnnotationPresent(Config.Group.class)) {
83+
annot = clazz.getAnnotation(Config.Group.class);
84+
} else if (Config.Group.class.isAssignableFrom(clazz)) {
7885
try {
79-
annot = (ConfigGroup) clazz.newInstance();
86+
annot = (Config.Group) clazz.newInstance();
8087
} catch (Exception e) {
8188
e.printStackTrace();
8289
}

src/main/java/myessentials/new_config/annotations/ConfigGroup.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/myessentials/new_config/annotations/ConfigProperty.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/myessentials/new_config/backends/JsonConfigBackend.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private Object getValue(JsonElement element, Class<?> type) {
8181
for (int i=0; i<jsonArray.size(); i++) {
8282
objArr[i] = getValue(jsonArray.get(i), type.getComponentType());
8383
}
84+
return objArr;
8485
} else if (element.isJsonPrimitive()) {
8586
JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
8687
if (Boolean.class.isAssignableFrom(type)) {

0 commit comments

Comments
 (0)