1- import { system , world } from "@minecraft/server" ;
2- import { SerializableKinds , JsonDatabase } from "./con-database" ;
1+ import { system , world , Container } from "@minecraft/server" ;
2+ import { SerializableKinds } from "./con-database" ;
33import { DynamicTable , Serializer } from "./con-database" ;
4+ import { APISerializableKinds , registryAPISerializers } from "./con-database" ;
45
56
6- class MyClassWithMethods {
7- constructor ( id , message ) {
8- this . id = id ;
9- this . message = message ;
7+ registryAPISerializers ( ) ;
8+ class ContainerMetadata {
9+ /**@param {Container } container */
10+ static FromContainer ( container ) {
11+ const meta = new ContainerMetadata ( ) ;
12+ for ( let i = 0 ; i < container . size ; i ++ ) {
13+ const item = container . getItem ( i ) ;
14+ if ( item ) {
15+ meta . items . push ( item ) ;
16+ meta . slots . push ( i ) ;
17+ }
18+ }
19+ return meta ;
1020 }
11- warn ( ) {
12- console . warn ( this . id , this . message ) ;
21+ static SaveToContainer ( container , meta ) {
22+ for ( const slot of meta . slots ) {
23+ container . setItem ( slot , meta . items . shift ( ) ) ;
24+ }
25+ }
26+ constructor ( ) {
27+ this . items = [ ] ;
28+ this . slots = [ ] ;
1329 }
1430}
1531Serializer . setSerializableClass (
16- MyClassWithMethods ,
17- "custom-kind- id" ,
18- function ( { id , message } ) { // serialization function, called when DynamicTable.set()
32+ ContainerMetadata ,
33+ "container-metedata-serialization- id" , //do not change this id its unique ti your serializer
34+ function * ( { items , slots } ) {
1935 const objectSerializer = Serializer . getSerializer ( SerializableKinds . Object ) ;
20- return objectSerializer ( { id, message} ) ; //basic serialization as poor object
36+ yield * objectSerializer ( slots ) ; //saving manifest info
37+ const itemSerialzer = Serializer . getSerializer ( APISerializableKinds . ItemStack ) ;
38+ for ( const item of items ) {
39+ throw new Error ( "Throw test" ) ;
40+ yield * itemSerialzer ( item ) ;
41+ }
2142 } ,
22- function ( n ) { //deserialization function, called when DynamicTable.get();
23- const object = Serializer . getDeserializer ( SerializableKinds . Object ) ( n ) ; //loaded poor object
24- return Object . setPrototypeOf ( object , MyClassWithMethods . prototype ) ; //add a MyClassWithMethods prototype
43+ function ( n ) {
44+ const slots = Serializer . getDeserializer ( SerializableKinds . Object ) ( n ) ; //loading info
45+ const itemDeserializer = Serializer . getDeserializer ( APISerializableKinds . ItemStack ) ;
46+ const obj = new ContainerMetadata ( ) ;
47+ obj . slots = slots ;
48+ for ( const s of slots ) {
49+ obj . items . push ( itemDeserializer ( n ) ) ;
50+ }
51+ return obj ;
2552 }
26- )
27-
28-
29-
53+ ) ;
3054
31- const table = DynamicTable . OpenCreate ( "id-of-the-table" ) ;
32- table . set ( "key-the-test" , new MyClassWithMethods ( "warn-id" , "My custom message" ) ) ;
3355
34-
35- table . get ( "key-the-test" ) . warn ( ) ; //warn method from MyClasswithMethods
36- const jsDB = new JsonDatabase ( "the id" ) ;
37- jsDB . set ( "Lmao" , "adfasdfsa" . repeat ( 600 ) ) ;
38- jsDB . set ( "Some test" , { asd :"asdfasdf" , asds :{ asdfasdfa :"asdfasdf" } } ) ;
39- for ( const [ k , v ] of jsDB ) {
40- console . warn ( k , v ) ;
41- }
42- jsDB . clear ( ) ;
43- DynamicTable . ClearAll ( ) ;
44- system . runTimeout ( ( ) => console . warn ( world . getDynamicPropertyTotalByteCount ( ) ) , 3 ) ;
56+ world . clearDynamicProperties ( ) ;
57+ const table = DynamicTable . OpenCreate ( "Testing so far" ) ;
58+ let saved = false ;
59+ world . afterEvents . chatSend . subscribe ( ( { sender, message} ) => {
60+ if ( message === "m" ) {
61+ } else {
62+ if ( ! saved ) {
63+ table . set ( "inv" , ContainerMetadata . FromContainer ( sender . getComponent ( "inventory" ) . container ) ) ;
64+ } else {
65+ ContainerMetadata . SaveToContainer ( sender . getComponent ( "inventory" ) . container , table . get ( "inv" ) ) ;
66+ }
67+ saved = ! saved ;
68+ }
69+ } ) ;
0 commit comments