Skip to content

Commit

Permalink
Bedrock support (#86)
Browse files Browse the repository at this point in the history
* start bedrock implementation

* comments

* Todo comments

* comment

* return BedrockItem and run standard

* Small changes

* remove tools

* matchNbt in equal()

* better matchNbt

* remove bedrock-protocol from devDependencies

* Notch -> Network in pc impl, add matchNbt to pc

* canPlaceOn and canDestroy

* CanPlaceOn and CanDestroy for java - NEED TO TEST

* Fix weird indentation from my formatter

* notch -> network

* revert the breaking change

* clarify in comments that this is not tested

* Start bedrock support in original Item class

* Linter

* blocksCanPlaceOn and blocksCanDestroy

* start implementing stack ID

* add stack ID to fromNotch()

* Initial support for <1.16.220, however item formats are inconsistent and this can be improved

* <1.16.220 support, can be improved later to use supportFeature()

* update types and docs

* update docs

* use NBT builder functions where possible

* nbt.simplify() to make it more readable

* use optional chaining

* update types

* update docs

* Use supportFeature and bedrock features added in mcdata PR; fixes

* Add stack ID to tests (temp) and damage default to 0

* remove separate BedrockItem class, don't check ench len

* linter

* don't use Or assignment to support older node

* revert checking enchs length

* use stackID parameter in tests

* remove network types and add stackID to fromNotch

* update docs and types

* stack ID is null in java (mcdata feature?)

* Fix tests for stack ID, start adding bedrock tests

* clean up some unnecessary values

* no need to test for null

* More readable notch methods

* Change blocksCanPlaceOn/Destroy to return [name, properties]

* Anvil is undefined if registry type is bedrock

* change blocksCanPlaceOn/Destroy

* update types and docs

* Update index.d.ts
  • Loading branch information
CreeperG16 committed Jul 22, 2023
1 parent 816ab75 commit 854c357
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 349 deletions.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,32 @@ console.log(Item.fromNotch(notchItem))

## API

### Item(type, count[, metadata], nbt)
### Item(type, count[, metadata, nbt, stackId])

#### Item.toNotch(item)
#### Item.toNotch(item[, serverAuthoritative])

Take an `item` in the format of the minecraft packets and return an `Item` instance.
Take an `Item` instance and returns it in the format of the minecraft packets.
- serverAuthoritative: Whether the server is using server authoritative inventory (whether or not to write a Stack ID)

#### Item.fromNotch(item)
#### Item.fromNotch(item[, stackId])

Take an `Item` instance and return it in the format of the minecraft packets.
Take an `item` in the format of the minecraft packets and return an `Item` instance.
- stackId for bedrock items before 1.16.220

### Item.anvil(itemOne, itemTwo, creative[, newName])

Take two seperate `item` instances, and makes one item using the same combining done by the vanilla anvil

### Item.equal(itemOne, itemTwo[, matchStackSize])
### Item.equal(itemOne, itemTwo[, matchStackSize, matchNbt])

`itemOne` - first item

`itemTwo` - second item

`matchStackSize` - whether to check for count equality

`matchNbt` - wether to check for NBT equality

Checks equality between two items based on itemType, count, metadata, and stringified nbt

#### item.type
Expand All @@ -58,6 +62,10 @@ See http://www.minecraftwiki.net/wiki/Data_values#Data

Buffer.

#### item.stackId

The stack ID of the item, if the version supports Stack IDs.

#### item.name

#### item.displayName
Expand All @@ -82,7 +90,22 @@ the item's custom lore (ie. set in give command)

#### item.enchants

A getter/setter for abstracting the underlying nbt (does calculations)
#### get item.enchants(): { name: string, lvl: number }[]

Returns an array of enchants on the Item with their name and level

#### set item.enchants({ name: string, lvl: number }[])

Updates the Item's NBT enchantments based on assigned array

#### get item.blocksCanPlaceOn(): [name][]
#### set item.blocksCanPlaceOn(blockNames: string[])
In adventure mode, the list of block names (as strings) that this Item can be placed on

#### get item.blocksCanDestroy(): [name][]
#### set item.blocksCanDestroy(blockNames: string[])

In adventure mode, the list of block names (as strings) that this Item can be used to break

#### item.repairCost

Expand All @@ -91,7 +114,7 @@ See https://minecraft.gamepedia.com/Anvil_mechanics#Anvil_Uses

#### item.spawnEggMobName

A getter for abstracting the underlying nbt, get's the mob name from a spawn egg Item. e.g. a zombie spawn egg on 1.8 will return `Zombie`
If the current item is a type of Spawn Egg, the protocol name of the entity that will be spawned. For example, a zombie spawn egg on 1.8 will return `Zombie`.


## History
Expand Down
70 changes: 33 additions & 37 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,39 @@ import { Tags, TagType } from 'prismarine-nbt'

export type ItemLike = Item | null

export declare class Item {
constructor(type: number, count: number, metadata?: number, nbt?: object);
type: number;
slot: number;
count: number;
metadata: number;
nbt: Tags[TagType] | null;
name: string;
displayName: string;
stackSize: number;
durabilityUsed: number;
enchants: NormalizedEnchant[];
repairCost: number;
customName: string | null;
customLore: string[] | null;
readonly spawnEggMobName: string;
static equal(item1: Item, item2: Item, matchStackSize: boolean): boolean;
static toNotch(item: ItemLike): NotchItem;
static fromNotch(item: NotchItem): ItemLike;
static anvil (itemOne: ItemLike, itemTwo: ItemLike, creative: boolean, rename: string | undefined): { xpCost: number, item: ItemLike }
}

export declare interface NotchItem {
// 1.8 - 1.12
blockId?: number;
itemDamage?: number;
// 1.13 - 1.15
present?: boolean;
itemId?: number;

itemCount?: number;
nbtData?: Buffer;
}

export declare interface NormalizedEnchant {
name: string;
lvl: number
export class Item {
constructor(type: number, count: number, metadata?: number, nbt?: object, stackId?: number);
type: number;
slot: number;
count: number;
metadata: number;
nbt: Tags[TagType] | null;
stackId: number | null;
name: string;
displayName: string;
stackSize: number;
durabilityUsed: number;
get enchants(): { name: string; lvl: number }[];
set enchants(enchantments: { name: string; lvl: number }[]);
get blocksCanPlaceOn(): [string][];
set blocksCanPlaceOn(blockNames: string[]);
get blocksCanDestroy(): [string][];
set blocksCanDestroy(blockNames: string[]);
repairCost: number;
customName: string | null;
customLore: string | string[] | null;
readonly spawnEggMobName: string;
static equal(item1: Item, item2: Item, matchStackSize?: boolean, matchNbt?: boolean): boolean;
static toNotch(item: ItemLike, serverAuthoritative?: boolean): object;
static fromNotch(item: object, stackId?: number): ItemLike;
static anvil(
itemOne: ItemLike,
itemTwo: ItemLike,
creative: boolean,
rename: string | undefined
): { xpCost: number; item: ItemLike };
static currentStackId: number;
static nextStackId(): number;
}

export default function loader(mcVersion: string): typeof Item;
Loading

0 comments on commit 854c357

Please sign in to comment.