Skip to content

Commit

Permalink
✨ World templates include rp and bp (#10), add appropiate config, fix…
Browse files Browse the repository at this point in the history
… uuid-fix bug
  • Loading branch information
phoenixr-codes committed Apr 12, 2024
1 parent b8a2a6b commit c5d1669
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,16 @@ pub struct WT {
#[serde(default)]
pub custom_manifest: bool,

/// Whether to use the `pack_icon.png` file in the `WT` directory instead of generating one.
/// Whether to exclude the resource pack from the world template.
#[serde(default)]
pub custom_pack_icon: bool,
pub exclude_rp: bool,

/// Whether to exclude the behavior pack from the world template.
#[serde(default)]
pub exclude_bp: bool,

#[serde(default)]
pub allow_random_seed: bool,

/// Override name for behavior pack.
#[serde(default)]
Expand Down
6 changes: 3 additions & 3 deletions src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Health {
if data.wt.module.is_none() {
if self.fix {
log::info!("Add missing WT module UUID");
data.bp.update_module(None);
data.wt.update_module(None);
modified = true;
} else {
log::error!("WT module UUID is missing; try `allay health --fix`");
Expand All @@ -175,8 +175,8 @@ impl Health {
}

/// Returns `true` when `dir` contains files or directories.
fn has_content(dir: &PathBuf) -> bool {
pub fn has_content(dir: &PathBuf) -> bool {
dir.read_dir()
.map(|entries| entries.count().eq(&0))
.map(|entries| entries.count() > 0)
.unwrap_or_default()
}
9 changes: 7 additions & 2 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ impl Manifest {
Ok(Manifest {
format_version: 2,
header: Header {
allow_random_seed: None,
base_game_version: None,
allow_random_seed: match pack {
Pack::WorldTemplate => Some(project.config.wt.allow_random_seed),
_ => None,
},
base_game_version: None, // FIXME: this seems to be required for world templates
description: String::from("pack.description"),
lock_template_options: None,
min_engine_version: Some(version_from_string(
Expand Down Expand Up @@ -348,6 +351,8 @@ impl From<BehaviorPackType> for ModuleType {
}
}

/// In the header of your world template's manifest, you will need to specify the Minecraft version your
/// world template was created for using the `base_game_version` field.
#[derive(Debug, Serialize)]
pub enum BaseGameVersion {
/// If your content is version agnostic (such as a simple survival spawn which
Expand Down
62 changes: 61 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::health::has_content;
use crate::localization::{
generate_language_json, update_language_files, Localized, OptionallyLocalized,
};
Expand Down Expand Up @@ -156,7 +157,7 @@ impl Project {
Pack::Behavior => self.config.bp.custom_pack_icon,
Pack::Resource => self.config.rp.custom_pack_icon,
Pack::Skin => self.config.sp.custom_pack_icon,
Pack::WorldTemplate => self.config.wt.custom_pack_icon,
Pack::WorldTemplate => false,
};
if copy_pack_icon {
log::debug!("Copying pack icon");
Expand Down Expand Up @@ -292,6 +293,65 @@ impl Project {
}
}

{
let rp = prebuild.join("RP");
let bp = prebuild.join("BP");
let wt = prebuild.join("WT");

let has_rp = has_content(&Pack::Resource.path_src().unwrap());
let has_bp = has_content(&Pack::Behavior.path_src().unwrap());
let has_wt = has_content(&Pack::WorldTemplate.path_src().unwrap());

if has_wt && !self.config.wt.exclude_bp && has_bp {
log::debug!("Copying behavior pack to world template");

let dest = wt.join("behavior_packs");
// TODO: rename `RP`/`BP` to a less generic name (?)
let copy = || {
if let Err(e) = fs_extra::dir::copy(bp, &dest, &copy_options) {
log::error!("Failed to copy behavior pack to world template: {}", e);
};
};
if dest.exists() {
copy()
} else {
match fs::create_dir(&dest) {
Ok(_) => copy(),
Err(e) => {
log::error!(
"Failed to create behavior pack directory in world template: {}",
e
);
}
};
}
}

if has_wt && !self.config.wt.exclude_rp && has_rp {
log::debug!("Copying resource pack to world template");

let dest = wt.join("resource_packs");
let copy = || {
if let Err(e) = fs_extra::dir::copy(rp, &dest, &copy_options) {
log::error!("Failed to copy resource pack to world template: {}", e);
};
};
if dest.exists() {
copy();
} else {
match fs::create_dir(&dest) {
Ok(_) => copy(),
Err(e) => {
log::error!(
"Failed to create resource pack directory in world template: {}",
e
);
}
};
}
}
}

log::debug!("Zipping add-ons");
{
let mut bundles: Vec<PathBuf> = Vec::new();
Expand Down

0 comments on commit c5d1669

Please sign in to comment.