[WIP] major rewrite, grass and leaves working already
This commit is contained in:
@@ -1,24 +1,16 @@
|
||||
package mods.betterfoliage.resource.generated
|
||||
|
||||
import mods.betterfoliage.resource.Identifier
|
||||
import mods.betterfoliage.BetterFoliageMod
|
||||
import mods.betterfoliage.util.Atlas
|
||||
import mods.betterfoliage.util.HasLogger
|
||||
import mods.betterfoliage.util.completedVoid
|
||||
import mods.betterfoliage.util.map
|
||||
import mods.octarinecore.client.resource.AsyncSpriteProvider
|
||||
import mods.octarinecore.client.resource.AtlasFuture
|
||||
import mods.octarinecore.client.resource.StitchPhases
|
||||
import net.minecraft.client.renderer.model.ModelBakery
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.client.resources.ClientResourcePackInfo
|
||||
import net.minecraft.resources.*
|
||||
import net.minecraft.resources.ResourcePackType.CLIENT_RESOURCES
|
||||
import net.minecraft.resources.data.IMetadataSectionSerializer
|
||||
import net.minecraft.util.ResourceLocation
|
||||
import net.minecraft.util.text.StringTextComponent
|
||||
import org.apache.logging.log4j.Logger
|
||||
import java.io.IOException
|
||||
import org.apache.logging.log4j.Level.INFO
|
||||
import java.util.*
|
||||
import java.util.concurrent.CompletableFuture
|
||||
import java.util.concurrent.ExecutionException
|
||||
import java.util.function.Predicate
|
||||
import java.util.function.Supplier
|
||||
|
||||
@@ -28,57 +20,46 @@ import java.util.function.Supplier
|
||||
* @param[name] Name of the resource pack
|
||||
* @param[generators] List of resource generators
|
||||
*/
|
||||
class GeneratedBlockTexturePack(val nameSpace: String, val packName: String, override val logger: Logger) : HasLogger, IResourcePack,
|
||||
AsyncSpriteProvider<ModelBakery> {
|
||||
class GeneratedTexturePack(
|
||||
val nameSpace: String, val packName: String
|
||||
) : IResourcePack {
|
||||
|
||||
val logger = BetterFoliageMod.detailLogger(this)
|
||||
|
||||
override fun getName() = packName
|
||||
override fun getResourceNamespaces(type: ResourcePackType) = setOf(nameSpace)
|
||||
override fun <T : Any?> getMetadata(deserializer: IMetadataSectionSerializer<T>) = null
|
||||
override fun getRootResourceStream(id: String) = null
|
||||
override fun getAllResourceLocations(type: ResourcePackType, namespace:String, path: String, maxDepth: Int, filter: Predicate<String>) = emptyList<Identifier>()
|
||||
override fun getAllResourceLocations(type: ResourcePackType, namespace:String, path: String, maxDepth: Int, filter: Predicate<String>) = emptyList<ResourceLocation>()
|
||||
|
||||
override fun close() {}
|
||||
|
||||
protected var manager: CompletableFuture<IResourceManager>? = null
|
||||
val identifiers = Collections.synchronizedMap(mutableMapOf<Any, Identifier>())
|
||||
val resources = Collections.synchronizedMap(mutableMapOf<Identifier, CompletableFuture<ByteArray>>())
|
||||
protected var manager: IResourceManager = Minecraft.getInstance().resourceManager
|
||||
val identifiers = Collections.synchronizedMap(mutableMapOf<Any, ResourceLocation>())
|
||||
val resources = Collections.synchronizedMap(mutableMapOf<ResourceLocation, ByteArray>())
|
||||
|
||||
fun register(key: Any, func: (IResourceManager)->ByteArray): Identifier {
|
||||
if (manager == null) throw IllegalStateException("Cannot register resources unless block textures are being reloaded")
|
||||
fun register(atlas: Atlas, key: Any, func: (IResourceManager)->ByteArray): ResourceLocation {
|
||||
identifiers[key]?.let { return it }
|
||||
|
||||
val id = Identifier(nameSpace, UUID.randomUUID().toString())
|
||||
val resource = manager!!.map { func(it) }
|
||||
val id = ResourceLocation(nameSpace, UUID.randomUUID().toString())
|
||||
val fileName = atlas.file(id)
|
||||
val resource = func(manager)
|
||||
|
||||
identifiers[key] = id
|
||||
resources[Atlas.BLOCKS.wrap(id)] = resource
|
||||
log("generated resource $key -> $id")
|
||||
resources[fileName] = resource
|
||||
logger.log(INFO, "generated resource $key -> $fileName")
|
||||
return id
|
||||
}
|
||||
|
||||
override fun getResourceStream(type: ResourcePackType, id: Identifier) =
|
||||
if (type != CLIENT_RESOURCES) null else
|
||||
try { resources[id]!!.get().inputStream() }
|
||||
catch (e: ExecutionException) { (e.cause as? IOException)?.let { throw it } } // rethrow wrapped IOException if present
|
||||
override fun getResourceStream(type: ResourcePackType, id: ResourceLocation) =
|
||||
if (type != CLIENT_RESOURCES) null else resources[id]?.inputStream()
|
||||
|
||||
override fun resourceExists(type: ResourcePackType, id: Identifier) =
|
||||
override fun resourceExists(type: ResourcePackType, id: ResourceLocation) =
|
||||
type == CLIENT_RESOURCES && resources.containsKey(id)
|
||||
|
||||
override fun setup(manager: IResourceManager, bakeryF: CompletableFuture<ModelBakery>, atlas: AtlasFuture): StitchPhases {
|
||||
this.manager = CompletableFuture.completedFuture(manager)
|
||||
return StitchPhases(
|
||||
completedVoid(),
|
||||
atlas.runAfter {
|
||||
this.manager = null
|
||||
identifiers.clear()
|
||||
resources.clear()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
val finder = object : IPackFinder {
|
||||
val packInfo = ClientResourcePackInfo(
|
||||
packName, true, Supplier { this@GeneratedBlockTexturePack },
|
||||
packName, true, Supplier { this@GeneratedTexturePack },
|
||||
StringTextComponent(packName),
|
||||
StringTextComponent("Generated block textures resource pack"),
|
||||
PackCompatibility.COMPATIBLE, ResourcePackInfo.Priority.TOP, true, null, true
|
||||
@@ -87,4 +68,4 @@ class GeneratedBlockTexturePack(val nameSpace: String, val packName: String, ove
|
||||
(nameToPackMap as MutableMap<String, ResourcePackInfo>).put(packName, packInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user