Skip to content

Commit 9eb821b

Browse files
committed
added updater and fixed unmapped versions
1 parent c42fae9 commit 9eb821b

30 files changed

+69716
-11
lines changed

src/main/java/net/potato/tuff/TuffX.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public record WorldChunkKey(String worldName, int x, int z) {}
7777

7878
@Override
7979
public void onEnable() {
80+
System.out.println("UPDATED!!!");
8081
saveDefaultConfig();
82+
//saveResource("version.json", true);
8183
this.CHUNKS_PER_TICK = getConfig().getInt("chunks-per-tick", 6);
8284
this.debug = getConfig().getBoolean("debug-mode", false);
8385
this.enabledWorlds = new HashSet<>(getConfig().getStringList("enabled-worlds"));
@@ -108,6 +110,8 @@ public void onEnable() {
108110
this.chunkProcessorPool = Executors.newFixedThreadPool(threadCount);
109111

110112
startProcessorTask();
113+
114+
new Updater(this).scheduleCheck();
111115
}
112116

113117
public record ChunkSectionCoord(int cx, int cy, int cz) {}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package net.potato.tuff;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.plugin.java.JavaPlugin;
7+
import org.bukkit.scheduler.BukkitRunnable;
8+
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.nio.channels.Channels;
16+
import java.nio.channels.ReadableByteChannel;
17+
18+
public class Updater {
19+
20+
private final JavaPlugin plugin;
21+
private final String currentVersion;
22+
23+
public Updater(JavaPlugin plugin) {
24+
this.plugin = plugin;
25+
this.currentVersion = plugin.getDescription().getVersion();
26+
}
27+
28+
public void scheduleCheck() {
29+
if (!plugin.getConfig().getBoolean("updater.enabled", true)) return;
30+
long interval = plugin.getConfig().getLong("updater.check-interval-minutes", 60) * 60 * 20;
31+
new BukkitRunnable() {
32+
@Override
33+
public void run() {
34+
checkForUpdates();
35+
}
36+
}.runTaskTimerAsynchronously(plugin, 0L, interval);
37+
}
38+
39+
private void checkForUpdates() {
40+
String localLatestVersion = currentVersion;
41+
42+
String versionUrl = "https://verytuffautoupdater.netlify.app/version-remote.json";
43+
44+
try {
45+
URL url = new URL(versionUrl);
46+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
47+
connection.setConnectTimeout(5000);
48+
49+
if (connection.getResponseCode() == 200) {
50+
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
51+
JsonObject versionInfo = JsonParser.parseReader(reader).getAsJsonObject();
52+
reader.close();
53+
54+
String webLatestVersion = versionInfo.get("latestVersion").getAsString();
55+
56+
if (isNewer(webLatestVersion, currentVersion)) {
57+
plugin.getLogger().info("A new version is available: " + webLatestVersion + "! Downloading...");
58+
String downloadUrl = versionInfo.get("downloadUrl").getAsString();
59+
downloadUpdate(downloadUrl);
60+
} else {
61+
plugin.getLogger().info("You are running the latest version (" + currentVersion + ").");
62+
}
63+
} else {
64+
plugin.getLogger().warning("Could not check for updates. (Response code: " + connection.getResponseCode() + ")");
65+
}
66+
} catch (Exception e) {
67+
plugin.getLogger().warning("An error occurred while checking for updates: " + e.getMessage());
68+
}
69+
}
70+
71+
private String getLocalLatestVersion() {
72+
try (InputStream in = plugin.getResource("version.json")) {
73+
if (in == null) {
74+
return "0.0.0";
75+
}
76+
InputStreamReader reader = new InputStreamReader(in);
77+
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
78+
return json.get("version").getAsString();
79+
} catch (Exception e) {
80+
plugin.getLogger().warning("Could not read internal latest.json file.");
81+
return "0.0.0";
82+
}
83+
}
84+
85+
private boolean isNewer(String latest, String current) {
86+
return !latest.equalsIgnoreCase(current);
87+
}
88+
89+
private void downloadUpdate(String url) {
90+
try {
91+
File updateFolder = plugin.getServer().getUpdateFolderFile();
92+
if (!updateFolder.exists()) {
93+
updateFolder.mkdirs();
94+
}
95+
File downloadTarget = new File(updateFolder, plugin.getName() + ".jar");
96+
97+
URL downloadUrl = new URL(url);
98+
ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream());
99+
FileOutputStream fos = new FileOutputStream(downloadTarget);
100+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
101+
fos.close();
102+
rbc.close();
103+
104+
plugin.getLogger().info("Successfully downloaded the new version to the 'update' folder.");
105+
plugin.getLogger().info("The update will be applied on the next server restart.");
106+
107+
if (plugin.getConfig().getBoolean("updater.restart-on-update", false)) {
108+
plugin.getLogger().info("Restarting server to apply the update...");
109+
new BukkitRunnable() {
110+
@Override
111+
public void run() {
112+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "restart");
113+
}
114+
}.runTask(plugin);
115+
}
116+
} catch (Exception e) {
117+
plugin.getLogger().severe("The update download failed: " + e.getMessage());
118+
}
119+
}
120+
}

src/main/java/net/potato/tuff/ViaBlockIds.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,55 @@ private String getServerMinecraftVersion() {
8686
return "1.21";
8787
}
8888

89+
private InputStream findMappingFile() {
90+
String[] versionParts = serverVersion.split("\\.");
91+
92+
int major, minor, patch;
93+
94+
try {
95+
major = Integer.parseInt(versionParts[0]);
96+
minor = Integer.parseInt(versionParts[1]);
97+
patch = versionParts.length > 2 ? Integer.parseInt(versionParts[2]) : 0;
98+
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
99+
Bukkit.getLogger().severe("[TuffX] Could not parse server version string: " + serverVersion);
100+
return plugin.getResource("mapping-" + serverVersion + ".json");
101+
}
102+
103+
Bukkit.getLogger().info("[TuffX] Searching for mappings, starting from " + serverVersion + " and going down.");
104+
105+
for (int m = minor; m >= 16; m--) {
106+
int startPatch = (m == minor) ? patch : 9;
107+
108+
for (int p = startPatch; p >= 0; p--) {
109+
String versionToTest = major + "." + m + "." + p;
110+
String filename = "mapping-" + versionToTest + ".json";
111+
112+
InputStream is = plugin.getResource(filename);
113+
114+
if (is != null) {
115+
if (!versionToTest.equals(serverVersion)) {
116+
Bukkit.getLogger().info("[TuffX] Using fallback mapping file: " + filename);
117+
} else {
118+
Bukkit.getLogger().info("[TuffX] Found exact mapping file: " + filename);
119+
}
120+
return is;
121+
}
122+
}
123+
124+
String minorVersionFilename = "mapping-" + major + "." + m + ".json";
125+
InputStream is = plugin.getResource(minorVersionFilename);
126+
if (is != null) {
127+
Bukkit.getLogger().info("[TuffX] Using fallback mapping file: " + minorVersionFilename);
128+
return is;
129+
}
130+
}
131+
132+
Bukkit.getLogger().severe("[TuffX] Could not find any suitable mapping file after checking all versions down to " + major + ".16.0");
133+
return null;
134+
}
135+
89136
private void generateAndSaveMappings(File file) {
90-
try (InputStream is = plugin.getResource("mapping-" + serverVersion + ".json")) {
137+
try (InputStream is = findMappingFile()) {
91138
if (is == null) {
92139
Bukkit.getLogger().severe("[TuffX] Failed to find mapping-" + serverVersion + ".json in plugin resources!");
93140
return;

src/main/resources/config.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ chunk-processor-threads: -1
1515
#the size of the cache
1616
cache-size: 512
1717
#how long until the cache expires (minutes)
18-
cache-expiration: 5
18+
cache-expiration: 5
19+
20+
#auto updater config
21+
updater:
22+
enabled: true
23+
24+
# update checking interval
25+
check-interval-minutes: 30
26+
27+
# restart the server on update
28+
restart-on-update: true

0 commit comments

Comments
 (0)