This commit is contained in:
parent
b1604023ad
commit
d73e2bc1af
@ -1,5 +1,6 @@
|
||||
package com.thebrokenrail.scriptcraft.bridge;
|
||||
|
||||
import com.thebrokenrail.scriptcraft.util.Util;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.Identifier;
|
||||
@ -10,7 +11,19 @@ class ItemStackBridges {
|
||||
Bridges.addBridge("ItemStack.create", args -> new ItemStack(Registry.ITEM.get(new Identifier((String) args[0])), ((Double) args[1]).intValue()));
|
||||
|
||||
Bridges.addBridge("ItemStack.getItem", args -> Registry.ITEM.getId(((ItemStack) args[0]).getItem()).toString());
|
||||
|
||||
Bridges.addBridge("ItemStack.setCount", args -> {
|
||||
((ItemStack) args[0]).setCount((int) Util.toDouble(args[1], 0));
|
||||
return null;
|
||||
});
|
||||
Bridges.addBridge("ItemStack.getCount", args -> (double) ((ItemStack) args[0]).getCount());
|
||||
|
||||
Bridges.addBridge("ItemStack.setDamage", args -> {
|
||||
((ItemStack) args[0]).setDamage((int) Util.toDouble(args[1], 0));
|
||||
return null;
|
||||
});
|
||||
Bridges.addBridge("ItemStack.getDamage", args -> (double) ((ItemStack) args[0]).getDamage());
|
||||
Bridges.addBridge("ItemStack.isDamageable", args -> ((ItemStack) args[0]).isDamageable());
|
||||
|
||||
Bridges.addBridge("ItemStack.getTag", args -> ((ItemStack) args[0]).getTag());
|
||||
Bridges.addBridge("ItemStack.setTag", args -> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { World } from './world';
|
||||
import { PlayerEntity } from './entity';
|
||||
import { useBridge, addBridge, Identifier, Hand, Pos, ActionResult, Direction, SingleRegistry } from './core';
|
||||
import { useBridge, addBridge, Identifier, Hand, Pos, ActionResult, Direction, SimpleRegistry } from './core';
|
||||
import { CompoundTag } from './tag';
|
||||
|
||||
/**
|
||||
@ -130,6 +130,15 @@ export class BlockState {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Custom Block
|
||||
* @returns Custom Block
|
||||
*/
|
||||
getCustomBlock(): CustomBlock {
|
||||
const id = this.getBlock();
|
||||
return BlockRegistry.INSTANCE.get(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -213,7 +222,7 @@ export abstract class CustomBlockWithEntity extends CustomBlock {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export class BlockRegistry implements SingleRegistry<CustomBlock> {
|
||||
export class BlockRegistry implements SimpleRegistry<CustomBlock> {
|
||||
static INSTANCE = new BlockRegistry();
|
||||
|
||||
#blocks: Map<string, CustomBlock>;
|
||||
@ -238,6 +247,15 @@ export class BlockRegistry implements SingleRegistry<CustomBlock> {
|
||||
return this.#blocks.get(id.toString());
|
||||
}
|
||||
|
||||
getID(obj: CustomBlock): Identifier {
|
||||
for (const block of this.#blocks) {
|
||||
if (block[1] === obj) {
|
||||
return new Identifier(block[0]);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getBlockEntity(id: Identifier, i: number): CustomBlockEntity {
|
||||
return this.#blockEntities.get(id.toString())[i];
|
||||
}
|
||||
|
@ -245,10 +245,27 @@ export class Pos {
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Simple Registry
|
||||
*/
|
||||
export interface SingleRegistry<T> {
|
||||
export interface SimpleRegistry<T> {
|
||||
/**
|
||||
* Register Object
|
||||
* @param id ID
|
||||
* @param obj Object
|
||||
*/
|
||||
register(id: Identifier, obj: T): void;
|
||||
|
||||
/**
|
||||
* Get Object From ID
|
||||
* @param id ID
|
||||
* @returns Object
|
||||
*/
|
||||
get(id: Identifier): T;
|
||||
|
||||
/**
|
||||
* Get ID From Object
|
||||
* @param obj Object
|
||||
* @returns ID
|
||||
*/
|
||||
getID(obj: T): Identifier;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { useBridge, Identifier, Hand, ActionResult, addBridge, Pos, Direction, SingleRegistry } from './core';
|
||||
import { useBridge, Identifier, Hand, ActionResult, addBridge, Pos, Direction, SimpleRegistry } from './core';
|
||||
import { World } from './world';
|
||||
import { PlayerEntity, LivingEntity } from './entity';
|
||||
import { CompoundTag } from './tag';
|
||||
@ -46,6 +46,14 @@ export class ItemStack {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Custom Item
|
||||
* @returns Custom Item
|
||||
*/
|
||||
getCustomItem(): CustomItem {
|
||||
return ItemRegistry.INSTANCE.get(this.getItem());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Item Count
|
||||
* @returns Item Count
|
||||
@ -54,6 +62,38 @@ export class ItemStack {
|
||||
return useBridge('ItemStack.getCount', this.javaObject) as number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Item Count
|
||||
* @param count New Count
|
||||
*/
|
||||
setCount(count: number) {
|
||||
useBridge('ItemStack.setCount', this.javaObject, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Item Damage
|
||||
* @returns Item Damage
|
||||
*/
|
||||
getDamage(): number {
|
||||
return useBridge('ItemStack.getDamage', this.javaObject) as number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Item Damage
|
||||
* @param count New Damage
|
||||
*/
|
||||
setDamage(damage: number) {
|
||||
useBridge('ItemStack.setDamage', this.javaObject, damage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Item Damageable
|
||||
* @returns True If Item Is Damageable
|
||||
*/
|
||||
isDamageable(): boolean {
|
||||
return useBridge('ItemStack.isDamageable', this.javaObject) as boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Item Tag
|
||||
* @returns Item Tag
|
||||
@ -207,7 +247,7 @@ export class BlockItem {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export class ItemRegistry implements SingleRegistry<CustomItem | BlockItem> {
|
||||
export class ItemRegistry implements SimpleRegistry<CustomItem | BlockItem> {
|
||||
static INSTANCE = new ItemRegistry();
|
||||
|
||||
#items: Map<string, CustomItem>;
|
||||
@ -228,6 +268,15 @@ export class ItemRegistry implements SingleRegistry<CustomItem | BlockItem> {
|
||||
get(id: Identifier): CustomItem {
|
||||
return this.#items.get(id.toString());
|
||||
}
|
||||
|
||||
getID(obj: CustomItem): Identifier {
|
||||
for (const item of this.#items) {
|
||||
if (item[1] === obj) {
|
||||
return new Identifier(item[0]);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
addBridge('CustomItem.onUse', (id: string, world: JavaObject, player: JavaObject, hand: keyof typeof Hand): string => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Identifier, SingleRegistry } from './core';
|
||||
import { Identifier, SimpleRegistry } from './core';
|
||||
import { BlockRegistry } from './block';
|
||||
import { ItemRegistry } from './item';
|
||||
|
||||
@ -21,7 +21,7 @@ export abstract class Registry {
|
||||
* @param id ID
|
||||
* @param obj Object
|
||||
*/
|
||||
static register<T>(registry: SingleRegistry<T>, id: Identifier, obj: T): T {
|
||||
static register<T>(registry: SimpleRegistry<T>, id: Identifier, obj: T): T {
|
||||
registry.register(id, obj);
|
||||
return obj;
|
||||
}
|
||||
@ -30,8 +30,19 @@ export abstract class Registry {
|
||||
* Get Object From Registry
|
||||
* @param registry Target Registry
|
||||
* @param id ID
|
||||
* @returns Object
|
||||
*/
|
||||
static get<T>(registry: SingleRegistry<T>, id: Identifier): T {
|
||||
static get<T>(registry: SimpleRegistry<T>, id: Identifier): T {
|
||||
return registry.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ID Of Object From Registry
|
||||
* @param registry Target Registry
|
||||
* @param obj Object
|
||||
* @returns ID
|
||||
*/
|
||||
static getID<T>(registry: SimpleRegistry<T>, obj: T): Identifier {
|
||||
return registry.getID(obj);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user