1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 | use serde::{Serialize, Deserialize, de::Error};
use std::fs::{File, create_dir_all, copy};
use std::path::PathBuf;
struct Triple<T>(T, T, T);
impl<T: Serialize + Copy> Serialize for Triple<T> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
vec![self.0, self.1, self.2].serialize(s)
}
}
impl<'de, T: Deserialize<'de> + Copy> Deserialize<'de> for Triple<T> {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let v = Vec::deserialize(d)?;
match &v[..] {
&[x, y, z] => Ok(Self(x, y, z)),
_ => Err(D::Error::custom("list had wrong number of elements")),
}
}
}
#[derive(Serialize)]
struct Block {
#[serde(rename = "Name")]
name: &'static str,
}
#[derive(Serialize)]
struct BlockRef {
state: i32,
pos: Triple<i32>,
}
#[derive(Serialize)]
struct EntityData {
id: &'static str,
#[serde(rename = "Tags")]
tags: &'static [&'static str],
}
#[derive(Serialize)]
struct Entity {
pos: Triple<f64>,
#[serde(rename = "blockPos")]
block_pos: Triple<i32>,
nbt: EntityData,
}
#[derive(Serialize)]
struct Structure {
#[serde(rename = "DataVersion")]
data_version: i32,
size: Triple<i32>,
palette: &'static [Block],
entities: Vec<Entity>,
blocks: Vec<BlockRef>,
}
// This is rather stupid.
#[derive(Deserialize)]
struct Storage {
data: StorageData,
}
#[derive(Deserialize)]
struct StorageData {
contents: StorageContents,
}
#[derive(Deserialize)]
struct StorageContents {
out: StorageResult,
}
#[derive(Deserialize)]
struct StorageResult {
#[serde(rename = "Data")]
data: Vec<Triple<f64>>,
}
const PATHS: [(&'static str, &[&'static str]); 4] = [
("world/generated/minecraft/structures/", &[]),
("world/datapacks/mc21/", &["pack.mcmeta"]),
("world/datapacks/mc21/data/mc21/functions/", &["load.mcfunction", "tick.mcfunction", "end.mcfunction", "do_density.mcfunction"]),
("world/datapacks/mc21/data/minecraft/tags/functions/", &["tick.json", "load.json"]),
];
pub fn entry(mut grid: Vec<bool>, width: usize, height: usize, x: usize, y: usize) -> Vec<bool> {
let mut blocks = Vec::new();
for y in 0..height {
for x in 0..width {
let state = grid[y*width+x] as i32;
blocks.push(BlockRef { state, pos: Triple(x as i32, 0, y as i32)});
blocks.push(BlockRef { state, pos: Triple(x as i32, 1, y as i32) });
}
}
let entity = Entity {
pos: Triple(x as f64, 0.0, y as f64),
block_pos: Triple(x as i32, 0, y as i32),
nbt: EntityData { id: "minecraft:marker", tags: &["origin"] },
};
let structure = Structure {
data_version: 3117,
size: Triple(width as i32, 2, height as i32),
palette: &[
Block { name: "air" },
Block { name: "stone" },
],
entities: vec![entity],
blocks,
};
for (dir, files) in PATHS {
create_dir_all(dir).unwrap();
for file in files {
let path: PathBuf = [dir, file].iter().collect();
copy(file, path).unwrap();
}
}
nbt::to_gzip_writer(&mut File::create("world/generated/minecraft/structures/start.nbt").unwrap(), &structure, None).unwrap();
std::process::Command::new("java")
.args(["-jar", "server.jar", "nogui"])
.spawn()
.unwrap();
loop {
std::thread::sleep(std::time::Duration::from_secs(20));
if let Ok(file) = File::open("world/data/command_storage_mc21.dat") {
let storage: Storage = nbt::from_gzip_reader(file).unwrap();
for Triple(x, _, y) in storage.data.contents.out.data {
grid[y as usize*width+x as usize] = true;
}
return grid;
}
}
}
|
post a comment