forked from deoxxa/jsmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.example.js
executable file
·211 lines (167 loc) · 7.58 KB
/
server.example.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
// This is an example `server.js`! It shows you how jsmc allows you to very
// heavily customise your server in an easy to manage way.
//
// This server just does the basics and shows off the included plugins.
// nconf and optimist are external libraries that make configuration easy. You
// can replace these with any other library you want though - it's all up to
// you.
var nconf = require("nconf"),
optimist = require("optimist");
nconf.argv();
optimist.argv._.reverse().concat(["config.json"]).forEach(function(file) { nconf.file(file.toLowerCase().replace(/[^a-z0-9]+/g, "_"), file); });
nconf.defaults({
server: {
port: 25565,
},
game: {
name: "Another jsmc server",
mode: 1,
max_players: 25,
difficulty: 0,
},
});
// This is the `Map` object. It keeps a list of routines that generate your map.
var Map = require("./lib/map");
var map = new Map();
// The `MapgenLoad` plugin loads existing map data from disk. The argument given
// to it is the directory to store map data in.
var MapgenLoad = require("./plugins/mapgen-load"),
mapgen_load = new MapgenLoad(__dirname + "/map");
map.add_generator(mapgen_load.modify.bind(mapgen_load));
// The `MapgenSimplex` plugin generates pseudorandom terrain using an algorithm
// related to _Perlin Noise_, [Simplex Noise](http://en.wikipedia.org/wiki/Simplex_noise).
// The argument you see here is the "seed" value.
var MapgenSimplex = require("./plugins/mapgen-simplex"),
mapgen_simplex = new MapgenSimplex(1);
map.add_generator(mapgen_simplex.modify.bind(mapgen_simplex));
// The `MapgenWater` plugin fills the bottom of the map, up to a specified `y`
// value, with water. It does this by replacing all air blocks with a `y` less
// than that specified with water.
var MapgenWater = require("./plugins/mapgen-water"),
mapgen_water = new MapgenWater(100);
map.add_generator(mapgen_water.modify.bind(mapgen_water));
// The `MapgenGlowstone` plugin places glowstone randomly. Yay! Great for
// testing lighting, or just impressing your friends.
var MapgenGlowstone = require("./plugins/mapgen-glowstone"),
mapgen_glowstone = new MapgenGlowstone();
map.add_generator(mapgen_glowstone.modify.bind(mapgen_glowstone));
// The `MapgenGlassGrid` plugin puts a grid of glass pillars in the world.
// Again this is mostly useful for debugging.
var MapgenGlassGrid = require("./plugins/mapgen-glass-grid"),
mapgen_glass_grid = new MapgenGlassGrid();
map.add_generator(mapgen_glass_grid.modify.bind(mapgen_glass_grid));
// The `MapgenFloor` plugin puts a single layer of bedrock at the bottom of the
// world. This stops people digging through to nothing by accident.
var MapgenFloor = require("./plugins/mapgen-floor"),
mapgen_floor = new MapgenFloor();
map.add_generator(mapgen_floor.modify.bind(mapgen_floor));
// This is the `Game` object. It's (probably) the core of your server. It holds
// a lot of the wiring between different components.
var Game = require("./lib/game");
var game = new Game({
name: nconf.get("game:name"),
mode: nconf.get("game:mode"),
max_players: nconf.get("game:max_players"),
difficulty: nconf.get("game:difficulty"),
admins: nconf.get("game:admins"),
banned: nconf.get("game:banned"),
map: map,
});
// Load some plugins
// This plugin provides the server ping functionality for reporting server
// status in the "Play Multiplayer" screen of Minecraft.
var ServerPingPlugin = require("./plugins/server-ping");
game.use(ServerPingPlugin());
// This plugin gracefully stops the server on Ctrl+C (SIGINT).
var ServerStopPlugin = require('./plugins/server-stop');
game.use(ServerStopPlugin());
// This plugin handles login for players, creating a new Player object,
// attaching it to a client and finally adding it to the active Game object.
var LoginPlugin = require("./plugins/login");
game.use(LoginPlugin());
// This plugin handles chat messages between players.
var ChatPlugin = require("./plugins/chat");
game.use(ChatPlugin());
// This plugin handles... You guessed it, digging!
var DiggingPlugin = require("./plugins/digging");
game.use(DiggingPlugin());
// This plugin handles.. BUILDING!
var BuildingPlugin = require('./plugins/building');
game.use(BuildingPlugin());
// This plugin does all the setup of a player to get them into the world. This
// includes things like setting their initial spawn position, sending them a
// bunch of chunks to get started, telling them about other players connected,
// etc.
var InitialSpawnPlugin = require("./plugins/initial-spawn");
game.use(InitialSpawnPlugin());
// This plugin displays chat messages when players join or leave the game.
var JoinPartPlugin = require("./plugins/join-part");
game.use(JoinPartPlugin());
// This plugin handles movement of players. It keeps track of their position,
// sends the information to other players, and so on.
var MovementPlugin = require("./plugins/movement");
game.use(MovementPlugin());
// This plugin sends periodic ping messages to connected players, to make sure
// they don't time out.
var PingPlugin = require("./plugins/ping");
game.use(PingPlugin());
// This plugin provides a `/suicide` command for players to kill themselves.
// It's largely used for debugging or getting yourself out of a wall...
var RespawnPlugin = require("./plugins/respawn");
game.use(RespawnPlugin());
// This plugin despawns players when they quit the game.
var DespawnPlugin = require("./plugins/despawn");
game.use(DespawnPlugin());
// This plugin saves the map to disk every now and then, if it's changed. The
// argument given to it is the directory in which to save the map data.
var SaveMapPlugin = require("./plugins/save-map");
game.use(SaveMapPlugin(__dirname + "/map"));
// Provides administrative commands `/ban` `/op` etc.
var AdminPlugin = require('./plugins/admin');
game.use(AdminPlugin());
// This is used to keep track of player ability state between the client and the server
var PlayerAbilitiesPlugin = require('./plugins/player-abilities');
game.use(PlayerAbilitiesPlugin());
// This plugin manages players health & hunger
var PlayerHealthPlugin = require('./plugins/player-health');
game.use(PlayerHealthPlugin());
// This plugin controlls falling damage/death
var PlayerFallPlugin = require('./plugins/player-fall');
game.use(PlayerFallPlugin());
// The server object is basically a wrapper around `net.Server` that constructs
// `Client` objects as they connect.
var Server = require("./lib/server");
// Here's a server instance! Note that you're not limited to one. Feel free to
// create as many as you want and wire them all up to the same `Game` object.
// Or different objects. I'm not here to judge you.
var server = new Server();
// This is how you wire a `Server` up to a `Game`.
server.on("client:connect", game.add_client.bind(game));
// These listeners are really just for convenience and some info for the person
// running the server.
server.on("server:listening", function() {
console.log("listening");
});
server.on("server:close", function() {
console.log("closed");
});
// Generate the spawn area so the first player to join doesn't have to sit
// around like an idiot waiting while they log in.
console.info("Generating map...");
var chunks_generated = 0;
for (var x = -7; x <= 7; ++x) {
for (var y = -7; y <= 7; ++y) {
game.map.get_chunk(x, y, onChunk);
}
}
function onChunk(err, chunk) {
// We keep count of how many chunks have been generated here.
chunks_generated++;
// This is 15x15 chunks
if (chunks_generated === 225) {
// We've loaded all the chunks we need, so it's time to start the
// server listening so people can connect!
server.listen(nconf.get("server:port"), nconf.get("server:host"));
}
}