This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/src/main/resources/scriptcraft/minecraft/block.ts

158 lines
4.1 KiB
TypeScript

import { World } from './world';
import { PlayerEntity } from './entity';
import { useBridge, addBridge, Identifier, Hand, Pos, ActionResult, Direction, SingleRegistry } from './core';
/**
* Settings for {@link CustomBlock}
*/
export class BlockSettings {
readonly #material: string;
readonly #materialColor: string;
#resistance: number;
#hardness: number;
/**
* Create Block Settings
* @param material Material
* @param materialColor Material Color
*/
constructor(material: string);
constructor(material: string, materialColor: string);
constructor(material: string, materialColor?: string) {
this.#material = material;
this.#materialColor = materialColor;
this.#resistance = 0;
this.#hardness = 0;
}
/**
* Set Block Strength
* @param hardness Hardness Value
* @param resistance Blast Resistance Value
*/
setStrength(hardness: number, resistance: number): BlockSettings {
this.#hardness = hardness;
this.#resistance = resistance;
return this;
}
/**
* Set Hardness
* @param hardness Hardness Value
*/
setHardness(hardness: number): BlockSettings {
this.#hardness = hardness;
return this;
}
/**
* Set Blast Resistance
* @param resistance Blast Resistance Value
*/
setResistance(resistance: number): BlockSettings {
this.#resistance = resistance;
return this;
}
/**
* @internal
*/
createJavaObject(): JavaObject {
return useBridge('BlockSettings.create', this.#material, this.#materialColor, this.#hardness, this.#resistance) as JavaObject;
}
}
/**
* Custom Block
*/
export abstract class CustomBlock {
/**
* @internal
*/
settings: BlockSettings;
constructor(settings: BlockSettings) {
this.settings = settings;
}
/**
* Called When The Block Is Used
* @param world World
* @param blockState Block State
* @param pos Block Position
* @param side Side Of The Block Used
* @param player Player
* @param hand Hand
* @returns Action Result
*/
abstract onUse(world: World, blockState: BlockState, pos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult;
}
/**
* Block State
*/
export class BlockState {
/**
* @internal
*/
javaObject: JavaObject;
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Default Block State for Block
* @param block Block ID
* @returns Default Block State
*/
static getDefaultState(block: Identifier): BlockState {
const obj = useBridge('BlockState.getDefaultState', block.toString()) as JavaObject;
if (obj) {
return new BlockState(obj);
} else {
return null;
}
}
/**
* Get Block ID
* @returns Block ID
*/
getBlock(): Identifier {
const obj = useBridge('BlockState.getBlock', this.javaObject) as string;
if (obj) {
return new Identifier(obj);
} else {
return null;
}
}
}
/**
* @internal
*/
export class BlockRegistry implements SingleRegistry<CustomBlock> {
static INSTANCE = new BlockRegistry();
#blocks: Map<string, CustomBlock>;
private constructor() {
this.#blocks = new Map<string, CustomBlock>();
}
register(id: Identifier, obj: CustomBlock) {
this.#blocks.set(id.toString(), obj);
useBridge('Registry.registerBlock', id.toString(), obj.settings.createJavaObject());
}
get(id: Identifier): CustomBlock {
return this.#blocks.get(id.toString());
}
}
addBridge('CustomBlock.onUse', (id: string, world: JavaObject, state: JavaObject, x: number, y: number, z: number, side: keyof typeof Direction, player: JavaObject, hand: keyof typeof Hand): string => {
return BlockRegistry.INSTANCE.get(new Identifier(id)).onUse(new World(world), new BlockState(state), new Pos(x, y, z), Direction[side], new PlayerEntity(player), Hand[hand]);
});