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/scriptcraft/src/main/ts/src/minecraft/item.ts

359 lines
9.0 KiB
TypeScript
Raw Normal View History

2020-06-02 17:24:32 +00:00
import { Identifier, Hand, ActionResult, Pos, Direction, SimpleRegistry, JavaObjectWrapper } from './core';
2020-04-25 13:33:17 +00:00
import { World } from './world';
import { PlayerEntity, LivingEntity } from './entity';
2020-04-26 01:17:59 +00:00
import { CompoundTag } from './tag';
2020-04-28 00:15:24 +00:00
import { useBridge, addBridge } from 'scriptcraft-core';
2020-08-26 21:52:17 +00:00
import { Registry } from './registry';
2020-04-25 13:33:17 +00:00
/**
* Item Stack
*/
2020-06-02 17:24:32 +00:00
export class ItemStack extends JavaObjectWrapper {
2020-04-25 13:33:17 +00:00
/**
2020-06-02 17:24:32 +00:00
* Create Item Stack Wrapper From Java Object
* @param object Java Object
2020-04-25 13:33:17 +00:00
*/
2020-06-02 17:24:32 +00:00
constructor(javaObject: JavaObject | JavaObjectWrapper) {
super(javaObject);
this.assertValidJavaObject('ItemStack');
2020-04-25 13:33:17 +00:00
}
/**
* 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;
2020-04-28 14:16:47 +00:00
if (obj !== null) {
2020-04-25 13:33:17 +00:00
return new ItemStack(obj);
} else {
return null;
}
}
/**
* Get Item ID
* @returns Item ID
*/
getItem(): Identifier {
2020-06-02 17:24:32 +00:00
const obj = useBridge('ItemStack.getItem', this.getJavaObject()) as string;
2020-04-28 14:16:47 +00:00
if (obj !== null) {
2020-04-25 13:33:17 +00:00
return new Identifier(obj);
} else {
return null;
}
}
2020-04-26 18:09:31 +00:00
/**
* Get Custom Item
* @returns Custom Item
*/
getCustomItem(): CustomItem {
return ItemRegistry.INSTANCE.get(this.getItem());
}
2020-04-25 13:33:17 +00:00
/**
* Get Item Count
* @returns Item Count
*/
getCount(): number {
2020-06-02 17:24:32 +00:00
return useBridge('ItemStack.getCount', this.getJavaObject()) as number;
2020-04-25 13:33:17 +00:00
}
2020-04-26 01:17:59 +00:00
2020-04-26 18:09:31 +00:00
/**
* Set Item Count
* @param count New Count
*/
2020-04-28 02:30:12 +00:00
setCount(count: number): void {
2020-06-02 17:24:32 +00:00
useBridge('ItemStack.setCount', this.getJavaObject(), count);
2020-04-26 18:09:31 +00:00
}
/**
* Get Item Damage
* @returns Item Damage
*/
getDamage(): number {
2020-06-02 17:24:32 +00:00
return useBridge('ItemStack.getDamage', this.getJavaObject()) as number;
2020-04-26 18:09:31 +00:00
}
/**
* Set Item Damage
* @param count New Damage
*/
2020-04-28 02:30:12 +00:00
setDamage(damage: number): void {
2020-06-02 17:24:32 +00:00
useBridge('ItemStack.setDamage', this.getJavaObject(), damage);
2020-04-26 18:09:31 +00:00
}
/**
* Is Item Damageable
2020-04-27 20:22:28 +00:00
* @returns TRUE If Item Is Damageable, Otherwise FALSE
2020-04-26 18:09:31 +00:00
*/
isDamageable(): boolean {
2020-06-02 17:24:32 +00:00
return useBridge('ItemStack.isDamageable', this.getJavaObject()) as boolean;
2020-04-26 18:09:31 +00:00
}
2020-04-26 01:17:59 +00:00
/**
* Get Item Tag
* @returns Item Tag
*/
getTag(): CompoundTag {
2020-06-02 17:24:32 +00:00
const obj = useBridge('ItemStack.getTag', this.getJavaObject()) as JavaObject;
2020-04-28 14:16:47 +00:00
if (obj !== null) {
2020-04-26 01:17:59 +00:00
return new CompoundTag(obj);
} else {
return null;
}
}
/**
* Set Item Tag
* @param tag New Item Tag
*/
2020-04-28 02:30:12 +00:00
setTag(tag: CompoundTag): void {
2020-06-02 17:24:32 +00:00
useBridge('ItemStack.setTag', this.getJavaObject(), tag.getJavaObject());
2020-04-26 01:17:59 +00:00
}
2020-04-26 22:07:01 +00:00
/**
* Convert To Tag
* @returns Item Tag
*/
toTag(): CompoundTag {
2020-06-02 17:24:32 +00:00
const obj = useBridge('ItemStack.toTag', this.getJavaObject()) as JavaObject;
2020-04-28 14:16:47 +00:00
if (obj !== null) {
2020-04-26 22:07:01 +00:00
return new CompoundTag(obj);
} else {
return null;
}
}
/**
* Create From Tag
* @param tag Item Tag
* @returns Item Stack
*/
static fromTag(tag: CompoundTag): ItemStack {
2020-06-02 17:24:32 +00:00
const obj = useBridge('ItemStack.fromTag', tag.getJavaObject()) as JavaObject;
2020-04-28 14:16:47 +00:00
if (obj !== null) {
2020-04-26 22:07:01 +00:00
return new ItemStack(obj);
} else {
return null;
}
}
2020-04-28 20:18:22 +00:00
/**
* Split Item Stack
* @param amount Amount
* @returns New Item Stack
*/
split(amount: number): ItemStack {
2020-06-02 17:24:32 +00:00
const obj = useBridge('ItemStack.split', this.getJavaObject(), amount) as JavaObject;
2020-04-28 20:18:22 +00:00
if (obj !== null) {
return new ItemStack(obj);
} else {
return null;
}
}
2020-04-25 13:33:17 +00:00
}
/**
* Item Rarity
*/
2020-05-05 00:36:32 +00:00
export enum ItemRarity {
2020-04-25 13:33:17 +00:00
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
*/
2020-08-26 21:52:17 +00:00
maxCount(maxCount: number): ItemSettings {
2020-04-25 13:33:17 +00:00
this.#maxCount = maxCount;
return this;
}
/**
* Set Item Group
* @param itemGroup Item Group
*/
2020-08-26 21:52:17 +00:00
itemGroup(itemGroup: string | Identifier): ItemSettings {
2020-04-25 13:33:17 +00:00
this.#itemGroup = itemGroup.toString();
return this;
}
/**
* Set Item Rarity
* @param rarity Item Rarity
*/
2020-08-26 21:52:17 +00:00
rarity(rarity: ItemRarity): ItemSettings {
2020-04-25 13:33:17 +00:00
this.#rarity = rarity;
return this;
}
/**
2020-06-02 17:24:32 +00:00
* Create Java Object
* @returns Java Object
2020-04-25 13:33:17 +00:00
*/
createJavaObject(): JavaObject {
return useBridge('ItemSettings.create', this.#maxCount, this.#rarity, this.#itemGroup) as JavaObject;
}
}
/**
* Custom Item
*/
export class CustomItem {
2020-06-02 17:24:32 +00:00
readonly #settings: Readonly<ItemSettings>;
2020-04-25 13:33:17 +00:00
/**
2020-06-02 17:24:32 +00:00
* Get Item Settings
* @returns Item Settings
2020-04-25 13:33:17 +00:00
*/
2020-06-02 17:24:32 +00:00
getSettings(): Readonly<ItemSettings> {
return this.#settings;
}
2020-04-25 13:33:17 +00:00
2020-04-28 20:18:22 +00:00
/**
* Create Custom Item
* @param settings Item Settings
*/
2020-04-25 13:33:17 +00:00
constructor(settings: ItemSettings) {
2020-06-02 17:24:32 +00:00
this.#settings = Object.freeze(settings);
2020-04-25 13:33:17 +00:00
}
/**
* Called When The Item Is Used
* @param world World
* @param player Player
* @param hand Hand
* @returns Action Result
*/
2020-04-28 02:30:12 +00:00
onUse(world: World, player: PlayerEntity, hand: Hand): ActionResult {
return ActionResult.PASS;
}
2020-04-25 13:33:17 +00:00
/**
* 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
*/
2020-04-28 02:30:12 +00:00
onUseOnBlock(world: World, pos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
return ActionResult.PASS;
}
2020-04-25 13:33:17 +00:00
/**
* Called When The Item Is Used On An Entity
* @param player Player
* @param target Target Entity
* @param hand Hand
* @returns Action Result
*/
2020-04-28 02:30:12 +00:00
onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult {
return ActionResult.PASS;
}
2020-04-25 13:33:17 +00:00
}
/**
* Block Item
*/
export class BlockItem {
2020-06-02 17:24:32 +00:00
readonly #settings: Readonly<ItemSettings>;
2020-04-25 13:33:17 +00:00
/**
2020-06-02 17:24:32 +00:00
* Get Item Settings
* @returns Item Settings
2020-04-25 13:33:17 +00:00
*/
2020-06-02 17:24:32 +00:00
getSettings(): Readonly<ItemSettings> {
return this.#settings;
}
readonly #block: Identifier;
2020-04-25 13:33:17 +00:00
/**
2020-06-02 17:24:32 +00:00
* Get Block ID
* @returns Block ID
2020-04-25 13:33:17 +00:00
*/
2020-06-02 17:24:32 +00:00
getBlock(): Identifier {
return this.#block;
}
2020-04-25 13:33:17 +00:00
/**
* Create Block Item
* @param block Block ID
* @param settings Item Settings
*/
constructor(block: Identifier, settings: ItemSettings) {
2020-06-02 17:24:32 +00:00
this.#settings = Object.freeze(settings);
this.#block = block;
2020-04-25 13:33:17 +00:00
}
}
2020-08-26 21:52:17 +00:00
class ItemRegistry implements SimpleRegistry<CustomItem | BlockItem> {
2020-04-28 20:18:22 +00:00
static readonly INSTANCE = new ItemRegistry();
2020-04-25 13:33:17 +00:00
2020-04-28 20:18:22 +00:00
readonly #items: Map<string, CustomItem>;
2020-04-25 13:33:17 +00:00
private constructor() {
this.#items = new Map<string, CustomItem>();
}
2020-04-28 02:30:12 +00:00
register(id: Identifier, obj: CustomItem | BlockItem): void {
2020-04-25 13:33:17 +00:00
if (obj instanceof CustomItem) {
this.#items.set(id.toString(), obj);
2020-06-02 17:24:32 +00:00
useBridge('Registry.registerItem', id.toString(), obj.getSettings().createJavaObject());
2020-04-25 13:33:17 +00:00
} else {
2020-06-02 17:24:32 +00:00
useBridge('Registry.registerBlockItem', id.toString(), obj.getSettings().createJavaObject(), obj.getBlock().toString());
2020-04-25 13:33:17 +00:00
}
}
get(id: Identifier): CustomItem {
return this.#items.get(id.toString());
}
2020-04-26 18:09:31 +00:00
getID(obj: CustomItem): Identifier {
for (const item of this.#items) {
if (item[1] === obj) {
return new Identifier(item[0]);
}
}
return null;
}
2020-04-25 13:33:17 +00:00
}
2020-08-26 21:52:17 +00:00
Registry.ITEM = ItemRegistry.INSTANCE;
2020-04-25 13:33:17 +00:00
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]);
});
2020-06-11 02:38:25 +00:00
addBridge('CustomItem.onUseOnEntity', (id: string, player: JavaObject, target: JavaObject, hand: keyof typeof Hand): string => {
return ItemRegistry.INSTANCE.get(new Identifier(id)).onUseOnEntity(new PlayerEntity(player), new LivingEntity(target), Hand[hand]);
2020-04-25 13:33:17 +00:00
});