This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathworld.js
124 lines (100 loc) · 4.53 KB
/
world.js
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
var util = require('util'),
EventEmitter = require('events').EventEmitter,
randomstring = require("randomstring"),
Terrain = require('./terrain/terrain'),
PacketRouter = require('./packetRouter'),
PacketWriter = require('./network/packetWriter'),
PacketSender = require('./network/packetSender'),
Encryption = require('./network/encryption'),
EntityManager = require('./entityManager');
function World() {
EventEmitter.call(this);
var self = this;
this.worldTime = new Buffer(8);
this.worldTime.fill(0);
this.terrain = new Terrain();
this.packetRouter = new PacketRouter(this);
this.encryption = new Encryption();
this.serverId = randomstring.generate(15);
this.encryption.init(new Buffer(this.serverId, 'ascii'));
this.settings = require('./settings.json');
this.packetWriter = new PacketWriter();
this.packetSender = new PacketSender(this);
this.itemEntities = new EntityManager();
this.npcEntities = new EntityManager();
this.playerEntities = new EntityManager();
this.nextEntityId = 1;
this.registerHandlers();
};
World.prototype = Object.create(EventEmitter.prototype, { constructor: { value: World }});
World.prototype.register = function(handlerName) {
var handler = require('./handlers/' + handlerName);
handler(this);
return this;
};
World.prototype.startWorld = function () {
var self = this;
this.timeTimer = setInterval(function () {
self.emit('game_tick');
}, 50);
this.keepAliveTimer = setInterval(function () {
self.emit('keepalive_tick');
}, 1000);
};
World.prototype.registerHandlers = function() {
var self = this;
this.register('timeHandler');
this.register('serverPingHandler');
this.register('keepAliveHandler');
this.register('loginHandler');
this.register('chatHandler');
this.register('playerLookMovementHandler');
this.register('diggingHandler');
this.register('buildHandler');
this.register('inventoryHandler');
this.register('pluginHandler');
self.on('use_entity', function(data, player) { self.packetRouter.useEntity(data, player) });
self.on('entity_action', function(data, player) { self.packetRouter.entityAction(data, player) });
self.on('close_window', function(data, player) { self.packetRouter.closeWindow(data, player) });
self.on('click_window', function(data, player) { self.packetRouter.clickWindow(data, player) });
self.on('confirm_transaction', function(data, player) { self.packetRouter.confirmTransaction(data, player) });
self.on('creative_inventory_action', function(data, player) { self.packetRouter.creativeInventoryAction(data, player) });
self.on('enchant_item', function(data, player) { self.packetRouter.enchantItem(data, player) });
self.on('update_sign', function(data, player) { self.packetRouter.updateSign(data, player) });
self.on('player_abilities', function(data, player) { self.packetRouter.playerAbilities(data, player) });
self.on('locale_view_distance', function(data, player) { self.packetRouter.localeViewDistance(data, player) });
self.on('disconnect', function(data, player) { self.disconnect(data, player) });
self.on('end', function(player) { self.socketClosed(player) });
self.on('destroy', function(player) { self.socketClosed(player) });
};
World.prototype.sendEntitiesToPlayer = function(targetPlayer) {
targetPlayer.sendItemEntities(this.itemEntities, this.packetWriter);
targetPlayer.sendNpcEntities(this.npcEntities, this.packetWriter);
targetPlayer.sendPlayerEntities(this.playerEntities, this.packetWriter);
};
World.prototype.disconnect = function (data, player) {
//Do stuff like save them to the database.
this.playerEntities.remove(player.entityId);
var message = {
translate: 'chat.type.announcement',
using: ["Server", player.name + ' (' + player.id + ') has left the world!']
};
var leavingChat = this.packetWriter.build({ ptype: 0x03, message: JSON.stringify(message) });
var clientlist = this.packetWriter.build({
ptype: 0xC9,
playerName: player.name,
online: false,
ping: 0
});
var destroyEntity = this.packetWriter.build({
ptype: 0x1D,
entityIds: [ player.entityId ]
});
this.packetSender.sendToAllPlayers(clientlist);
this.packetSender.sendToAllPlayers(destroyEntity);
this.packetSender.sendToAllPlayers(leavingChat);
};
World.prototype.socketClosed = function(player) {
console.log('Player connection closed.');
};
module.exports = World;