forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (104 loc) · 3.74 KB
/
server.js
File metadata and controls
105 lines (104 loc) · 3.74 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Generated by CoffeeScript 2.5.1
(function() {
module.exports = function(config) {
var Chunk, app, express, fs, http, io, mineflayer, players, port, restoreWorld, saveWorld, server, sf, socketInfo, vec3, world;
fs = require("fs");
http = require("http");
server = http.createServer();
io = require("socket.io")(server);
express = require('express');
app = express();
mineflayer = require('mineflayer');
Chunk = require("prismarine-chunk")("1.16.1");
vec3 = require("vec3");
sf = {};
port = config["express-port"];
world = {};
//Zapisywanie i odczytywanie świata
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 = {};
app.use(express.static(__dirname + "/client/"));
app.use(function(req, res, next) {
res.set('Cache-Control', 'no-store');
return next();
});
app.get("/websocket/", function(req, res) {
return res.send(String(config["websocket-port"]));
});
app.get("/host/", function(req, res) {
return res.send(String(config["host"]));
});
app.listen(port);
server.listen(config["websocket-port"]);
//On connect
return io.sockets.on("connection", function(socket) {
//Trying to run special functions
socket.on("initClient", function(data) {
console.log("[+] " + data.nick);
//init socketInfo
socketInfo[socket.id] = data;
//socketInfo add Bot
socketInfo[socket.id].bot = mineflayer.createBot({
host: config.realServer.ip,
port: config.realServer.port,
username: socketInfo[socket.id].nick
});
//On recieve real Map Chunk
socketInfo[socket.id].bot._client.on("map_chunk", function(packet) {
var cell;
cell = new Chunk();
cell.load(packet.chunkData, packet.bitMap, false, true);
io.to(socket.id).emit("mapChunk", cell.sections, packet.x, packet.z);
});
// console.log packet
socketInfo[socket.id].bot.on('chat', function(username, message) {
if (username === socketInfo[socket.id].bot.username) {
return;
}
socketInfo[socket.id].bot.chat(message);
});
socketInfo[socket.id].bot.on('move', function() {
try {
console.log(socketInfo[socket.id].bot.entity.position);
io.to(socket.id).emit("botPosition", socketInfo[socket.id].bot.entity.position);
} catch (error) {}
});
//first world load
io.to(socket.id).emit("firstLoad", world);
});
socket.on("move", function(state, toggle) {
return socketInfo[socket.id].bot.setControlState(state, toggle);
});
socket.on("playerRotate", function(data) {
return socketInfo[socket.id].bot.look(...data);
});
socket.on("playerUpdate", function(data) {
players[socket.id] = data;
return io.sockets.emit("playerUpdate", players);
});
socket.on("blockUpdate", function(block) {
world[`${block[0]}:${block[1]}:${block[2]}`] = block[3];
if (block[3] === 0) {
delete world[`${block[0]}:${block[1]}:${block[2]}`];
}
io.sockets.emit("blockUpdate", block);
return saveWorld();
});
return socket.on("disconnect", function() {
console.log("[-] " + socketInfo[socket.id].nick);
//end bot session
socketInfo[socket.id].bot.end();
//delete socketinfo
delete players[socket.id];
delete socketInfo[socket.id];
});
});
};
}).call(this);