Send player equipment on join
This commit is contained in:
parent
6d7d1299ce
commit
23a1d124c0
2 changed files with 64 additions and 1 deletions
|
@ -171,6 +171,9 @@ export class MinecraftServer {
|
||||||
client.send(timePacket);
|
client.send(timePacket);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const memoryUsage = process.memoryUsage();
|
||||||
|
// console.log(`Memory Usage: ${(memoryUsage.heapUsed / 1024 / 1024).toFixed(1)}MB / ${(memoryUsage.heapTotal / 1024 / 1024).toFixed(1)}MB ArrayBuffers: ${(memoryUsage.arrayBuffers / 1024 / 1024).toFixed(1)}MB`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.worlds.forEach(world => {
|
this.worlds.forEach(world => {
|
||||||
|
@ -228,8 +231,13 @@ export class MinecraftServer {
|
||||||
const thisPlayerSpawn = new PacketNamedEntitySpawn(clientEntity.entityId, clientEntity.username, clientEntity.absPosition.x, clientEntity.absPosition.y, clientEntity.absPosition.z, clientEntity.absRotation.yaw, clientEntity.absRotation.pitch, clientEntity.mpClient?.getHeldItemStack()?.itemID).writeData();
|
const thisPlayerSpawn = new PacketNamedEntitySpawn(clientEntity.entityId, clientEntity.username, clientEntity.absPosition.x, clientEntity.absPosition.y, clientEntity.absPosition.z, clientEntity.absRotation.yaw, clientEntity.absRotation.pitch, clientEntity.mpClient?.getHeldItemStack()?.itemID).writeData();
|
||||||
world.players.forEach(player => {
|
world.players.forEach(player => {
|
||||||
if (player.entityId !== clientEntity.entityId && clientEntity.distanceTo(player) < World.ENTITY_MAX_SEND_DISTANCE) {
|
if (player.entityId !== clientEntity.entityId && clientEntity.distanceTo(player) < World.ENTITY_MAX_SEND_DISTANCE) {
|
||||||
|
// Inform the joining player of the players around them
|
||||||
socket.write(new PacketNamedEntitySpawn(player.entityId, player.username, player.absPosition.x, player.absPosition.y, player.absPosition.z, player.absRotation.yaw, player.absRotation.pitch, player.mpClient?.getHeldItemStack()?.itemID).writeData());
|
socket.write(new PacketNamedEntitySpawn(player.entityId, player.username, player.absPosition.x, player.absPosition.y, player.absPosition.z, player.absRotation.yaw, player.absRotation.pitch, player.mpClient?.getHeldItemStack()?.itemID).writeData());
|
||||||
|
player.sendPlayerEquipment(clientEntity);
|
||||||
|
|
||||||
|
// Inform players around the joining player of the joined player
|
||||||
player.mpClient?.send(thisPlayerSpawn);
|
player.mpClient?.send(thisPlayerSpawn);
|
||||||
|
clientEntity.sendPlayerEquipment(player);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import { ItemStack } from "../inventories/ItemStack";
|
||||||
import { Block } from "../blocks/Block";
|
import { Block } from "../blocks/Block";
|
||||||
import PlayerInventory from "../inventories/PlayerInventory";
|
import PlayerInventory from "../inventories/PlayerInventory";
|
||||||
import { Item } from "../items/Item";
|
import { Item } from "../items/Item";
|
||||||
|
import { PacketEntityEquipment } from "../packets/EntityEquipment";
|
||||||
|
|
||||||
const CHUNK_LOAD_RANGE = 15;
|
const CHUNK_LOAD_RANGE = 15;
|
||||||
|
|
||||||
|
@ -23,6 +24,8 @@ export class Player extends EntityLiving {
|
||||||
public mpClient?:MPClient;
|
public mpClient?:MPClient;
|
||||||
public inventory:PlayerInventory;
|
public inventory:PlayerInventory;
|
||||||
|
|
||||||
|
public trackedEquipment:Array<ItemStack | null>;
|
||||||
|
|
||||||
public constructor(server:MinecraftServer, world:World, username:string) {
|
public constructor(server:MinecraftServer, world:World, username:string) {
|
||||||
super(world);
|
super(world);
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
@ -38,10 +41,16 @@ export class Player extends EntityLiving {
|
||||||
this.inventory.setSlotItemStack(39, new ItemStack(Item.ironAxe, 1));
|
this.inventory.setSlotItemStack(39, new ItemStack(Item.ironAxe, 1));
|
||||||
this.inventory.setSlotItemStack(43, new ItemStack(Block.dirt, 32));
|
this.inventory.setSlotItemStack(43, new ItemStack(Block.dirt, 32));
|
||||||
|
|
||||||
|
this.trackedEquipment = new Array<ItemStack | null>();
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
this.trackedEquipment.push(null);
|
||||||
|
}
|
||||||
|
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.position.set(8, 64, 8);
|
this.position.set(8, 64, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forces a player chunk update *next tick*
|
||||||
public forceUpdatePlayerChunks() {
|
public forceUpdatePlayerChunks() {
|
||||||
this.firstUpdate = true;
|
this.firstUpdate = true;
|
||||||
}
|
}
|
||||||
|
@ -90,14 +99,60 @@ export class Player extends EntityLiving {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getEquipmentForVirtualSlot(slot:number) {
|
||||||
|
if (slot === 0) {
|
||||||
|
return this.mpClient?.getHeldItemStack() ?? null;
|
||||||
|
} else {
|
||||||
|
this.inventory.getSlotItemStack(4 + slot); // 5 - 8
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendEquipment(equipmentId:number, itemStack:ItemStack | null) {
|
||||||
|
this.sendToNearby(new PacketEntityEquipment(this.entityId, equipmentId, itemStack == null ? -1 : itemStack.itemID, itemStack == null ? 0 : itemStack.damage).writeData());
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendEquipmentPlayer(mpClient:MPClient, equipmentId:number, itemStack:ItemStack | null) {
|
||||||
|
mpClient.send(new PacketEntityEquipment(this.entityId, equipmentId, itemStack == null ? -1 : itemStack.itemID, itemStack == null ? 0 : itemStack.damage).writeData());
|
||||||
|
}
|
||||||
|
|
||||||
|
// For login.
|
||||||
|
public sendPlayerEquipment(playerToSendTo:Player) {
|
||||||
|
const mpClient = playerToSendTo.mpClient;
|
||||||
|
if (mpClient == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let slotId = 0; slotId < 5; slotId++) {
|
||||||
|
const itemStack = this.getEquipmentForVirtualSlot(slotId);
|
||||||
|
const trackedEquipment = this.trackedEquipment[slotId];
|
||||||
|
|
||||||
|
if ((itemStack == null || trackedEquipment == null) || !itemStack.compare(trackedEquipment)) {
|
||||||
|
this.trackedEquipment[slotId] = itemStack;
|
||||||
|
this.sendEquipmentPlayer(mpClient, slotId, itemStack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public onTick() {
|
public onTick() {
|
||||||
this.updatePlayerChunks();
|
this.updatePlayerChunks();
|
||||||
|
|
||||||
// Calculate player motion
|
// Calculate player motion since we don't have it serverside.
|
||||||
this.motion.set(this.position.x - this.lastPosition.x, this.position.y - this.lastPosition.y, this.position.z - this.lastPosition.z);
|
this.motion.set(this.position.x - this.lastPosition.x, this.position.y - this.lastPosition.y, this.position.z - this.lastPosition.z);
|
||||||
|
|
||||||
super.onTick();
|
super.onTick();
|
||||||
|
|
||||||
|
for (let slotId = 0; slotId < 5; slotId++) {
|
||||||
|
const itemStack = this.getEquipmentForVirtualSlot(slotId);
|
||||||
|
const trackedEquipment = this.trackedEquipment[slotId];
|
||||||
|
|
||||||
|
if ((itemStack == null || trackedEquipment == null) || !itemStack.compare(trackedEquipment)) {
|
||||||
|
this.trackedEquipment[slotId] = itemStack;
|
||||||
|
this.sendEquipment(slotId, itemStack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.health != this.lastHealth) {
|
if (this.health != this.lastHealth) {
|
||||||
this.lastHealth = this.health;
|
this.lastHealth = this.health;
|
||||||
this.mpClient?.send(new PacketUpdateHealth(this.health).writeData());
|
this.mpClient?.send(new PacketUpdateHealth(this.health).writeData());
|
||||||
|
|
Loading…
Reference in a new issue