244 lines
6.0 KiB
TypeScript
244 lines
6.0 KiB
TypeScript
import { useBridge, Identifier, Hand, ActionResult, addBridge, Pos, Direction, SingleRegistry } from './core';
|
|
import { World } from './world';
|
|
import { PlayerEntity, LivingEntity } from './entity';
|
|
import { CompoundTag } from './tag';
|
|
|
|
/**
|
|
* Item Stack
|
|
*/
|
|
export class ItemStack {
|
|
/**
|
|
* @internal
|
|
*/
|
|
readonly 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;
|
|
}
|
|
|
|
/**
|
|
* Get Item Tag
|
|
* @returns Item Tag
|
|
*/
|
|
getTag(): CompoundTag {
|
|
const obj = useBridge('ItemStack.getTag', this.javaObject) as JavaObject;
|
|
if (obj) {
|
|
return new CompoundTag(obj);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set Item Tag
|
|
* @param tag New Item Tag
|
|
*/
|
|
setTag(tag: CompoundTag) {
|
|
useBridge('ItemStack.setTag', this.javaObject, tag.javaObject);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
});
|