Skip to content

Commit 854c357

Browse files
authored
Bedrock support (#86)
* 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
1 parent 816ab75 commit 854c357

File tree

6 files changed

+657
-349
lines changed

6 files changed

+657
-349
lines changed

README.md

+31-8
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,32 @@ console.log(Item.fromNotch(notchItem))
1919

2020
## API
2121

22-
### Item(type, count[, metadata], nbt)
22+
### Item(type, count[, metadata, nbt, stackId])
2323

24-
#### Item.toNotch(item)
24+
#### Item.toNotch(item[, serverAuthoritative])
2525

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

28-
#### Item.fromNotch(item)
29+
#### Item.fromNotch(item[, stackId])
2930

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

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

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

36-
### Item.equal(itemOne, itemTwo[, matchStackSize])
38+
### Item.equal(itemOne, itemTwo[, matchStackSize, matchNbt])
3739

3840
`itemOne` - first item
3941

4042
`itemTwo` - second item
4143

4244
`matchStackSize` - whether to check for count equality
4345

46+
`matchNbt` - wether to check for NBT equality
47+
4448
Checks equality between two items based on itemType, count, metadata, and stringified nbt
4549

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

5963
Buffer.
6064

65+
#### item.stackId
66+
67+
The stack ID of the item, if the version supports Stack IDs.
68+
6169
#### item.name
6270

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

8391
#### item.enchants
8492

85-
A getter/setter for abstracting the underlying nbt (does calculations)
93+
#### get item.enchants(): { name: string, lvl: number }[]
94+
95+
Returns an array of enchants on the Item with their name and level
96+
97+
#### set item.enchants({ name: string, lvl: number }[])
98+
99+
Updates the Item's NBT enchantments based on assigned array
100+
101+
#### get item.blocksCanPlaceOn(): [name][]
102+
#### set item.blocksCanPlaceOn(blockNames: string[])
103+
In adventure mode, the list of block names (as strings) that this Item can be placed on
104+
105+
#### get item.blocksCanDestroy(): [name][]
106+
#### set item.blocksCanDestroy(blockNames: string[])
107+
108+
In adventure mode, the list of block names (as strings) that this Item can be used to break
86109

87110
#### item.repairCost
88111

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

92115
#### item.spawnEggMobName
93116

94-
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`
117+
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`.
95118

96119

97120
## History

index.d.ts

+33-37
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,39 @@ import { Tags, TagType } from 'prismarine-nbt'
44

55
export type ItemLike = Item | null
66

7-
export declare class Item {
8-
constructor(type: number, count: number, metadata?: number, nbt?: object);
9-
type: number;
10-
slot: number;
11-
count: number;
12-
metadata: number;
13-
nbt: Tags[TagType] | null;
14-
name: string;
15-
displayName: string;
16-
stackSize: number;
17-
durabilityUsed: number;
18-
enchants: NormalizedEnchant[];
19-
repairCost: number;
20-
customName: string | null;
21-
customLore: string[] | null;
22-
readonly spawnEggMobName: string;
23-
static equal(item1: Item, item2: Item, matchStackSize: boolean): boolean;
24-
static toNotch(item: ItemLike): NotchItem;
25-
static fromNotch(item: NotchItem): ItemLike;
26-
static anvil (itemOne: ItemLike, itemTwo: ItemLike, creative: boolean, rename: string | undefined): { xpCost: number, item: ItemLike }
27-
}
28-
29-
export declare interface NotchItem {
30-
// 1.8 - 1.12
31-
blockId?: number;
32-
itemDamage?: number;
33-
// 1.13 - 1.15
34-
present?: boolean;
35-
itemId?: number;
36-
37-
itemCount?: number;
38-
nbtData?: Buffer;
39-
}
40-
41-
export declare interface NormalizedEnchant {
42-
name: string;
43-
lvl: number
7+
export class Item {
8+
constructor(type: number, count: number, metadata?: number, nbt?: object, stackId?: number);
9+
type: number;
10+
slot: number;
11+
count: number;
12+
metadata: number;
13+
nbt: Tags[TagType] | null;
14+
stackId: number | null;
15+
name: string;
16+
displayName: string;
17+
stackSize: number;
18+
durabilityUsed: number;
19+
get enchants(): { name: string; lvl: number }[];
20+
set enchants(enchantments: { name: string; lvl: number }[]);
21+
get blocksCanPlaceOn(): [string][];
22+
set blocksCanPlaceOn(blockNames: string[]);
23+
get blocksCanDestroy(): [string][];
24+
set blocksCanDestroy(blockNames: string[]);
25+
repairCost: number;
26+
customName: string | null;
27+
customLore: string | string[] | null;
28+
readonly spawnEggMobName: string;
29+
static equal(item1: Item, item2: Item, matchStackSize?: boolean, matchNbt?: boolean): boolean;
30+
static toNotch(item: ItemLike, serverAuthoritative?: boolean): object;
31+
static fromNotch(item: object, stackId?: number): ItemLike;
32+
static anvil(
33+
itemOne: ItemLike,
34+
itemTwo: ItemLike,
35+
creative: boolean,
36+
rename: string | undefined
37+
): { xpCost: number; item: ItemLike };
38+
static currentStackId: number;
39+
static nextStackId(): number;
4440
}
4541

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

0 commit comments

Comments
 (0)