From 7af2a99b3fd633d2b52464e239c927b7f68dae52 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 19 Jun 2024 12:32:10 +0200 Subject: [PATCH] implement downloader --- .classpath | 2 +- release.properties | 21 ---- .../m724/realweather/RealWeatherPlugin.java | 1 - .../m724/realweather/commands/GeoCommand.java | 19 +++- .../realweather/commands/UpdateCommand.java | 1 - .../realweather/updater/UpdateDownloader.java | 103 ++++++++++++++++++ .../eu/m724/realweather/updater/Updater.java | 4 +- .../updater/metadata/MetadataRetriever.java | 9 +- 8 files changed, 127 insertions(+), 33 deletions(-) delete mode 100644 release.properties create mode 100644 src/main/java/eu/m724/realweather/updater/UpdateDownloader.java diff --git a/.classpath b/.classpath index 2a51121..fc8e3d6 100644 --- a/.classpath +++ b/.classpath @@ -26,7 +26,7 @@ - + diff --git a/release.properties b/release.properties deleted file mode 100644 index a595cec..0000000 --- a/release.properties +++ /dev/null @@ -1,21 +0,0 @@ -#release configuration -#Tue Jun 18 13:42:52 CEST 2024 -completedPhase=check-poms -exec.pomFileName=pom.xml -exec.snapshotReleasePluginAllowed=false -pinExternals=false -preparationGoals=clean verify -project.scm.eu.m724\:realweather.developerConnection=scm\:git\:git@git.724.rocks\:Minecon724/realweather.git -project.scm.eu.m724\:realweather.tag=HEAD -projectVersionPolicyConfig=${projectVersionPolicyConfig}\n -projectVersionPolicyId=default -pushChanges=true -releaseStrategyId=default -remoteTagging=true -scm.branchCommitComment=@{prefix} prepare branch @{releaseLabel} -scm.commentPrefix=[maven-release-plugin] -scm.developmentCommitComment=@{prefix} prepare for next development iteration -scm.releaseCommitComment=@{prefix} prepare release @{releaseLabel} -scm.rollbackCommitComment=@{prefix} rollback the release of @{releaseLabel} -scm.tagNameFormat=@{project.artifactId}-@{project.version} -scm.url=scm\:git\:git@git.724.rocks\:Minecon724/realweather.git diff --git a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java index c85eab7..defdb9e 100644 --- a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java +++ b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java @@ -14,7 +14,6 @@ import com.google.common.base.Charsets; import eu.m724.realweather.commands.AdminCommand; import eu.m724.realweather.commands.GeoCommand; import eu.m724.realweather.commands.LocalTimeCommand; -import eu.m724.realweather.commands.UpdateCommand; import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.MapperConfig; import eu.m724.realweather.object.UserException; diff --git a/src/main/java/eu/m724/realweather/commands/GeoCommand.java b/src/main/java/eu/m724/realweather/commands/GeoCommand.java index 9ce6785..fbd674c 100644 --- a/src/main/java/eu/m724/realweather/commands/GeoCommand.java +++ b/src/main/java/eu/m724/realweather/commands/GeoCommand.java @@ -8,12 +8,16 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import eu.m724.realweather.GlobalConstants; +import eu.m724.realweather.weather.PlayerWeatherDirectory; import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.object.Weather; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; public class GeoCommand implements CommandExecutor { + PlayerWeatherDirectory playerWeatherDirectory = + GlobalConstants.getPlayerWeatherDirectory(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -25,12 +29,19 @@ public class GeoCommand implements CommandExecutor { Coordinates coordinates = GlobalConstants.getMapper().locationToCoordinates(location); BaseComponent[] component = new ComponentBuilder("\nGeolocation: ").color(ChatColor.GOLD) - .append(String.format("lat: %f, lon: %f\n", coordinates.latitude, coordinates.longitude)).color(ChatColor.AQUA) + .append("lat: %f, lon: %f\n".formatted(coordinates.latitude, coordinates.longitude)).color(ChatColor.AQUA) .append("Position: ").color(ChatColor.GRAY) - .append(String.format("x: %f, z: %f\n", location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) + .append("x: %f, z: %f".formatted(location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) .create(); // TODO improve readability player.spigot().sendMessage(component); + Weather weather = playerWeatherDirectory.getWeather(player); + if (weather != null) { + component = new ComponentBuilder("You're in ").color(ChatColor.GRAY) + .append(weather.city + "\n").color(ChatColor.DARK_AQUA) + .create(); + player.spigot().sendMessage(component); + } } else { sender.sendMessage("Add arguments to use this command in console"); } @@ -49,9 +60,9 @@ public class GeoCommand implements CommandExecutor { Location location = GlobalConstants.getMapper().coordinatesToLocation(player.getWorld(), coordinates); BaseComponent[] component = new ComponentBuilder("\nPosition: ").color(ChatColor.GOLD) - .append(String.format("x: %f, z: %f\n", location.getX(), location.getZ())).color(ChatColor.AQUA) + .append("x: %f, z: %f\n".formatted(location.getX(), location.getZ())).color(ChatColor.AQUA) .append("Geolocation: ").color(ChatColor.GRAY) - .append(String.format("lat: %f, lon: %f\n", coordinates.latitude, coordinates.longitude)).color(ChatColor.DARK_AQUA) + .append("lat: %f, lon: %f\n".formatted(coordinates.latitude, coordinates.longitude)).color(ChatColor.DARK_AQUA) .create(); player.spigot().sendMessage(component); diff --git a/src/main/java/eu/m724/realweather/commands/UpdateCommand.java b/src/main/java/eu/m724/realweather/commands/UpdateCommand.java index bc5f1b8..f303e8b 100644 --- a/src/main/java/eu/m724/realweather/commands/UpdateCommand.java +++ b/src/main/java/eu/m724/realweather/commands/UpdateCommand.java @@ -3,7 +3,6 @@ package eu.m724.realweather.commands; import java.util.concurrent.CompletableFuture; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import eu.m724.realweather.updater.Updater; diff --git a/src/main/java/eu/m724/realweather/updater/UpdateDownloader.java b/src/main/java/eu/m724/realweather/updater/UpdateDownloader.java new file mode 100644 index 0000000..93eaab0 --- /dev/null +++ b/src/main/java/eu/m724/realweather/updater/UpdateDownloader.java @@ -0,0 +1,103 @@ +package eu.m724.realweather.updater; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ProxySelector; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpClient.Redirect; +import java.net.http.HttpResponse.BodyHandlers; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.HexFormat; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import org.bukkit.plugin.Plugin; + +public class UpdateDownloader { + private Plugin plugin; + + private File getRunningPluginFile() { + try { + return new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } + + private CompletableFuture download(String url) { + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url)) + .header("User-Agent", "rwu/1") // real weather updater v1 + .build(); + + CompletableFuture> responseFuture = + HttpClient.newBuilder() + .followRedirects(Redirect.NORMAL) + .proxy(ProxySelector.getDefault()).build(). + sendAsync(request, BodyHandlers.ofInputStream()); + + CompletableFuture fileFuture = + responseFuture.thenApply(response -> { + File downloadFile = null; + try { + InputStream bodyStream = response.body(); + downloadFile = Files.createTempFile("realweather", null).toFile(); + FileOutputStream fileStream = new FileOutputStream(downloadFile); + + while (bodyStream.available() > 0) { + bodyStream.transferTo(fileStream); + } + + bodyStream.close(); + } catch (IOException e) { + throw new CompletionException(e); + } + + return downloadFile; + }); + + return fileFuture; + } + + private void install(File file) throws IOException { + // TODO what if we changed File to Path and every ref in this file + Files.move(file.toPath(), getRunningPluginFile().toPath(), StandardCopyOption.REPLACE_EXISTING); + } + + private byte[] computeFileSha256Hash(File file) throws IOException { + MessageDigest digest = null; + try { + digest = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + + try (FileInputStream fileInputStream = new FileInputStream(file)) { + byte[] buffer = new byte[16384]; + int len; + + while ((len = fileInputStream.read(buffer)) != -1) + digest.update(buffer, 0, len); + } + + return digest.digest(); + } + + private boolean compareBytesHex(byte[] bytes, String hecks) { + return Arrays.equals(bytes, HexFormat.of().parseHex(hecks)); + } +} diff --git a/src/main/java/eu/m724/realweather/updater/Updater.java b/src/main/java/eu/m724/realweather/updater/Updater.java index 8a5d1c3..d89287b 100644 --- a/src/main/java/eu/m724/realweather/updater/Updater.java +++ b/src/main/java/eu/m724/realweather/updater/Updater.java @@ -156,7 +156,7 @@ public class Updater extends BukkitRunnable implements Listener { .append(currentMetadata.label).color(ChatColor.DARK_AQUA) .build(); - DebugLogger.info(updateMessage.toString(), 0); - DebugLogger.info(updateActionMessage.toString(), 0); + DebugLogger.info(updateMessage.toPlainText(), 0); + DebugLogger.info(updateActionMessage.toPlainText(), 0); } } diff --git a/src/main/java/eu/m724/realweather/updater/metadata/MetadataRetriever.java b/src/main/java/eu/m724/realweather/updater/metadata/MetadataRetriever.java index 192c05d..f61c7b1 100644 --- a/src/main/java/eu/m724/realweather/updater/metadata/MetadataRetriever.java +++ b/src/main/java/eu/m724/realweather/updater/metadata/MetadataRetriever.java @@ -26,13 +26,16 @@ public class MetadataRetriever { this.channel = channel; } + public String getFileUrl(String version, String file) { + return "https://git.724.rocks/Minecon724/realweather-metadata/raw/branch/master/data/%s/%s/%s" + .formatted(channel, version, file); + } + /** * the completablefuture can throw a completionexception with {@link eu.m724.realweather.updater.metadata.MetadataServerException} */ private CompletableFuture getMetadataOf(String version) { - String url = String.format( - "https://git.724.rocks/Minecon724/realweather-metadata/raw/branch/master/data/%s/%s/%s", - channel, version, "meta-v1.json"); + String url = getFileUrl(version, "meta-v1.json"); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url))