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/item.ts

222 lines
5.5 KiB
TypeScript

import { useBridge, Identifier, Hand, ActionResult, addBridge, Pos, Direction, SingleRegistry } from './core';
import { World } from './world';
import { PlayerEntity, LivingEntity } from './entity';
/**
* Item Stack
*/
export class ItemStack {
/**
* @internal
*/
javaObject: JavaObject;
/**
* @internal
*/
constructor(obj: JavaObject) {
this.javaObject = obj;
}
/**
* Create Item Stack
* @param item Item ID
* @param count Item Count
*/
static create(item: Identifier, count?: number): ItemStack {
const obj = useBridge('ItemStack.create', item.toString(), count ? count : 1) as JavaObject;
if (obj) {
return new ItemStack(obj);
} else {
return null;
}
}
/**
* Get Item ID
* @returns Item ID
*/
getItem(): Identifier {
const obj = useBridge('ItemStack.getItem', this.javaObject) as string;
if (obj) {
return new Identifier(obj);
} else {
return null;
}
}
/**
* Get Item Count
* @returns Item Count
*/
getCount(): number {
return useBridge('ItemStack.getCount', this.javaObject) as number;
}
}
/**
* Item Rarity
*/
enum ItemRarity {
COMMON = 'COMMON',
UNCOMMON = 'UNCOMMON',
RARE = 'RARE',
EPIC = 'EPIC'
}
/**
* Settings for {@link CustomItem} and {@link BlockItem}
*/
export class ItemSettings {
#maxCount: number;
#itemGroup: string;
#rarity: ItemRarity;
/**
* Create Item Settings
*/
constructor() {
this.#maxCount = 64;
this.#rarity = ItemRarity.COMMON;
}
/**
* Set Max Count
* @param maxCount Max Count
*/
setMaxCount(maxCount: number): ItemSettings {
this.#maxCount = maxCount;
return this;
}
/**
* Set Item Group
* @param itemGroup Item Group
*/
setItemGroup(itemGroup: string | Identifier): ItemSettings {
this.#itemGroup = itemGroup.toString();
return this;
}
/**
* Set Item Rarity
* @param rarity Item Rarity
*/
setRarity(rarity: ItemRarity): ItemSettings {
this.#rarity = rarity;
return this;
}
/**
* @internal
*/
createJavaObject(): JavaObject {
return useBridge('ItemSettings.create', this.#maxCount, this.#rarity, this.#itemGroup) as JavaObject;
}
}
/**
* Custom Item
*/
export abstract class CustomItem {
/**
* @internal
*/
settings: ItemSettings;
constructor(settings: ItemSettings) {
this.settings = settings;
}
/**
* Called When The Item Is Used
* @param world World
* @param player Player
* @param hand Hand
* @returns Action Result
*/
abstract onUse(world: World, player: PlayerEntity, hand: Hand): ActionResult;
/**
* Called When The Item Is Used On A Block
* @param world World
* @param pos Block Position
* @param side Side Of The Block Used
* @param player Player
* @param hand Hand
* @returns Action Result
*/
abstract onUseOnBlock(world: World, pos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult;
/**
* Called When The Item Is Used On An Entity
* @param player Player
* @param target Target Entity
* @param hand Hand
* @returns Action Result
*/
abstract onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult;
}
/**
* Block Item
*/
export class BlockItem {
/**
* @internal
*/
settings: ItemSettings;
/**
* @internal
*/
block: Identifier;
/**
* Create Block Item
* @param block Block ID
* @param settings Item Settings
*/
constructor(block: Identifier, settings: ItemSettings) {
this.settings = settings;
this.block = block;
}
}
/**
* @internal
*/
export class ItemRegistry implements SingleRegistry<CustomItem | BlockItem> {
static INSTANCE = new ItemRegistry();
#items: Map<string, CustomItem>;
private constructor() {
this.#items = new Map<string, CustomItem>();
}
register(id: Identifier, obj: CustomItem | BlockItem) {
if (obj instanceof CustomItem) {
this.#items.set(id.toString(), obj);
useBridge('Registry.registerItem', id.toString(), obj.settings.createJavaObject());
} else {
useBridge('Registry.registerBlockItem', id.toString(), obj.settings.createJavaObject(), obj.block.toString());
}
}
get(id: Identifier): CustomItem {
return this.#items.get(id.toString());
}
}
addBridge('CustomItem.onUse', (id: string, world: JavaObject, player: JavaObject, hand: keyof typeof Hand): string => {
return ItemRegistry.INSTANCE.get(new Identifier(id)).onUse(new World(world), new PlayerEntity(player), Hand[hand]);
});
addBridge('CustomItem.onUseOnBlock', (id: string, world: JavaObject, x: number, y: number, z: number, side: keyof typeof Direction, player: JavaObject, hand: keyof typeof Hand): string => {
return ItemRegistry.INSTANCE.get(new Identifier(id)).onUseOnBlock(new World(world), new Pos(x, y, z), Direction[side], new PlayerEntity(player), Hand[hand]);
});
addBridge('CustomItem.onUseOnEntity', (id: string, player: JavaObject, target: JavaObject, hand: keyof typeof Hand): boolean => {
return ItemRegistry.INSTANCE.get(new Identifier(id)).onUseOnEntity(new PlayerEntity(player), new LivingEntity(target), Hand[hand]) === ActionResult.SUCCESS ? true : false;
});