Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bedrock support #86

Merged
merged 51 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
44b6105
start bedrock implementation
CreeperG16 Mar 1, 2023
cc7829d
comments
CreeperG16 Mar 1, 2023
9a7634f
Todo comments
CreeperG16 Mar 2, 2023
befe982
comment
CreeperG16 Mar 2, 2023
c0a5b3e
return BedrockItem and run standard
CreeperG16 Mar 2, 2023
2e1238e
Small changes
CreeperG16 Mar 2, 2023
f8bc473
remove tools
CreeperG16 Mar 2, 2023
137af50
matchNbt in equal()
CreeperG16 Mar 2, 2023
821ea9a
better matchNbt
CreeperG16 Mar 2, 2023
f3fc3a8
remove bedrock-protocol from devDependencies
CreeperG16 Mar 2, 2023
58557ea
Notch -> Network in pc impl, add matchNbt to pc
CreeperG16 Mar 2, 2023
9e50812
canPlaceOn and canDestroy
CreeperG16 Mar 2, 2023
06269b8
CanPlaceOn and CanDestroy for java - NEED TO TEST
CreeperG16 Mar 2, 2023
a444f90
Fix weird indentation from my formatter
CreeperG16 Mar 2, 2023
5c182d8
notch -> network
CreeperG16 Mar 2, 2023
c97d5ef
revert the breaking change
CreeperG16 Mar 2, 2023
50d943f
clarify in comments that this is not tested
CreeperG16 Mar 2, 2023
397cd6b
Start bedrock support in original Item class
CreeperG16 Mar 3, 2023
c07449f
Linter
CreeperG16 Mar 3, 2023
b784243
blocksCanPlaceOn and blocksCanDestroy
CreeperG16 Mar 4, 2023
738356d
start implementing stack ID
CreeperG16 Mar 4, 2023
11b1b3e
add stack ID to fromNotch()
CreeperG16 Mar 4, 2023
20937d4
Initial support for <1.16.220, however item formats are inconsistent …
CreeperG16 Mar 5, 2023
afb0d99
<1.16.220 support, can be improved later to use supportFeature()
CreeperG16 Mar 5, 2023
e8d03a4
update types and docs
CreeperG16 Mar 5, 2023
6aaa066
update docs
CreeperG16 Mar 5, 2023
a81f906
use NBT builder functions where possible
CreeperG16 Mar 5, 2023
75c2a72
nbt.simplify() to make it more readable
CreeperG16 Mar 5, 2023
982d132
use optional chaining
CreeperG16 Mar 6, 2023
40462c3
update types
CreeperG16 Mar 6, 2023
e6a48f1
update docs
CreeperG16 Mar 6, 2023
be38e1b
Use supportFeature and bedrock features added in mcdata PR; fixes
CreeperG16 Mar 15, 2023
e1d8ea6
Add stack ID to tests (temp) and damage default to 0
CreeperG16 Mar 16, 2023
398f5c1
remove separate BedrockItem class, don't check ench len
CreeperG16 Mar 16, 2023
bdecc1a
linter
CreeperG16 Mar 16, 2023
d32d4b0
don't use Or assignment to support older node
CreeperG16 Mar 16, 2023
9a5e9cd
revert checking enchs length
CreeperG16 Mar 16, 2023
edc9aa4
use stackID parameter in tests
CreeperG16 Mar 17, 2023
b32b23a
remove network types and add stackID to fromNotch
CreeperG16 Mar 17, 2023
057f0b2
update docs and types
CreeperG16 Apr 11, 2023
bf66f42
stack ID is null in java (mcdata feature?)
CreeperG16 Apr 11, 2023
5dc0be5
Fix tests for stack ID, start adding bedrock tests
CreeperG16 Apr 11, 2023
c598975
clean up some unnecessary values
CreeperG16 Apr 11, 2023
60bbb4a
no need to test for null
CreeperG16 Apr 11, 2023
a42ce35
Merge branch 'master' into master
CreeperG16 Jul 6, 2023
96d061d
More readable notch methods
CreeperG16 Jul 18, 2023
497ae97
Change blocksCanPlaceOn/Destroy to return [name, properties]
CreeperG16 Jul 19, 2023
e3c384d
Anvil is undefined if registry type is bedrock
CreeperG16 Jul 19, 2023
4eae3bf
change blocksCanPlaceOn/Destroy
CreeperG16 Jul 19, 2023
60ec722
update types and docs
CreeperG16 Jul 19, 2023
916a7fa
Update index.d.ts
CreeperG16 Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this should return a [block name, properties] tuple instead of raw block strings for future proofing. It does seem odd that the matching is just done by block name here. But I'm not sure if we should block on that.

Copy link
Member

@extremeheat extremeheat Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a prismarine-block isn't what I meant. I meant returning properties associated with a block. Like stone[variant=andersite] --> [["stone", {"variant":"andersite"]] as opposed to just ["stone"].

Since we don't actually have the prop data as a discriminator for these fields, returning undefined as a second param for now makes sense to me for now [["stone"]].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to type/document it, if it never returns those properties anyway as there isn't a way of getting them, should they be documented to return those properties at all?


#### 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
CreeperG16 marked this conversation as resolved.
Show resolved Hide resolved

#### 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