1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-26 17:06:54 +00:00

Automatically bake models from turtle modellers (#1531)

This avoids us having to list a bunch of model locations twice, and
hopefully makes things a little cleaner for add-on authors too
This commit is contained in:
Jonathan Coates 2023-07-22 20:51:50 +01:00 committed by GitHub
parent dd08d1ec8e
commit 940f59b116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 20 deletions

View File

@ -11,10 +11,13 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser;
import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
/** /**
* Provides models for a {@link ITurtleUpgrade}. * Provides models for a {@link ITurtleUpgrade}.
@ -50,6 +53,21 @@ public interface TurtleUpgradeModeller<T extends ITurtleUpgrade> {
return getModel(upgrade, (ITurtleAccess) null, side); return getModel(upgrade, (ITurtleAccess) null, side);
} }
/**
* Get a list of models that this turtle modeller depends on.
* <p>
* Models included in this list will be loaded and baked alongside item and block models, and so may be referenced
* by {@link TransformedModel#of(ResourceLocation)}. You do not need to override this method if you will load models
* by other means.
*
* @return A list of models that this modeller depends on.
* @see UnbakedModel#getDependencies()
*/
default Collection<ResourceLocation> getDependencies() {
return List.of();
}
/** /**
* A basic {@link TurtleUpgradeModeller} which renders using the upgrade's {@linkplain ITurtleUpgrade#getCraftingItem() * A basic {@link TurtleUpgradeModeller} which renders using the upgrade's {@linkplain ITurtleUpgrade#getCraftingItem()
* crafting item}. * crafting item}.
@ -74,7 +92,8 @@ public interface TurtleUpgradeModeller<T extends ITurtleUpgrade> {
* @return The constructed modeller. * @return The constructed modeller.
*/ */
static <T extends ITurtleUpgrade> TurtleUpgradeModeller<T> sided(ModelResourceLocation left, ModelResourceLocation right) { static <T extends ITurtleUpgrade> TurtleUpgradeModeller<T> sided(ModelResourceLocation left, ModelResourceLocation right) {
return (upgrade, turtle, side) -> TransformedModel.of(side == TurtleSide.LEFT ? left : right); // TODO(1.21.0): Remove this.
return sided((ResourceLocation) left, right);
} }
/** /**
@ -86,6 +105,16 @@ public interface TurtleUpgradeModeller<T extends ITurtleUpgrade> {
* @return The constructed modeller. * @return The constructed modeller.
*/ */
static <T extends ITurtleUpgrade> TurtleUpgradeModeller<T> sided(ResourceLocation left, ResourceLocation right) { static <T extends ITurtleUpgrade> TurtleUpgradeModeller<T> sided(ResourceLocation left, ResourceLocation right) {
return (upgrade, turtle, side) -> TransformedModel.of(side == TurtleSide.LEFT ? left : right); return new TurtleUpgradeModeller<>() {
@Override
public TransformedModel getModel(T upgrade, @Nullable ITurtleAccess turtle, TurtleSide side) {
return TransformedModel.of(side == TurtleSide.LEFT ? left : right);
}
@Override
public Collection<ResourceLocation> getDependencies() {
return List.of(left, right);
}
};
} }
} }

View File

@ -13,6 +13,7 @@ import dan200.computercraft.client.render.RenderTypes;
import dan200.computercraft.client.render.TurtleBlockEntityRenderer; import dan200.computercraft.client.render.TurtleBlockEntityRenderer;
import dan200.computercraft.client.render.monitor.MonitorBlockEntityRenderer; import dan200.computercraft.client.render.monitor.MonitorBlockEntityRenderer;
import dan200.computercraft.client.turtle.TurtleModemModeller; import dan200.computercraft.client.turtle.TurtleModemModeller;
import dan200.computercraft.client.turtle.TurtleUpgradeModellers;
import dan200.computercraft.core.util.Colour; import dan200.computercraft.core.util.Colour;
import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.common.IColouredItem;
@ -107,24 +108,6 @@ public final class ClientRegistry {
} }
private static final String[] EXTRA_MODELS = new String[]{ private static final String[] EXTRA_MODELS = new String[]{
// Turtle upgrades
"block/turtle_modem_normal_off_left",
"block/turtle_modem_normal_on_left",
"block/turtle_modem_normal_off_right",
"block/turtle_modem_normal_on_right",
"block/turtle_modem_advanced_off_left",
"block/turtle_modem_advanced_on_left",
"block/turtle_modem_advanced_off_right",
"block/turtle_modem_advanced_on_right",
"block/turtle_crafting_table_left",
"block/turtle_crafting_table_right",
"block/turtle_speaker_left",
"block/turtle_speaker_right",
// Turtle block renderer
"block/turtle_colour", "block/turtle_colour",
"block/turtle_elf_overlay", "block/turtle_elf_overlay",
"block/turtle_rainbow_overlay", "block/turtle_rainbow_overlay",
@ -133,6 +116,7 @@ public final class ClientRegistry {
public static void registerExtraModels(Consumer<ResourceLocation> register) { public static void registerExtraModels(Consumer<ResourceLocation> register) {
for (var model : EXTRA_MODELS) register.accept(new ResourceLocation(ComputerCraftAPI.MOD_ID, model)); for (var model : EXTRA_MODELS) register.accept(new ResourceLocation(ComputerCraftAPI.MOD_ID, model));
TurtleUpgradeModellers.getDependencies().forEach(register);
} }
public static void registerItemColours(BiConsumer<ItemColor, ItemLike> register) { public static void registerItemColours(BiConsumer<ItemColor, ItemLike> register) {

View File

@ -13,6 +13,9 @@ import dan200.computercraft.shared.turtle.upgrades.TurtleModem;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
/** /**
* A {@link TurtleUpgradeModeller} for modems, providing different models depending on if the modem is on/off. * A {@link TurtleUpgradeModeller} for modems, providing different models depending on if the modem is on/off.
*/ */
@ -48,4 +51,9 @@ public class TurtleModemModeller implements TurtleUpgradeModeller<TurtleModem> {
? TransformedModel.of(active ? leftOnModel : leftOffModel) ? TransformedModel.of(active ? leftOnModel : leftOffModel)
: TransformedModel.of(active ? rightOnModel : rightOffModel); : TransformedModel.of(active ? rightOnModel : rightOffModel);
} }
@Override
public Collection<ResourceLocation> getDependencies() {
return List.of(leftOffModel, rightOffModel, leftOnModel, rightOnModel);
}
} }

View File

@ -15,10 +15,12 @@ import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.impl.UpgradeManager; import dan200.computercraft.impl.UpgradeManager;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
/** /**
* A registry of {@link TurtleUpgradeModeller}s. * A registry of {@link TurtleUpgradeModeller}s.
@ -71,4 +73,8 @@ public final class TurtleUpgradeModellers {
var modeller = turtleModels.get(wrapper.serialiser()); var modeller = turtleModels.get(wrapper.serialiser());
return modeller == null ? NULL_TURTLE_MODELLER : modeller; return modeller == null ? NULL_TURTLE_MODELLER : modeller;
} }
public static Stream<ResourceLocation> getDependencies() {
return turtleModels.values().stream().flatMap(x -> x.getDependencies().stream());
}
} }