From 5a250cb601eafe4a0e31325b7b6c201b611ecba6 Mon Sep 17 00:00:00 2001 From: Holly Date: Thu, 9 Nov 2023 17:04:11 +0000 Subject: [PATCH] WIP: Player Saving --- server/entities/Entity.ts | 23 +++++++++++++++++++---- server/entities/IEntity.ts | 2 +- server/inventories/IInventory.ts | 3 +++ server/inventories/Inventory.ts | 21 ++++++++++++++++++++- server/inventories/ItemStack.ts | 16 ++++++++++++++++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/server/entities/Entity.ts b/server/entities/Entity.ts index a3beae4..70247c8 100644 --- a/server/entities/Entity.ts +++ b/server/entities/Entity.ts @@ -1,3 +1,4 @@ +import { IReader, IWriter } from "bufferstuff"; import AABB from "../AABB"; import { Chunk } from "../Chunk"; import { MetadataEntry, MetadataWriter } from "../MetadataWriter"; @@ -37,8 +38,6 @@ export class Entity implements IEntity { public absRotation:Rotation; public lastAbsRotation:Rotation; - public velocity:Vec3; - public health:number; public wasHurt:boolean; public isDead:boolean; @@ -85,8 +84,6 @@ export class Entity implements IEntity { this.absRotation = new Rotation(); this.lastAbsRotation = new Rotation(); - this.velocity = new Vec3(); - this.crouching = this.lastCrouchState = this.lastFireState = this.queuedChunkUpdate = false; this.chunk = world.getChunk(this.position.x >> 4, this.position.z >> 4); @@ -96,6 +93,24 @@ export class Entity implements IEntity { this.isDead = false; } + public fromSave(reader:IReader) { + this.position.set(reader.readDouble(), reader.readDouble(), reader.readDouble()); + this.motion.set(reader.readFloat(), reader.readFloat(), reader.readFloat()); + this.rotation.set(reader.readFloat(), reader.readFloat()); + this.fire = reader.readShort(); + this.fallDistance = reader.readFloat(); + this.health = reader.readByte(); + } + + public toSave(writer:IWriter) { + writer.writeDouble(this.position.x).writeDouble(this.position.y).writeDouble(this.position.z) // Position + .writeDouble(this.motion.x).writeDouble(this.motion.y).writeDouble(this.motion.z) // Motion + .writeFloat(this.rotation.x).writeFloat(this.rotation.y) // Rotation + .writeShort(this.fire) + .writeFloat(this.fallDistance) + .writeByte(this.health); + } + sendToNearby(buffer:Buffer) { this.world.sendToNearbyClients(this, buffer); } diff --git a/server/entities/IEntity.ts b/server/entities/IEntity.ts index 621bca1..f1a2788 100644 --- a/server/entities/IEntity.ts +++ b/server/entities/IEntity.ts @@ -3,8 +3,8 @@ import Vec3 from "../Vec3" export interface IEntity { entityId:number, position:Vec3, + motion:Vec3, lastPosition:Vec3, - velocity:Vec3, crouching:boolean, updateMetadata:() => void, distanceTo:(entity:IEntity) => number, diff --git a/server/inventories/IInventory.ts b/server/inventories/IInventory.ts index 58013d5..95ce96f 100644 --- a/server/inventories/IInventory.ts +++ b/server/inventories/IInventory.ts @@ -1,6 +1,9 @@ +import { IReader, IWriter } from "bufferstuff"; import { ItemStack } from "./ItemStack"; export default interface IInventory { + fromSave:(reader:IReader) => void, + toSave:(writer:IWriter) => void, getInventoryName:() => string, getInventorySize:() => number, getSlotItemStack:(slotId:number) => ItemStack | null diff --git a/server/inventories/Inventory.ts b/server/inventories/Inventory.ts index 0f1a1dd..1cdd86c 100644 --- a/server/inventories/Inventory.ts +++ b/server/inventories/Inventory.ts @@ -1,4 +1,4 @@ -import { Endian, createWriter } from "bufferstuff"; +import { Endian, IReader, IWriter, createWriter } from "bufferstuff"; import { ItemStack } from "./ItemStack"; import IInventory from "./IInventory"; @@ -18,6 +18,25 @@ export class Inventory implements IInventory { this.name = name; } + public fromSave(reader:IReader) { + const inventorySize = reader.readByte(); + for (let i = 0; i < inventorySize; i++) { + this.itemStacks[i] = ItemStack.FromSave(reader); + } + } + + public toSave(writer:IWriter) { + writer.writeByte(this.size); + for (const itemStack of this.itemStacks) { + if (itemStack === null) { + writer.writeShort(-1); + continue; + } + + itemStack.toSave(writer); + } + } + addItemStack(itemStack:ItemStack) { // Check bottom inventory row (hotbar) first. /*let workingItemStack:ItemStack | null; diff --git a/server/inventories/ItemStack.ts b/server/inventories/ItemStack.ts index c417cb5..f698a39 100644 --- a/server/inventories/ItemStack.ts +++ b/server/inventories/ItemStack.ts @@ -1,3 +1,4 @@ +import { IReader, IWriter } from "bufferstuff"; import { Block } from "../blocks/Block"; import { IEntity } from "../entities/IEntity"; import { Player } from "../entities/Player"; @@ -65,6 +66,21 @@ export class ItemStack { this.itemStackId = ItemStack.ITEMSTACK_ID_ADDER++; } + public static FromSave(reader:IReader) { + const itemId = reader.readShort(); + if (itemId === -1) { + return null; + } + + return new ItemStack(itemId, reader.readByte(), reader.readShort()); + } + + public toSave(writer:IWriter) { + writer.writeShort(this.itemID) + .writeByte(this.size) + .writeShort(this.damage); + } + public static Compare(itemStack1:ItemStack, itemStack2:ItemStack) { return itemStack1.itemStackId === itemStack2.itemStackId; }