Merge pull request #531 from ludovic111/feat/mod-content-game
Content mods in the game: loot, recipes, models, names and icons from mod namespaces
This commit is contained in:
@@ -14,7 +14,7 @@ use serde_json::Value;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs,
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@@ -1182,18 +1182,33 @@ impl Recipes {
|
||||
let mut crafting = Vec::new();
|
||||
let mut cooking = Vec::new();
|
||||
let mut meta = HashMap::new();
|
||||
let mut files: Vec<_> = fs::read_dir(root.join("data/minecraft/recipe"))?
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.extension().is_some_and(|e| e == "json"))
|
||||
let json_files = |dir: &Path| -> Result<Vec<PathBuf>> {
|
||||
let mut files: Vec<_> = fs::read_dir(dir)?
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.extension().is_some_and(|e| e == "json"))
|
||||
.collect();
|
||||
files.sort();
|
||||
Ok(files)
|
||||
};
|
||||
// Vanilla's recipes by bare name, then content mods' as `<ns>:<name>`.
|
||||
let mut files: Vec<(String, PathBuf)> = json_files(&root.join("data/minecraft/recipe"))?
|
||||
.into_iter()
|
||||
.map(|p| (String::new(), p))
|
||||
.collect();
|
||||
files.sort();
|
||||
for path in files {
|
||||
let name = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("")
|
||||
.to_owned();
|
||||
for namespace in crate::mod_content::namespaces(root) {
|
||||
let dir = root.join(format!("data/{namespace}/recipe"));
|
||||
if dir.is_dir() {
|
||||
files.extend(
|
||||
json_files(&dir)?
|
||||
.into_iter()
|
||||
.map(|p| (format!("{namespace}:"), p)),
|
||||
);
|
||||
}
|
||||
}
|
||||
for (prefix, path) in files {
|
||||
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
|
||||
let name = format!("{prefix}{stem}");
|
||||
let v: Value = serde_json::from_slice(&fs::read(&path)?)?;
|
||||
let text = |k: &str| v[k].as_str().unwrap_or("").to_owned();
|
||||
meta.insert(name.clone(), (text("category"), text("group")));
|
||||
|
||||
@@ -49,17 +49,24 @@ pub struct Names {
|
||||
}
|
||||
impl Names {
|
||||
pub fn load(root: &Path) -> Self {
|
||||
let map = fs::read(root.join("assets/minecraft/lang/en_us.json"))
|
||||
.ok()
|
||||
.and_then(|b| serde_json::from_slice::<HashMap<String, String>>(&b).ok())
|
||||
.unwrap_or_default();
|
||||
let read = |namespace: &str| {
|
||||
fs::read(root.join(format!("assets/{namespace}/lang/en_us.json")))
|
||||
.ok()
|
||||
.and_then(|b| serde_json::from_slice::<HashMap<String, String>>(&b).ok())
|
||||
.unwrap_or_default()
|
||||
};
|
||||
let mut map = read("minecraft");
|
||||
// Content mods' names (docs/modpacks.md); none in a vanilla root.
|
||||
for namespace in crate::mod_content::namespaces(root) {
|
||||
map.extend(read(&namespace));
|
||||
}
|
||||
Self { map }
|
||||
}
|
||||
pub fn item(&self, id: &str) -> String {
|
||||
let path = id.trim_start_matches("minecraft:");
|
||||
let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id));
|
||||
self.map
|
||||
.get(&format!("item.minecraft.{path}"))
|
||||
.or_else(|| self.map.get(&format!("block.minecraft.{path}")))
|
||||
.get(&format!("item.{namespace}.{path}"))
|
||||
.or_else(|| self.map.get(&format!("block.{namespace}.{path}")))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| path.replace('_', " "))
|
||||
}
|
||||
|
||||
@@ -9748,6 +9748,11 @@ pub fn run() -> Result<()> {
|
||||
let vanilla_root = options.resources.clone();
|
||||
let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt"));
|
||||
options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs);
|
||||
options.resources = mods::content_root(
|
||||
&options.resources,
|
||||
&early_settings.mods.enabled,
|
||||
&options.dev_mods,
|
||||
);
|
||||
let assets = match Assets::load(&options.resources) {
|
||||
Ok(assets) => assets,
|
||||
// Minecraft.rollbackResourcePacks: the packs break loading, start
|
||||
|
||||
@@ -47,6 +47,60 @@ fn vanilla_command(name: &str) -> bool {
|
||||
.any(|&c| d.nodes[c].name() == name)
|
||||
}
|
||||
|
||||
/// `root` with the content mods that are on layered over it
|
||||
/// (docs/modpacks.md): their blocks, items, data and assets. Read once at
|
||||
/// start, before the resources load; a mod whose content is refused is left
|
||||
/// out here and its row on the Mods page says why.
|
||||
pub(super) fn content_root(
|
||||
root: &std::path::Path,
|
||||
enabled: &std::collections::BTreeSet<String>,
|
||||
dev: &[std::path::PathBuf],
|
||||
) -> std::path::PathBuf {
|
||||
use horizon_client::mod_content::{self, ContentMod};
|
||||
use horizon_mods::{Capability, Content, Package};
|
||||
let here = std::env::current_dir().unwrap_or_default();
|
||||
let mut paths = horizon_mods::find(&here.join(mod_list::DIR));
|
||||
paths.retain(|p| !dev.contains(p));
|
||||
paths.extend(dev.iter().cloned());
|
||||
let mut mods: Vec<ContentMod> = Vec::new();
|
||||
for path in &paths {
|
||||
let Ok(package) = Package::read(path) else {
|
||||
continue;
|
||||
};
|
||||
let m = &package.manifest;
|
||||
let on = enabled.contains(&m.id) || dev.contains(path);
|
||||
if !on
|
||||
|| !m.capabilities.contains(&Capability::Registries)
|
||||
|| mods.iter().any(|c| c.id == m.id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
match Content::read(path, m) {
|
||||
Ok(content) => mods.push(ContentMod {
|
||||
id: m.id.clone(),
|
||||
content,
|
||||
}),
|
||||
Err(e) => log::warn!("Horizon: mod {} content not loaded: {e}", m.id),
|
||||
}
|
||||
}
|
||||
match mod_content::resource_root(root, &mods, &here.join(".horizon-cache")) {
|
||||
Ok(out) => {
|
||||
if out != root {
|
||||
log::info!(
|
||||
"Horizon: content from {} mod(s) in {}",
|
||||
mods.len(),
|
||||
out.display()
|
||||
);
|
||||
}
|
||||
out
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Horizon: content mods not loaded: {e}");
|
||||
root.to_owned()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
/// Reads `horizon-mods/` and the dev mods, and starts the ones turned on.
|
||||
pub(super) fn start_mods(&mut self) {
|
||||
|
||||
@@ -102,8 +102,10 @@ fn atlas_sprites(root: &Path, packed: &[PackedImage]) -> Vec<PackedImage> {
|
||||
let image = match &origin {
|
||||
SpriteOrigin::File(texture) => {
|
||||
let path = texture.strip_prefix("minecraft:").unwrap_or(texture);
|
||||
// Packed under its own name already: only a renamed file is new.
|
||||
if path == name {
|
||||
// A vanilla file is packed under its own name already:
|
||||
// only a renamed one is new. Other namespaces' files
|
||||
// (content mods) are never in the walked directories.
|
||||
if texture.starts_with("minecraft:") && path == name {
|
||||
continue;
|
||||
}
|
||||
load(texture, "textures").map(|(w, h, p)| {
|
||||
|
||||
@@ -512,11 +512,16 @@ impl Items {
|
||||
let mut loot = HashMap::new();
|
||||
// Every loot table, keyed by its id (blocks/stone, entities/sheep/white,
|
||||
// gameplay/chicken_lay, shearing/sheep/red...).
|
||||
fn walk_loot(dir: &Path, base: &Path, loot: &mut HashMap<String, Value>) -> Result<()> {
|
||||
fn walk_loot(
|
||||
dir: &Path,
|
||||
base: &Path,
|
||||
namespace: &str,
|
||||
loot: &mut HashMap<String, Value>,
|
||||
) -> Result<()> {
|
||||
for entry in fs::read_dir(dir)?.flatten() {
|
||||
let p = entry.path();
|
||||
if p.is_dir() {
|
||||
walk_loot(&p, base, loot)?;
|
||||
walk_loot(&p, base, namespace, loot)?;
|
||||
} else if p.extension().is_some_and(|e| e == "json") {
|
||||
let id = p
|
||||
.strip_prefix(base)?
|
||||
@@ -524,7 +529,7 @@ impl Items {
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
loot.insert(
|
||||
format!("minecraft:{id}"),
|
||||
format!("{namespace}:{id}"),
|
||||
serde_json::from_slice(&fs::read(&p)?)?,
|
||||
);
|
||||
}
|
||||
@@ -532,7 +537,14 @@ impl Items {
|
||||
Ok(())
|
||||
}
|
||||
let loot_root = root.join("data/minecraft/loot_table");
|
||||
walk_loot(&loot_root, &loot_root, &mut loot)?;
|
||||
walk_loot(&loot_root, &loot_root, "minecraft", &mut loot)?;
|
||||
// Content mods' tables (docs/modpacks.md): `<ns>:blocks/...`.
|
||||
for namespace in crate::mod_content::namespaces(root) {
|
||||
let dir = root.join(format!("data/{namespace}/loot_table"));
|
||||
if dir.is_dir() {
|
||||
walk_loot(&dir, &dir, &namespace, &mut loot)?;
|
||||
}
|
||||
}
|
||||
let registry_order = registry_order(root, &order);
|
||||
Ok(Self {
|
||||
items,
|
||||
@@ -644,12 +656,15 @@ impl Items {
|
||||
state: BlockStateId,
|
||||
) -> Option<String> {
|
||||
let block = registry.state(state)?;
|
||||
let name = block.name.trim_start_matches("minecraft:");
|
||||
let (namespace, name) = block
|
||||
.name
|
||||
.split_once(':')
|
||||
.unwrap_or(("minecraft", &block.name));
|
||||
let standing = name.replacen("_wall_", "_", 1);
|
||||
let standing = standing.strip_prefix("wall_").unwrap_or(&standing);
|
||||
let standing = standing.replacen("wall_", "", 1);
|
||||
self.loot_sequence(&format!("minecraft:blocks/{name}"))
|
||||
.or_else(|| self.loot_sequence(&format!("minecraft:blocks/{standing}")))
|
||||
self.loot_sequence(&format!("{namespace}:blocks/{name}"))
|
||||
.or_else(|| self.loot_sequence(&format!("{namespace}:blocks/{standing}")))
|
||||
}
|
||||
/// Evaluate the block's loot table for a player break without explosions.
|
||||
pub fn block_drops<R: RandomSource>(
|
||||
@@ -737,7 +752,10 @@ impl Items {
|
||||
let Some(block) = registry.state(state) else {
|
||||
return vec![];
|
||||
};
|
||||
let name = block.name.trim_start_matches("minecraft:");
|
||||
let (namespace, name) = block
|
||||
.name
|
||||
.split_once(':')
|
||||
.unwrap_or(("minecraft", &block.name));
|
||||
// `Blocks.wallVariant`: wall signs, banners, heads, fans and torches
|
||||
// reuse the standing block's loot table.
|
||||
let standing = name.replacen("_wall_", "_", 1);
|
||||
@@ -745,8 +763,8 @@ impl Items {
|
||||
let standing = standing.replacen("wall_", "", 1);
|
||||
let table = self
|
||||
.loot
|
||||
.get(&format!("minecraft:blocks/{name}"))
|
||||
.or_else(|| self.loot.get(&format!("minecraft:blocks/{standing}")));
|
||||
.get(&format!("{namespace}:blocks/{name}"))
|
||||
.or_else(|| self.loot.get(&format!("{namespace}:blocks/{standing}")));
|
||||
let Some(table) = table else {
|
||||
return vec![];
|
||||
};
|
||||
|
||||
@@ -524,10 +524,19 @@ impl<'a> Plan<'a> {
|
||||
"items.json" => self.items(doc, true),
|
||||
"command-registries.json" => {
|
||||
self.names(doc.at("blocks").ok_or("no blocks")?, false);
|
||||
let new_items = || self.items.iter().map(|i| J::Str(i.rename.to.clone()));
|
||||
let new_blocks = || self.blocks.iter().map(|b| J::Str(b.decl.id.clone()));
|
||||
let Some(J::Arr(items)) = doc.at("items") else {
|
||||
return Err("no items".into());
|
||||
};
|
||||
items.extend(self.items.iter().map(|i| J::Str(i.rename.to.clone())));
|
||||
items.extend(new_items());
|
||||
// The registries commands check ids against.
|
||||
if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:item/ids") {
|
||||
ids.extend(new_items());
|
||||
}
|
||||
if let Some(J::Arr(ids)) = doc.at("by_registry/minecraft:block/ids") {
|
||||
ids.extend(new_blocks());
|
||||
}
|
||||
}
|
||||
"mob-data.json" => states(doc, "states", false, false)?,
|
||||
"worldgen/block_flags.json" => states(doc, "", false, false)?,
|
||||
@@ -616,6 +625,19 @@ const NBT_TABLES: [&str; 6] = [
|
||||
"worldgen/blocks/cached-support.nbt",
|
||||
"worldgen/blocks/cached-collision.nbt",
|
||||
];
|
||||
/// In a root built here: the content mods' ids, the namespaces loaders
|
||||
/// read besides `minecraft`.
|
||||
const MARKER: &str = "horizon-mod-content.json";
|
||||
|
||||
/// The content mods' namespaces in a root built by [`build`]; none in any
|
||||
/// other root, so vanilla's loaders read exactly what they did.
|
||||
pub fn namespaces(root: &Path) -> Vec<String> {
|
||||
fs::read(root.join(MARKER))
|
||||
.ok()
|
||||
.and_then(|b| serde_json::from_slice(&b).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Bundles of block tag files (tag id → the tag file's text).
|
||||
const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"];
|
||||
|
||||
@@ -799,6 +821,23 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
for (name, doc) in &bundles {
|
||||
overlay.write(name, &serde_json::to_vec(doc)?)?;
|
||||
}
|
||||
// A block without its own blockstate looks like its template, so every
|
||||
// state still has a model.
|
||||
for m in mods {
|
||||
for block in &m.content.blocks {
|
||||
let (ns, path) = block.id.split_once(':').unwrap_or((&m.id, &block.id));
|
||||
let own = format!("assets/{ns}/blockstates/{path}.json");
|
||||
if m.content.files.contains(&own) {
|
||||
continue;
|
||||
}
|
||||
let template = block.template.trim_start_matches("minecraft:");
|
||||
let vanilla = base.join(format!("assets/minecraft/blockstates/{template}.json"));
|
||||
overlay.write(&own, &fs::read(&vanilla)?)?;
|
||||
}
|
||||
}
|
||||
let mut ids: Vec<&str> = mods.iter().map(|m| m.id.as_str()).collect();
|
||||
ids.sort();
|
||||
overlay.write(MARKER, &serde_json::to_vec(&ids)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -177,6 +177,11 @@ fn content_root_loads_with_every_table() {
|
||||
crate::motion::MotionResources::load(&root, &assets).unwrap();
|
||||
horizon_world::worldgen::generator::GeneratorData::load(&root.join("worldgen"), new).unwrap();
|
||||
assert!(root.join("assets/ruby/lang/en_us.json").is_file());
|
||||
// Blocks without a blockstate of their own take their template's.
|
||||
assert_eq!(
|
||||
fs::read(root.join("assets/ruby/blockstates/ruby_door.json")).unwrap(),
|
||||
fs::read(base.join("assets/minecraft/blockstates/oak_door.json")).unwrap()
|
||||
);
|
||||
|
||||
// The base root was only read.
|
||||
assert!(
|
||||
|
||||
@@ -73,7 +73,7 @@ pub(crate) fn prepare_with_models(
|
||||
let blocks: Vec<&str> = facts
|
||||
.keys()
|
||||
.filter(|name| registry.default_state(name).is_some())
|
||||
.filter_map(|name| name.strip_prefix("minecraft:"))
|
||||
.map(|name| name.strip_prefix("minecraft:").unwrap_or(name))
|
||||
.collect();
|
||||
let catalog =
|
||||
StateModels::from_facts_with_models(root, registry, &blocks, sprites, &facts, models)?;
|
||||
|
||||
@@ -94,7 +94,9 @@ impl StateModels {
|
||||
}
|
||||
}
|
||||
for block in blocks.iter().copied().collect::<BTreeSet<_>>() {
|
||||
let name = format!("minecraft:{block}");
|
||||
// Vanilla blocks come bare; content mods' blocks with their namespace.
|
||||
let (namespace, path) = block.split_once(':').unwrap_or(("minecraft", block));
|
||||
let name = format!("{namespace}:{path}");
|
||||
let facts = facts.get(&name).ok_or("missing block model state schema")?;
|
||||
let schema: PropertySchema = facts["schema"]
|
||||
.as_object()
|
||||
@@ -133,7 +135,7 @@ impl StateModels {
|
||||
.collect::<Result<_>>()?;
|
||||
let possible: Vec<Properties> = serde_json::from_value(facts["states"].clone())?;
|
||||
let resource = fs::read_to_string(
|
||||
root.join(format!("assets/minecraft/blockstates/{block}.json")),
|
||||
root.join(format!("assets/{namespace}/blockstates/{path}.json")),
|
||||
)?;
|
||||
let definition = BlockStateDefinition::from_json(&resource)?;
|
||||
let bound = definition.bind(&schema, &possible)?;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"variants": {
|
||||
"": {
|
||||
"model": "ruby:block/ruby_block"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"model": {
|
||||
"type": "minecraft:model",
|
||||
"model": "ruby:item/ruby"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"model": {
|
||||
"type": "minecraft:model",
|
||||
"model": "ruby:block/ruby_block"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"block.ruby.ruby_block": "Block of Ruby",
|
||||
"item.ruby.ruby": "Ruby"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"parent": "minecraft:block/cube_all",
|
||||
"textures": {
|
||||
"all": "ruby:block/ruby_block"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"parent": "minecraft:item/generated",
|
||||
"textures": {
|
||||
"layer0": "ruby:item/ruby"
|
||||
}
|
||||
}
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 149 B |
Binary file not shown.
|
After Width: | Height: | Size: 149 B |
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"values": [
|
||||
"ruby:ruby_block"
|
||||
]
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"values": [
|
||||
"ruby:ruby_block"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"template": "minecraft:iron_block",
|
||||
"properties": {
|
||||
"hardness": 4.0,
|
||||
"light": 3,
|
||||
"sounds": "minecraft:amethyst_block",
|
||||
"map_color": 28
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"template": "minecraft:diamond",
|
||||
"rarity": "uncommon"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"condition": {
|
||||
"type": "minecraft:survives_explosion"
|
||||
},
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "ruby:ruby_block"
|
||||
}
|
||||
],
|
||||
"rolls": 1
|
||||
}
|
||||
],
|
||||
"random_sequence": "ruby:blocks/ruby_block"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"type": "minecraft:crafting_shaped",
|
||||
"category": "building",
|
||||
"key": {
|
||||
"#": "ruby:ruby"
|
||||
},
|
||||
"pattern": [
|
||||
"###",
|
||||
"###",
|
||||
"###"
|
||||
],
|
||||
"result": {
|
||||
"id": "ruby:ruby_block"
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"type": "minecraft:crafting_shapeless",
|
||||
"ingredients": [
|
||||
"ruby:ruby_block"
|
||||
],
|
||||
"result": {
|
||||
"count": 9,
|
||||
"id": "ruby:ruby"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# A content-only Horizon mod (API 0.2, docs/modpacks.md): no code, a block
|
||||
# and an item declared from vanilla templates, with their recipes, loot,
|
||||
# tags, models, textures and names. Try it: horizon-client --dev-mod <this folder>
|
||||
id = "ruby"
|
||||
name = "Ruby"
|
||||
version = "0.1.0"
|
||||
description = "A ruby gem and a block of ruby"
|
||||
authors = ["Horizon"]
|
||||
api = "0.2"
|
||||
side = "both"
|
||||
capabilities = ["registries"]
|
||||
Reference in New Issue
Block a user