Skip to content

Commit

Permalink
Use capes from the new git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Sep 18, 2024
1 parent 399ad70 commit 49cfec1
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/frozenblock/lib/FrozenClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
66 changes: 65 additions & 1 deletion src/main/java/net/frozenblock/lib/cape/api/CapeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<JsonElement> allowedUUIDList = allowedUUIDs.asList();
if (allowedUUIDList.isEmpty()) {
registerCape(capeLocation);
} else {
List<UUID> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation> 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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.frozenblock.lib.cape.client;
package net.frozenblock.lib.cape.client.impl;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -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;
Expand All @@ -38,7 +37,7 @@
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class FrozenClientCapeData {
public class ClientCapeData {
private static final Map<UUID, Cape> CAPES_IN_SERVER = new HashMap<>();

public static Optional<ResourceLocation> getCapeTexture(UUID uuid) {
Expand Down Expand Up @@ -68,7 +67,9 @@ private static void setPlayerCapeTexture(UUID uuid, @NotNull Optional<Cape> 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)));
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/net/frozenblock/lib/cape/impl/ServerCapeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,13 +88,6 @@ public static void init() {
UUID.fromString("3f89b045-9f47-49ac-b9a0-be2c225bd7fa") // Stella
);

ArrayList<UUID> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.

0 comments on commit 49cfec1

Please sign in to comment.