From e062e4d6a94c25a613f0847fc8e10276f44da491 Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Fri, 9 Oct 2015 17:18:13 -0700 Subject: [PATCH 1/4] Start of ticks, added daycycle, fixed inventory + chest things --- src/lib/playerPlugins/animations.js | 11 +++++++---- src/lib/playerPlugins/chest.js | 15 +++++++++++---- src/lib/playerPlugins/inventory.js | 8 ++++---- src/lib/playerPlugins/login.js | 2 +- src/lib/serverPlugins/daycycle.js | 21 +++++++++++++++++++++ src/lib/serverPlugins/tick.js | 25 +++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 src/lib/serverPlugins/daycycle.js create mode 100644 src/lib/serverPlugins/tick.js diff --git a/src/lib/playerPlugins/animations.js b/src/lib/playerPlugins/animations.js index 4476fa1..c61400b 100644 --- a/src/lib/playerPlugins/animations.js +++ b/src/lib/playerPlugins/animations.js @@ -19,13 +19,16 @@ function inject(serv, player) } player._client.on("entity_action", function(packet) { - if(packet.actionId == 3) + if(packet.actionId == 3) { setMetadata([{"key":0,"type":0,"value": 0x08}]); - if(packet.actionId == 4) + } else if(packet.actionId == 4) { setMetadata([{"key":0,"type":0,"value": 0x00}]); - if(packet.actionId == 0) + } else if(packet.actionId == 0) { setMetadata([{"key":0,"type":0,"value": 0x02}]); - if(packet.actionId == 1) + player.entity.crouching = true; + } else if(packet.actionId == 1) { setMetadata([{"key":0,"type":0,"value": 0x00}]); + player.entity.crouching = false; + } }); } \ No newline at end of file diff --git a/src/lib/playerPlugins/chest.js b/src/lib/playerPlugins/chest.js index a0c0260..be80f8d 100644 --- a/src/lib/playerPlugins/chest.js +++ b/src/lib/playerPlugins/chest.js @@ -4,16 +4,23 @@ module.exports=inject; function inject(serv, player) { - player._client.on('block_place', function (packet) { + player._client.on('block_place', async function (packet) { var referencePosition=new vec3(packet.location.x,packet.location.y,packet.location.z); - var id = serv.world.getBlock(referencePosition).type; - if(id==54) + if (player.entity.crouching) return; + var id = await serv.world.getBlockType(referencePosition); + var blockAbove = await serv.world.getBlockType(referencePosition.clone().add(new vec3(0, 1, 0))); + + if(id==54) { + if (blockAbove) { + return; + } player._client.write("open_window",{ windowId:165, inventoryType:"minecraft:chest", windowTitle:JSON.stringify("Chest"), - slotCount:26 + slotCount:9*3 + 8 // 3 rows, make nicer later }); + } }); } \ No newline at end of file diff --git a/src/lib/playerPlugins/inventory.js b/src/lib/playerPlugins/inventory.js index 0e2dbf3..2bd3086 100644 --- a/src/lib/playerPlugins/inventory.js +++ b/src/lib/playerPlugins/inventory.js @@ -8,16 +8,16 @@ function inject(serv, player) player._client.on("held_item_slot", function (packet) { player.heldItemSlot = packet.slotId; - if(player.inventory[36+heldItemSlot]===undefined){ - player.inventory[36+heldItemSlot]={ + if(player.inventory[36+player.heldItemSlot]===undefined){ + player.inventory[36+player.heldItemSlot]={ blockId:-1 }; } - player.heldItem = player.inventory[36+heldItemSlot]; + player.heldItem = player.inventory[36+player.heldItemSlot]; player._writeOthers("entity_equipment",{ entityId:player.entity.id, slot:0, - item:heldItem + item:player.heldItem }); }); diff --git a/src/lib/playerPlugins/login.js b/src/lib/playerPlugins/login.js index c91cc35..d7c5378 100644 --- a/src/lib/playerPlugins/login.js +++ b/src/lib/playerPlugins/login.js @@ -136,7 +136,7 @@ function inject(serv,player) { player._client.write('update_time', { age: [0, 0], - time: [0, 1] + time: [0, serv.time] }); } diff --git a/src/lib/serverPlugins/daycycle.js b/src/lib/serverPlugins/daycycle.js new file mode 100644 index 0000000..c256510 --- /dev/null +++ b/src/lib/serverPlugins/daycycle.js @@ -0,0 +1,21 @@ + +module.exports = inject; + +function inject(serv, settings) { + serv.setTime = function(time) { + serv.time = time; + serv._writeAll('update_time', { + age: [0, 0], // TODO + time: [0, serv.time] + }); + } + + serv.doDaylightCycle = true; + + serv.time = 0; + + serv.on('tick', function() { + if (!serv.doDaylightCycle) return; + if (serv.tick % 20 == 0) serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second + }) +} \ No newline at end of file diff --git a/src/lib/serverPlugins/tick.js b/src/lib/serverPlugins/tick.js new file mode 100644 index 0000000..802c939 --- /dev/null +++ b/src/lib/serverPlugins/tick.js @@ -0,0 +1,25 @@ + +module.exports = inject; + +function inject(serv, settings) { + serv.setTickInterval = setTickInterval; + serv.stopTickInterval = stopTickInterval; + serv.tick = 0; + + serv.setTickInterval(20); +} + +function setTickInterval(ticksPerSecond) { + var serv = this; + if (serv.tickInterval) clearInterval(serv.tickInterval); + + serv.tickInterval = setInterval(function() { + serv.tick++; + serv.emit('tick', serv.tick); + }, 1000/ticksPerSecond); +} + +function stopTickInterval() { + if (serv.tickInterval) clearInterval(serv.tickInterval); + serv.tickInterval = null; +} \ No newline at end of file From 7a028e7193c660e88310433204d16f933e5c1b59 Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Fri, 9 Oct 2015 17:30:10 -0700 Subject: [PATCH 2/4] Quick fixes, adding everything to api.md --- doc/api.md | 34 +++++++++++++++++++++++++++++++ src/lib/serverPlugins/daycycle.js | 4 ++-- src/lib/serverPlugins/tick.js | 8 ++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/doc/api.md b/doc/api.md index 8fe4b2f..88b4f45 100644 --- a/doc/api.md +++ b/doc/api.md @@ -14,11 +14,15 @@ - [serv.world](#servworld) - [serv.entities](#serventities) - [serv.bannedPlayers](#servbannedplayers) + - [serv.time](#servtime) + - [serv.tickCount](#servtickcount) + - [serv.doDaylightCycle](#servdodaylightcycle) - [Events](#events) - ["error" (error)](#error-error) - ["listening" (port)](#listening-port) - ["newPlayer" (player)](#newplayer-player) - ["banned" (banner,bannedUsername,reason)](#banned-bannerbannedusernamereason) + - ["tick" (count)](#tick-count) - [Methods](#methods) - [serv.createLog()](#servcreatelog) - [serv.log(message)](#servlogmessage) @@ -30,6 +34,8 @@ - [server.pardonUsername(username,callback)](#serverpardonusernameusernamecallback) - [server.pardon(uuid)](#serverpardonuuid) - [server.getUUIDFromUsername(username,callback)](#servergetuuidfromusernameusernamecallback) + - [server.setTime(time)](#serversettimetime) + - [server.setTickInterval(ticksPerSecond)](#serversettickintervaltickspersecond) - [Player](#player) - [Properties](#properties-1) - [player.entity](#playerentity) @@ -112,6 +118,20 @@ Example player: } ``` +#### serv.time + +Current daylight cycle time in ticks. Morning is 0, noon is 6000, evening is 12000, and night is 18000. +Resets to 0 at 24000. Use `serv.setTime(time)` to set the time. + +#### serv.tickCount + +Total number of ticks that have passed since the start of the world. +Best to use with modulo (e.g. Something every 10 seconds is `serv.tickCount % 20*10 == 0`) + +#### serv.doDaylightCycle + +Default `true`. If false, time will not automatically pass. + ### Events #### "error" (error) @@ -130,6 +150,10 @@ Fires when `player` login, allow external player plugins. `banner` banned `bannedUsername` with `reason` +#### "tick" (count) + +Fires when one tick has passed (default is 50ms). count is the total world ticks (same as serv.tickCount) + ### Methods #### serv.createLog() @@ -174,6 +198,16 @@ Gets UUID from username. Since it needs to fetch from mojang servers, it is not Arguments in format: `callback(uuid)`. `uuid` is null if no such username exists. +#### server.setTime(time) + +Set daylight cycle time in ticks. See `serv.time` for more info. + +#### server.setTickInterval(ticksPerSecond) + +Resets tick interval to occur `ticksPerSecond` times per second. + +Use `server.stopTickInterval()` if you want but this method already calls that and you can use `serv.doDaylightCycle` to stop it anyway. + ## Player ### Properties diff --git a/src/lib/serverPlugins/daycycle.js b/src/lib/serverPlugins/daycycle.js index c256510..1331193 100644 --- a/src/lib/serverPlugins/daycycle.js +++ b/src/lib/serverPlugins/daycycle.js @@ -14,8 +14,8 @@ function inject(serv, settings) { serv.time = 0; - serv.on('tick', function() { + serv.on('tick', function(count) { if (!serv.doDaylightCycle) return; - if (serv.tick % 20 == 0) serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second + if (count % 20 == 0) serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second }) } \ No newline at end of file diff --git a/src/lib/serverPlugins/tick.js b/src/lib/serverPlugins/tick.js index 802c939..a6ea6d4 100644 --- a/src/lib/serverPlugins/tick.js +++ b/src/lib/serverPlugins/tick.js @@ -4,18 +4,18 @@ module.exports = inject; function inject(serv, settings) { serv.setTickInterval = setTickInterval; serv.stopTickInterval = stopTickInterval; - serv.tick = 0; + serv.tickCount = 0; serv.setTickInterval(20); } function setTickInterval(ticksPerSecond) { var serv = this; - if (serv.tickInterval) clearInterval(serv.tickInterval); + stopTickInterval(); serv.tickInterval = setInterval(function() { - serv.tick++; - serv.emit('tick', serv.tick); + serv.tickCount++; + serv.emit('tick', serv.tickCount); }, 1000/ticksPerSecond); } From 7fd8e56d3eecd622b46dff539322b867f93ea24a Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Fri, 9 Oct 2015 17:33:29 -0700 Subject: [PATCH 3/4] Of course it breaks --- src/lib/serverPlugins/tick.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/serverPlugins/tick.js b/src/lib/serverPlugins/tick.js index a6ea6d4..6d50fbb 100644 --- a/src/lib/serverPlugins/tick.js +++ b/src/lib/serverPlugins/tick.js @@ -11,7 +11,7 @@ function inject(serv, settings) { function setTickInterval(ticksPerSecond) { var serv = this; - stopTickInterval(); + serv.stopTickInterval(); serv.tickInterval = setInterval(function() { serv.tickCount++; @@ -20,6 +20,6 @@ function setTickInterval(ticksPerSecond) { } function stopTickInterval() { - if (serv.tickInterval) clearInterval(serv.tickInterval); - serv.tickInterval = null; + if (this.tickInterval) clearInterval(serv.tickInterval); + this.tickInterval = null; } \ No newline at end of file From 51148036e492748fa5d923ae984d453ec10ecbbd Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 10 Oct 2015 15:56:47 +0200 Subject: [PATCH 4/4] put modpe in its own command namespace (/modpe ), add the same /time command that minecraft has --- src/lib/playerPlugins/commands.js | 19 +++++++++++++++++-- src/lib/serverPlugins/daycycle.js | 4 +++- src/lib/serverPlugins/modpe.js | 9 +++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/lib/playerPlugins/commands.js b/src/lib/playerPlugins/commands.js index 91e839d..707fba5 100644 --- a/src/lib/playerPlugins/commands.js +++ b/src/lib/playerPlugins/commands.js @@ -5,8 +5,6 @@ module.exports = inject; function inject(serv, player, options) { function handleCommand(command) { var results; - if (options.modpe) - player.chat("§1######## VANILLA ########"); if (options.commands[command]) player.chat("" + options.commands[command]); else if (results = command.match(/^gamemode ([0-3])$/)) { @@ -81,6 +79,23 @@ function inject(serv, player, options) { .catch(err => player.chat(results[1] + " is not banned")); } } + else if(results = command.match(/^time (add|query|set)(?: ([0-9]+))?/)) { + var action=results[1]; + var value=results[2]!==undefined ? parseInt(results[2]) : null; + if(action=="query") + player.chat("It is "+serv.time); + else if(action=="set") { + player.chat("Time was changed from "+serv.time+" to "+value); + serv.setTime(value); + } + else if(action=="add") { + player.chat("Time was changed from "+serv.time+" to "+(value + serv.time)); + serv.setTime(value + serv.time); + } + } + else if(results = command.match(/^modpe (.+)$/)) { + player.emit("modpe",results[1]); + } else player.chat("Invalid command."); } diff --git a/src/lib/serverPlugins/daycycle.js b/src/lib/serverPlugins/daycycle.js index 1331193..fd8f61e 100644 --- a/src/lib/serverPlugins/daycycle.js +++ b/src/lib/serverPlugins/daycycle.js @@ -16,6 +16,8 @@ function inject(serv, settings) { serv.on('tick', function(count) { if (!serv.doDaylightCycle) return; - if (count % 20 == 0) serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second + if (count % 20 == 0) { + serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second + } }) } \ No newline at end of file diff --git a/src/lib/serverPlugins/modpe.js b/src/lib/serverPlugins/modpe.js index 84082d6..0c7b9f1 100644 --- a/src/lib/serverPlugins/modpe.js +++ b/src/lib/serverPlugins/modpe.js @@ -183,11 +183,9 @@ function inject(serv,settings) serv.world.getBlockType(new vec3(packet.location.x, packet.location.y, packet.location.z))); }); - player._client.on('chat', function (packet) { - if(packet.message[0]=="/") { - var command = packet.message.slice(1); - procCmd(command); - } + player.on('modpe', function (command) { + console.log(command); + procCmd(command); }); function newLevel() { @@ -216,7 +214,6 @@ function inject(serv,settings) } function procCmd(command) { - player.chat("§2######### MODPE #########"); mods.forEach(function (element, index, array) { element.procCmd(command); });