mc-beta-server/server/entities/Player.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-04-09 04:19:10 +01:00
import { FunkyArray } from "../../funkyArray";
import { Chunk } from "../Chunk";
import { MPClient } from "../MPClient";
2023-04-08 20:52:47 +01:00
import { MinecraftServer } from "../MinecraftServer";
import { World } from "../World";
2023-04-09 04:19:10 +01:00
import { PacketMapChunk } from "../packets/MapChunk";
import { EntityLiving } from "./EntityLiving";
2023-04-08 20:52:47 +01:00
import { Entity } from "./Entity";
2023-04-09 04:19:10 +01:00
import { Socket } from "net";
import { PacketPreChunk } from "../packets/PreChunk";
2023-04-08 20:52:47 +01:00
2023-04-09 04:19:10 +01:00
export class Player extends EntityLiving {
2023-04-08 20:52:47 +01:00
public username:string;
private server:MinecraftServer;
2023-04-09 04:19:10 +01:00
private firstUpdate:boolean;
public loadedChunks:Array<number>;
public justUnloaded:Array<number>;
public mpClient?:MPClient;
2023-04-08 20:52:47 +01:00
public constructor(server:MinecraftServer, world:World, username:string) {
super(world);
this.server = server;
2023-04-09 04:19:10 +01:00
this.firstUpdate = true;
this.loadedChunks = new Array<number>();
this.justUnloaded = new Array<number>();
2023-04-08 20:52:47 +01:00
this.username = username;
this.x = 8;
this.y = 64;
this.z = 8;
}
2023-04-09 04:19:10 +01:00
onTick() {
const bitX = this.x >> 4;
const bitZ = this.z >> 4;
if (bitX != this.lastX >> 4 || bitZ != this.lastZ >> 4 || this.firstUpdate) {
if (this.firstUpdate) {
this.firstUpdate = false;
this.mpClient?.send(new PacketPreChunk(0, 0, true).writeData());
const chunk = this.world.getChunk(0, 0);
(async () => {
const chunkData = await (new PacketMapChunk(0, 0, 0, 15, 127, 15, chunk).writeData());
this.mpClient?.send(chunkData);
})();
}
// Load or keep any chunks we need
const currentLoads = [];
for (let x = bitX - 6; x < bitX + 6; x++) {
for (let z = bitZ - 6; z < bitZ + 6; z++) {
const coordPair = Chunk.CreateCoordPair(x, z);
if (!this.loadedChunks.includes(coordPair)) {
const chunk = this.world.getChunk(x, z);
this.mpClient?.send(new PacketPreChunk(x, z, true).writeData());
this.loadedChunks.push(coordPair);
chunk.playersInChunk.set(this.entityId, this);
(async () => {
const chunkData = await (new PacketMapChunk(x, 0, z, 15, 127, 15, chunk).writeData());
this.mpClient?.send(chunkData);
})();
}
currentLoads.push(coordPair);
}
}
// Mark any unaccounted chunks for unload
for (let coordPair of this.loadedChunks) {
if (!currentLoads.includes(coordPair)) {
this.justUnloaded.push(coordPair);
const chunkToUnload = this.world.getChunkByCoordPair(coordPair);
this.mpClient?.send(new PacketPreChunk(chunkToUnload.x, chunkToUnload.z, false).writeData());
}
}
// Overwrite loaded chunks
this.loadedChunks = currentLoads;
}
super.onTick();
}
2023-04-08 20:52:47 +01:00
}