Skip to content

Commit 71abdb3

Browse files
committed
/pit downloadを実装
1 parent 1bbe461 commit 71abdb3

File tree

4 files changed

+109
-38
lines changed

4 files changed

+109
-38
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@
140140
<version>23.0.0</version>
141141
</dependency>
142142
<dependency>
143-
<groupId>net.william278</groupId>
143+
<groupId>com.github.william278</groupId>
144144
<artifactId>Annotaml</artifactId>
145-
<version>2.0.1</version>
146-
<scope>compile</scope>
145+
<version>1.0.0</version>
146+
<scope>system</scope>
147+
<systemPath>${project.basedir}/lib/Annotaml.jar</systemPath>
147148
</dependency>
148149
</dependencies>
149150
</project>

src/main/java/com/github/elic0de/thejpspit/spigot/command/PitCommand.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.elic0de.thejpspit.spigot.player.PitPlayer;
1313
import com.github.elic0de.thejpspit.spigot.player.PitPlayerManager;
1414
import com.github.elic0de.thejpspit.spigot.util.LocationData;
15+
import com.github.elic0de.thejpspit.spigot.util.DownloadUtil;
1516
import com.github.elic0de.thejpspit.spigot.villager.VillagerNPCManager;
1617
import org.bukkit.entity.Player;
1718

@@ -60,4 +61,17 @@ public void onGetItem(Player player, String itemId) {
6061
if (entry == null) return;
6162
player.getInventory().addItem(entry.getItemStack());
6263
}
64+
65+
@Subcommand("download")
66+
@CommandPermission("tjp.download")
67+
public void onDownload(Player player) {
68+
DownloadUtil.download(player);
69+
}
70+
71+
@Subcommand("reload")
72+
@CommandPermission("tjp.reload")
73+
public void onReload(Player player) {
74+
TheJpsPit.getInstance().reload();
75+
player.sendMessage("リロードしました");
76+
}
6377
}
Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
11
package com.github.elic0de.thejpspit.spigot.config;
22

3-
import net.william278.annotaml.YamlComment;
43
import net.william278.annotaml.YamlFile;
54
import net.william278.annotaml.YamlKey;
65

76
@YamlFile()
87
public class Settings {
98

10-
// Database settings
11-
/*@YamlComment("Database connection settings")
12-
@YamlKey("database.type")
13-
private Database.Type databaseType = Database.Type.SQLITE;*/
9+
@YamlKey("github_token")
10+
private String githubToken = "";
1411

15-
@YamlKey("database.mysql.credentials.host")
16-
private String mySqlHost = "localhost";
17-
18-
@YamlKey("database.mysql.credentials.port")
19-
private int mySqlPort = 3306;
20-
21-
@YamlKey("database.mysql.credentials.database")
22-
private String mySqlDatabase = "TheJpsPit";
23-
24-
@YamlKey("database.mysql.credentials.username")
25-
private String mySqlUsername = "root";
26-
27-
@YamlKey("database.mysql.credentials.password")
28-
private String mySqlPassword = "pa55w0rd";
29-
30-
@YamlComment("MySQL connection pool properties")
31-
@YamlKey("database.mysql.connection_pool.size")
32-
private int mySqlConnectionPoolSize = 10;
33-
34-
@YamlKey("database.mysql.connection_pool.idle")
35-
private int mySqlConnectionPoolIdle = 10;
36-
37-
@YamlKey("database.mysql.connection_pool.lifetime")
38-
private long mySqlConnectionPoolLifetime = 1800000;
39-
40-
@YamlKey("database.mysql.connection_pool.keepalive")
41-
private long mySqlConnectionPoolKeepAlive = 30000;
42-
43-
@YamlKey("database.mysql.connection_pool.timeout")
44-
private long mySqlConnectionPoolTimeout = 20000;
12+
public String getGithubToken() {
13+
return githubToken;
14+
}
4515
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.github.elic0de.thejpspit.spigot.util;
2+
3+
import com.github.elic0de.thejpspit.spigot.TheJpsPit;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParser;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
import java.net.URL;
13+
import java.net.URLConnection;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.StandardCopyOption;
17+
import org.bukkit.ChatColor;
18+
import org.bukkit.entity.Player;
19+
import org.bukkit.scheduler.BukkitRunnable;
20+
21+
public class DownloadUtil {
22+
23+
private static final String GITHUB_REPO_RELEASES = "https://api.github.com/repos/JavaJava19/TheJpsPit/releases";
24+
private static final String GITHUB_RELEASES_LATEST_URL = GITHUB_REPO_RELEASES + "/latest";
25+
26+
// https://www.spigotmc.org/threads/auto-updater-using-github.324545/
27+
public static void download(final Player player){
28+
try {
29+
final String TOKEN = TheJpsPit.getInstance().getSettings().getGithubToken();
30+
31+
if (TOKEN.equalsIgnoreCase("")) {
32+
player.sendMessage("トークンを設定してください");
33+
return;
34+
}
35+
final URL api = new URL(GITHUB_RELEASES_LATEST_URL); //You want to set //this to your own project URL
36+
final URLConnection con = api.openConnection();
37+
con.setRequestProperty("Accept", "application/vnd.github+json");
38+
con.setRequestProperty("Authorization", "Bearer %token%".replaceAll("%token%", TOKEN));
39+
con.setRequestProperty("X-GitHub-Api-Version", "2022-11-28");
40+
con.setConnectTimeout(15000);
41+
con.setReadTimeout(15000);
42+
43+
final JsonObject json = JsonParser.parseReader(new InputStreamReader(con.getInputStream())).getAsJsonObject();
44+
45+
if (json.has("assets")) {
46+
final JsonArray assets = json.get("assets").getAsJsonArray();
47+
for (JsonElement asset : assets) {
48+
final JsonObject assetJson = asset.getAsJsonObject();
49+
final String ASSETS_URL = assetJson.get("url").getAsString();
50+
final String ASSETS_NAME = assetJson.get("name").getAsString();
51+
final URL download = new URL(ASSETS_URL); //This is where you put your download URL for your latest release, be sure to change this to //your own.
52+
final URLConnection urlConnection = download.openConnection();
53+
urlConnection.setRequestProperty("Authorization", "Bearer %token%".replaceAll("%token%", TOKEN));
54+
urlConnection.setRequestProperty("Accept", "application/octet-stream");
55+
56+
player.sendMessage(
57+
ChatColor.GREEN + "[TheJpsPit] " + ASSETS_NAME + " をダウンロードします");
58+
59+
new BukkitRunnable() {
60+
@Override
61+
public void run() {
62+
try {
63+
64+
InputStream in = urlConnection.getInputStream();
65+
File temp = new File(
66+
"plugins/update"); //We want to make the folder if it doesn't exist //already
67+
if (!temp.exists()) {
68+
temp.mkdir();
69+
}
70+
Path path = new File("plugins/update" + File.separator
71+
+ ASSETS_NAME).toPath(); //Here you will //put your file name, it must be named the same everytime for it to properly replace the existing, it will send //the file to the updates folder, from there the server will handle the rest.
72+
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
73+
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
}.runTaskLaterAsynchronously(TheJpsPit.getInstance(),
79+
0); //We want this to be an async task so we don't clog up the main thread, also ThisPlugin.getPlugin() is just a short cut for me not to have to use this, it saves me time incase you were wondering
80+
}
81+
}
82+
}catch(IOException e){
83+
e.printStackTrace();
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)