forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
83 lines (66 loc) · 2.11 KB
/
start.js
File metadata and controls
83 lines (66 loc) · 2.11 KB
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
// Generated by CoffeeScript 2.5.1
(function() {
var Chunk, fs, mineflayer, players, restoreWorld, saveWorld, socketInfo, vec3, webmc, world;
webmc = require("./webmc");
mineflayer = require('mineflayer');
vec3 = require("vec3");
fs = require("fs");
Chunk = require("prismarine-chunk")("1.16.1");
vec3 = require("vec3");
world = {};
saveWorld = function() {
return fs.writeFileSync(__dirname + "/savedWorld.json", JSON.stringify(world));
};
restoreWorld = function() {
return world = JSON.parse(fs.readFileSync(__dirname + '/savedWorld.json'));
};
restoreWorld();
players = {};
socketInfo = {};
webmc.on("blockUpdate", function(x, y, z, v) {
world[`${x}:${y}:${z}`] = v;
if (v === 0) {
delete world[`${x}:${y}:${z}`];
}
webmc.broadcast("blockUpdate", [x, y, z, v]);
return saveWorld();
});
webmc.on("join", function(socketid, data) {
console.log("NEW: " + socketid);
//init socketInfo
socketInfo[socketid] = data;
//socketInfo add Bot
socketInfo[socketid].bot = mineflayer.createBot({
host: webmc.config.realServer.ip,
port: webmc.config.realServer.port,
username: socketInfo[socketid].nick
});
socketInfo[socketid].bot._client.on("map_chunk", function(packet) {
// cell=new Chunk()
// cell.load packet.chunkData,packet.bitMap,false,true
// console.log cell.fromJson
webmc.send(socketid, "mapChunk", packet);
});
// console.log packet
socketInfo[socketid].bot.on('chat', function(username, message) {
if (username === socketInfo[socketid].bot.username) {
return;
}
socketInfo[socketid].bot.chat(message);
});
//first world load
webmc.send(socketid, "firstLoad", world);
});
webmc.on("leave", function(socketid) {
console.log("DIS: " + socketid);
//end bot session
socketInfo[socketid].bot.end();
//delete socketinfo
delete players[socket.id];
delete socketInfo[socket.id];
});
webmc.on("playerUpdate", function(data) {
players[socket.id] = data;
return webmc.broadcast("playerUpdate", data);
});
}).call(this);