Skip to content

Commit

Permalink
send operator a notification when update available
Browse files Browse the repository at this point in the history
  • Loading branch information
Lori3f6 committed Mar 20, 2020
1 parent ef7f9e8 commit d340da9
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pipeline {

post {
always {
archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
archiveArtifacts artifacts: 'target/MelonScoop-*.jar', fingerprint: true
cleanWs()
}
}
Expand Down
38 changes: 37 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>moe.langua.lab.minecraft</groupId>
<artifactId>MelonScoop</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -19,6 +19,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -38,6 +42,13 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
</dependencies>

Expand All @@ -58,6 +69,31 @@
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>moe.langua.lab.minecraft.scoop.bstats</shadedPattern>
</relocation>
</relocations>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/moe/langua/lab/minecraft/scoop/BootStrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.bukkit.Bukkit;
import org.bstats.bukkit.Metrics;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

public class BootStrap extends JavaPlugin {
public SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

Expand Down Expand Up @@ -52,9 +53,10 @@ public void onEnable() {
//register commands and listeners
this.getServer().getScheduler().runTaskTimerAsynchronously(this, this::save, 6000, 6000/*Auto saving per five minute*/);
this.getServer().getPluginManager().registerEvents(new LoginListener(this), this);
this.getServer().getPluginManager().registerEvents(new UpdateChecker(this),this);
this.getCommand("dig").setExecutor(new DigCommand(this));
this.getLogger().info(ChatColor.DARK_AQUA + "Done! " + uniqueIDIndexMap.size() + " players with " + addressIndexMap.size() + " IP addresses loaded.");

new Metrics(this, 6823); // setup bStats
}

private void setup() throws IOException {
Expand All @@ -79,9 +81,9 @@ private void setup() throws IOException {
this.getLogger().info(ChatColor.DARK_AQUA +"Processing log file generated in "+dateFormatter.format(new Date(x))+" ("+(read++) + " out of "+ fileNumber + " files completed)");
File file = fileHashMap.get(x);
if (!file.getName().endsWith(".gz")) {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8));
} else {
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), StandardCharsets.UTF_8));
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), UTF_8));
}
String line;
while ((line = reader.readLine()) != null) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/moe/langua/lab/minecraft/scoop/DigCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

public class DigCommand implements CommandExecutor {

/*
private static final String IPV4_ADDRESS_REGEX = "^(([01]?\\\\d\\\\d?|2[0-4]\\\\d|25[0-5])\\\\.){3}([01]?\\\\d\\\\d?|2[0-4]\\\\d|25[0-5])$";
private static final Pattern IPV4_ADDRESS_REGEX_PATTERN = Pattern.compile(IPV4_ADDRESS_REGEX);
private static final String IPV6_ADDRESS_REGEX = "(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))";
private static final Pattern IPV6_ADDRESS_REGEX_PATTERN = Pattern.compile(IPV6_ADDRESS_REGEX);

*/

private BootStrap instance;

Expand Down Expand Up @@ -120,7 +121,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
for (long x : timeList) {
OfflinePlayer player = Bukkit.getOfflinePlayer(result.get(x));
stringBuilder.append(ChatColor.YELLOW).append(" -").append(player.getName());
if(player.isBanned()) stringBuilder.append(ChatColor.RED).append(" (Banned)");
if (player.isBanned()) stringBuilder.append(ChatColor.RED).append(" (Banned)");
stringBuilder.append(ChatColor.GRAY).append(ChatColor.ITALIC).append(" (").append(instance.dateFormatter.format(new Date(x))).append(")").append("\n");
}
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent;

import java.net.InetAddress;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,7 +44,7 @@ public void onLogin(AsyncPlayerPreLoginEvent event) {
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
instance.getLogger().warning(stringBuilder.toString());
for (Player x : Bukkit.getOnlinePlayers()) {
if (x.hasPermission("melonscoop.dig")) x.sendMessage(stringBuilder.toString());
if (x.hasPermission("melonscoop.alarm")) x.sendMessage(stringBuilder.toString());
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/moe/langua/lab/minecraft/scoop/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package moe.langua.lab.minecraft.scoop;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.json.JSONException;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import static java.nio.charset.StandardCharsets.UTF_8;

public class UpdateChecker implements Listener {
BootStrap instance;
private boolean hasUpdate = false;
private String latestVersion = "";
private URL repoAPIURL = null;

public UpdateChecker(BootStrap instance) {
this.instance = instance;
try {
repoAPIURL = new URL("https://api.github.com/repos/NyaaCat/MelonScoop/releases/latest");
} catch (MalformedURLException ignore) {
}

instance.getServer().getScheduler().runTaskTimerAsynchronously(instance, () -> {
try {
JSONObject result = new JSONObject(jsonAPIGet(repoAPIURL));
latestVersion = result.getString("tag_name");
if (!instance.getDescription().getVersion().equalsIgnoreCase(latestVersion)) hasUpdate = true;
} catch (IOException ignore) {
}
}, 0, 20 * 3600 * 12/*Check update every 12 hour*/);
}

@EventHandler
public void onOperatorLogin(PlayerJoinEvent event) {
if (!event.getPlayer().hasPermission("melonscoop.update")) return;
if (!hasUpdate) return;
instance.getServer().getScheduler().runTaskLater(instance,()->event.getPlayer().sendMessage(ChatColor.YELLOW + "[MelonScoop] " + ChatColor.WHITE + "A new version of MelonScoop(" + latestVersion + ") is available. Click the link to download now: https://github.com/NyaaCat/MelonScoop/releases/" + latestVersion),20);
}

private static String jsonAPIGet(URL reqURL) throws JSONException, IOException {
HttpsURLConnection connection = (HttpsURLConnection) reqURL.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type", "application/json");
connection.setInstanceFollowRedirects(false);
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tmpString;
StringBuilder stringBuilder = new StringBuilder();
while (true) {
tmpString = bufferedReader.readLine();
if (tmpString == null) break;
stringBuilder.append(tmpString);
}
return stringBuilder.toString();
}
}

0 comments on commit d340da9

Please sign in to comment.