This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
StephanBH
committed
Jan 8, 2020
1 parent
b126c12
commit 63ffa66
Showing
5 changed files
with
60 additions
and
75 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
54 changes: 21 additions & 33 deletions
54
src/main/java/net/noodles/antibot/antibotmain/Utils/UpdateChecker.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 |
---|---|---|
@@ -1,49 +1,37 @@ | ||
package net.noodles.antibot.antibotmain.Utils; | ||
|
||
import net.noodles.antibot.antibotmain.AntiBot; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Scanner; | ||
import java.util.function.Consumer; | ||
|
||
public class UpdateChecker | ||
{ | ||
public AntiBot plugin; | ||
public String version; | ||
|
||
public UpdateChecker(AntiBot plugin) { | ||
|
||
private AntiBot plugin; | ||
private int resourceId; | ||
|
||
public UpdateChecker(AntiBot plugin, int resourceId) { | ||
this.plugin = plugin; | ||
this.version = this.getLatestVersion(); | ||
this.resourceId = resourceId; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public String getLatestVersion() { | ||
try { | ||
final int resource = 45325; | ||
final HttpURLConnection con = (HttpURLConnection)new URL("https://api.spigotmc.org/legacy/update.php?resource=45325").openConnection(); | ||
con.setDoOutput(true); | ||
con.setRequestMethod("POST"); | ||
con.getOutputStream().write("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=45325".getBytes("UTF-8")); | ||
final String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); | ||
if (version.length() <= 7) { | ||
return version; | ||
|
||
public void getLatestVersion(Consumer<String> consumer) { | ||
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { | ||
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { | ||
if (scanner.hasNext()) { | ||
consumer.accept(scanner.next()); | ||
} | ||
} catch (IOException exception) { | ||
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage()); | ||
} | ||
} | ||
catch (Exception ex) { | ||
System.out.println("---------------------------------"); | ||
this.plugin.getLogger().info("Failed to check for a update!"); | ||
System.out.println("---------------------------------"); | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isConnected() { | ||
return this.version != null; | ||
} | ||
|
||
public boolean hasUpdate() { | ||
return !this.version.equals(this.plugin.getDescription().getVersion()); | ||
}); | ||
} | ||
} |
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