253 lines
5.6 KiB
TypeScript
253 lines
5.6 KiB
TypeScript
import { ItemStack } from './item';
|
|
import { Hand, Identifier, Pos } from './core';
|
|
import { World } from './world';
|
|
import { CompoundTag } from './tag';
|
|
import { useBridge } from 'scriptcraft-core';
|
|
|
|
/**
|
|
* Damage Source
|
|
*/
|
|
export class DamageSource {
|
|
/**
|
|
* @internal
|
|
*/
|
|
readonly javaObject: JavaObject;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
constructor(javaObject: JavaObject) {
|
|
this.javaObject = javaObject;
|
|
}
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
private static build(bridge: string, value: BridgeValueType): DamageSource {
|
|
const obj = useBridge(bridge, value) as JavaObject;
|
|
if (obj !== null) {
|
|
return new DamageSource(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create Damage Source from a name
|
|
* @param value Name
|
|
* @returns Damage Source
|
|
*/
|
|
static create(value: string): DamageSource {
|
|
return this.build('DamageSource.create', value);
|
|
}
|
|
|
|
/**
|
|
* Create Damage Source from a player ({@link PlayerEntity})
|
|
* @param value Player
|
|
* @returns Damage Source
|
|
*/
|
|
static createFromPlayer(value: PlayerEntity): DamageSource {
|
|
return this.build('DamageSource.createFromPlayer', value.javaObject);
|
|
}
|
|
|
|
/**
|
|
* Create Damage Source from a mob ({@link LivingEntity})
|
|
* @param value Mob
|
|
* @returns Damage Source
|
|
*/
|
|
static createFromMob(value: LivingEntity): DamageSource {
|
|
return this.build('DamageSource.createFromMob', value.javaObject);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Entity
|
|
*/
|
|
export class Entity {
|
|
/**
|
|
* @internal
|
|
*/
|
|
readonly javaObject: JavaObject;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
constructor(object: JavaObject | Entity) {
|
|
if (object instanceof Entity) {
|
|
this.javaObject = object.javaObject;
|
|
} else {
|
|
this.javaObject = object;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Entity World
|
|
* @returns Entity World
|
|
*/
|
|
getEntityWorld(): World {
|
|
const obj = useBridge('Entity.getEntityWorld', this.javaObject) as JavaObject;
|
|
if (obj !== null) {
|
|
return new World(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Entity ID
|
|
* @returns Entity ID
|
|
*/
|
|
getID(): Identifier {
|
|
const obj = useBridge('Entity.getID', this.javaObject) as string;
|
|
if (obj !== null) {
|
|
return new Identifier(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Entity Name
|
|
* @returns Entity Name
|
|
*/
|
|
getName(): string {
|
|
return useBridge('Entity.getName', this.javaObject) as string;
|
|
}
|
|
|
|
/**
|
|
* Get Entity Display Name
|
|
* @returns Entity Display Name
|
|
*/
|
|
getDisplayName(): string {
|
|
return useBridge('Entity.getDisplayName', this.javaObject) as string;
|
|
}
|
|
|
|
/**
|
|
* Get Entity Custom Name
|
|
* @returns Entity Custom Name
|
|
*/
|
|
getCustomName(): string {
|
|
return useBridge('Entity.getCustomName', this.javaObject) as string;
|
|
}
|
|
|
|
/**
|
|
* Convert To String
|
|
*/
|
|
toString(): string {
|
|
return this.getName();
|
|
}
|
|
|
|
/**
|
|
* Kill Entity
|
|
*/
|
|
kill(): void {
|
|
useBridge('Entity.kill', this.javaObject);
|
|
}
|
|
|
|
/**
|
|
* Remove Entity
|
|
*/
|
|
remove(): void {
|
|
useBridge('Entity.remove', this.javaObject);
|
|
}
|
|
|
|
/**
|
|
* Set Entity Fire Ticks
|
|
* @param ticks Fire Ticks
|
|
*/
|
|
setFireTicks(ticks: number): void {
|
|
useBridge('Entity.setFireTicks', this.javaObject, ticks);
|
|
}
|
|
|
|
/**
|
|
* Get Entity Fire Ticks
|
|
* @returns Fire Ticks
|
|
*/
|
|
getFireTicks(): number {
|
|
return useBridge('Entity.getFireTicks', this.javaObject) as number;
|
|
}
|
|
|
|
/**
|
|
* Damage Entity
|
|
* @param source Damage Source
|
|
* @param amount Damage Amount
|
|
*/
|
|
damage(source: DamageSource, amount: number): void {
|
|
useBridge('Entity.damage', source.javaObject, amount);
|
|
}
|
|
|
|
/**
|
|
* Get Entity Position
|
|
* @returns Entity Position
|
|
*/
|
|
getPosition(): Pos {
|
|
const pos: number[] = useBridge('Entity.getPosition', this.javaObject) as number[];
|
|
if (pos !== null && pos.length === 3) {
|
|
return new Pos(pos[0], pos[1], pos[2]);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set Entity Position
|
|
* @param pos Entity Position
|
|
*/
|
|
setPosition(pos: Pos): void {
|
|
useBridge('Entity.setPosition', this.javaObject, pos.getX(), pos.getY(), pos.getZ());
|
|
}
|
|
|
|
/**
|
|
* Save Data To {@link CompoundTag}
|
|
* @returns Tag
|
|
*/
|
|
toTag(): CompoundTag {
|
|
const obj = useBridge('Entity.toTag', this.javaObject) as JavaObject;
|
|
if (obj !== null) {
|
|
return new CompoundTag(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load Data From {@link CompoundTag}
|
|
* @param tag Tag
|
|
*/
|
|
fromTag(tag: CompoundTag): void {
|
|
useBridge('Entity.fromTag', this.javaObject, tag.javaObject);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Living Entity
|
|
*/
|
|
export class LivingEntity extends Entity {
|
|
/**
|
|
* Get Stack in {@link Hand}
|
|
* @param hand Hand
|
|
* @returns Item Stack
|
|
*/
|
|
getStackInHand(hand: Hand): ItemStack {
|
|
const obj = useBridge('LivingEntity.getStackInHand', this.javaObject, hand.toString()) as JavaObject;
|
|
if (obj) {
|
|
return new ItemStack(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Health
|
|
* @returns Entity Health
|
|
*/
|
|
getHealth(): number {
|
|
return useBridge('LivingEntity.getHealth', this.javaObject) as number;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Player Entity
|
|
*/
|
|
export class PlayerEntity extends LivingEntity {}
|