This commit is contained in:
Norbi Peti 2016-10-04 21:40:09 +02:00
parent b94a689a1e
commit f0959c1abe
2 changed files with 54 additions and 4 deletions

View file

@ -134,6 +134,10 @@
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>com.github.TBMCPlugins.ButtonCore</groupId>
<artifactId>ButtonCore</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View file

@ -5,18 +5,23 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.base.Charsets;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import buttondevteam.bucket.core.TBMCCoreAPI;
import sx.blah.discord.api.*;
import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.MissingPermissionsException;
import sx.blah.discord.util.RateLimitException;
/**
* Hello world!
*
*/
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private static final String SubredditURL = "https://www.reddit.com/r/ChromaGamers";
private static boolean stop = false;
@Override
public void onEnable() {
try {
@ -25,6 +30,13 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
cb.withToken(IOUtils.toString(getClass().getResourceAsStream("/Token.txt"), Charsets.UTF_8));
IDiscordClient dc = cb.login();
dc.getDispatcher().registerListener(this);
Runnable r = new Runnable() {
public void run() {
FlairGetterThreadMethod();
}
};
Thread t = new Thread(r);
t.start();
} catch (Exception e) {
e.printStackTrace();
Bukkit.getPluginManager().disablePlugin(this);
@ -39,4 +51,38 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
e.printStackTrace();
}
}
@Override
public void onDisable() {
stop = true;
}
private void FlairGetterThreadMethod() {
while (!stop) {
try {
String body = TBMCCoreAPI.DownloadString(SubredditURL + ".json?limit=10"); // TODO: Save last announcement
JsonArray json = new JsonParser().parse(body).getAsJsonArray().get(1).getAsJsonObject().get("data")
.getAsJsonObject().get("children").getAsJsonArray();
for (Object obj : json) {
JsonObject item = (JsonObject) obj;
String author = item.get("data").getAsJsonObject().get("author").getAsString();
String post = item.get("data").getAsJsonObject().get("body").getAsString();
System.out.println("author: " + author);
System.out.println("post: " + post);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}