This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
forked from Doclic/NoEncryption
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from V1nc3ntWasTaken/1.19.2
Merge 1.19.2 support to the main branch
- Loading branch information
Showing
8 changed files
with
318 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/me/doclic/noencryption/VersionDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package me.doclic.noencryption; | ||
|
||
import me.doclic.noencryption.compatibility.Compatibility; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.ReadableByteChannel; | ||
|
||
public class VersionDownloader { | ||
|
||
protected static NoEncryption plugin; | ||
|
||
protected static boolean deleteOnShutdown; | ||
|
||
public static void initialize(NoEncryption plugin) { | ||
|
||
VersionDownloader.plugin = plugin; | ||
|
||
} | ||
|
||
public static void downloadVersion() { | ||
|
||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | ||
|
||
try { | ||
|
||
File saveLocation = new File("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar"); | ||
String url = "https://github.com/V1nc3ntWasTaken/NoEncryption/releases/download/" + plugin.getDescription().getVersion() + "/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar"; | ||
|
||
ReadableByteChannel byteChannel = Channels.newChannel(new URL(url).openStream()); | ||
FileOutputStream outStream = new FileOutputStream(saveLocation); | ||
FileChannel channel = outStream.getChannel(); | ||
|
||
channel.transferFrom(byteChannel, 0, Long.MAX_VALUE); | ||
|
||
doneDownloading(); | ||
|
||
} catch (IOException e) { | ||
|
||
plugin.getLogger().severe("Failed to reach URL for download."); | ||
e.printStackTrace(); | ||
|
||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
private static void doneDownloading() { | ||
setDeleteOnShutdown(); | ||
|
||
plugin.getLogger().info("plugins/" + plugin.getDescription().getName() + "-v" + plugin.getDescription().getVersion() + "--" + Compatibility.getFormattedBukkitVersion() + "_only.jar" + " successfully downloaded"); | ||
plugin.getLogger().info(NoEncryption.getJARFile().getName() + " (current version) will be deleted automatically upon restart"); | ||
plugin.getLogger().info("It is now safe to restart your server"); | ||
|
||
} | ||
|
||
private static void setDeleteOnShutdown() { | ||
|
||
deleteOnShutdown = true; | ||
|
||
} | ||
|
||
public static boolean getDeleteOnShutdown() { | ||
|
||
return deleteOnShutdown; | ||
|
||
} | ||
|
||
public static void shutdown() { | ||
|
||
try { | ||
|
||
if (getDeleteOnShutdown()) { | ||
|
||
if (NoEncryption.getJARFile().exists() && !NoEncryption.getJARFile().delete()) { | ||
throw new IOException(); | ||
} | ||
|
||
} | ||
|
||
} catch (IOException e) { | ||
|
||
plugin.getLogger().severe("Unable to delete " + NoEncryption.getJARFile().getName() + ". Manual deletion is required"); | ||
e.printStackTrace(); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.