Skip to content

Commit 943196e

Browse files
committed
v1.2.1 Update
1 parent 29c2a2a commit 943196e

14 files changed

Lines changed: 430 additions & 121 deletions

File tree

.idea/artifacts/CubicCountdown.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
<id>sonatype</id>
6262
<url>https://oss.sonatype.org/content/groups/public/</url>
6363
</repository>
64+
<repository>
65+
<id>placeholderapi</id>
66+
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
67+
</repository>
6468
</repositories>
6569

6670
<dependencies>
@@ -70,5 +74,21 @@
7074
<version>1.19.4-R0.1-SNAPSHOT</version>
7175
<scope>provided</scope>
7276
</dependency>
77+
<dependency>
78+
<groupId>me.clip</groupId>
79+
<artifactId>placeholderapi</artifactId>
80+
<version>2.11.3</version>
81+
<scope>provided</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.httpcomponents</groupId>
85+
<artifactId>httpclient</artifactId>
86+
<version>4.5.14</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.json</groupId>
90+
<artifactId>json</artifactId>
91+
<version>20230618</version>
92+
</dependency>
7393
</dependencies>
7494
</project>

src/main/java/de/timecoding/cc/CubicCountdown.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.timecoding.cc;
22

3+
import de.timecoding.cc.api.AutoUpdater;
34
import de.timecoding.cc.api.CubicExpansion;
45
import de.timecoding.cc.api.Metrics;
56
import de.timecoding.cc.command.CubicCommand;
@@ -10,11 +11,18 @@
1011
import de.timecoding.cc.listener.CubicListener;
1112
import de.timecoding.cc.util.CountdownModule;
1213
import de.timecoding.cc.util.CubicAPI;
14+
import org.apache.http.HttpResponse;
15+
import org.apache.http.client.methods.HttpGet;
16+
import org.apache.http.impl.client.CloseableHttpClient;
17+
import org.apache.http.impl.client.HttpClientBuilder;
18+
import org.apache.http.util.EntityUtils;
1319
import org.bukkit.Bukkit;
1420
import org.bukkit.command.PluginCommand;
1521
import org.bukkit.entity.Player;
1622
import org.bukkit.plugin.java.JavaPlugin;
23+
import org.json.JSONObject;
1724

25+
import java.io.IOException;
1826
import java.util.HashMap;
1927
import java.util.List;
2028

@@ -27,13 +35,16 @@ public class CubicCountdown extends JavaPlugin {
2735
//FOR API USAGE ONLY
2836
private CubicCountdown plugin;
2937
private CubicListener cubicListener;
38+
39+
private AutoUpdater autoUpdater;
3040
private Metrics metrics = null;
3141

3242
public void onEnable() {
3343
this.plugin = this;
3444
this.configHandler = new ConfigHandler(this);
3545
this.dataHandler = new DataHandler(this);
3646
this.cubicListener = new CubicListener(this);
47+
this.autoUpdater = new AutoUpdater(this, plugin.getDescription().getVersion());
3748
this.getServer().getPluginManager().registerEvents(this.cubicListener, this);
3849
cubicAPI = new CubicAPI(this);
3950
PluginCommand pluginCommand = this.getCommand("cubiccountdown");
@@ -58,6 +69,7 @@ public void onEnable() {
5869
}
5970
}
6071

72+
6173
public ConfigHandler getConfigHandler() {
6274
return configHandler;
6375
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package de.timecoding.cc.api;
2+
3+
import de.timecoding.cc.CubicCountdown;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.client.methods.HttpGet;
6+
import org.apache.http.impl.client.CloseableHttpClient;
7+
import org.apache.http.impl.client.HttpClientBuilder;
8+
import org.apache.http.util.EntityUtils;
9+
import org.bukkit.Bukkit;
10+
import org.bukkit.ChatColor;
11+
import org.json.JSONObject;
12+
13+
import java.io.*;
14+
import java.net.URL;
15+
16+
public class AutoUpdater {
17+
18+
19+
private final CubicCountdown plugin;
20+
private final String downloadBase = "https://github.com/TimeCodings/CubicCountdown/releases/download/";
21+
private final String pluginVersion;
22+
private boolean autoUpdaterEnabled = true;
23+
private final String newestPluginVersion;
24+
private final boolean sent = false;
25+
26+
public AutoUpdater(CubicCountdown plugin, String newestPluginVersion) {
27+
this.plugin = plugin;
28+
this.pluginVersion = plugin.getDescription().getVersion();
29+
if(this.plugin.getConfigHandler().keyExists("AutoUpdater")) {
30+
this.autoUpdaterEnabled = this.plugin.getConfigHandler().getBoolean("AutoUpdater");
31+
}
32+
this.newestPluginVersion = newestPluginVersion;
33+
checkForPluginUpdate();
34+
}
35+
36+
public String getNewestPluginVersion() {
37+
String url = "https://api.github.com/repos/TimeCodings/CubicCountdown/releases/latest";
38+
try {
39+
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
40+
HttpGet request = new HttpGet(url);
41+
request.addHeader("content-type", "application/vnd.github.v3+json");
42+
HttpResponse result = httpClient.execute(request);
43+
JSONObject json = new JSONObject(EntityUtils.toString(result.getEntity(), "UTF-8"));
44+
if(json != null){
45+
return json.get("tag_name").toString();
46+
}
47+
} catch (IOException ex) {
48+
Bukkit.getConsoleSender().sendMessage("§cCould not fetch the newest §eCubicCountdown §crelease! You may be offline!");
49+
}
50+
return pluginVersion;
51+
}
52+
53+
public boolean pluginUpdateAvailable() {
54+
return !getNewestPluginVersion().replace("v", "").equalsIgnoreCase(pluginVersion);
55+
}
56+
57+
public void checkForPluginUpdate() {
58+
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Checking for updates...");
59+
if (pluginUpdateAvailable()) {
60+
if (autoUpdaterEnabled) {
61+
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Update found! (" + getNewestPluginVersion() + ") Trying to download the newest update...");
62+
//Trying to download update
63+
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, new Runnable() {
64+
@Override
65+
public void run() {
66+
downloadUpdate();
67+
}
68+
});
69+
} else {
70+
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Update found (" + getNewestPluginVersion() + ")! You can download it here: "+ChatColor.YELLOW+this.downloadBase + ""+getNewestPluginVersion()+"/CubicCountdown.jar");
71+
}
72+
} else {
73+
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "No update found! You're running the latest version (v" + pluginVersion + ")");
74+
}
75+
}
76+
77+
private boolean downloadUpdate() {
78+
try {
79+
URL download = new URL(this.downloadBase+getNewestPluginVersion()+"/CubicCountdown.jar?timestamp=" + System.currentTimeMillis());
80+
BufferedInputStream in = null;
81+
FileOutputStream fout = null;
82+
BufferedOutputStream bout = null;
83+
try {
84+
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Trying to download the newest CubicCountdown update...");
85+
in = new BufferedInputStream(download.openStream());
86+
fout = new FileOutputStream("plugins//" + this.getPluginNameByJar());
87+
bout = new BufferedOutputStream(fout);
88+
final byte[] data = new byte[1024];
89+
int count;
90+
while ((count = in.read(data, 0, 1024)) >= 0) {
91+
bout.write(data, 0, count);
92+
}
93+
} catch (Exception e) {
94+
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Failed to download the newest CubicCountdown update!");
95+
return false;
96+
} finally {
97+
if (in != null) {
98+
in.close();
99+
}
100+
if (bout != null) {
101+
bout.close();
102+
}
103+
}
104+
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Successfully downloaded the CubicCountdown update!");
105+
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "The plugin will now try to reload the server...");
106+
Bukkit.reload();
107+
return true;
108+
} catch (IOException e) {
109+
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Failed to download the newest CubicCountdown update!");
110+
return false;
111+
}
112+
}
113+
114+
private String getPluginNameByJar() {
115+
return new File(CubicCountdown.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName();
116+
}
117+
118+
}

0 commit comments

Comments
 (0)