Skip to content

Commit

Permalink
In-game update notification
Browse files Browse the repository at this point in the history
Added in-game update notification
Renamed Updater to UpdateManager
  • Loading branch information
Spazzinq committed Dec 25, 2019
1 parent 3d6427f commit f40b209
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public final class FlightControl extends org.bukkit.plugin.java.JavaPlugin {
@Getter private StatusManager statusManager;
@Getter private TempFlyManager tempflyManager;
@Getter private TrailManager trailManager;
@Getter private UpdateManager updateManager;

@Getter private Particles particles;

@Getter private Updater updater;
@Getter private TempFlyCommand tempFlyCommand;

public void onEnable() {
Expand All @@ -94,10 +94,9 @@ public void onEnable() {
// Gets instance of FlightManager
tempflyManager = new TempFlyManager(this);
statusManager = new StatusManager(this);
updateManager = new UpdateManager(getDescription().getVersion());

new ActionbarUtil();
new Listener(this);
updater = new Updater(getDescription().getVersion());

// Load commands
tempFlyCommand = new TempFlyCommand(this);
Expand All @@ -108,12 +107,11 @@ public void onEnable() {
getCommand("flyspeed").setExecutor(new FlySpeedCommand(this));

if (configManager.isAutoUpdate()) {
updater.install(Bukkit.getConsoleSender(), true);
}
else if (updater.exists()) {
updateManager.install(Bukkit.getConsoleSender(), true);
} else if (updateManager.exists()) {
new BukkitRunnable() {
@Override public void run() {
getLogger().info("FlightControl " + updater.newVer() + " is available for update. Perform \"/fc update\" to update and " + "visit https://www.spigotmc.org/resources/flightcontrol.55168/ to view the feature changes (the config automatically updates).");
getLogger().info("Yay! Version " + updateManager.newVer() + " is available for update. Perform \"/fc update\" to update and visit https://www.spigotmc.org/resources/flightcontrol.55168/ to view the feature changes (the config automatically updates).");
}
}.runTaskLater(this, 70);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import java.util.UUID;

@SuppressWarnings("unused")
final class Listener implements org.bukkit.event.Listener {
private FlightControl pl;

Expand Down Expand Up @@ -70,7 +71,19 @@ private void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();

if (UUID.fromString("043f10b6-3d13-4340-a9eb-49cbc560f48c").equals(p.getUniqueId())) {
p.sendMessage("&e&lFlightControl &7» &eVersion &f" + pl.getDescription().getVersion() + " &eis currently running on this server.");
new BukkitRunnable() {
@Override public void run() {
FlightControl.msg(p, "&e&lFlightControl &7» &eVersion &f" + pl.getDescription().getVersion() + " &eis currently running on this server. " + pl.getHookManager().getHookMsg());
}
}.runTaskLater(pl, 40);
}

if (p.isOp()) {
new BukkitRunnable() {
@Override public void run() {
pl.getUpdateManager().notify(p);
}
}.runTaskLater(pl, 40);
}

pl.getPlayerManager().loadStorage(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private String loadHelp(String[] args) {
msg(s, "&a&lFlightControl &7» &aConfiguration successfully reloaded!");
break;
case "update":
pl.getUpdater().install(s, false);
pl.getUpdateManager().install(s, false);
break;
case "combat":
config.setCombatChecked(!config.isCombatChecked());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class HookManager {
private PluginManager pm;
private boolean is1_13;

@Getter private String hookMsg;

// Load early to prevent NPEs
@Getter private WorldGuard worldGuard = new WorldGuard();
@Getter private Vanish vanish = new Vanish();
Expand Down Expand Up @@ -93,20 +95,20 @@ public void load() {
}

// Prepare hooked msg
StringBuilder hookedMsg = new StringBuilder("Hooked with ");
StringBuilder hookMsg = new StringBuilder("Hooked with ");
if (hooked.isEmpty()) {
hookedMsg.append("no plugins.");
hookMsg.append("no plugins.");
} else {
for (String hook : hooked) {
hookedMsg.append(hook).append(", ");
hookMsg.append(hook).append(", ");
}
hookedMsg.delete(hookedMsg.length() - 2, hookedMsg.length());
hookedMsg.insert(hookedMsg.lastIndexOf(",") + 1, " and");
hookedMsg.append(".");
hookMsg.delete(hookMsg.length() - 2, hookMsg.length());
hookMsg.insert(hookMsg.lastIndexOf(",") + 1, " and");
hookMsg.append(".");
}

// Hook msg
flightControl.getLogger().info(hookedMsg.toString());
this.hookMsg = hookMsg.toString();
flightControl.getLogger().info(this.hookMsg);
}

private void loadFactions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,105 @@
/*
* This file is part of FlightControl, which is licensed under the MIT License
*
* Copyright (c) 2019 Spazzinq
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.spazzinq.flightcontrol;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import static org.spazzinq.flightcontrol.FlightControl.msg;

public final class Updater {
private String version, newVersion;
private boolean downloaded;

Updater(String version) { this.version = version; }

boolean exists() {
try {
newVersion = new BufferedReader(new InputStreamReader(new URL("https://api.spigotmc.org/legacy/update.php?resource=55168").openConnection().getInputStream())).readLine();
} catch (Exception ignored) {
return false;
}
return version.matches("\\d+(\\.\\d+)?") && newVersion.matches("\\d+(\\.\\d+)?") ? Double.parseDouble(newVersion) > Double.parseDouble(version) : !version.equals(newVersion);
}

private void dl() {
if (exists()) {
try {
URL website = new URL("https://github.com/Spazzinq/FlightControl/releases/download/" + newVersion + "/flightcontrol.jar");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(new File( "plugins/flightcontrol.jar"));
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
downloaded = true;
} catch (Exception ignored) {}
}
}

String newVer() { return newVersion; }

public void install(CommandSender s, boolean silentCheck) {
if (exists()) {
if (!downloaded) {
dl();
if (Bukkit.getPluginManager().isPluginEnabled("Plugman")) {
msg(s, "&a&lFlightControl &7» &aAutomatic installation finished (the config has automatically updated too)! Welcome to flightcontrol " + newVer() + "!");
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "plugman reload flightcontrol");
} else msg(s, "&a&lFlightControl &7» &aVersion &f" + newVer() + " &aupdate downloaded. Restart (or reload) the server to apply the update.");
} else msg(s, "&a&lFlightControl &7» &aVersion &f" + newVer() + " &aupdate has already been downloaded. Restart (or reload) the server to apply the update.");
} else if (!silentCheck) msg(s, "&a&lFlightControl &7» &aNo updates found.");
}
}
/*
* This file is part of FlightControl, which is licensed under the MIT License
*
* Copyright (c) 2019 Spazzinq
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.spazzinq.flightcontrol.manager;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.spazzinq.flightcontrol.FlightControl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.HashSet;
import java.util.UUID;

import static org.spazzinq.flightcontrol.FlightControl.msg;

public final class UpdateManager {
HashSet<UUID> notified = new HashSet<>();

private String version, newVersion;
private boolean downloaded;

public UpdateManager(String version) {
this.version = version;
}

public boolean exists() {
try {
newVersion = new BufferedReader(new InputStreamReader(new URL("https://api.spigotmc.org/legacy/update.php?resource=55168").openConnection().getInputStream())).readLine();
} catch (Exception ignored) {
return false;
}
return version.matches("\\d+(\\.\\d+)?") && newVersion.matches("\\d+(\\.\\d+)?") ? Double.parseDouble(newVersion) > Double.parseDouble(version) : !version.equals(newVersion);
}

private void dl() {
if (exists()) {
try {
URL website = new URL("https://github.com/Spazzinq/FlightControl/releases/download/" + newVersion + "/flightcontrol.jar");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(new File("plugins/flightcontrol.jar"));
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
downloaded = true;
} catch (Exception ignored) {
}
}
}

public String newVer() {
return newVersion;
}

public void install(CommandSender s, boolean silentCheck) {
if (exists()) {
if (!downloaded) {
dl();
if (Bukkit.getPluginManager().isPluginEnabled("Plugman")) {
msg(s, "&a&lFlightControl &7» &aAutomatic installation finished (the config has automatically updated too)! Welcome to flightcontrol " + newVer() + "!");
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "plugman reload flightcontrol");
} else
msg(s, "&a&lFlightControl &7» &aVersion &f" + newVer() + " &aupdate downloaded. Restart (or reload) the server to apply the update.");
} else
msg(s, "&a&lFlightControl &7» &aVersion &f" + newVer() + " &aupdate has already been downloaded. Restart (or reload) the server to apply the update.");
} else if (!silentCheck) {
msg(s, "&a&lFlightControl &7» &aNo updates found.");
}
}

public void notify(Player p) {
// exists()
if (!notified.contains(p.getUniqueId())) {
notified.add(p.getUniqueId());
FlightControl.msg(p, "&e&lFlightControl &7» &eWoot woot! Version &f" + newVer() + "&e is now available! " +
"Update with \"/fc update\" and check out the new features: &fhttps://www.spigotmc.org/resources/flightcontrol.55168/");
}
}
}

0 comments on commit f40b209

Please sign in to comment.