From 9c669288547ab58ca10b885a206c897bcbc1fbe5 Mon Sep 17 00:00:00 2001 From: Dennis C Date: Fri, 16 Dec 2022 17:48:20 +0100 Subject: [PATCH] Add block atlas config to register forge:white texture (#9187) --- .../assets/minecraft/atlases/blocks.json | 8 ++ .../net/minecraftforge/common/ForgeMod.java | 3 + .../data/ForgeSpriteSourceProvider.java | 26 ++++++ .../common/data/SpriteSourceProvider.java | 82 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 src/generated/resources/assets/minecraft/atlases/blocks.json create mode 100644 src/main/java/net/minecraftforge/common/data/ForgeSpriteSourceProvider.java create mode 100644 src/main/java/net/minecraftforge/common/data/SpriteSourceProvider.java diff --git a/src/generated/resources/assets/minecraft/atlases/blocks.json b/src/generated/resources/assets/minecraft/atlases/blocks.json new file mode 100644 index 0000000000..0dbd944957 --- /dev/null +++ b/src/generated/resources/assets/minecraft/atlases/blocks.json @@ -0,0 +1,8 @@ +{ + "sources": [ + { + "type": "minecraft:single", + "resource": "forge:white" + } + ] +} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/ForgeMod.java b/src/main/java/net/minecraftforge/common/ForgeMod.java index ee9400c8c7..0f3477b273 100644 --- a/src/main/java/net/minecraftforge/common/ForgeMod.java +++ b/src/main/java/net/minecraftforge/common/ForgeMod.java @@ -43,6 +43,7 @@ import net.minecraftforge.common.crafting.IntersectionIngredient; import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.common.data.ForgeBiomeTagsProvider; import net.minecraftforge.common.data.ForgeFluidTagsProvider; +import net.minecraftforge.common.data.ForgeSpriteSourceProvider; import net.minecraftforge.common.extensions.IForgeEntity; import net.minecraftforge.common.extensions.IForgePlayer; import net.minecraftforge.common.loot.CanToolPerformAction; @@ -488,6 +489,8 @@ public class ForgeMod gen.addProvider(event.includeServer(), new ForgeRecipeProvider(packOutput)); gen.addProvider(event.includeServer(), new ForgeLootTableProvider(packOutput)); gen.addProvider(event.includeServer(), new ForgeBiomeTagsProvider(packOutput, lookupProvider, existingFileHelper)); + + gen.addProvider(event.includeClient(), new ForgeSpriteSourceProvider(packOutput, existingFileHelper)); } public void missingSoundMapping(MissingMappingsEvent event) diff --git a/src/main/java/net/minecraftforge/common/data/ForgeSpriteSourceProvider.java b/src/main/java/net/minecraftforge/common/data/ForgeSpriteSourceProvider.java new file mode 100644 index 0000000000..dba1b29516 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/data/ForgeSpriteSourceProvider.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.common.data; + +import net.minecraft.client.renderer.texture.atlas.sources.SingleFile; +import net.minecraft.data.PackOutput; +import net.minecraft.resources.ResourceLocation; + +import java.util.Optional; + +public class ForgeSpriteSourceProvider extends SpriteSourceProvider +{ + public ForgeSpriteSourceProvider(PackOutput output, ExistingFileHelper fileHelper) + { + super(output, fileHelper, "forge"); + } + + @Override + protected void addSources() + { + atlas(SpriteSourceProvider.BLOCKS_ATLAS).addSource(new SingleFile(new ResourceLocation("forge:white"), Optional.empty())); + } +} diff --git a/src/main/java/net/minecraftforge/common/data/SpriteSourceProvider.java b/src/main/java/net/minecraftforge/common/data/SpriteSourceProvider.java new file mode 100644 index 0000000000..22ee9ffe76 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/data/SpriteSourceProvider.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.common.data; + +import com.mojang.serialization.JsonOps; +import net.minecraft.client.renderer.texture.atlas.SpriteSource; +import net.minecraft.client.renderer.texture.atlas.SpriteSources; +import net.minecraft.data.PackOutput; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.PackType; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; + +/** + *

Data provider for atlas configuration files.
+ * An atlas configuration is bound to a specific texture atlas such as the {@code minecraft:blocks} atlas and + * allows adding additional textures to the atlas by adding {@link SpriteSource}s to the configuration.

+ *

See {@link SpriteSources} for the available sources and the constants in this class for the + * atlases used in vanilla Minecraft

+ */ +public abstract class SpriteSourceProvider extends JsonCodecProvider> +{ + protected static final ResourceLocation BLOCKS_ATLAS = new ResourceLocation("blocks"); + protected static final ResourceLocation BANNER_PATTERNS_ATLAS = new ResourceLocation("banner_patterns"); + protected static final ResourceLocation BEDS_ATLAS = new ResourceLocation("beds"); + protected static final ResourceLocation CHESTS_ATLAS = new ResourceLocation("chests"); + protected static final ResourceLocation SHIELD_PATTERNS_ATLAS = new ResourceLocation("shield_patterns"); + protected static final ResourceLocation SHULKER_BOXES_ATLAS = new ResourceLocation("shulker_boxes"); + protected static final ResourceLocation SIGNS_ATLAS = new ResourceLocation("signs"); + protected static final ResourceLocation MOB_EFFECTS_ATLAS = new ResourceLocation("mob_effects"); + protected static final ResourceLocation PAINTINGS_ATLAS = new ResourceLocation("paintings"); + protected static final ResourceLocation PARTICLES_ATLAS = new ResourceLocation("particles"); + + private final Map atlases = new HashMap<>(); + + public SpriteSourceProvider(PackOutput output, ExistingFileHelper fileHelper, String modid) + { + super(output, fileHelper, modid, JsonOps.INSTANCE, PackType.CLIENT_RESOURCES, "atlases", SpriteSources.FILE_CODEC, Map.of()); + } + + @Override + protected final void gather(BiConsumer> consumer) + { + addSources(); + atlases.forEach((atlas, srcList) -> consumer.accept(atlas, srcList.sources)); + } + + protected abstract void addSources(); + + /** + * Get or create a {@link SourceList} for the given atlas + * @param atlas The texture atlas the sources should be added to, see constants at the top for the format + * and the vanilla atlases + * @return an existing {@code SourceList} for the given atlas or a new one if not present yet + */ + protected final SourceList atlas(ResourceLocation atlas) + { + return atlases.computeIfAbsent(atlas, $ -> new SourceList()); + } + + protected static final class SourceList + { + private final List sources = new ArrayList<>(); + + /** + * Add the given {@link SpriteSource} to this atlas configuration + * @param source The {@code SpriteSource} to be added + */ + public SourceList addSource(SpriteSource source) + { + sources.add(source); + return this; + } + } +}