Did some minor changes. Added ChatColor class. Made PacketMapChunk way faster by pregenerating example chunk instead of generating it on each 'writeData()' call. I'll most likely will do some more changes soon? Or i'll forget about it for 5 months again eh.

This commit is contained in:
MrMasrozYTLIVE 2024-05-06 20:12:05 +03:00
parent 251b016169
commit 7bfc95e0bb
7 changed files with 49 additions and 23 deletions

View File

@ -2,7 +2,6 @@ import { PacketManager } from "./utils/PacketManager";
import {PlayerManager} from "./utils/PlayerManager";
import {Server} from "node:net"; // TODO: Replace with https://bun.sh/docs/api/tcp
import {PacketDisconnectKick} from "./packet/impl/player/PacketDisconnectKick";
import {Player} from "./Player";
export class MinecraftServer {
public server: Server = new Server();
@ -10,6 +9,7 @@ export class MinecraftServer {
public static PlayerManagers = new Map<String, PlayerManager>();
// EntityMap = new Map<Number, IEntity>();
public static MAX_PLAYERS = 20;
public async start() {
this.server.listen(25565, async () => {

View File

@ -12,7 +12,7 @@ export class Player extends Entity {
public xPosition: number = 0;
public prevXPosition: number = this.xPosition;
public yPosition: number = 100;
public yPosition: number = 140;
public prevYPosition: number = this.yPosition;
public zPosition: number = 0;
public prevZPosition: number = this.zPosition;

View File

@ -9,6 +9,8 @@ import {PacketPreChunk} from "../world/PacketPreChunk";
import {PacketMapChunk} from "../world/PacketMapChunk";
import {PacketPosition} from "../player/PacketPosition";
import {PacketPositionLook} from "../player/PacketPositionLook";
import {MinecraftServer} from "../../../MinecraftServer";
import {ChatColor} from "../../../utils/ChatColor";
export class PacketLogin extends Packet {
constructor() {
@ -19,15 +21,17 @@ export class PacketLogin extends Packet {
readData(reader: IReader, player: Player) {
const protocol = reader.readInt();
if(protocol > 14) player.playerManager.kickPlayer(`Server is outdated!`);
else if (protocol < 14) player.playerManager.kickPlayer(`Client is outdated!`);
if(protocol > 14) return player.playerManager.kickPlayer(`Server is outdated!`);
else if (protocol < 14) return player.playerManager.kickPlayer(`Client is outdated!`);
if(MinecraftServer.PlayerManagers.size > MinecraftServer.MAX_PLAYERS) return player.playerManager.kickPlayer(`Server is full!`);
const username = reader.readString16();
const seed = reader.readLong();
const dimension = reader.readByte();
player.playerManager.sendPacket(this);
player.playerManager.sendPacket(new PacketPositionLook());
PlayerManager.sendPacketToAll(new PacketChat(`§e<${username}> has joined the game.`));
PlayerManager.sendPacketToAll(new PacketChat(`${ChatColor.YELLOW}<${username}> has joined the game.`));
player.playerManager.sendPacket(new PacketPreChunk());
player.playerManager.sendPacket(new PacketMapChunk());

View File

@ -3,6 +3,7 @@ import {PacketEnum} from "../../../utils/PacketEnum";
import {createWriter, Endian, IReader} from "bufferstuff";
import {Player} from "../../../Player";
import {PlayerManager} from "../../../utils/PlayerManager";
import {MinecraftServer} from "../../../MinecraftServer";
export class PacketServerList extends Packet {
constructor() {
@ -12,7 +13,7 @@ export class PacketServerList extends Packet {
}
readData(reader: IReader, player: Player) {
player.playerManager.kickPlayer(`Beta 1.7.3 Server§0§0`);
player.playerManager.kickPlayer(`Beta 1.7.3 Server§${MinecraftServer.PlayerManagers.size}§${MinecraftServer.MAX_PLAYERS}`);
}
writeData() {

View File

@ -6,30 +6,32 @@ import {deflate} from "node:zlib";
import * as zlib from "zlib";
export class PacketMapChunk extends Packet {
public world: Buffer;
constructor() {
super({
packetID: PacketEnum.MapChunk
})
const chunk = createWriter(Endian.BE);
for(let x = 0; x < 256; x++) {
for(let i = 0; i < 90; i++) {
chunk.writeByte(1);
}
for(let i = 0; i < 38; i++) {
chunk.writeByte(0);
}
}
this.world = zlib.deflateSync(chunk.toBuffer());
}
readData(reader: IReader, player: Player) {
}
writeData() {
const world = createWriter(Endian.BE);
for(let x = 0; x < 256; x++) {
for(let i = 0; i < 10; i++) {
world.writeByte(1);
}
for(let i = 0; i < 118; i++) {
world.writeByte(0);
}
}
const buf = zlib.deflateSync(world.toBuffer());
return createWriter(Endian.BE).writeUByte(this.options.packetID)
.writeInt(0)
.writeShort(0)
@ -37,8 +39,8 @@ export class PacketMapChunk extends Packet {
.writeByte(15)
.writeByte(127)
.writeByte(15)
.writeInt(buf.length)
.writeBuffer(buf)
.writeInt(this.world.length)
.writeBuffer(this.world)
.toBuffer();
}
}

18
src/utils/ChatColor.ts Normal file
View File

@ -0,0 +1,18 @@
export class ChatColor {
public static DARK_RED: string = "§4";
public static RED: string = "§c";
public static GOLD: string = "§6";
public static YELLOW: string = "§e";
public static DARK_GREEN: string = "§2";
public static GREEN: string = "§a";
public static AQUA: string = "§b";
public static DARK_AQUA: string = "§3";
public static DARK_BLUE: string = "§1";
public static BLUE: string = "§9";
public static LIGHT_PURPLE: string = "§d";
public static DARK_PURPLE: string = "§5";
public static WHITE: string = "§f";
public static GRAY: string = "§7";
public static DARK_GRAY: string = "§8";
public static BLACK: string = "§0";
}

View File

@ -8,6 +8,7 @@ import {PacketManager} from "./PacketManager";
import {MinecraftServer} from "../MinecraftServer";
import {PacketEnum} from "./PacketEnum";
import {PacketHandshake} from "../packet/impl/login/PacketHandshake";
import {ChatColor} from "./ChatColor";
export class PlayerManager {
public player: Player;
@ -71,7 +72,7 @@ export class PlayerManager {
if(MinecraftServer.debug) console.log(`Player ${this.username} left. Deleting from the map!`);
MinecraftServer.PlayerManagers.delete(this.username);
PlayerManager.sendPacketToAll(new PacketChat(`§e<${this.username}> left the game.`));
PlayerManager.sendPacketToAll(new PacketChat(`${ChatColor.YELLOW}<${this.username}> left the game.`));
}
public static getPlayer(username: String) {