Add Item and Vector type implementations to MetadataWriter

This commit is contained in:
Holly Stubbs 2024-02-16 09:34:02 +00:00
parent 9ae490bba3
commit 61586092d1
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 25 additions and 2 deletions

View file

@ -1,12 +1,14 @@
import { createWriter, Endian } from "bufferstuff"; import { createWriter, Endian } from "bufferstuff";
import { FunkyArray } from "../funkyArray"; import { FunkyArray } from "../funkyArray";
import { MetadataFieldType } from "./enums/MetadataFieldType"; import { MetadataFieldType } from "./enums/MetadataFieldType";
import Vec3 from "./Vec3";
import { ItemStack } from "./inventories/ItemStack";
export class MetadataEntry { export class MetadataEntry {
public type:MetadataFieldType; public type:MetadataFieldType;
public value:number|string; public value:number|string|Vec3|ItemStack;
public constructor(type:MetadataFieldType, value:number|string) { public constructor(type:MetadataFieldType, value:number|string|Vec3|ItemStack) {
this.type = type; this.type = type;
this.value = value; this.value = value;
} }
@ -38,6 +40,18 @@ export class MetadataWriter {
} else { } else {
throw "Non-string value assigned to a String MetadataEntry"; throw "Non-string value assigned to a String MetadataEntry";
} }
case MetadataFieldType.Item:
if (entry.value instanceof ItemStack) {
size += 5;
} else {
throw "Non-ItemStack value assigned to an ItemStack MetadataEntry";
}
case MetadataFieldType.Vector:
if (entry.value instanceof Vec3) {
size += 12;
} else {
throw "Non-Vec3 value assigned to an Vec3 MetadataEntry";
}
} }
}) })
@ -59,6 +73,11 @@ export class MetadataWriter {
} }
} else if (typeof(entry.value) === "string") { } else if (typeof(entry.value) === "string") {
writer.writeString16(entry.value); writer.writeString16(entry.value);
} else if (entry.value instanceof ItemStack) {
writer.writeShort(entry.value.itemID).writeByte(entry.value.size).writeShort(entry.value.damage);
} else if (entry.value instanceof Vec3) {
const absVec = entry.value.toAbs();
writer.writeInt(absVec.x).writeInt(absVec.y).writeInt(absVec.z);
} }
} }
} }

View file

@ -68,4 +68,8 @@ export default class Vec3 {
throw new Error(`Invalid arguments for Vec3.mult : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`); throw new Error(`Invalid arguments for Vec3.mult : ${typeof(x)}, ${typeof(y)}, ${typeof(z)}`);
} }
} }
toAbs() {
return new Vec3(Math.round(this.x * 32), Math.round(this.y * 32), Math.round(this.z * 32));
}
} }