mirror of
https://github.com/nkomarn/harbor.git
synced 2025-01-20 06:21:26 +01:00
💥 Nuke the auto-updater
This commit is contained in:
parent
e8de0f35c1
commit
63f03458d0
@ -7,7 +7,6 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
import xyz.nkomarn.Harbor.command.HarborCommand;
|
||||
import xyz.nkomarn.Harbor.listener.AfkListener;
|
||||
import xyz.nkomarn.Harbor.listener.BedListener;
|
||||
import xyz.nkomarn.Harbor.listener.JoinListener;
|
||||
import xyz.nkomarn.Harbor.task.Checker;
|
||||
import xyz.nkomarn.Harbor.util.Config;
|
||||
import xyz.nkomarn.Harbor.util.Metrics;
|
||||
@ -20,14 +19,18 @@ public class Harbor extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
saveDefaultConfig();
|
||||
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this,
|
||||
Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this,
|
||||
new Checker(), 0L, Config.getInteger("values.timer") * 20);
|
||||
|
||||
final PluginManager pluginManager = getServer().getPluginManager();
|
||||
getCommand("harbor").setExecutor(new HarborCommand());
|
||||
pluginManager.registerEvents(new JoinListener(), this);
|
||||
pluginManager.registerEvents(new BedListener(), this);
|
||||
|
||||
if (!Config.getString("version").equals(version)) {
|
||||
getLogger().warning(String.format("The configuration version is '%s' but you're running Harbor " +
|
||||
"version '%s'. Please update your configuration.", Config.getString("version"), version));
|
||||
}
|
||||
|
||||
// bStats
|
||||
new Metrics(this);
|
||||
|
||||
@ -36,7 +39,7 @@ public class Harbor extends JavaPlugin {
|
||||
|
||||
// If Essentials isn't present, enable fallback AFK system
|
||||
if (essentials == null) {
|
||||
System.out.println("Registered Listener");
|
||||
getLogger().info("Essentials not present- registering fallback AFK detection system.");
|
||||
getServer().getPluginManager().registerEvents(new AfkListener(), this);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
package xyz.nkomarn.Harbor.command;
|
||||
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -13,11 +10,9 @@ import xyz.nkomarn.Harbor.Harbor;
|
||||
import xyz.nkomarn.Harbor.task.AccelerateNightTask;
|
||||
import xyz.nkomarn.Harbor.task.Checker;
|
||||
import xyz.nkomarn.Harbor.util.Config;
|
||||
import xyz.nkomarn.Harbor.util.Updater;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class HarborCommand implements TabExecutor {
|
||||
@Override
|
||||
@ -64,45 +59,6 @@ public class HarborCommand implements TabExecutor {
|
||||
+ "&7Forcing night skip in your world."));
|
||||
return true;
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("update")) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);
|
||||
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
|
||||
prefix + "&fChecking for updates.")));
|
||||
}
|
||||
|
||||
boolean updateAvailable;
|
||||
try {
|
||||
updateAvailable = Updater.check().get();
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7Failed to check for a "
|
||||
+ "new update. Check console for full log."));
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (updateAvailable) {
|
||||
try {
|
||||
// More fancy actionbar stuff
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0f, 1.0f);
|
||||
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
|
||||
prefix + "&fUpdate found, upgrading.")));
|
||||
}
|
||||
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7"
|
||||
+ Updater.upgrade().get()));
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7You're already running "
|
||||
+ "the latest version of Harbor. Great work!"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise, send unrecognized argument messages
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
|
||||
|
@ -1,44 +0,0 @@
|
||||
package xyz.nkomarn.Harbor.listener;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import xyz.nkomarn.Harbor.Harbor;
|
||||
import xyz.nkomarn.Harbor.util.Config;
|
||||
import xyz.nkomarn.Harbor.util.Updater;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class JoinListener implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
if (!event.getPlayer().hasPermission("harbor.admin") || !Config.getBoolean("features.notifier")) return;
|
||||
|
||||
// Check for updates
|
||||
Bukkit.getScheduler().runTaskAsynchronously(Harbor.instance, () -> {
|
||||
boolean updateAvailable = false;
|
||||
try {
|
||||
updateAvailable = Updater.check().get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
if (!updateAvailable) return;
|
||||
TextComponent updateMessage = new TextComponent(ChatColor.translateAlternateColorCodes('&',
|
||||
Config.getString("messages.miscellaneous.prefix")
|
||||
+ "&7Hey there, Harbor " + Updater.latest + " is now out!"
|
||||
+ " Click this message to upgrade automatically."));
|
||||
updateMessage.setColor(ChatColor.GRAY);
|
||||
updateMessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
|
||||
new ComponentBuilder("§a§l↑ §7Click to update Harbor now!").create()));
|
||||
updateMessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/harbor update"));
|
||||
event.getPlayer().spigot().sendMessage(updateMessage);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package xyz.nkomarn.Harbor.util;
|
||||
|
||||
import xyz.nkomarn.Harbor.Harbor;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Updater {
|
||||
public static String latest;
|
||||
|
||||
// Checks if an update is available
|
||||
public static Future<Boolean> check() {
|
||||
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
||||
|
||||
ForkJoinPool.commonPool().submit(() -> {
|
||||
try {
|
||||
URL latestVersion = new URL("https://api.spigotmc.org/legacy/update.php?resource=60088");
|
||||
URLConnection request = latestVersion.openConnection();
|
||||
request.addRequestProperty("User-Agent", "Harbor");
|
||||
request.connect();
|
||||
InputStream inputStream = request.getInputStream();
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
latest = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||
future.complete(!Harbor.version.equals(latest));
|
||||
} catch (IOException e) {
|
||||
future.complete(false);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
// Download latest JAR and put it in Bukkit's update folder
|
||||
public static Future<String> upgrade() {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
ForkJoinPool.commonPool().submit(() -> {
|
||||
String jarName = new File(Updater.class.getProtectionDomain().getCodeSource().getLocation()
|
||||
.getPath()).getName();
|
||||
|
||||
try {
|
||||
URL downloadURL = new URL("http://aqua.api.spiget.org/v2/resources/60088/download");
|
||||
File updatedJarFile = new File("plugins" + File.separator + "update"
|
||||
+ File.separator + jarName);
|
||||
updatedJarFile.mkdirs();
|
||||
InputStream inputStream = downloadURL.openStream();
|
||||
Files.copy(inputStream, Paths.get(updatedJarFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
|
||||
future.complete("Updated Harbor. Changes will take effect after a server reload/reboot.");
|
||||
} catch (IOException e) {
|
||||
future.complete("Failed to update Harbor. Check console for full log.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return future;
|
||||
}
|
||||
}
|
@ -16,19 +16,20 @@ values:
|
||||
timer: 2 # How often (in seconds) to run the clock task (used to detect sleep, AFK players, time actionbar, etc.)
|
||||
percent: 50 # Percent of players that need to sleep to skip night (must be between 0 to 100)
|
||||
interval: 60 # Time skip interval that is added when the night is accelerated.
|
||||
multiplier: 0.4 # Used as the variable multiplier for the variable-interval feature.
|
||||
timeout: 15 # Time (in minutes) until a player is considered AFK (for internal AFK detection system only- when Essentials isn't present)
|
||||
|
||||
features:
|
||||
skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)
|
||||
instant-skip: false # Toggle the instant skipping of night (instead of showing the full animation). Requires "skip" to be true.
|
||||
variable-interval: false # Change the night skip tick interval based on the amount of online players
|
||||
clear-rain: true # Clear rain when skipping the night
|
||||
clear-thunder: true # Clear thunder when skipping the night
|
||||
phantoms: false # Reset the sleep statistic (practically disables phantom spawns - false = no phantoms | Set the doInsomnia gamerule to false to disable phantoms if you're on 1.15+)
|
||||
bypass: true # Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count
|
||||
ignore: true # Toggle exclusion of players in creative and spectator mode
|
||||
vanish: true # Toggle exclusion of vanished players
|
||||
exclude-vanished: true # Toggle exclusion of vanished players
|
||||
afk: true # Detect AFK players and remove them from the sleep count (Essentials API used for detection when possible)
|
||||
notifier: true # Displays a notification when a new update is released
|
||||
|
||||
messages:
|
||||
chat:
|
||||
|
Loading…
Reference in New Issue
Block a user