From 49cfec15ff71bdb5aba268e2f06249704b881c06 Mon Sep 17 00:00:00 2001 From: AViewFromTheTop <87103914+AViewFromTheTop@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:20:19 -0400 Subject: [PATCH] Use capes from the new git repo --- .../net/frozenblock/lib/FrozenClient.java | 4 +- .../frozenblock/lib/cape/api/CapeUtil.java | 66 +++++++++++++++++- .../lib/cape/client/api/ClientCapeUtil.java | 45 ++++++++++++ .../ClientCapeData.java} | 9 +-- .../lib/cape/impl/ServerCapeData.java | 11 +-- .../frozenlib_config/FrozenLibConfig.java | 1 - .../networking/FrozenClientNetworking.java | 6 +- .../frozenlib/textures/cape/fallback.png | Bin 0 -> 142 bytes .../frozenlib/textures/cape/frozenblock.png | Bin 512 -> 0 bytes .../textures/cape/very_blue_cape.png | Bin 147 -> 0 bytes 10 files changed, 121 insertions(+), 21 deletions(-) create mode 100644 src/main/java/net/frozenblock/lib/cape/client/api/ClientCapeUtil.java rename src/main/java/net/frozenblock/lib/cape/client/{FrozenClientCapeData.java => impl/ClientCapeData.java} (93%) create mode 100644 src/main/resources/assets/frozenlib/textures/cape/fallback.png delete mode 100644 src/main/resources/assets/frozenlib/textures/cape/frozenblock.png delete mode 100644 src/main/resources/assets/frozenlib/textures/cape/very_blue_cape.png diff --git a/src/main/java/net/frozenblock/lib/FrozenClient.java b/src/main/java/net/frozenblock/lib/FrozenClient.java index 2c3b9f368..47d8077c2 100644 --- a/src/main/java/net/frozenblock/lib/FrozenClient.java +++ b/src/main/java/net/frozenblock/lib/FrozenClient.java @@ -23,7 +23,7 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; -import net.frozenblock.lib.cape.client.FrozenClientCapeData; +import net.frozenblock.lib.cape.client.impl.ClientCapeData; import net.frozenblock.lib.config.frozenlib_config.FrozenLibConfig; import net.frozenblock.lib.debug.client.impl.DebugRenderManager; import net.frozenblock.lib.debug.networking.StructureDebugRequestPayload; @@ -66,7 +66,7 @@ public void onInitializeClient() { particleRegistry.register(FrozenParticleTypes.DEBUG_POS, DebugPosParticle.Provider::new); Panoramas.addPanorama(ResourceLocation.withDefaultNamespace("textures/gui/title/background/panorama")); - FrozenClientCapeData.init(); + ClientCapeData.init(); var resourceLoader = ResourceManagerHelper.get(PackType.CLIENT_RESOURCES); resourceLoader.registerReloadListener(BlockSoundGroupManager.INSTANCE); diff --git a/src/main/java/net/frozenblock/lib/cape/api/CapeUtil.java b/src/main/java/net/frozenblock/lib/cape/api/CapeUtil.java index acefd9605..3037838e4 100644 --- a/src/main/java/net/frozenblock/lib/cape/api/CapeUtil.java +++ b/src/main/java/net/frozenblock/lib/cape/api/CapeUtil.java @@ -18,9 +18,24 @@ package net.frozenblock.lib.cape.api; import com.google.common.collect.ImmutableList; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; +import net.fabricmc.api.EnvType; +import net.fabricmc.loader.api.FabricLoader; +import net.frozenblock.lib.FrozenSharedConstants; +import net.frozenblock.lib.cape.client.api.ClientCapeUtil; import net.frozenblock.lib.cape.impl.Cape; import net.frozenblock.lib.registry.api.FrozenRegistry; import net.minecraft.core.Registry; @@ -58,7 +73,56 @@ public static boolean canPlayerUserCape(UUID uuid, @NotNull Cape cape) { return Registry.register(FrozenRegistry.CAPE, id, new Cape(id, buildCapeTextureLocation(id), Optional.of(ImmutableList.copyOf(uuids)))); } - private static ResourceLocation buildCapeTextureLocation(@NotNull ResourceLocation cape) { + public static void registerCapesFromURL(String urlString) { + try { + URL url = URI.create(urlString).toURL(); + URLConnection request = url.openConnection(); + request.connect(); + + JsonElement parsedJson = JsonParser.parseReader(new InputStreamReader((InputStream) request.getContent())); + JsonObject capeDir = parsedJson.getAsJsonObject(); + JsonArray capeArray = capeDir.get("capes").getAsJsonArray(); + + capeArray.forEach(jsonElement -> { + registerCapeFromURL(jsonElement.getAsString()); + }); + } catch (IOException ignored) {} + } + + private static void registerCapeFromURL(String urlString) { + try { + URL url = URI.create(urlString).toURL(); + URLConnection request = url.openConnection(); + request.connect(); + + JsonElement parsedJson = JsonParser.parseReader(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element + JsonObject capeJson = parsedJson.getAsJsonObject(); + String capeId = capeJson.get("id").getAsString(); + String capeTexture = capeJson.get("texture").getAsString(); + JsonArray allowedUUIDs = capeJson.get("allowed_uuids").getAsJsonArray(); + + ResourceLocation capeLocation = ResourceLocation.tryParse(capeId); + if (capeLocation != null) { + ResourceLocation capeTextureLocation = CapeUtil.buildCapeTextureLocation(capeLocation); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + ClientCapeUtil.registerCapeTextureFromURL(capeLocation, capeTextureLocation, capeTexture); + } + + List allowedUUIDList = allowedUUIDs.asList(); + if (allowedUUIDList.isEmpty()) { + registerCape(capeLocation); + } else { + List uuidList = new ArrayList<>(); + allowedUUIDList.forEach(jsonElement -> uuidList.add(UUID.fromString(jsonElement.getAsString()))); + registerCapeWithWhitelist(capeLocation, ImmutableList.copyOf(uuidList)); + } + } + } catch (IOException ignored) { + FrozenSharedConstants.LOGGER.error("Failed to parse Cape from URL: {}", urlString); + } + } + + public static ResourceLocation buildCapeTextureLocation(@NotNull ResourceLocation cape) { return ResourceLocation.tryBuild(cape.getNamespace(), "textures/cape/" + cape.getPath() + ".png"); } } diff --git a/src/main/java/net/frozenblock/lib/cape/client/api/ClientCapeUtil.java b/src/main/java/net/frozenblock/lib/cape/client/api/ClientCapeUtil.java new file mode 100644 index 000000000..41bc86dda --- /dev/null +++ b/src/main/java/net/frozenblock/lib/cape/client/api/ClientCapeUtil.java @@ -0,0 +1,45 @@ +package net.frozenblock.lib.cape.client.api; + +import com.google.gson.JsonIOException; +import java.nio.file.Path; +import java.util.concurrent.CompletableFuture; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; +import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.HttpTexture; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.resources.ResourceManager; +import org.jetbrains.annotations.NotNull; + +@Environment(EnvType.CLIENT) +public class ClientCapeUtil { + public static final Path CAPE_CACHE_PATH = Minecraft.getInstance().gameDirectory.toPath().resolve("frozenlib_cape_cache"); + + public static void registerCapeTextureFromURL( + @NotNull ResourceLocation capeLocation, ResourceLocation capeTextureLocation, String textureURL + ) throws JsonIOException { + ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() { + @Override + public ResourceLocation getFabricId() { + return capeLocation; + } + + @Override + public void onResourceManagerReload(@NotNull ResourceManager resourceManager) { + CompletableFuture completableFuture = new CompletableFuture<>(); + HttpTexture httpTexture = new HttpTexture( + CAPE_CACHE_PATH.resolve(capeLocation.getNamespace()).resolve(capeLocation.getPath() + ".png").toFile(), + textureURL, + DefaultPlayerSkin.getDefaultTexture(), + false, + () -> completableFuture.complete(capeTextureLocation) + ); + Minecraft.getInstance().getTextureManager().register(capeTextureLocation, httpTexture); + } + }); + } +} diff --git a/src/main/java/net/frozenblock/lib/cape/client/FrozenClientCapeData.java b/src/main/java/net/frozenblock/lib/cape/client/impl/ClientCapeData.java similarity index 93% rename from src/main/java/net/frozenblock/lib/cape/client/FrozenClientCapeData.java rename to src/main/java/net/frozenblock/lib/cape/client/impl/ClientCapeData.java index 8fef3a05e..3a1ad2212 100644 --- a/src/main/java/net/frozenblock/lib/cape/client/FrozenClientCapeData.java +++ b/src/main/java/net/frozenblock/lib/cape/client/impl/ClientCapeData.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package net.frozenblock.lib.cape.client; +package net.frozenblock.lib.cape.client.impl; import java.util.HashMap; import java.util.Map; @@ -27,7 +27,6 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.frozenblock.lib.cape.client.impl.AbstractClientPlayerCapeInterface; import net.frozenblock.lib.cape.impl.Cape; import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket; import net.frozenblock.lib.config.frozenlib_config.FrozenLibConfig; @@ -38,7 +37,7 @@ import org.jetbrains.annotations.NotNull; @Environment(EnvType.CLIENT) -public class FrozenClientCapeData { +public class ClientCapeData { private static final Map CAPES_IN_SERVER = new HashMap<>(); public static Optional getCapeTexture(UUID uuid) { @@ -68,7 +67,9 @@ private static void setPlayerCapeTexture(UUID uuid, @NotNull Optional cape } public static void init() { - ClientLifecycleEvents.CLIENT_STOPPING.register(client -> CAPES_IN_SERVER.clear()); + ClientLifecycleEvents.CLIENT_STOPPING.register(client -> { + CAPES_IN_SERVER.clear(); + }); ClientPlayConnectionEvents.DISCONNECT.register((clientPacketListener, minecraft) -> CAPES_IN_SERVER.clear()); ClientPlayConnectionEvents.JOIN.register((clientPacketListener, packetSender, minecraft) -> { ClientPlayNetworking.send(CapeCustomizePacket.createPacket(minecraft.getUser().getProfileId(), ResourceLocation.parse(FrozenLibConfig.get().cape))); diff --git a/src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java b/src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java index 8516f1f11..7c87a3f80 100644 --- a/src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java +++ b/src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java @@ -18,7 +18,6 @@ package net.frozenblock.lib.cape.impl; import com.google.common.collect.ImmutableList; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,7 +26,6 @@ import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.frozenblock.lib.FrozenSharedConstants; import net.frozenblock.lib.cape.api.CapeUtil; import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket; import net.minecraft.server.level.ServerPlayer; @@ -90,13 +88,6 @@ public static void init() { UUID.fromString("3f89b045-9f47-49ac-b9a0-be2c225bd7fa") // Stella ); - ArrayList allArray = new ArrayList<>(devs); - allArray.addAll(artists); - allArray.addAll(builders); - allArray.addAll(composers); - allArray.addAll(soundDesigners); - - CapeUtil.registerCapeWithWhitelist(FrozenSharedConstants.id("frozenblock"), allArray); - CapeUtil.registerCapeWithWhitelist(FrozenSharedConstants.id("very_blue_cape"), UUID.fromString("ccaa0664-8fd4-4176-96b4-eab6f8c75083")); // Alex + CapeUtil.registerCapesFromURL("https://raw.githubusercontent.com/FrozenBlock/CapeRepo/refs/heads/master/cape_directory.json"); } } diff --git a/src/main/java/net/frozenblock/lib/config/frozenlib_config/FrozenLibConfig.java b/src/main/java/net/frozenblock/lib/config/frozenlib_config/FrozenLibConfig.java index 89b9b1fb3..fee1e51bb 100644 --- a/src/main/java/net/frozenblock/lib/config/frozenlib_config/FrozenLibConfig.java +++ b/src/main/java/net/frozenblock/lib/config/frozenlib_config/FrozenLibConfig.java @@ -28,7 +28,6 @@ import net.frozenblock.lib.config.api.registry.ConfigRegistry; import net.frozenblock.lib.config.api.sync.SyncBehavior; import net.frozenblock.lib.config.api.sync.annotation.EntrySyncData; -import net.minecraft.resources.ResourceLocation; import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixerBuilder; import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixes; diff --git a/src/main/java/net/frozenblock/lib/networking/FrozenClientNetworking.java b/src/main/java/net/frozenblock/lib/networking/FrozenClientNetworking.java index 802b6aa19..3b0be5664 100644 --- a/src/main/java/net/frozenblock/lib/networking/FrozenClientNetworking.java +++ b/src/main/java/net/frozenblock/lib/networking/FrozenClientNetworking.java @@ -24,7 +24,7 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.frozenblock.lib.cape.client.FrozenClientCapeData; +import net.frozenblock.lib.cape.client.impl.ClientCapeData; import net.frozenblock.lib.cape.impl.networking.CapeCustomizePacket; import net.frozenblock.lib.config.api.instance.Config; import net.frozenblock.lib.config.api.registry.ConfigRegistry; @@ -332,9 +332,9 @@ private static void receiveCapePacket() { ClientPlayNetworking.registerGlobalReceiver(CapeCustomizePacket.PACKET_TYPE, (packet, ctx) -> { UUID uuid = packet.getPlayerUUID(); if (packet.isEnabled()) { - FrozenClientCapeData.setCapeForUUID(uuid, packet.getCapeId()); + ClientCapeData.setCapeForUUID(uuid, packet.getCapeId()); } else { - FrozenClientCapeData.removeCapeForUUID(uuid); + ClientCapeData.removeCapeForUUID(uuid); } }); } diff --git a/src/main/resources/assets/frozenlib/textures/cape/fallback.png b/src/main/resources/assets/frozenlib/textures/cape/fallback.png new file mode 100644 index 0000000000000000000000000000000000000000..e4564238709882b9dbc4d19d13ac523546f8ad38 GIT binary patch literal 142 zcmeAS@N?(olHy`uVBq!ia0vp^4nVBH!3HE3&8=$zQlXwMjv*C{Z!a*i9dO`rxw!ZL z(qN4Sp|c9#YS|nf&D|~)yZ%?)KCZRZ`|sB^oReqBvOjP@AE-}*VKXB`fi+XZ2_Q${ nZ>A#y!yitDhRMtmR6Hli`Odbxcb;<|$Q}kyS3j3^P6P)t-s0000y zK8pYXC;$T%6952rm(WjTyC@`I8y7gf%KF^t`v3j^|NZ{j>HGi&Isgey{r~y^4p|)* zHzp=r+3NoP|Nl>9y-;Mk{r~xfljEZR0004WQchCTS7P%l#Gxu7E9-9U_Yj9s03dzsNkF1yZgyj zbtWXck4dUz*kgg^o9SVmZzfqxc+rDa1;B!b0>IPb6M{(bE>k+g5#k8 z&r1^-o5r5N;?QYQf)kPhL6BvPfJ1^k=?ge@QBrJXBw&p2!EhKZtQp{<2{2w>n32FR zW7_1mMZPEjUa!Bhi?O-B_Tss+e4KXKDkI@g5N5qLG5ZhH4t(F`w7&)CI}U(g{3{o%(MgagM=L{UW*Rg#70000_!@pW9{kU7*fHQyyE}==7;vo0)PJhZ-2Pb6Mw<&;$U0;4aDl