implement downloader
This commit is contained in:
parent
2f8560fc41
commit
7af2a99b3f
8 changed files with 127 additions and 33 deletions
|
@ -26,7 +26,7 @@
|
|||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
|
|
|
@ -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>${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
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
103
src/main/java/eu/m724/realweather/updater/UpdateDownloader.java
Normal file
103
src/main/java/eu/m724/realweather/updater/UpdateDownloader.java
Normal file
|
@ -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<File> download(String url) {
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("User-Agent", "rwu/1") // real weather updater v1
|
||||
.build();
|
||||
|
||||
CompletableFuture<HttpResponse<InputStream>> responseFuture =
|
||||
HttpClient.newBuilder()
|
||||
.followRedirects(Redirect.NORMAL)
|
||||
.proxy(ProxySelector.getDefault()).build().
|
||||
sendAsync(request, BodyHandlers.ofInputStream());
|
||||
|
||||
CompletableFuture<File> 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));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<VersionMetadata> 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))
|
||||
|
|
Loading…
Reference in a new issue