add status bossbar

This commit is contained in:
1e99 2024-12-15 21:26:32 +01:00
parent e15a7d9666
commit eb21ecfddc
6 changed files with 90 additions and 7 deletions

View file

@ -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"
}

View file

@ -9,6 +9,7 @@
"defaultRequire": 1
},
"client": [
"BossBarHudMixin",
"MessageHandlerMixin"
]
}

View file

@ -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;

View file

@ -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);
}

View file

@ -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()));
}
}
}

View file

@ -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<UUID, ClientBossBar> 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);
}
}