This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update checker and reload commands, also permissions
- Loading branch information
1 parent
6913291
commit 2c69473
Showing
5 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
120 changes: 120 additions & 0 deletions
120
src/main/java/me/shakeforprotein/safedamagehere/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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package me.shakeforprotein.safedamagehere; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
|
||
public class UpdateChecker { | ||
|
||
|
||
//These two need to be changed for different plugins | ||
private SafeDamageHere pl; | ||
|
||
public UpdateChecker(SafeDamageHere pl) { | ||
this.pl = pl; | ||
} | ||
|
||
public String requiredPermission = "stoneores.updatechecker"; | ||
|
||
public Boolean getCheckDownloadURL(Player p) { | ||
// Code courtesy of Spigot user Ftbastler | ||
// Adjustments by ShakeforProtein | ||
if (pl.getConfig().getString("checkUpdates").equalsIgnoreCase("true")) { | ||
try { | ||
URL url = new URL(pl.getConfig().getString("apiLink")); | ||
URLConnection conn = url.openConnection(); | ||
conn.setRequestProperty("User-Agent", "Mozilla/1.0"); | ||
conn.setConnectTimeout(5000); | ||
conn.connect(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
String inputLine = in.readLine(); | ||
String gitVersion = ""; | ||
Boolean getLinkNow = false; | ||
|
||
while (inputLine != null) { | ||
|
||
JSONParser parser = new JSONParser(); | ||
JSONObject json = (JSONObject) parser.parse(inputLine); | ||
gitVersion = json.get("tag_name").toString(); | ||
if (!gitVersion.equalsIgnoreCase(pl.getDescription().getVersion())) { | ||
p.sendMessage("Latest " + pl.getDescription().getName() + " Version is " + gitVersion); | ||
p.sendMessage("Your " + pl.getDescription().getName() + " Version is " + pl.getDescription().getVersion()); | ||
p.sendMessage(ChatColor.GOLD + "Please update " + pl.getDescription().getName() + " with version at " + pl.getConfig().getString("releasePage")); | ||
} | ||
|
||
break; | ||
} | ||
|
||
if (!gitVersion.equalsIgnoreCase(pl.getDescription().getVersion())) { | ||
Bukkit.getConsoleSender().sendMessage("Latest " + pl.getDescription().getName() + " Version is " + gitVersion); | ||
Bukkit.getConsoleSender().sendMessage("Your " + pl.getDescription().getName() + " Version is " + pl.getDescription().getVersion()); | ||
Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "Please consider updating " + pl.getDescription().getName() + " with version at " + pl.getConfig().getString("releasePage")); | ||
} | ||
in.close(); | ||
|
||
return true; | ||
} catch (Exception e) { | ||
if (p != null) { | ||
p.sendMessage(pl.getDescription().getName() + " Failed to check for updates"); | ||
} | ||
pl.getServer().getLogger().warning("Something went wrong while downloading an update."); | ||
pl.getServer().getLogger().info("Please check the plugin's release page to see if there are any updates available."); | ||
pl.getServer().getLogger().info("" + pl.getConfig().getString("releasePage")); | ||
pl.getServer().getLogger().fine(e.getMessage()); | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public Boolean getCheckDownloadURL() { | ||
// Code courtesy of Spigot user Ftbastler | ||
// Adjustments by ShakeforProtein | ||
if (pl.getConfig().getString("checkUpdates").equalsIgnoreCase("true")) { | ||
try { | ||
URL url = new URL(pl.getConfig().getString("apiLink")); | ||
URLConnection conn = url.openConnection(); | ||
conn.setRequestProperty("User-Agent", "Mozilla/1.0"); | ||
conn.setConnectTimeout(5000); | ||
conn.connect(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
String inputLine = in.readLine(); | ||
String gitVersion = ""; | ||
Boolean getLinkNow = false; | ||
|
||
while (inputLine != null) { | ||
|
||
JSONParser parser = new JSONParser(); | ||
JSONObject json = (JSONObject) parser.parse(inputLine); | ||
gitVersion = json.get("tag_name").toString(); | ||
|
||
|
||
if (!gitVersion.equalsIgnoreCase(pl.getDescription().getVersion())) { | ||
Bukkit.getConsoleSender().sendMessage("Latest " + pl.getDescription().getName() + " Version is " + gitVersion); | ||
Bukkit.getConsoleSender().sendMessage("Your " + pl.getDescription().getName() + " Version is " + pl.getDescription().getVersion()); | ||
Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "Please consider updating " + pl.getDescription().getName() + " with version at " + pl.getConfig().getString("releasePage")); | ||
} | ||
break; | ||
} | ||
in.close(); | ||
|
||
return true; | ||
} catch (Exception e) { | ||
pl.getServer().getLogger().warning("Something went wrong while downloading an update."); | ||
pl.getServer().getLogger().info("Please check the plugin's release page to see if there are any updates available."); | ||
pl.getServer().getLogger().info("" + pl.getConfig().getString("releasePage")); | ||
pl.getServer().getLogger().fine(e.getMessage()); | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,3 +1,7 @@ | ||
version: | ||
apiLink: 'https://api.github.com/repos/TreeboMC/SafeDamageHere/releases/latest' | ||
releasePage: 'https://github.com/TreeboMC/SafeDamageHere/releases/latest' | ||
checkUpdates: 'true' | ||
|
||
|
||
world: |
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