-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LanguageProvider & DirectResourceProvider
- Loading branch information
Showing
5 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
src/main/kotlin/net/mcbrawls/packin/lang/LanguageList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package net.mcbrawls.packin.lang | ||
|
||
/** | ||
* A utility class for all of Minecraft's languages. | ||
*/ | ||
object LanguageList { | ||
/** | ||
* The store of languages. | ||
*/ | ||
private val languages: List<String> = listOf( | ||
"af_za", | ||
"ar_sa", | ||
"ast_es", | ||
"az_az", | ||
"ba_ru", | ||
"bar", | ||
"be_by", | ||
"bg_bg", | ||
"br_fr", | ||
"brb", | ||
"bs_ba", | ||
"ca_es", | ||
"cs_cz", | ||
"cy_gb", | ||
"da_dk", | ||
"de_at", | ||
"de_ch", | ||
"de_de", | ||
"el_gr", | ||
"en_au", | ||
"en_ca", | ||
"en_gb", | ||
"en_nz", | ||
"en_pt", | ||
"en_ud", | ||
"en_us", | ||
"enp", | ||
"enws", | ||
"eo_uy", | ||
"es_ar", | ||
"es_cl", | ||
"es_ec", | ||
"es_es", | ||
"es_mx", | ||
"es_uy", | ||
"es_ve", | ||
"esan", | ||
"et_ee", | ||
"eu_es", | ||
"fa_ir", | ||
"fi_fi", | ||
"fil_ph", | ||
"fo_fo", | ||
"fr_ca", | ||
"fr_fr", | ||
"fra_de", | ||
"fur_it", | ||
"fy_nl", | ||
"ga_ie", | ||
"gd_gb", | ||
"gl_es", | ||
"haw_us", | ||
"he_il", | ||
"hi_in", | ||
"hr_hr", | ||
"hu_hu", | ||
"hy_am", | ||
"id_id", | ||
"ig_ng", | ||
"io_en", | ||
"is_is", | ||
"isv", | ||
"it_it", | ||
"ja_jp", | ||
"jbo_en", | ||
"ka_ge", | ||
"kk_kz", | ||
"kn_in", | ||
"ko_kr", | ||
"ksh", | ||
"kw_gb", | ||
"la_la", | ||
"lb_lu", | ||
"li_li", | ||
"lmo", | ||
"lol_us", | ||
"lt_lt", | ||
"lv_lv", | ||
"lzh", | ||
"mk_mk", | ||
"mn_mn", | ||
"ms_my", | ||
"mt_mt", | ||
"nah", | ||
"nds_de", | ||
"nl_be", | ||
"nl_nl", | ||
"nn_no", | ||
"no_no", | ||
"oc_fr", | ||
"ovd", | ||
"pl_pl", | ||
"pt_br", | ||
"pt_pt", | ||
"qya_aa", | ||
"ro_ro", | ||
"rpr", | ||
"ru_ru", | ||
"ry_ua", | ||
"se_no", | ||
"sk_sk", | ||
"sl_si", | ||
"so_so", | ||
"sq_al", | ||
"sr_sp", | ||
"sv_se", | ||
"sxu", | ||
"szl", | ||
"ta_in", | ||
"th_th", | ||
"tl_ph", | ||
"tlh_aa", | ||
"tok", | ||
"tr_tr", | ||
"tt_ru", | ||
"uk_ua", | ||
"val_es", | ||
"vec_it", | ||
"vi_vn", | ||
"yi_de", | ||
"yo_ng", | ||
"zh_cn", | ||
"zh_hk", | ||
"zh_tw", | ||
"zlm_arab", | ||
) | ||
|
||
val containers = listOf( | ||
"barrel", | ||
"blast_furnace", | ||
"brewing", | ||
"cartography_table", | ||
"chest", | ||
"chestDouble", | ||
"crafting", | ||
"dispenser", | ||
"dropper", | ||
"enderchest", | ||
"furnace", | ||
"grindstone_title", | ||
"hopper", | ||
"inventory", | ||
"loom", | ||
"repair", | ||
"shulkerBox", | ||
"smoker", | ||
"stonecutter", | ||
"merchant.trades", | ||
).map { "container.$it" } | ||
|
||
fun forEachLanguage(action: (String) -> Unit) { | ||
languages.forEach(action) | ||
} | ||
|
||
fun forEachContainer(action: (String) -> Unit) { | ||
containers.forEach(action) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/net/mcbrawls/packin/resource/provider/DirectResourceProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.mcbrawls.packin.resource.provider | ||
|
||
import net.mcbrawls.packin.listener.PackinResourceLoader | ||
import net.mcbrawls.packin.resource.pack.PackinResourcePack | ||
import net.mcbrawls.packin.resource.pack.ResourceCollector | ||
import net.minecraft.util.Identifier | ||
|
||
/** | ||
* Provides the contents of a Packin pack. | ||
*/ | ||
class DirectResourceProvider( | ||
/** | ||
* A predicate to test the path of a resource to include. | ||
*/ | ||
val pathPredicate: (Identifier) -> Boolean = { true }, | ||
|
||
/** | ||
* The id of the Packin pack to provide. | ||
*/ | ||
val packId: String? = null, | ||
) : ResourceProvider { | ||
constructor(paths: Set<Identifier>, packId: String? = null) : this(paths::contains, packId) | ||
constructor(vararg paths: Identifier, packId: String? = null) : this(paths.toSet(), packId) | ||
|
||
/** | ||
* Creates a provider which checks within a namespace against a collection of starting path predicates. | ||
*/ | ||
constructor(namespace: String, vararg startingPaths: String, packId: String? = null) : this( | ||
createStartingPathPredicate(namespace, *startingPaths), packId | ||
) | ||
|
||
override fun collectResources(pack: PackinResourcePack, collector: ResourceCollector) { | ||
val allResources = PackinResourceLoader.getAll(packId, pathPredicate) | ||
allResources.forEach(collector::collect) | ||
} | ||
|
||
companion object { | ||
fun createStartingPathPredicate(namespace: String, vararg startingPaths: String): (Identifier) -> Boolean { | ||
return { id -> id.namespace == namespace && startingPaths.any(id.path::startsWith) } | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/net/mcbrawls/packin/resource/provider/LanguageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.mcbrawls.packin.resource.provider | ||
|
||
import com.google.gson.JsonObject | ||
import net.mcbrawls.packin.resource.pack.PackinResourcePack | ||
import net.mcbrawls.packin.resource.pack.ResourceCollector | ||
import net.minecraft.util.Identifier | ||
|
||
/** | ||
* A resource provider to add a language override. | ||
*/ | ||
class LanguageProvider( | ||
/** | ||
* The language code of this provider. | ||
* For example, "minecraft:en_us". | ||
*/ | ||
val language: Identifier, | ||
|
||
/** | ||
* All translations for this provider. | ||
*/ | ||
val translations: Map<String, String>, | ||
) : ResourceProvider { | ||
constructor(language: Identifier, builder: MutableMap<String, String>.() -> Unit) : this(language, mutableMapOf<String, String>().apply(builder)) | ||
|
||
override fun collectResources(pack: PackinResourcePack, collector: ResourceCollector) { | ||
val languageJson = JsonObject().apply { | ||
translations.forEach(::addProperty) | ||
} | ||
|
||
val languagePath = language.withPath { "lang/$it.json" } | ||
collector.collect(languagePath, languageJson.toString().encodeToByteArray()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters