Skip to content

Commit

Permalink
Merge pull request louis-e#138 from louis-e/sign-implementation
Browse files Browse the repository at this point in the history
Added set_sign in world_editor.rs
  • Loading branch information
louis-e authored Jan 3, 2025
2 parents 2afd508 + 11c1c18 commit b37aa81
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/block_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Block {
110 => "bedrock",
111 => "snow_block",
112 => "snow",
113 => "oak_sign",
_ => panic!("Invalid id"),
}
}
Expand Down Expand Up @@ -176,6 +177,16 @@ impl Block {
map
})),

113 => Some(Value::Compound({
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("rotation".to_string(), Value::String("6".to_string()));
map.insert(
"waterlogged".to_string(),
Value::String("false".to_string()),
);
map
})),

_ => None,
}
}
Expand Down Expand Up @@ -289,6 +300,7 @@ pub const OXIDIZED_COPPER: Block = Block::new(103);
pub const YELLOW_TERRACOTTA: Block = Block::new(104);
pub const SNOW_BLOCK: Block = Block::new(111);
pub const SNOW_LAYER: Block = Block::new(112);
pub const SIGN: Block = Block::new(113);

pub const CARROTS: Block = Block::new(105);
pub const DARK_OAK_DOOR_LOWER: Block = Block::new(106);
Expand Down
11 changes: 11 additions & 0 deletions src/data_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ pub fn generate_world(
let mut editor: WorldEditor =
WorldEditor::new(&region_dir, scale_factor_x, scale_factor_z, args);

editor.set_sign(
"↑".to_string(),
"Generated World".to_string(),
"This direction".to_string(),
"".to_string(),
9,
-61,
9,
6,
);

// Process data
let elements_count: usize = elements.len();
let process_pb: ProgressBar = ProgressBar::new(elements_count as u64);
Expand Down
62 changes: 62 additions & 0 deletions src/world_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use fastnbt::{LongArray, Value};
use fnv::FnvHashMap;
use indicatif::{ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;

Expand Down Expand Up @@ -137,6 +138,7 @@ impl Default for SectionToModify {
#[derive(Default)]
struct ChunkToModify {
sections: FnvHashMap<i8, SectionToModify>,
other: FnvHashMap<String, Value>,
}

impl ChunkToModify {
Expand Down Expand Up @@ -274,6 +276,65 @@ impl<'a> WorldEditor<'a> {
self.world.get_block(x, y, z).is_some()
}*/

#[allow(clippy::too_many_arguments)]
pub fn set_sign(
&mut self,
line1: String,
line2: String,
line3: String,
line4: String,
x: i32,
y: i32,
z: i32,
_rotation: i8,
) {
let chunk_x = x >> 4;
let chunk_z = z >> 4;
let region_x = chunk_x >> 5;
let region_z = chunk_z >> 5;

let mut block_entities = HashMap::new();

let messages = vec![
Value::String(format!("\"{}\"", line1)),
Value::String(format!("\"{}\"", line2)),
Value::String(format!("\"{}\"", line3)),
Value::String(format!("\"{}\"", line4)),
];

let mut text_data = HashMap::new();
text_data.insert("messages".to_string(), Value::List(messages));
text_data.insert("color".to_string(), Value::String("black".to_string()));
text_data.insert("has_glowing_text".to_string(), Value::Byte(0));

block_entities.insert("front_text".to_string(), Value::Compound(text_data));
block_entities.insert(
"id".to_string(),
Value::String("minecraft:sign".to_string()),
);
block_entities.insert("is_waxed".to_string(), Value::Byte(0));
block_entities.insert("keepPacked".to_string(), Value::Byte(0));
block_entities.insert("x".to_string(), Value::Int(x));
block_entities.insert("y".to_string(), Value::Int(y));
block_entities.insert("z".to_string(), Value::Int(z));

let region: &mut RegionToModify = self.world.get_or_create_region(region_x, region_z);
let chunk: &mut ChunkToModify = region.get_or_create_chunk(chunk_x & 31, chunk_z & 31);

if let Some(chunk_data) = chunk.other.get_mut("block_entities") {
if let Value::List(entities) = chunk_data {
entities.push(Value::Compound(block_entities));
}
} else {
chunk.other.insert(
"block_entities".to_string(),
Value::List(vec![Value::Compound(block_entities)]),
);
}

self.set_block(SIGN, x, y, z, None, None);
}

/// Sets a block of the specified type at the given coordinates.
pub fn set_block(
&mut self,
Expand Down Expand Up @@ -408,6 +469,7 @@ impl<'a> WorldEditor<'a> {

if let Some(chunk_to_modify) = region_to_modify.get_chunk(chunk_x, chunk_z) {
chunk.sections = chunk_to_modify.sections().collect();
chunk.other.extend(chunk_to_modify.other.clone());
}

chunk.x_pos = chunk_x + region_x * 32;
Expand Down

0 comments on commit b37aa81

Please sign in to comment.