diff options
Diffstat (limited to 'src/main/java/shcm/shsupercm')
13 files changed, 279 insertions, 118 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java index 726cdbf..0ecd632 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java @@ -1,23 +1,55 @@ package shcm.shsupercm.fabric.citresewn; import net.minecraft.client.render.model.BakedModel; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ArmorItem; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; +import shcm.shsupercm.fabric.citresewn.pack.cits.*; -import java.util.Collection; +import java.util.*; public class ActiveCITs { + public final Collection<CIT> cits; + public final Map<Item, List<CITItem>> citItems = new HashMap<>(); + public final Map<ArmorItem, List<CITArmor>> citArmor = new HashMap<>(); + public final List<CITElytra> citElytra = new ArrayList<>(); + public final List<CITEnchantment> citEnchantments = new ArrayList<>(); public ActiveCITs(Collection<CIT> cits) { - + this.cits = cits; + for (CIT cit : cits) { + if (cit instanceof CITItem item) + for (Item type : item.items) + citItems.computeIfAbsent(type, t -> new ArrayList<>()).add(item); + else if (cit instanceof CITArmor armor) + for (Item type : armor.items) + if (type instanceof ArmorItem armorType) + citArmor.computeIfAbsent(armorType, t -> new ArrayList<>()).add(armor); + else + CITResewn.LOG.error("Skipping item type: " + Registry.ITEM.getId(type) + " is not armor in " + cit.pack.resourcePack.getName() + " -> " + cit.propertiesIdentifier.getPath()); + else if (cit instanceof CITElytra) + citElytra.add((CITElytra) cit); + else if (cit instanceof CITEnchantment) + citEnchantments.add((CITEnchantment) cit); + } } public void dispose() { - + for (CIT cit : cits) { + cit.dispose(); + } + cits.clear(); + citItems.clear(); + citArmor.clear(); + citElytra.clear(); + citEnchantments.clear(); } - public BakedModel getItemModel(ItemStack stack, BakedModel model) { + public BakedModel getItemModel(ItemStack stack, BakedModel model, World world, LivingEntity entity) { //stub return null; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index 8880010..1c45468 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -3,23 +3,19 @@ package shcm.shsupercm.fabric.citresewn; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.render.model.BakedModel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.WeakHashMap; - @Environment(EnvType.CLIENT) public class CITResewn implements ClientModInitializer { - public static final Logger LOG = LogManager.getLogger("citresewn"); + public static final Logger LOG = LogManager.getLogger("CITResewn"); public static CITResewn INSTANCE; - public final WeakHashMap<BakedModel, String> bakedOverridesCache = new WeakHashMap<>(); - public ActiveCITs activeCITs = null; @Override public void onInitializeClient() { + INSTANCE = this; /*net.minecraft.client.render.entity.feature.ArmorFeatureRenderer#renderArmor take entity.getEquippedStack(armorSlot) eval override diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITLoadException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITLoadException.java new file mode 100644 index 0000000..0a3c54c --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITLoadException.java @@ -0,0 +1,13 @@ +package shcm.shsupercm.fabric.citresewn.ex; + +import net.minecraft.resource.ResourcePack; +import net.minecraft.util.Identifier; + +/** + * Thrown when a cit failed to be loaded + */ +public class CITLoadException extends Exception { + public CITLoadException(ResourcePack resourcePack, Identifier identifier, String message) { + super("Couldn't load CIT: " + message + " in " + resourcePack.getName() + " -> " + identifier.getPath()); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java index 10b67f0..49a0eb9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java @@ -8,10 +8,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Identifier.class) public class IdentifierMixin { - + /*//todo remove @Inject(method = "isPathValid(Ljava/lang/String;)Z", cancellable = true, at = @At("HEAD")) private static void isPathValid(String path, CallbackInfoReturnable<Boolean> ci) { - if(path.startsWith("citresewn") || path.startsWith("optifine") || path.startsWith("mcpatcher")) + if(path.startsWith("textures/block") || path.startsWith("block") || path.startsWith("optifine")) ci.setReturnValue(true); return; - } + }*/ } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java index 1679188..fd593ee 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java @@ -18,7 +18,7 @@ public class ItemRendererMixin { if (CITResewn.INSTANCE.activeCITs == null) return; - BakedModel citModel = CITResewn.INSTANCE.activeCITs.getItemModel(stack, cir.getReturnValue()); + BakedModel citModel = CITResewn.INSTANCE.activeCITs.getItemModel(stack, cir.getReturnValue(), world, entity); if (citModel != null) cir.setReturnValue(citModel); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java new file mode 100644 index 0000000..295af4f --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java @@ -0,0 +1,104 @@ +package shcm.shsupercm.fabric.citresewn.mixin; + +import net.minecraft.client.render.model.*; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.texture.TextureManager; +import net.minecraft.client.util.ModelIdentifier; +import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; +import net.minecraft.util.profiler.Profiler; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.pack.CITParser; +import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cits.CITItem; + +import java.util.*; +import java.util.stream.Collectors; + +@Mixin(ModelLoader.class) +public abstract class ModelLoaderMixin { + @Shadow @Final private ResourceManager resourceManager; + @Shadow @Final private Set<Identifier> modelsToLoad; + @Shadow @Final private Map<Identifier, UnbakedModel> unbakedModels; + @Shadow @Final private Map<Identifier, UnbakedModel> modelsToBake; + @Shadow @Final private Map<Identifier, BakedModel> bakedModels; + + private Map<Identifier, BakedModel> citOverrideCacheMap = new HashMap<>(); + + @Inject(method = "addModel", at = @At("TAIL")) + public void addCITModels(ModelIdentifier eventModelId, CallbackInfo ci) { if (eventModelId != ModelLoader.MISSING_ID) return; + if (CITResewn.INSTANCE.activeCITs != null) { + CITResewn.INSTANCE.activeCITs.dispose(); + CITResewn.INSTANCE.activeCITs = null; + } + + Collection<CIT> parsed = CITParser.parse(resourceManager.streamResourcePacks().collect(Collectors.toCollection(ArrayList::new))); + + for (CIT cit : parsed) + if (cit instanceof CITItem citItem) { + try { + citItem.loadUnbakedAssets(resourceManager); + + for (JsonUnbakedModel unbakedModel : citItem.unbakedAssets.values()) { + ResewnItemModelIdentifier id = new ResewnItemModelIdentifier(unbakedModel.id); + this.unbakedModels.put(id, unbakedModel); + this.modelsToLoad.addAll(unbakedModel.getModelDependencies()); + this.modelsToBake.put(id, unbakedModel); + } + } catch (Exception e) { + CITResewn.LOG.error(e.getMessage()); + } + } + + if (parsed.size() > 0) + CITResewn.INSTANCE.activeCITs = new ActiveCITs(parsed); + } + + @Inject(method = "bake", at = @At("RETURN")) + public void onBake(Identifier id, ModelBakeSettings settings, CallbackInfoReturnable<BakedModel> cir) { + this.citOverrideCacheMap.put(id, cir.getReturnValue()); + } + + @Inject(method = "upload", at = @At("RETURN")) + public void linkBakedModel(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable<SpriteAtlasManager> cir) { + profiler.push("citresewn_linking"); + + if (CITResewn.INSTANCE.activeCITs != null) { + for (CITItem citItem : CITResewn.INSTANCE.activeCITs.citItems.values().stream().flatMap(Collection::stream).distinct().collect(Collectors.toList())) { + for (Map.Entry<Identifier, JsonUnbakedModel> citModelEntry : citItem.unbakedAssets.entrySet()) { + if (citModelEntry.getKey() == null) { + citItem.bakedModel = this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id)); + } else { + if (citItem.subItems == null) + citItem.subItems = new HashMap<>(); + + BakedModel override = citOverrideCacheMap.get(citModelEntry.getKey()); + + if (override == null) + override = citOverrideCacheMap.get(new ModelIdentifier(citModelEntry.getKey().getNamespace(), citModelEntry.getKey().getPath().substring(5), "inventory")); + + if (override != null) + citItem.subItems.put(override, this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id))); + else { + CITResewn.LOG.error("Skipping sub cit: No such sub item model \"" + citModelEntry.getKey().getPath().substring(5) + "\" in " + citItem.pack.resourcePack.getName() + " -> " + citItem.propertiesIdentifier.getPath()); + } + } + } + citItem.unbakedAssets = null; + } + } + + this.citOverrideCacheMap = null; + + profiler.pop(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java deleted file mode 100644 index 0bfaa4e..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java +++ /dev/null @@ -1,27 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.mixin; - -import net.minecraft.client.render.model.BakedModel; -import net.minecraft.client.render.model.ModelLoader; -import net.minecraft.client.render.model.UnbakedModel; -import net.minecraft.client.render.model.json.JsonUnbakedModel; -import net.minecraft.client.render.model.json.ModelOverride; -import net.minecraft.client.render.model.json.ModelOverrideList; -import net.minecraft.util.Identifier; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.CITResewn; - -import java.util.function.Function; - -@Mixin(ModelOverrideList.class) -public class ModelOverrideListMixin { - @Inject(method = "bakeOverridingModel", at = @At("RETURN")) - public void onBakeOverridingModel(ModelLoader loader, JsonUnbakedModel parent, Function<Identifier, UnbakedModel> unbakedModelGetter, ModelOverride override, CallbackInfoReturnable<BakedModel> cir) { - if (cir.getReturnValue() != null) { - String[] split = override.getModelId().getPath().split("/"); - CITResewn.INSTANCE.bakedOverridesCache.put(cir.getReturnValue(), split[split.length - 1]); - } - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java deleted file mode 100644 index fb5a267..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java +++ /dev/null @@ -1,45 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.mixin; - -import net.minecraft.resource.*; -import net.minecraft.util.Identifier; -import net.minecraft.util.Unit; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.ActiveCITs; -import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.pack.CITParser; -import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; - -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; -import java.util.function.Predicate; -import java.util.stream.Stream; - -@Mixin(ReloadableResourceManagerImpl.class) -public abstract class ReloadableResourceManagerImplMixin implements ReloadableResourceManager { - @Shadow @Final private ResourceType type; - @Shadow public abstract Stream<ResourcePack> streamResourcePacks(); - - @Shadow public abstract Collection<Identifier> findResources(String startingPath, Predicate<String> pathPredicate); - - @Inject(method = "reload", at = @At("HEAD")) - private void onReload(Executor prepareExecutor, Executor applyExecutor, CompletableFuture<Unit> initialStage, List<ResourcePack> packs, CallbackInfoReturnable<ResourceReload> cir) { - if (this.type != ResourceType.CLIENT_RESOURCES) - return; - - if (CITResewn.INSTANCE.activeCITs != null) { - CITResewn.INSTANCE.activeCITs.dispose(); - CITResewn.INSTANCE.activeCITs = null; - } - - Collection<CIT> cits = CITParser.parse(packs); - - if (cits.size() > 0) - CITResewn.INSTANCE.activeCITs = new ActiveCITs(cits); - } -}
\ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java new file mode 100644 index 0000000..dc21ae1 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java @@ -0,0 +1,18 @@ +package shcm.shsupercm.fabric.citresewn.mixin; + +import net.minecraft.client.texture.SpriteAtlasTexture; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier; + +@Mixin(SpriteAtlasTexture.class) +public class SpriteAtlasTextureMixin { + @Inject(method = "getTexturePath", cancellable = true, at = @At("HEAD")) + public void forceLiteralResewnTextureIdentifier(Identifier id, CallbackInfoReturnable<Identifier> cir) { + if (id instanceof ResewnTextureIdentifier) + cir.setReturnValue(id); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java new file mode 100644 index 0000000..2313ba4 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java @@ -0,0 +1,13 @@ +package shcm.shsupercm.fabric.citresewn.pack; + +import net.minecraft.util.Identifier; + +/** + * Marks models as cit item models. + * @see shcm.shsupercm.fabric.citresewn.mixin.ModelLoaderMixin + */ +public class ResewnItemModelIdentifier extends Identifier { + public ResewnItemModelIdentifier(String id) { + super(id); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnTextureIdentifier.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnTextureIdentifier.java new file mode 100644 index 0000000..4a13bc2 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnTextureIdentifier.java @@ -0,0 +1,13 @@ +package shcm.shsupercm.fabric.citresewn.pack; + +import net.minecraft.util.Identifier; + +/** + * Marks path identifiers as forced literal texture paths. + * @see shcm.shsupercm.fabric.citresewn.mixin.SpriteAtlasTextureMixin + */ +public class ResewnTextureIdentifier extends Identifier { + public ResewnTextureIdentifier(Identifier identifier) { + super(identifier.getNamespace(), identifier.getPath()); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java index 218f28b..2bd2b76 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java @@ -19,6 +19,9 @@ import java.util.function.Predicate; import java.util.regex.Pattern; public abstract class CIT { + public final CITPack pack; + public final Identifier propertiesIdentifier; + public final Set<Item> items = new HashSet<>(); public final int damageMin, damageMax; @@ -39,6 +42,8 @@ public abstract class CIT { private final int weight; public CIT(CITPack pack, Identifier identifier, Properties properties) throws CITParseException { + this.pack = pack; + this.propertiesIdentifier = identifier; try { for (String itemId : (properties.getProperty("items", properties.getProperty("matchItems", " "))).split(" ")) if (!itemId.isEmpty()) { @@ -222,6 +227,10 @@ public abstract class CIT { } } + public void dispose() { + //stub + } + /** * Takes a defined path and resolves it to an identifier pointing to the resourcepack's path of the specified extension(returns null if no path can be resolved).<br> * If definedPath is null, will try to resolve a relative file with the same name as the propertyIdentifier with the extension, otherwise: <br> diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java index a7d0c10..1774378 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java @@ -1,62 +1,97 @@ package shcm.shsupercm.fabric.citresewn.pack.cits; +import com.google.common.collect.ImmutableMap; +import com.mojang.datafixers.util.Either; import net.minecraft.client.render.model.BakedModel; +import net.minecraft.client.render.model.json.JsonUnbakedModel; +import net.minecraft.client.render.model.json.ModelTransformation; +import net.minecraft.client.texture.SpriteAtlasTexture; +import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; -import shcm.shsupercm.fabric.citresewn.CITResewn; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.ex.CITLoadException; import shcm.shsupercm.fabric.citresewn.ex.CITParseException; import shcm.shsupercm.fabric.citresewn.pack.CITPack; +import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.*; public class CITItem extends CIT { - private BakedModel bakedModel = null; - private HashMap<String, BakedModel> subItems = null; + public Map<Identifier, Identifier> assetIdentifiers = new HashMap<>(); + public Map<Identifier, JsonUnbakedModel> unbakedAssets = new HashMap<>(); + + public BakedModel bakedModel = null; + public Map<BakedModel, BakedModel> subItems = null; public CITItem(CITPack pack, Identifier identifier, Properties properties) throws CITParseException { super(pack, identifier, properties); try { - Identifier modelIdentifier = resolvePath(identifier, properties.getProperty("model"), ".json", pack.resourcePack); - Map<String, Identifier> subIdentifiers = new HashMap<>(); + Identifier assetIdentifier = resolvePath(identifier, properties.getProperty("texture"), ".png", pack.resourcePack); + if (assetIdentifier != null) + assetIdentifiers.put(null, assetIdentifier); + + assetIdentifier = resolvePath(identifier, properties.getProperty("model"), ".json", pack.resourcePack); + if (assetIdentifier != null) + assetIdentifiers.put(null, assetIdentifier); + + for (Object o : properties.keySet()) + if (o instanceof String property && property.startsWith("texture.")) { + Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".png", pack.resourcePack); + if (subIdentifier == null) + throw new Exception("Cannot resolve path for " + property); + assetIdentifiers.put(new Identifier("minecraft", "item/" + property.substring(8)), subIdentifier); + } for (Object o : properties.keySet()) if (o instanceof String property && property.startsWith("model.")) { Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".json", pack.resourcePack); if (subIdentifier == null) throw new Exception("Cannot resolve path for " + property); - subIdentifiers.put(property.substring(5), subIdentifier); + assetIdentifiers.put(new Identifier("minecraft", "item/" + property.substring(6)), subIdentifier); } - if (modelIdentifier == null && subIdentifiers.size() == 0) { - Identifier textureIdentifier = resolvePath(identifier, properties.getProperty("texture"), ".png", pack.resourcePack); + if (assetIdentifiers.size() == 0) + throw new Exception("Cannot resolve path for model/texture"); + } catch (Exception e) { + throw new CITParseException(pack.resourcePack, identifier, (e.getClass() == Exception.class ? "" : e.getClass().getSimpleName() + ": ") + e.getMessage()); + } + } - for (Object o : properties.keySet()) - if (o instanceof String property && property.startsWith("texture.")) { - Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".png", pack.resourcePack); - if (subIdentifier == null) - throw new Exception("Cannot resolve path for " + property); - subIdentifiers.put(property.substring(5), subIdentifier); - } + public void loadUnbakedAssets(ResourceManager resourceManager) throws CITLoadException { + try { + Identifier baseIdentifier = assetIdentifiers.remove(null); - if (textureIdentifier == null && subIdentifiers.size() == 0) - throw new Exception("Cannot resolve path for model/texture"); + if (baseIdentifier != null) + unbakedAssets.put(null, loadUnbakedAsset(resourceManager, baseIdentifier)); - // load textures - } + for (Map.Entry<Identifier, Identifier> assetEntry : assetIdentifiers.entrySet()) + unbakedAssets.put(assetEntry.getKey(), loadUnbakedAsset(resourceManager, assetEntry.getValue())); - // load models + assetIdentifiers = null; } catch (Exception e) { - throw new CITParseException(pack.resourcePack, identifier, (e.getClass() == Exception.class ? "" : e.getClass().getSimpleName() + ": ") + e.getMessage()); + throw new CITLoadException(pack.resourcePack, propertiesIdentifier, (e.getClass() == Exception.class ? "" : e.getClass().getSimpleName() + ": ") + e.getMessage()); } } - public BakedModel getBakedModel(BakedModel originalModel) { - - String subItemName; - if (originalModel == null || (subItemName = CITResewn.INSTANCE.bakedOverridesCache.get(originalModel)) == null) - return this.bakedModel; - - return this.subItems == null ? this.bakedModel : this.subItems.getOrDefault(subItemName, this.bakedModel); + private JsonUnbakedModel loadUnbakedAsset(ResourceManager resourceManager, Identifier identifier) throws Exception { + InputStream is = null; + try { + JsonUnbakedModel json; + if (identifier.getPath().endsWith(".json")) { + json = JsonUnbakedModel.deserialize(IOUtils.toString(is = resourceManager.getResource(identifier).getInputStream(), StandardCharsets.UTF_8)); + json.id = identifier.toString(); json.id = json.id.substring(0, json.id.length() - 5); + return json; + } else if (identifier.getPath().endsWith(".png")) { + json = new JsonUnbakedModel(new Identifier("minecraft", "item/generated"), new ArrayList<>(), ImmutableMap.of("layer0", Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(identifier)))), true, JsonUnbakedModel.GuiLight.ITEM, ModelTransformation.NONE, new ArrayList<>()); + json.id = identifier.toString(); json.id = json.id.substring(0, json.id.length() - 4); + return json; + } + } finally { + IOUtils.closeQuietly(is); + } + throw new Exception("Unknown asset type"); } } |