Merge pull request #70 from demipixel/ticks

Tick... Tick... Tick...
This commit is contained in:
Romain Beaumont 2015-10-10 15:56:58 +02:00
commit 0448c19111
9 changed files with 125 additions and 21 deletions

View file

@ -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

View file

@ -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;
}
});
}

View file

@ -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
});
}
});
}

View file

@ -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.");
}

View file

@ -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
});
});

View file

@ -136,7 +136,7 @@ function inject(serv,player)
{
player._client.write('update_time', {
age: [0, 0],
time: [0, 1]
time: [0, serv.time]
});
}

View file

@ -0,0 +1,23 @@
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(count) {
if (!serv.doDaylightCycle) return;
if (count % 20 == 0) {
serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second
}
})
}

View file

@ -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);
});

View file

@ -0,0 +1,25 @@
module.exports = inject;
function inject(serv, settings) {
serv.setTickInterval = setTickInterval;
serv.stopTickInterval = stopTickInterval;
serv.tickCount = 0;
serv.setTickInterval(20);
}
function setTickInterval(ticksPerSecond) {
var serv = this;
serv.stopTickInterval();
serv.tickInterval = setInterval(function() {
serv.tickCount++;
serv.emit('tick', serv.tickCount);
}, 1000/ticksPerSecond);
}
function stopTickInterval() {
if (this.tickInterval) clearInterval(serv.tickInterval);
this.tickInterval = null;
}