This commit is contained in:
Holly Stubbs 2021-08-15 00:21:41 +01:00
parent 19880f1e8d
commit 45bdbdc354
4 changed files with 64 additions and 14 deletions

View file

@ -0,0 +1,15 @@
const Entity = require("./Entity.js");
class EntityPlayer extends Entity {
constructor(x = 0, y = 0, z = 0) {
super(x, y, z);
}
onTick() {
super.onTick();
}
}
module.exports = EntityPlayer;

View file

@ -0,0 +1,13 @@
const EntityLiving = require("./EntityLiving.js");
class EntityPlayer extends EntityLiving {
constructor(itemID = 0x00, x = 0, y = 0, z = 0) {
super(x, y, z);
this.motionX = (Math.random() * 0.2 - 0.1);
this.motionY = 0.2;
this.motionZ = (Math.random() * 0.2 - 0.1);
}
}
module.exports = EntityPlayer;

View file

@ -1,3 +1,10 @@
/*
===========- bufferStuff.js -===========
Created by Holly (tgpethan) (c) 2021
Licensed under MIT
========================================
*/
module.exports.Writer = class { module.exports.Writer = class {
constructor() { constructor() {
this.buffer = Buffer.alloc(0); this.buffer = Buffer.alloc(0);
@ -60,7 +67,8 @@ module.exports.Writer = class {
writeLong(data = 0) { writeLong(data = 0) {
const buff = Buffer.alloc(8); const buff = Buffer.alloc(8);
buff.writeBigInt64BE(BigInt(data), 0); if (data instanceof BigInt) buff.writeBigInt64BE(data, 0);
else buff.writeBigInt64BE(BigInt(data), 0);
this.writeBuffer(buff); this.writeBuffer(buff);
} }

View file

@ -11,6 +11,7 @@ let idPool = 1;
global.fromIDPool = function() { global.fromIDPool = function() {
const oldVal = idPool; const oldVal = idPool;
idPool++; idPool++;
console.log(idPool);
return oldVal; return oldVal;
} }
@ -52,6 +53,7 @@ module.exports.init = function(config) {
if (tickCounter % tickRate == 0) { if (tickCounter % tickRate == 0) {
for (let key of netUserKeys) { for (let key of netUserKeys) {
netUsers[key].socket.write(new PacketMappingTable[NamedPackets.KeepAlive]().writePacket()); netUsers[key].socket.write(new PacketMappingTable[NamedPackets.KeepAlive]().writePacket());
} }
} }
// Update Chunks // Update Chunks
@ -82,20 +84,22 @@ module.exports.init = function(config) {
// Update users // Update users
for (let key of netUserKeys) { for (let key of netUserKeys) {
const user = netUsers[netUserKeys]; const user = netUsers[key];
let itemsToRemove = []; if (user.loginFinished) {
for (let i = 0; i < Math.min(user.chunksToSend.getLength(), 128); i++) { let itemsToRemove = [];
const chunkKey = user.chunksToSend.itemKeys[i]; for (let i = 0; i < Math.min(user.chunksToSend.getLength(), 128); i++) {
itemsToRemove.push(chunkKey); const chunkKey = user.chunksToSend.itemKeys[i];
user.socket.write(user.chunksToSend.items[chunkKey]); itemsToRemove.push(chunkKey);
user.socket.write(user.chunksToSend.items[chunkKey]);
}
for (let item of itemsToRemove) {
user.chunksToSend.remove(item, false);
}
user.chunksToSend.regenerateIterableArray();
} }
for (let item of itemsToRemove) {
user.chunksToSend.remove(item, false);
}
user.chunksToSend.regenerateIterableArray();
} }
tickCounter++; tickCounter++;
@ -108,7 +112,9 @@ module.exports.connection = async function(socket = new Socket) {
socket.on('data', function(chunk) { socket.on('data', function(chunk) {
const reader = new bufferStuff.Reader(chunk); const reader = new bufferStuff.Reader(chunk);
switch(reader.readByte()) { const packetID = reader.readByte();
switch(packetID) {
case NamedPackets.KeepAlive: case NamedPackets.KeepAlive:
socket.write(new PacketMappingTable[NamedPackets.KeepAlive]().writePacket()); socket.write(new PacketMappingTable[NamedPackets.KeepAlive]().writePacket());
break; break;
@ -151,6 +157,14 @@ module.exports.connection = async function(socket = new Socket) {
socket.write(new PacketMappingTable[NamedPackets.Handshake](thisUser.username).writePacket()); socket.write(new PacketMappingTable[NamedPackets.Handshake](thisUser.username).writePacket());
break; break;
case NamedPackets.Player:
break;
default:
console.log("0x" + packetID.toString(16));
break;
} }
}); });