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
|
#include "applet.h"
#include <stdio.h>
#include <string.h>
#include <err.h>
static int snd(const ac_entity_t *player, const char *prefix, const char *postfix) {
char m[512];
if (prefix && !postfix) {
snprintf(m, 512,
"tellraw @a "
"[\"\",{\"text\":\"%s %s @ %s[x:%.0lf, y:%.0lf, z:%.0lf]\"}]",
player->name,
prefix,
get_world(player->world),
player->pos.x,
player->pos.y,
player->pos.z
);
} else if (prefix && postfix) {
snprintf(m, 512,
"tellraw @a "
"[\"\",{\"text\":\"%s %s @ %s[x:%.0lf, y:%.0lf, z:%.0lf]: %s\"}]",
player->name,
prefix,
get_world(player->world),
player->pos.x,
player->pos.y,
player->pos.z,
postfix
);
} else {
snprintf(m, 512,
"tellraw @a "
"[\"\",{\"text\":\"%s @ %s[x:%.0lf, y:%.0lf, z:%.0lf]\"}]",
player->name,
get_world(player->world),
player->pos.x,
player->pos.y,
player->pos.z
);
}
ac_request_cmd_t cmd = {
.type = AC_REQUEST_CMD,
.id = 1,
.config = NULL,
.cmd = m
};
int r;
if ((r = ac_request(connection, (ac_request_t *) &cmd))) {
return r;
}
return 0;
}
void handle(ac_obj_t *obj) {
int r = 0;
switch (obj->type) {
case AC_RESPONSE_ERROR: {
ac_response_error_t *err = (ac_response_error_t *) obj;
fprintf(stderr, "Err: %s (%d).\n", err->message, err->code);
break;
}
case AC_EVENT_PLAYER_JOIN: {
ac_event_player_join_t *join = (ac_event_player_join_t *) obj;
r = snd(&join->player, "joined", NULL);
break;
}
case AC_EVENT_PLAYER_MESSAGE: {
ac_event_player_message_t *msg = (ac_event_player_message_t *) obj;
if (strncmp(".h", msg->text, 2)) {
break;
}
r = snd(&msg->player, NULL, NULL);
break;
}
case AC_RESPONSE_CMD_OUT: {
ac_response_cmd_out_t *out = (ac_response_cmd_out_t *) obj;
fprintf(stderr, "%s\n", out->out);
break;
}
case AC_RESPONSE_CMD_RESULT: {
ac_response_cmd_result_t *res = (ac_response_cmd_result_t *) obj;
if (!res->success) {
fprintf(stderr, "CmdErr: %d.\n", res->result);
}
break;
}
}
ac_object_free(obj);
if (r) errx(r, "Cannot send message: %d.", r);
}
|