Readd update notification permission implementation

This commit is contained in:
NotMyFault 2020-02-16 02:34:51 +01:00
parent 4ed108b5c0
commit 7b83f0146e
2 changed files with 35 additions and 29 deletions

View file

@ -3,20 +3,14 @@ package com.github.intellectualsites.plotsquared.bukkit.listeners;
import com.destroystokyo.paper.MaterialTags;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil;
import com.github.intellectualsites.plotsquared.bukkit.util.UpdateUtility;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotHandler;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotInventory;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.StringWrapper;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.EntityUtil;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
@ -129,7 +123,12 @@ import org.bukkit.projectiles.BlockProjectileSource;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
@ -153,6 +152,7 @@ import java.util.regex.Pattern;
private boolean tmpTeleport = true;
private Field fieldPlayer;
private PlayerMoveEvent moveTmp;
private String spigotVersion;
{
try {
@ -721,6 +721,25 @@ import java.util.regex.Pattern;
}
EventUtil.manager.doJoinTask(pp);
}, 20);
if (pp.hasPermission(Captions.PERMISSION_ADMIN_UPDATE_NOTIFICATION.getTranslated())
&& Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=1177").openConnection();
connection.setRequestMethod("GET");
this.spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine();
} catch (IOException e) {
PlotSquared.log(Captions.PREFIX + "&6Unable to check for updates because: " + e);
return;
}
if (!UpdateUtility.internalVersion.equals(UpdateUtility.spigotVersion)) {
new PlotMessage("-----------------------------------").send(pp);
new PlotMessage(Captions.PREFIX + "There appears to be a PlotSquared update available!").color("$1").tooltip("https://www.spigotmc.org/resources/1177/updates").send(pp);
new PlotMessage(Captions.PREFIX + "https://www.spigotmc.org/resources/1177/updates").color("$1").tooltip("https://www.spigotmc.org/resources/1177/updates").send(pp);
new PlotMessage("-----------------------------------").send(pp);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)

View file

@ -4,11 +4,7 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
@ -20,13 +16,13 @@ import java.net.URL;
public class UpdateUtility implements Listener {
private final JavaPlugin javaPlugin;
private final String internalVersion;
private String spigotVersion;
public static String internalVersion;
public static String spigotVersion;
public final JavaPlugin javaPlugin;
public UpdateUtility(final JavaPlugin javaPlugin) {
this.javaPlugin = javaPlugin;
this.internalVersion = javaPlugin.getDescription().getVersion();
internalVersion = javaPlugin.getDescription().getVersion();
}
public void updateChecker() {
@ -37,30 +33,21 @@ public class UpdateUtility implements Listener {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=1177").openConnection();
connection.setRequestMethod("GET");
UpdateUtility.this.spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine();
spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine();
} catch (IOException e) {
PlotSquared.log(Captions.PREFIX + "&6Unable to check for updates because: " + e);
this.cancel();
return;
}
if (!UpdateUtility.this.internalVersion.equals(UpdateUtility.this.spigotVersion)) {
if (!internalVersion.equals(spigotVersion)) {
PlotSquared.log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!");
PlotSquared.log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/1177/updates");
Bukkit.getScheduler().runTask(UpdateUtility.this.javaPlugin, () -> Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(final PlayerJoinEvent event) {
final Player player = event.getPlayer();
if (player.hasPermission("plots.admin.update.notify")) {
PlotSquared.log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!");
PlotSquared.log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/1177/updates");
}
}
}, UpdateUtility.this.javaPlugin));
} else {
PlotSquared.log(Captions.PREFIX + "Congratulations! You are running the latest PlotSquared version.");
}
} this.cancel();
}
this.cancel();
});
}
}.runTaskTimer(this.javaPlugin, 0L, 12000L);