From eb21ecfddc7199b9ffb3ebc4010385a844a42c09 Mon Sep 17 00:00:00 2001 From: 1e99 Date: Sun, 15 Dec 2024 21:26:32 +0100 Subject: [PATCH] add status bossbar --- .../assets/pixelchat/lang/en_us.json | 5 ++- mod-fabric/resources/pixelchat.mixins.json | 1 + .../eu/e99/pixelchat/fabric/PixelChat.java | 1 + .../e99/pixelchat/fabric/duck/BossBars.java | 14 ++++++ .../pixelchat/fabric/image/ImageUploader.java | 43 ++++++++++++++++--- .../fabric/mixin/BossBarHudMixin.java | 33 ++++++++++++++ 6 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 mod-fabric/src/eu/e99/pixelchat/fabric/duck/BossBars.java create mode 100644 mod-fabric/src/eu/e99/pixelchat/fabric/mixin/BossBarHudMixin.java diff --git a/mod-fabric/resources/assets/pixelchat/lang/en_us.json b/mod-fabric/resources/assets/pixelchat/lang/en_us.json index 67bbac5..aa69a57 100644 --- a/mod-fabric/resources/assets/pixelchat/lang/en_us.json +++ b/mod-fabric/resources/assets/pixelchat/lang/en_us.json @@ -1,4 +1,5 @@ { - "pixelshare.downloading": "Downloading image...", - "pixelshare.failed": "Download failed." + "pixelchat.uploading": "Uploading", + "pixelchat.upload_failed": "Upload failed", + "pixelchat.upload_completed": "Upload completed" } \ No newline at end of file diff --git a/mod-fabric/resources/pixelchat.mixins.json b/mod-fabric/resources/pixelchat.mixins.json index af19f47..e0fda93 100644 --- a/mod-fabric/resources/pixelchat.mixins.json +++ b/mod-fabric/resources/pixelchat.mixins.json @@ -9,6 +9,7 @@ "defaultRequire": 1 }, "client": [ + "BossBarHudMixin", "MessageHandlerMixin" ] } \ No newline at end of file diff --git a/mod-fabric/src/eu/e99/pixelchat/fabric/PixelChat.java b/mod-fabric/src/eu/e99/pixelchat/fabric/PixelChat.java index 3b3eda1..d2bc5ea 100644 --- a/mod-fabric/src/eu/e99/pixelchat/fabric/PixelChat.java +++ b/mod-fabric/src/eu/e99/pixelchat/fabric/PixelChat.java @@ -3,6 +3,7 @@ package eu.e99.pixelchat.fabric; import eu.e99.pixelchat.fabric.image.ImageUploader; import net.fabricmc.api.ClientModInitializer; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.hud.BossBarHud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mod-fabric/src/eu/e99/pixelchat/fabric/duck/BossBars.java b/mod-fabric/src/eu/e99/pixelchat/fabric/duck/BossBars.java new file mode 100644 index 0000000..7d687de --- /dev/null +++ b/mod-fabric/src/eu/e99/pixelchat/fabric/duck/BossBars.java @@ -0,0 +1,14 @@ +package eu.e99.pixelchat.fabric.duck; + +import net.minecraft.client.gui.hud.ClientBossBar; + +import java.util.UUID; + +public interface BossBars { + + void add(UUID uuid, ClientBossBar bar); + + void remove(UUID uuid); + + ClientBossBar get(UUID uuid); +} diff --git a/mod-fabric/src/eu/e99/pixelchat/fabric/image/ImageUploader.java b/mod-fabric/src/eu/e99/pixelchat/fabric/image/ImageUploader.java index dfcd365..f332bcd 100644 --- a/mod-fabric/src/eu/e99/pixelchat/fabric/image/ImageUploader.java +++ b/mod-fabric/src/eu/e99/pixelchat/fabric/image/ImageUploader.java @@ -1,15 +1,18 @@ package eu.e99.pixelchat.fabric.image; import eu.e99.pixelchat.fabric.PixelChat; +import eu.e99.pixelchat.fabric.duck.BossBars; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.hud.ClientBossBar; +import net.minecraft.entity.boss.BossBar; +import net.minecraft.text.Text; -import java.io.IOException; 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.nio.file.Path; +import java.util.UUID; public class ImageUploader { @@ -28,8 +31,22 @@ public class ImageUploader { } private void innerUploadImage(Path path) { + BossBars bossBars = (BossBars) minecraft.inGameHud.getBossBarHud(); + ClientBossBar bossBar = new ClientBossBar( + UUID.randomUUID(), + Text.translatable("pixelchat.uploading"), + 0f, + BossBar.Color.BLUE, + BossBar.Style.PROGRESS, + false, + false, + false + ); + try { - PixelChat.LOGGER.info("Uploading {}", path); + this.minecraft.send(() -> bossBars.add(bossBar.getUuid(), bossBar)); + + // TODO: Report some sort of progress? HttpRequest req = HttpRequest.newBuilder() .POST(HttpRequest.BodyPublishers.ofFile(path)) .uri(this.endpoint) @@ -45,18 +62,34 @@ public class ImageUploader { String downloadUrl = res.body(); - PixelChat.LOGGER.info("Uploaded to {}", downloadUrl); this.minecraft.send(() -> { // TODO: Are we actually playing? if (this.minecraft.player == null) { return; } + bossBar.setName(Text.translatable("pixelchat.upload_completed")); + bossBar.setPercent(1.0f); + bossBar.setColor(BossBar.Color.GREEN); + this.minecraft.inGameHud.getChatHud().addToMessageHistory(downloadUrl); this.minecraft.player.networkHandler.sendChatMessage(downloadUrl); }); } catch (Throwable e) { - PixelChat.LOGGER.error("Failed to upload", e); + PixelChat.LOGGER.error("Upload failed", e); + + this.minecraft.send(() -> { + bossBar.setName(Text.translatable("pixelchat.upload_failed")); + bossBar.setPercent(1.0f); + bossBar.setColor(BossBar.Color.RED); + }); + } finally { + try { + Thread.sleep(5000); + } catch (InterruptedException ignored) { + } + + this.minecraft.send(() -> bossBars.remove(bossBar.getUuid())); } } } diff --git a/mod-fabric/src/eu/e99/pixelchat/fabric/mixin/BossBarHudMixin.java b/mod-fabric/src/eu/e99/pixelchat/fabric/mixin/BossBarHudMixin.java new file mode 100644 index 0000000..f9d5d3c --- /dev/null +++ b/mod-fabric/src/eu/e99/pixelchat/fabric/mixin/BossBarHudMixin.java @@ -0,0 +1,33 @@ +package eu.e99.pixelchat.fabric.mixin; + +import eu.e99.pixelchat.fabric.duck.BossBars; +import net.minecraft.client.gui.hud.BossBarHud; +import net.minecraft.client.gui.hud.ClientBossBar; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.Map; +import java.util.UUID; + +@Mixin(BossBarHud.class) +public class BossBarHudMixin implements BossBars { + + @Shadow @Final + Map bossBars; + + @Override + public void add(UUID uuid, ClientBossBar bar) { + this.bossBars.put(uuid, bar); + } + + @Override + public void remove(UUID uuid) { + this.bossBars.remove(uuid); + } + + @Override + public ClientBossBar get(UUID uuid) { + return this.bossBars.get(uuid); + } +}