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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
//! Commands for debugging and getting the current state of the bot.
use std::{env, fs::File, io::Write, process, thread, time::Duration};
use azalea::{
BlockPos,
brigadier::prelude::*,
chunks::ReceiveChunkEvent,
entity::{LookDirection, Position},
interact::pick::HitResultComponent,
packet::game,
pathfinder::{ExecutingPath, Pathfinder},
prelude::ContainerClientExt,
world::MinecraftEntityId,
};
use azalea_core::hit_result::HitResult;
use azalea_entity::EntityKindComponent;
use azalea_inventory::components::MaxStackSize;
use azalea_world::InstanceContainer;
use bevy_ecs::event::Events;
use parking_lot::Mutex;
use super::{CommandSource, Ctx};
pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
commands.register(literal("ping").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.reply("pong!");
1
}));
commands.register(literal("disconnect").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.bot.disconnect();
1
}));
commands.register(literal("whereami").executes(|ctx: &Ctx| {
let mut source = ctx.source.lock();
let Some(entity) = source.entity() else {
source.reply("You aren't in render distance!");
return 0;
};
let position = source.bot.entity_component::<Position>(entity);
source.reply(format!(
"You are at {}, {}, {}",
position.x, position.y, position.z
));
1
}));
commands.register(literal("entityid").executes(|ctx: &Ctx| {
let mut source = ctx.source.lock();
let Some(entity) = source.entity() else {
source.reply("You aren't in render distance!");
return 0;
};
let entity_id = source.bot.entity_component::<MinecraftEntityId>(entity);
source.reply(format!(
"Your Minecraft ID is {} and your ECS id is {entity:?}",
*entity_id
));
1
}));
let whereareyou = |ctx: &Ctx| {
let source = ctx.source.lock();
let position = source.bot.position();
source.reply(format!(
"I'm at {}, {}, {}",
position.x, position.y, position.z
));
1
};
commands.register(literal("whereareyou").executes(whereareyou));
commands.register(literal("pos").executes(whereareyou));
commands.register(literal("whoareyou").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.reply(format!(
"I am {} ({})",
source.bot.username(),
source.bot.uuid()
));
1
}));
commands.register(literal("getdirection").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let direction = source.bot.component::<LookDirection>();
source.reply(format!(
"I'm looking at {}, {}",
direction.y_rot, direction.x_rot
));
1
}));
commands.register(literal("health").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let health = source.bot.health();
source.reply(format!("I have {health} health"));
1
}));
commands.register(literal("lookingat").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let hit_result = source.bot.component::<HitResultComponent>();
match &*hit_result {
HitResult::Block(r) => {
if r.miss {
source.reply("I'm not looking at anything");
return 0;
}
let block_pos = r.block_pos;
let block = source.bot.world().read().get_block_state(block_pos);
source.reply(format!("I'm looking at {block:?} at {block_pos:?}"));
}
HitResult::Entity(r) => {
let entity_kind = *source.bot.entity_component::<EntityKindComponent>(r.entity);
source.reply(format!(
"I'm looking at {entity_kind} ({:?}) at {}",
r.entity, r.location
));
}
}
1
}));
commands.register(literal("getblock").then(argument("x", integer()).then(
argument("y", integer()).then(argument("z", integer()).executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let x = get_integer(ctx, "x").unwrap();
let y = get_integer(ctx, "y").unwrap();
let z = get_integer(ctx, "z").unwrap();
println!("getblock xyz {x} {y} {z}");
let block_pos = BlockPos::new(x, y, z);
let block = source.bot.world().read().get_block_state(block_pos);
source.reply(format!("Block at {block_pos} is {block:?}"));
1
})),
)));
commands.register(literal("getfluid").then(argument("x", integer()).then(
argument("y", integer()).then(argument("z", integer()).executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let x = get_integer(ctx, "x").unwrap();
let y = get_integer(ctx, "y").unwrap();
let z = get_integer(ctx, "z").unwrap();
println!("getfluid xyz {x} {y} {z}");
let block_pos = BlockPos::new(x, y, z);
let block = source.bot.world().read().get_fluid_state(block_pos);
source.reply(format!("Fluid at {block_pos} is {block:?}"));
1
})),
)));
commands.register(literal("pathfinderstate").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let pathfinder = source.bot.get_component::<Pathfinder>();
let Some(pathfinder) = pathfinder else {
source.reply("I don't have the Pathfinder ocmponent");
return 1;
};
source.reply(format!(
"pathfinder.is_calculating: {}",
pathfinder.is_calculating
));
let executing_path = source.bot.get_component::<ExecutingPath>();
let Some(executing_path) = executing_path else {
source.reply("I'm not executing a path");
return 1;
};
source.reply(format!(
"is_path_partial: {}, path.len: {}, queued_path.len: {}",
executing_path.is_path_partial,
executing_path.path.len(),
if let Some(queued) = executing_path.queued_path {
queued.len().to_string()
} else {
"n/a".to_string()
},
));
1
}));
commands.register(literal("startuseitem").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.bot.start_use_item();
source.reply("Ok!");
1
}));
commands.register(literal("maxstacksize").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
let max_stack_size = source
.bot
.get_held_item()
.get_component::<MaxStackSize>()
.map_or(-1, |s| s.count);
source.reply(format!("{max_stack_size}"));
1
}));
commands.register(literal("debugecsleak").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.reply("Ok!");
source.bot.disconnect();
let ecs = source.bot.ecs.clone();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
// dump the ecs
let ecs = ecs.lock();
let report_path = env::temp_dir().join("azalea-ecs-leak-report.txt");
let mut report = File::create(&report_path).unwrap();
for entity in ecs.iter_entities() {
writeln!(report, "Entity: {}", entity.id()).unwrap();
let archetype = entity.archetype();
let component_count = archetype.component_count();
let component_names = archetype
.components()
.map(|c| ecs.components().get_info(c).unwrap().name())
.collect::<Vec<_>>();
writeln!(
report,
"- {component_count} components: {}",
component_names.join(", ")
)
.unwrap();
}
writeln!(report).unwrap();
for (info, _) in ecs.iter_resources() {
let name = info.name();
writeln!(report, "Resource: {name}").unwrap();
// writeln!(report, "- Size: {} bytes",
// info.layout().size()).unwrap();
match name {
"azalea_world::container::InstanceContainer" => {
let instance_container = ecs.resource::<InstanceContainer>();
for (instance_name, instance) in &instance_container.instances {
writeln!(report, "- Name: {instance_name}").unwrap();
writeln!(report, "- Reference count: {}", instance.strong_count())
.unwrap();
if let Some(instance) = instance.upgrade() {
let instance = instance.read();
let strong_chunks = instance
.chunks
.map
.iter()
.filter(|(_, v)| v.strong_count() > 0)
.count();
writeln!(
report,
"- Chunks: {} strongly referenced, {} in map",
strong_chunks,
instance.chunks.map.len()
)
.unwrap();
writeln!(
report,
"- Entities: {}",
instance.entities_by_chunk.len()
)
.unwrap();
}
}
}
"bevy_ecs::event::collections::Events<azalea_client::packet::game::ReceivePacketEvent>" => {
let events = ecs.resource::<Events<game::ReceiveGamePacketEvent>>();
writeln!(report, "- Event count: {}", events.len()).unwrap();
}
"bevy_ecs::event::collections::Events<azalea_client::chunks::ReceiveChunkEvent>" => {
let events = ecs.resource::<Events<ReceiveChunkEvent>>();
writeln!(report, "- Event count: {}", events.len()).unwrap();
}
_ => {}
}
}
println!("\x1b[1mWrote report to {}\x1b[m", report_path.display());
});
1
}));
commands.register(literal("exit").executes(|ctx: &Ctx| {
let source = ctx.source.lock();
source.reply("bye!");
source.bot.disconnect();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
process::exit(0);
});
1
}));
}
|