Skip to content
This repository has been archived by the owner on Dec 31, 2019. It is now read-only.

Commit

Permalink
Update checker and reload commands, also permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShakeforProtein committed Dec 5, 2018
1 parent 6913291 commit 2c69473
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Array;
import java.util.Set;

public final class SafeDamageHere extends JavaPlugin implements Listener {


private UpdateChecker uc;

@Override
public void onEnable() {
// Plugin startup logic
getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
getConfig().set("version", this.getDescription().getVersion());
saveConfig();
this.uc = new UpdateChecker(this);
uc.getCheckDownloadURL();
}

@Override
Expand All @@ -30,7 +33,8 @@ public void onDisable() {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("sdh")) {
if (cmd.getName().equalsIgnoreCase("sdh") && sender.hasPermission("safedamagehere.configure")){

if (sender instanceof Player) {
Player p = (Player) sender;
if (args.length == 0) {
Expand All @@ -49,6 +53,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
int pY = (int) loc.getY();
int pZ = (int) loc.getZ();
String pWorld = p.getWorld().getName();
if (args.length == 1 && args[0].equalsIgnoreCase("reload") && p.hasPermission("safedamagehere.reload")){reloadConfig();}
if (args.length == 2) {
if (args[1].equalsIgnoreCase("setA")) {
String region = args[0];
Expand Down Expand Up @@ -147,7 +152,6 @@ private void onPlayerTakeDamage(EntityDamageEvent e) {

if (x1 == 0 || x2 == 0 || y1 == 0 || y2 == 0 || z1 == 0 || z2 == 0) {
doIt = "false";
p.sendMessage("false");
}
if (doIt == "true") {
String damageType = getConfig().getString("worlds." + p.getWorld().getName() + ".regions." + item + ".damageType");
Expand All @@ -156,8 +160,6 @@ private void onPlayerTakeDamage(EntityDamageEvent e) {
}
}
}
} else {
p.sendMessage("Invalid world");
}
}
}
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/me/shakeforprotein/safedamagehere/UpdateChecker.java
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;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
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:
13 changes: 12 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ website: http://play.treebomc.com
commands:
sdh:
description: Safe Damage Here Commands
usage: /<command> <subcommand>
usage: /<command> <subcommand>

permissions:
safedamagehere.updatechecker:
description: 'Issues update check when player logs in'
default: op
safedamagehere.reload:
description: 'Allows a player to reload the plugin config.'
default: op
safedamagehere.configure:
description: 'Allows a player to create damage exclusion zones.'
default: op

0 comments on commit 2c69473

Please sign in to comment.