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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::parser::{command::resource_location::ResourceLocationRef, Line, ScheduleOperation};
use minect::command::named_logged_command;
use std::collections::{BTreeMap, BTreeSet};
pub struct TemplateEngine<'l> {
replacements: BTreeMap<&'l str, &'l str>,
replacements_owned: BTreeMap<&'l str, String>,
adapter_listener_name: Option<&'l str>,
}
impl<'l> TemplateEngine<'l> {
pub fn new(
replacements: BTreeMap<&'l str, &'l str>,
adapter_listener_name: Option<&'l str>,
) -> TemplateEngine<'l> {
TemplateEngine {
replacements,
replacements_owned: BTreeMap::new(),
adapter_listener_name,
}
}
pub fn extend<T: IntoIterator<Item = (&'l str, &'l str)>>(
&self,
iter: T,
) -> TemplateEngine<'l> {
let mut replacements = self.replacements.clone();
replacements.extend(iter);
TemplateEngine {
replacements,
replacements_owned: self.replacements_owned.clone(),
adapter_listener_name: self.adapter_listener_name,
}
}
pub fn extend_orig_name<N: AsRef<str>>(
&'l self,
orig_name: &'l ResourceLocationRef<N>,
) -> TemplateEngine<'l> {
let mut engine = self.extend([
("-orig_ns-", orig_name.namespace()),
("-orig/fn-", orig_name.path()),
]);
let orig_fn_tag = orig_name.path().replace('/', "+");
engine.replacements_owned.insert("-orig+fn-", orig_fn_tag);
engine
}
pub fn expand(&self, string: &str) -> String {
let mut with_replacements_applied = string.to_owned();
for (from, to) in &self.replacements {
with_replacements_applied = with_replacements_applied.replace(from, to);
}
for (from, to) in &self.replacements_owned {
with_replacements_applied = with_replacements_applied.replace(from, to);
}
let mut result = String::new();
let mut lines = with_replacements_applied.split_inclusive('\n');
while let Some(line) = lines.next() {
match line.trim() {
"# -minect_log-" => {
if let Some(command) = lines.next() {
if let Some(adapter_listener_name) = self.adapter_listener_name {
result.push_str(&named_logged_command(
adapter_listener_name,
command.trim(),
));
if command.ends_with('\n') {
result.push('\n');
}
}
}
}
"# -if_not_adapter-" => {
if let Some(command) = lines.next() {
if self.adapter_listener_name.is_none() {
result.push_str(command);
}
}
}
_ => {
result.push_str(line);
}
}
}
result
}
pub fn expand_line(&self, (_line_number, line, command): &(usize, String, Line)) -> String {
match command {
Line::Breakpoint => {
unreachable!()
}
Line::FunctionCall { .. } => {
unreachable!()
}
Line::OptionalSelectorCommand {
missing_selector,
selectors,
..
} => {
const SELF_SELECTOR: &str = " @s";
let mut line = line.to_string();
line.insert_str(*missing_selector, SELF_SELECTOR);
let mut selectors = selectors
.iter()
.map(|x| {
if x >= missing_selector {
x + SELF_SELECTOR.len()
} else {
*x
}
})
.collect::<BTreeSet<_>>();
selectors.insert(*missing_selector + 1);
let line = exclude_internal_entites_from_selectors(&line, &selectors);
self.expand(&line)
}
Line::Schedule {
schedule_start,
function,
operation,
selectors,
..
} => {
let schedule_fn = function.path().replace('/', "+");
let execute =
exclude_internal_entites_from_selectors(&line[..*schedule_start], selectors);
let mut engine = self.extend([
("-schedule_ns-", function.namespace()),
("-schedule+fn-", &schedule_fn),
("execute run ", &execute),
]);
let ticks;
if let ScheduleOperation::APPEND { time } | ScheduleOperation::REPLACE { time } =
operation
{
ticks = time.as_ticks().to_string();
engine = engine.extend([("-ticks-", ticks.as_str())]);
}
let template = match operation {
ScheduleOperation::APPEND { .. } => {
include_template!("data/template/functions/schedule_append.mcfunction")
}
ScheduleOperation::CLEAR => {
include_template!("data/template/functions/schedule_clear.mcfunction")
}
ScheduleOperation::REPLACE { .. } => {
include_template!("data/template/functions/schedule_replace.mcfunction")
}
};
engine.expand(template)
}
Line::OtherCommand { selectors, .. } => {
let line = exclude_internal_entites_from_selectors(line, selectors);
self.expand(&line)
}
Line::Comment => self.expand(&line),
Line::Empty => line.to_owned(),
}
}
}
pub fn exclude_internal_entites_from_selectors(line: &str, selectors: &BTreeSet<usize>) -> String {
let mut index = 0;
let mut result = String::new();
for selector in selectors {
const MIN_SELECTOR_LEN: usize = "@e".len();
let (prefix, remaining_line) = line.split_at(selector + MIN_SELECTOR_LEN);
result.push_str(&prefix[index..]);
index = prefix.len();
result.push_str("[tag=!-ns-");
if remaining_line.starts_with('[') {
index += 1;
result.push(',');
} else {
result.push(']');
}
}
result.push_str(&line[index..]);
result
}