This commit is contained in:
Lennart ten Wolde 2016-02-24 16:50:05 +01:00
parent fe0467f17d
commit a45d264f15
10 changed files with 103 additions and 296 deletions

6
latest_version.json Normal file
View file

@ -0,0 +1,6 @@
{
"name": "2.0",
"type": "release",
"gameVersion": "1.8.8",
"downloadURL": "TODO"
}

123
pom.xml
View file

@ -40,19 +40,12 @@
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.spigotmc</groupId>-->
<!--<artifactId>spigot</artifactId>-->
<!--<version>1.8-R0.1-SNAPSHOT</version>-->
<!--<scope>system</scope>-->
<!--<systemPath>${project.basedir}/lib/spigot.jar</systemPath>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.google.guava</groupId>-->
<!--<artifactId>guava</artifactId>-->
<!--<version>17.0</version>-->
<!--<scope>compile</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.lenis0012.updater</groupId>
<artifactId>updater-api</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -61,6 +54,87 @@
</dependency>
</dependencies>
<profiles>
<profile>
<id>bukkit</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.lenis0012.updater</groupId>
<artifactId>updater-bukkit</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>Marriage-Bukkit</finalName>
<artifactSet>
<includes>
<include>com.lenis0012.updater:updater-api</include>
<include>com.lenis0012.updater:updater-bukkit</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>spigot</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.lenis0012.updater</groupId>
<artifactId>updater-spigot</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>Marriage-Spigot</finalName>
<artifactSet>
<includes>
<include>com.lenis0012.updater:updater-api</include>
<include>com.lenis0012.updater:updater-spigot</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!-- Build -->
<build>
<!-- Resources -->
@ -73,7 +147,7 @@
</includes>
</resource>
</resources>
<defaultGoal>clean install</defaultGoal>
<!--<defaultGoal>install</defaultGoal>-->
<finalName>${project.name}</finalName>
<!-- Plugins -->
<plugins>
@ -98,27 +172,6 @@
<show>public</show>
</configuration>
</plugin>
<!-- Shade guava to make sure we have the reflection API even on older versions of bukkit -->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-shade-plugin</artifactId>-->
<!--<version>2.2</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>shade</goal>-->
<!--</goals>-->
<!--<configuration>-->
<!--<artifactSet>-->
<!--<includes>-->
<!--<include>com.google.guava:guava</include>-->
<!--</includes>-->
<!--</artifactSet>-->
<!--</configuration>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>
</project>

View file

@ -3,8 +3,8 @@ package com.lenis0012.bukkit.marriage2.commands;
import com.lenis0012.bukkit.marriage2.Marriage;
import com.lenis0012.bukkit.marriage2.config.Settings;
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
import com.lenis0012.bukkit.marriage2.misc.update.Updater;
import com.lenis0012.bukkit.marriage2.misc.update.Version;
import com.lenis0012.updater.api.Updater;
import com.lenis0012.updater.api.Version;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;

View file

@ -4,14 +4,12 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.lenis0012.bukkit.marriage2.MData;
import com.lenis0012.bukkit.marriage2.commands.CommandPriest;
import com.lenis0012.bukkit.marriage2.internal.Register.Type;
import com.lenis0012.bukkit.marriage2.internal.data.DataConverter;
import com.lenis0012.bukkit.marriage2.misc.update.Updater;
import com.lenis0012.updater.api.Updater;
import com.lenis0012.updater.api.UpdaterFactory;
import org.bukkit.event.Listener;
import com.lenis0012.bukkit.marriage2.MPlayer;
@ -66,7 +64,8 @@ public class MarriageCore extends MarriageBase {
@Register(name = "updater", type = Type.ENABLE, priority = 9)
public void loadUpdater() {
this.updater = new Updater(this, MarriagePlugin.MARRIAGE_MOD_ID, plugin.getPluginFile());
UpdaterFactory factory = new UpdaterFactory(plugin);
this.updater = factory.newUpdater(plugin.getPluginFile(), Settings.ENABLE_UPDATE_CHACKER.value());
}
@Register(name = "converter", type = Register.Type.ENABLE, priority = 10)

View file

@ -11,7 +11,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.lenis0012.bukkit.marriage2.Marriage;
public class MarriagePlugin extends JavaPlugin {
public static final int MARRIAGE_MOD_ID = 44364;
private static MarriageCore core;
public static Marriage getInstance() {

View file

@ -2,8 +2,8 @@ package com.lenis0012.bukkit.marriage2.listeners;
import com.lenis0012.bukkit.marriage2.config.Message;
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
import com.lenis0012.bukkit.marriage2.misc.update.Updater;
import com.lenis0012.bukkit.marriage2.misc.update.Version;
import com.lenis0012.updater.api.Updater;
import com.lenis0012.updater.api.Version;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

View file

@ -1,7 +0,0 @@
package com.lenis0012.bukkit.marriage2.misc.update;
public enum ReleaseType {
ALPHA,
BETA,
RELEASE
}

View file

@ -1,213 +0,0 @@
package com.lenis0012.bukkit.marriage2.misc.update;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lenis0012.bukkit.marriage2.config.Settings;
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
import com.lenis0012.bukkit.marriage2.internal.MarriagePlugin;
import com.lenis0012.bukkit.marriage2.misc.BConfig;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;
import java.util.logging.Level;
public class Updater {
private static final long UPDATE_CACHE = TimeUnit.HOURS.toMillis(3); // Cache for 3 hours
private static final String BASE_URL = "https://api.curseforge.com";
private static final String API_FILES = "/servermods/files?projectIds=";
private final int projectId;
private final JsonParser jsonParser;
private String currentVersion;
private final File pluginFile;
private boolean enabled;
private String apiKey;
private Version newVersion = null;
private boolean isOutdated = false;
private long lastUpdateCheck = 0L;
private ItemStack changelog;
public Updater(MarriageCore core, int projectId, File pluginFile) {
MarriagePlugin plugin = core.getPlugin();
this.projectId = projectId;
this.jsonParser = new JsonParser();
this.currentVersion = plugin.getDescription().getVersion();
this.enabled = Settings.ENABLE_UPDATE_CHACKER.value();
this.pluginFile = pluginFile;
// Support for gravity updater
File updateFile = new File(new File(plugin.getDataFolder().getParentFile(), "Updater"), "config.yml");
if(updateFile.exists()) {
BConfig config = new BConfig(core, updateFile);
this.apiKey = config.get("api-key", String.class);
// this.enabled = !config.getOrDefault("disable", false);
}
}
public boolean hasUpdate() {
if(!enabled) return false;
if(lastUpdateCheck < System.currentTimeMillis()) {
this.lastUpdateCheck = System.currentTimeMillis() + UPDATE_CACHE;
read();
}
return !isOutdated;
}
public ItemStack getChangelog() {
return changelog;
}
public Version getNewVersion() {
return newVersion;
}
public String downloadVersion() {
if(newVersion == null) return "No new version available!";
InputStream input = null;
FileOutputStream output = null;
MarriagePlugin.getInstance().getLogger().log(Level.INFO, "Downloading update " + newVersion.getName());
try {
Bukkit.getUpdateFolderFile().mkdir();
URL url = new URL(newVersion.getDownloadURL());
input = url.openStream();
File dest = new File(Bukkit.getUpdateFolderFile(), pluginFile.getName());
output = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while((length = input.read(buffer, 0, buffer.length)) != -1) {
output.write(buffer, 0, length);
}
// Don't warn players again hehehe :)
this.currentVersion = newVersion.getName();
this.isOutdated = false;
readChangelog(dest); // Try to read changelog
MarriagePlugin.getInstance().getLogger().log(Level.INFO, "Download complete");
return null;
} catch(IOException e) {
MarriagePlugin.getInstance().getLogger().log(Level.WARNING, "Failed to download new file", e);
return e.getMessage();
} finally {
if(input != null) {
try {
input.close();
} catch(IOException e) {
}
} if(output != null) {
try {
output.close();
} catch(IOException e) {
}
}
}
}
private void read() {
try {
URLConnection connection = new URL(BASE_URL + API_FILES + projectId).openConnection();
connection.addRequestProperty("User-Agent", "BukkitUpdater/v1 (by lenis0012)");
if(apiKey != null) {
connection.addRequestProperty("X-API-Key", apiKey);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
JsonArray files = jsonParser.parse(builder.toString()).getAsJsonArray();
JsonObject latest = files.get(files.size() - 1).getAsJsonObject();
String name = latest.get("name").getAsString();
ReleaseType type = ReleaseType.valueOf(latest.get("releaseType").getAsString().toUpperCase());
String serverVersion = latest.get("gameVersion").getAsString();
String downloadURL = latest.get("downloadUrl").getAsString();
this.newVersion = new Version(name, type, serverVersion, downloadURL);
this.isOutdated = !compateVersions(currentVersion, name);
} catch(IOException e) {
MarriagePlugin.getInstance().getLogger().log(Level.WARNING, "Failed to check for updates", e);
}
}
/**
* Read changelog from file inside of jar called changelog.json.
*
* @param file Jar File to read from
*/
private void readChangelog(File file) {
ItemStack book = new ItemStack(Material.WRITTEN_BOOK, 1);
BookMeta meta = (BookMeta) book.getItemMeta();
meta.setAuthor("lenis0012");
meta.setTitle(currentVersion + " Changelog");
JsonObject json;
JarFile jarFile = null;
try {
jarFile = new JarFile(file);
InputStream input = jarFile.getInputStream(jarFile.getEntry("changelog.json"));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
json = jsonParser.parse(builder.toString()).getAsJsonObject();
} catch(Exception e) {
MarriagePlugin.getInstance().getLogger().log(Level.WARNING, "Failed to read jar file", e);
return;
} finally {
if(jarFile != null) {
try {
jarFile.close();
} catch(IOException e) {
}
}
}
if(!json.get("version").getAsString().equalsIgnoreCase(newVersion.getName())) {
// Changelog outdated, don't show
return;
}
JsonArray pages = json.get("data").getAsJsonArray();
for(int i = 0; i < pages.size(); i++) {
JsonArray lines = pages.get(i).getAsJsonArray();
StringBuilder page = new StringBuilder();
for(int j = 0; j < lines.size(); j++) {
page.append(lines.get(j).getAsString()).append('\n');
}
page.setLength(page.length() - 1);
meta.addPage(page.toString());
}
book.setItemMeta(meta);
this.changelog = book;
}
private boolean compateVersions(String oldVersion, String newVersion) {
int oldId = matchLength(oldVersion, newVersion);
int newId = matchLength(newVersion, oldVersion);
return newId > oldId;
}
private int matchLength(String a, String b) {
a = a.replaceAll("[^0-9]", "");
b = b.replaceAll("[^0-9]", "");
while(a.length() < b.length()) {
a += "0";
}
return Integer.parseInt(a);
}
}

View file

@ -1,31 +0,0 @@
package com.lenis0012.bukkit.marriage2.misc.update;
public class Version {
private final String name;
private final ReleaseType type;
private final String serverVersion;
private final String downloadURL;
public Version(String name, ReleaseType type, String serverVersion, String downloadURL) {
this.name = name;
this.type = type;
this.serverVersion = serverVersion;
this.downloadURL = downloadURL;
}
public String getName() {
return name;
}
public ReleaseType getType() {
return type;
}
public String getServerVersion() {
return serverVersion;
}
protected String getDownloadURL() {
return downloadURL;
}
}

View file

@ -0,0 +1 @@
${project.updaterInfo}