1
mirror of https://github.com/Shiewk/ViewServerResources.git synced 2026-04-28 03:44:17 +02:00

Add setting for showing URLs in chat

This commit is contained in:
Shy
2024-08-24 16:56:59 +02:00
parent f09aeadc6e
commit ee4407928e
4 changed files with 72 additions and 21 deletions
@@ -14,6 +14,7 @@ import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.net.URL;
@@ -23,6 +24,7 @@ public class ResourcePackPrivacyClient implements ClientModInitializer {
private static final ObjectArrayList<String> whitelistedURLs = new ObjectArrayList<>();
private static final ObjectArrayList<String> whitelistedHosts = new ObjectArrayList<>();
private static boolean broadcastDownloads = true;
private static File whitelistFile;
private static final Gson gson = new Gson();
@@ -68,6 +70,8 @@ public class ResourcePackPrivacyClient implements ClientModInitializer {
for (JsonElement whitelistURL : whitelistURLs) {
whitelistedURLs.add(whitelistURL.getAsString());
}
final JsonElement bdl = cfg.get("broadcastDownloads");
broadcastDownloads = bdl == null || !bdl.isJsonPrimitive() || bdl.getAsBoolean();
} catch (FileNotFoundException e) {
ResourcePackPrivacy.LOGGER.warn("Config file not found");
} catch (IOException e) {
@@ -78,31 +82,38 @@ public class ResourcePackPrivacyClient implements ClientModInitializer {
public static void saveConfig() {
ResourcePackPrivacy.LOGGER.info("Saving config");
try (FileWriter fw = new FileWriter(whitelistFile)) {
JsonObject cfg = new JsonObject();
JsonObject whitelist = new JsonObject();
JsonArray hosts = new JsonArray();
for (String whitelistedHost : whitelistedHosts) {
hosts.add(whitelistedHost);
}
whitelist.add("hosts", hosts);
JsonArray urls = new JsonArray();
for (String whitelistedURL : whitelistedURLs) {
urls.add(whitelistedURL);
}
whitelist.add("urls", urls);
cfg.add("whitelist", whitelist);
final JsonObject cfg = getConfigObject();
try (JsonWriter jsonWriter = new JsonWriter(fw)) {
Streams.write(cfg, jsonWriter);
}
} catch (IOException e) {
ResourcePackPrivacy.logThrowable(e);
}
}
private static @NotNull JsonObject getConfigObject() {
JsonObject cfg = new JsonObject();
JsonObject whitelist = new JsonObject();
JsonArray hosts = new JsonArray();
for (String whitelistedHost : whitelistedHosts) {
hosts.add(whitelistedHost);
}
whitelist.add("hosts", hosts);
JsonArray urls = new JsonArray();
for (String whitelistedURL : whitelistedURLs) {
urls.add(whitelistedURL);
}
whitelist.add("urls", urls);
cfg.add("whitelist", whitelist);
cfg.addProperty("broadcastDownloads", broadcastDownloads);
return cfg;
}
public static List<String> getWhitelistedURLs() {
return whitelistedURLs;
}
@@ -111,6 +122,14 @@ public class ResourcePackPrivacyClient implements ClientModInitializer {
return whitelistedHosts;
}
public static boolean isBroadcastDownloads() {
return broadcastDownloads;
}
public static void setBroadcastDownloads(boolean broadcastDownloads) {
ResourcePackPrivacyClient.broadcastDownloads = broadcastDownloads;
}
@Override
public void onInitializeClient() {
whitelistFile = new File(MinecraftClient.getInstance().runDirectory.getPath() + "/resourcepackprivacy.json");
@@ -1,5 +1,6 @@
package de.shiewk.resourcepackprivacy.mixin;
import de.shiewk.resourcepackprivacy.client.ResourcePackPrivacyClient;
import de.shiewk.resourcepackprivacy.event.ChatAnnouncer;
import net.minecraft.client.resource.server.ServerResourcePackLoader;
import net.minecraft.text.Text;
@@ -17,9 +18,11 @@ public class MixinServerResourcePackLoader {
@Inject(at = @At("HEAD"), method = "addResourcePack(Ljava/util/UUID;Ljava/net/URL;Ljava/lang/String;)V")
public void onResourcePackAdd(UUID id, URL url, String hash, CallbackInfo ci){
ChatAnnouncer.announce(Text.translatable("gui.resourcepackprivacy.downloading",
Text.literal(url.toString()))
.withColor(Color.ORANGE.getRGB())
);
if (ResourcePackPrivacyClient.isBroadcastDownloads()){
ChatAnnouncer.announce(Text.translatable("gui.resourcepackprivacy.downloading",
Text.literal(url.toString()))
.withColor(Color.ORANGE.getRGB())
);
}
}
}
@@ -1,6 +1,8 @@
package de.shiewk.resourcepackprivacy.screen;
import de.shiewk.resourcepackprivacy.client.ResourcePackPrivacyClient;
import it.unimi.dsi.fastutil.booleans.Boolean2ObjectFunction;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.GridWidget;
@@ -8,9 +10,13 @@ import net.minecraft.client.gui.widget.SimplePositioningWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;
import java.awt.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class ResourcePackPrivacyConfigScreen extends Screen {
private final Screen parent;
private static final int buttonWidth = 192;
private boolean cfgDirty = false;
public ResourcePackPrivacyConfigScreen(Screen parent) {
super(Text.translatable("gui.resourcepackprivacy.config"));
this.parent = parent;
@@ -18,6 +24,9 @@ public class ResourcePackPrivacyConfigScreen extends Screen {
@Override
public void close() {
if (cfgDirty){
ResourcePackPrivacyClient.saveConfig();
}
assert client != null;
client.setScreen(parent);
}
@@ -33,14 +42,24 @@ public class ResourcePackPrivacyConfigScreen extends Screen {
final GridWidget gw = new GridWidget();
gw.getMainPositioner().margin(4, 4, 4, 0);
final GridWidget.Adder adder = gw.createAdder(2);
adder.add(createToggleableLargeButton(
ResourcePackPrivacyClient.isBroadcastDownloads(),
bl -> Text.translatable("resourcepackprivacy.settings.broadcast", Text.translatable(bl ? "gui.yes" : "gui.no")).withColor(bl ? new Color(100, 255, 100).getRGB() : new Color(255, 100, 100).getRGB()),
bl -> {
ResourcePackPrivacyClient.setBroadcastDownloads(bl);
cfgDirty = true;
}
), 2);
adder.add(createButton(Text.translatable("resourcepackprivacy.settings.whitelistedURLs"), btn -> {
btn.active = false;
assert client != null;
cfgDirty = true;
client.setScreen(new ManageListScreen<>(Text.translatable("resourcepackprivacy.settings.whitelistedURLs"), this, ResourcePackPrivacyClient.getWhitelistedURLs()));
}));
adder.add(createButton(Text.translatable("resourcepackprivacy.settings.whitelistedHosts"), btn -> {
btn.active = false;
assert client != null;
cfgDirty = true;
client.setScreen(new ManageListScreen<>(Text.translatable("resourcepackprivacy.settings.whitelistedHosts"), this, ResourcePackPrivacyClient.getWhitelistedHosts()));
}));
gw.refreshPositions();
@@ -52,4 +71,13 @@ public class ResourcePackPrivacyConfigScreen extends Screen {
private ButtonWidget createButton(Text m, ButtonWidget.PressAction action){
return new ButtonWidget.Builder(m, action).width(buttonWidth).build();
}
private ButtonWidget createToggleableLargeButton(boolean state, Boolean2ObjectFunction<Text> function, BooleanConsumer onToggle){
AtomicBoolean bl = new AtomicBoolean(state);
return new ButtonWidget.Builder(function.get(state), btn -> {
bl.set(!bl.get());
onToggle.accept(bl.get());
btn.setMessage(function.apply(bl.get()));
}).width(buttonWidth * 2 + 8).build();
}
}
@@ -13,5 +13,6 @@
"gui.resourcepackprivacy.config": "Config",
"resourcepackprivacy.settings": "ResourcePackPrivacy Settings",
"resourcepackprivacy.settings.whitelistedURLs": "Manage allowed URLs",
"resourcepackprivacy.settings.whitelistedHosts": "Manage allowed pack hosts"
"resourcepackprivacy.settings.whitelistedHosts": "Manage allowed pack hosts",
"resourcepackprivacy.settings.broadcast": "Show downloaded pack URLs in chat: %s"
}