Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuresanSergiu committed Aug 20, 2015
2 parents 887159d + 19cf9b7 commit ab2f9c6
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 69 deletions.
37 changes: 37 additions & 0 deletions src/main/java/myessentials/curse/Curse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package myessentials.curse;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public final class Curse {
private Curse() {
}

/**
* Gets CurseModInfo using the mcf widget api. http://widget.mcf.li/
* @param projectid The CurseForge project ID
* @return The CurseModInfo object
* @throws IOException
*/
public static CurseModInfo getModInfo(String projectid) throws IOException {
URL url = new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectid + ".json");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
return (new Gson()).fromJson(reader, CurseModInfo.class);
}

/**
* Returns a direct download link of the file for the modid
* @param projectid The CurseForge
* @param downloadid The download id
* @return The download URL
* @throws MalformedURLException
*/
public static URL getDownloadURL(String projectid, String downloadid) throws MalformedURLException {
return new URL("http://minecraft.curseforge.com/mc-mods/" + projectid + "/files/" + downloadid + "/download");
}
}
92 changes: 92 additions & 0 deletions src/main/java/myessentials/curse/CurseModInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package myessentials.curse;

import java.util.Map;

public class CurseModInfo {
private String title;
private String game;
private String category;
private String url;
private String thumbnail;
private String[] authors;
// TODO Downloads Counters
private int favorites;
private int likes;
private String updated_at;
private String created_at;
private String project_url;
private String release_type;
private String license;
private VersionInfo download;
private Map<String, VersionInfo[]> versions;

public VersionInfo[] getVersions(String mcVersion) {
return versions.get(mcVersion);
}

public VersionInfo getNewestVersion(String mcVersion) {
VersionInfo newest = null;
for (VersionInfo versionInfo : getVersions(mcVersion)) {
if (newest == null || versionInfo.getId() > newest.getId()) {
newest = versionInfo;
}
}
return newest;
}

public String getTitle() {
return title;
}

public String getGame() {
return game;
}

public String getCategory() {
return category;
}

public String getUrl() {
return url;
}

public String getThumbnail() {
return thumbnail;
}

public String[] getAuthors() {
return authors;
}

public int getFavorites() {
return favorites;
}

public int getLikes() {
return likes;
}

public String getUpdated_at() {
return updated_at;
}

public String getCreated_at() {
return created_at;
}

public String getProject_url() {
return project_url;
}

public String getRelease_type() {
return release_type;
}

public String getLicense() {
return license;
}

public VersionInfo getDownload() {
return download;
}
}
39 changes: 39 additions & 0 deletions src/main/java/myessentials/curse/VersionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package myessentials.curse;

public class VersionInfo {
private int id;
private String url;
private String name;
private String type;
private String version;
private int downloads;
private String created_at;

public int getId() {
return id;
}

public String getUrl() {
return url;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public String getVersion() {
return version;
}

public int getDownloads() {
return downloads;
}

public String getCreated_at() {
return created_at;
}
}
88 changes: 88 additions & 0 deletions src/main/java/myessentials/new_config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package myessentials.new_config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks a class as a Config.
*
* Not currently used, but there are plans!
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Config {
/**
* The filename of the config class
* @return
*/
String value() default "";

/**
* The configuration backend to load (forge or json)
* @return
*/
String backend() default "forge";

/**
* Marks a field to hold the ConfigData
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Instance {
}

/**
* Marks a class as a config group
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Group {
/**
* Returns the name of the category.
* If set to empty string or null, name will be name of the class its attached to.
*
* @return
*/
String name() default "";

/**
* Returns the comment of the category.
*
* @return Comment of the category
*/
String comment() default "";

Class<?>[] classes() default {};
}

/**
* Marks a field as a config property
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Property {
/**
* Returns the name of the property.
* If set to empty string or null, name will be name of the field its attached to.
*
* @return Name of property
*/
String name() default "";

/**
* Returns the comment of the property.
*
* @return Comment of the property
*/
String comment() default "";

/**
* Returns if this property can be changed by commands.
*
* @return Can be changed by commands
*/
boolean command() default true;
}
}
27 changes: 17 additions & 10 deletions src/main/java/myessentials/new_config/ConfigProcessor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package myessentials.new_config;

import myessentials.new_config.annotations.ConfigGroup;
import myessentials.new_config.annotations.ConfigProperty;
import myessentials.new_config.backends.ForgeConfigBackend;
import myessentials.new_config.backends.JsonConfigBackend;
import myessentials.new_config.data.ConfigGroupData;
Expand All @@ -27,13 +25,22 @@ public static ConfigData load(Class<?> clazz, IConfigBackend backend) {
Map<String, ConfigGroupData> groups = genGroups(clazz, null, backend);
ConfigData cfgData = new ConfigData(groups, backend);
cfgData.load();
for (Field f : clazz.getFields()) {
Config.Instance instAnnot = clazz.getAnnotation(Config.Instance.class);
if (instAnnot == null || !ConfigData.class.isAssignableFrom(f.getClass())) continue;
try {
f.set(null, cfgData);
} catch (IllegalAccessException e) {
// TODO Handle this exception nicely?
}
}
return cfgData;
}

private static Map<String, ConfigGroupData> genGroups(Class<?> clazz, String parentFullName, IConfigBackend backend) {
Map<String, ConfigGroupData> groupsMap = new HashMap<String, ConfigGroupData>();
for (Class<?> c : clazz.getClasses()) {
ConfigGroup groupAnnot = getConfigGroupAnnotation(c);
Config.Group groupAnnot = getConfigGroupAnnotation(c);
if (groupAnnot == null) continue;
String groupName = (groupAnnot.name() == null || groupAnnot.name().trim().isEmpty()) ? c.getSimpleName() : groupAnnot.name();
String groupFullName = (parentFullName != null ? (parentFullName + ".") : "") + groupName;
Expand All @@ -59,7 +66,7 @@ private static Map<String, ConfigGroupData> genGroups(Class<?> clazz, String par
private static Map<String, ConfigPropertyData> genProps(Class<?> clazz, String groupFullName, IConfigBackend backend) {
Map<String, ConfigPropertyData> propertiesMap = new HashMap<String, ConfigPropertyData>();
for (Field field : clazz.getFields()) {
ConfigProperty propAnnot = field.getAnnotation(ConfigProperty.class);
Config.Property propAnnot = field.getAnnotation(Config.Property.class);
if (propAnnot == null) continue;
String propName = (propAnnot.name() == null || propAnnot.name().trim().isEmpty()) ? field.getName() : propAnnot.name();
String propFullName = groupFullName + "." + propName;
Expand All @@ -70,13 +77,13 @@ private static Map<String, ConfigPropertyData> genProps(Class<?> clazz, String g
return propertiesMap;
}

private static ConfigGroup getConfigGroupAnnotation(Class<?> clazz) {
ConfigGroup annot = null;
if (clazz.isAnnotationPresent(ConfigGroup.class)) {
annot = clazz.getAnnotation(ConfigGroup.class);
} else if (ConfigGroup.class.isAssignableFrom(clazz)) {
private static Config.Group getConfigGroupAnnotation(Class<?> clazz) {
Config.Group annot = null;
if (clazz.isAnnotationPresent(Config.Group.class)) {
annot = clazz.getAnnotation(Config.Group.class);
} else if (Config.Group.class.isAssignableFrom(clazz)) {
try {
annot = (ConfigGroup) clazz.newInstance();
annot = (Config.Group) clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/myessentials/new_config/annotations/ConfigGroup.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private Object getValue(JsonElement element, Class<?> type) {
for (int i=0; i<jsonArray.size(); i++) {
objArr[i] = getValue(jsonArray.get(i), type.getComponentType());
}
return objArr;
} else if (element.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
if (Boolean.class.isAssignableFrom(type)) {
Expand Down
Loading

0 comments on commit ab2f9c6

Please sign in to comment.