-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
62 lines (50 loc) · 1.63 KB
/
commands.c
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
#include "commands.h"
#include "logging.h"
#include "inventory.h"
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void command_kick_me(Player *p, Server *s, char *cmd){
Player_disconnect(p, "Get outa here!");
}
void command_give_stuff(Player *p, Server *s, char *cmd){
Slot *slot = Slot_new_basic(5, 64, 0);
pthread_rwlock_wrlock(&p->lock);
Player_give_slot(p, slot);
pthread_rwlock_unlock(&p->lock);
Slot_free(slot);
}
void command_spawn(Player *p, Server *s, char *cmd){
pthread_rwlock_wrlock(&p->lock);
Player_teleport(p, s->spawnx, s->spawny, s->spawnz);
pthread_rwlock_unlock(&p->lock);
}
void command_item(Player *p, Server *s, char *cmd){
int id;
int count;
int metadata;
int read = sscanf(cmd, "%d %d %d", &id, &count, &metadata);
Slot *slot;
if (read == 1){
slot = Slot_new_basic(id, 64, 0);
} else if (read == 2){
slot = Slot_new_basic(id, count, 0);
} else if (read == 3){
slot = Slot_new_basic(id, count, metadata);
} else {
Player_send_message(p, "Invalid number of arguments!");
return;
}
Player_give_slot(p, slot);
free(slot);
}
void handle_command(Player *p, Server *s, char *cmd){
char *first;
char *remainder;
first = strtok_r(cmd, " ", &remainder);
if (strcmp(first, "/kickme") == 0) command_kick_me(p, s, remainder);
else if (strcmp(first, "/givestuff") == 0) command_give_stuff(p, s, remainder);
else if (strcmp(first, "/spawn") == 0) command_spawn(p, s, remainder);
else if (strcmp(first, "/i") == 0) command_item(p, s, remainder);
else Player_send_message(p, "Unrecognized command!");
}