Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload confirmation #1

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ labyMod {
author = "RappyTV"
description = "Upload your minecraft screenshots to your custom destinations."
minecraftVersion = "*"
version = System.getenv().getOrDefault("VERSION", "1.0.0")
version = System.getenv().getOrDefault("VERSION", "1.0.1")
}

minecraft {
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/com/rappytv/uploader/UploaderAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import com.rappytv.uploader.api.uploaders.ImgurUploader;
import com.rappytv.uploader.api.uploaders.XBackBoneUploader;
import com.rappytv.uploader.api.uploaders.ZiplineUploader;
import com.rappytv.uploader.command.UploadCommand;
import com.rappytv.uploader.config.UploaderConfig;
import com.rappytv.uploader.listeners.ScreenshotListener;
import com.rappytv.uploader.listener.ScreenshotListener;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.format.NamedTextColor;
Expand All @@ -23,7 +24,8 @@ public class UploaderAddon extends LabyAddon<UploaderConfig> {
@Override
protected void enable() {
registerSettingCategory();
registerListener(new ScreenshotListener(this));
registerCommand(new UploadCommand(this));
registerListener(new ScreenshotListener());

loadUploaders();
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/com/rappytv/uploader/api/ApiRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rappytv.uploader.api;

import com.rappytv.uploader.UploaderAddon;
import java.io.File;
import java.net.URI;
import java.net.http.HttpClient;
Expand All @@ -17,8 +16,8 @@ public class ApiRequest {
private final Uploader uploader;
private final File file;

public ApiRequest(UploaderAddon addon, File file) {
this.uploader = addon.configuration().uploader();
public ApiRequest(Uploader uploader, File file) {
this.uploader = uploader;
this.file = file;
}

Expand Down
77 changes: 77 additions & 0 deletions core/src/main/java/com/rappytv/uploader/command/UploadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.rappytv.uploader.command;

import com.rappytv.uploader.UploaderAddon;
import com.rappytv.uploader.api.ApiRequest;
import com.rappytv.uploader.api.Uploader;
import net.labymod.api.client.chat.command.Command;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.event.ClickEvent;
import net.labymod.api.client.component.event.HoverEvent;
import net.labymod.api.client.component.format.NamedTextColor;
import net.labymod.api.client.component.format.Style;
import net.labymod.api.client.component.format.TextDecoration;
import java.io.File;

public class UploadCommand extends Command {

private final UploaderAddon addon;

public UploadCommand(UploaderAddon addon) {
super("supload");
this.addon = addon;
}

@Override
public boolean execute(String prefix, String[] args) {
Uploader uploader = addon.configuration().uploader();
if(uploader.getAuth()[1].isBlank()) {
displayMessage(UploaderAddon.prefix.copy().append(Component.translatable("uploader.upload.noToken", NamedTextColor.RED)));
return true;
}
if(args.length < 1) {
displayMessage(UploaderAddon.prefix.copy().append(Component.translatable("uploader.upload.file", NamedTextColor.RED)));
return true;
}
File file = new File(System.getProperty("user.dir") + "/screenshots/" + args[0]);
if(!file.exists()) {
displayMessage(UploaderAddon.prefix.copy().append(Component.translatable("uploader.upload.file", NamedTextColor.RED)));
return true;
}
displayMessage(UploaderAddon.prefix.copy().append(Component.translatable("uploader.upload.uploading", NamedTextColor.GRAY)));
ApiRequest request = new ApiRequest(uploader, file);
request.sendAsyncRequest().thenAccept((result) -> {
if(request.isSuccessful()) {
Component copy = Component.translatable(
"uploader.upload.copy",
Style.empty()
.color(NamedTextColor.AQUA)
.decorate(TextDecoration.BOLD)
.hoverEvent(HoverEvent.showText(Component.translatable("uploader.upload.hover").color(NamedTextColor.GREEN)))
.clickEvent(ClickEvent.copyToClipboard(!request.getUploadLink().isBlank() ? request.getUploadLink() : ""))
);
Component component = Component.translatable(
"uploader.upload.uploaded",
!request.getUploadLink().isBlank() ? copy : Component.text("")
).color(NamedTextColor.GRAY);

displayMessage(UploaderAddon.prefix.copy().append(component));
} else {
displayMessage(
UploaderAddon.prefix.copy().append(Component.text(
request.getError(),
NamedTextColor.RED
))
);
}
}).exceptionally((e) -> {
displayMessage(
UploaderAddon.prefix.copy().append(Component.text(
e.getMessage(),
NamedTextColor.RED
))
);
return null;
});
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.rappytv.uploader.listener;

import com.rappytv.uploader.UploaderAddon;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.event.ClickEvent;
import net.labymod.api.client.component.event.HoverEvent;
import net.labymod.api.client.component.format.NamedTextColor;
import net.labymod.api.client.component.format.Style;
import net.labymod.api.event.Subscribe;
import net.labymod.api.event.client.misc.WriteScreenshotEvent;

public class ScreenshotListener {

@Subscribe
public void onScreenshot(WriteScreenshotEvent event) {
Component component = UploaderAddon.prefix.copy().append(
Component.translatable(
"uploader.upload.upload",
Style.empty()
.color(NamedTextColor.AQUA)
.hoverEvent(HoverEvent.showText(Component.translatable("uploader.upload.hover").color(NamedTextColor.GREEN)))
.clickEvent(ClickEvent.runCommand("/supload " + event.getDestination().getName()))
)
);

Laby.references().chatExecutor().displayClientMessage(component);
}
}

This file was deleted.

6 changes: 5 additions & 1 deletion core/src/main/resources/assets/uploader/i18n/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
}
},
"upload": {
"msg": "Screenshot uploaded! %s",
"noToken": "Please provide the API Key for your uploader in the addon config.",
"file": "File not found!",
"upload": "Click here to upload the screenshot!",
"uploading": "Uploading screenshot...",
"uploaded": "Screenshot uploaded! %s",
"hover": "Clickable",
"copy": "[COPY URL]",
"emptyError": "Empty error!",
Expand Down