1111import  net .minecraft .core .NonNullList ;
1212import  net .minecraft .nbt .CompoundTag ;
1313import  net .minecraft .network .chat .Component ;
14+ import  net .minecraft .network .protocol .game .ClientboundBlockEntityDataPacket ;
1415import  net .minecraft .world .ContainerHelper ;
1516import  net .minecraft .world .MenuProvider ;
1617import  net .minecraft .world .WorldlyContainer ;
1718import  net .minecraft .world .entity .player .Inventory ;
1819import  net .minecraft .world .entity .player .Player ;
1920import  net .minecraft .world .inventory .AbstractContainerMenu ;
2021import  net .minecraft .world .item .ItemStack ;
22+ import  net .minecraft .world .level .Level ;
2123import  net .minecraft .world .level .block .entity .BaseContainerBlockEntity ;
2224import  net .minecraft .world .level .block .entity .BlockEntityType ;
2325import  net .minecraft .world .level .block .state .BlockState ;
@@ -119,17 +121,49 @@ public ITextComponent getDisplayName() {
119121    }*/ 
120122
121123    @ Override 
122-     public  void  saveAdditional (@ NotNull  CompoundTag  compound ) {
124+     public  void  load (@ NotNull  CompoundTag  compound ) {
125+         ContainerHelper .loadAllItems (compound , items );
126+         peripheralSettings  = compound .getCompound (PERIPHERAL_SETTINGS_KEY );
127+         super .load (compound );
128+     }
129+ 
130+     /** 
131+      * will automatically adds shared data at the end 
132+      * 
133+      * @see saveShared 
134+      */ 
135+     @ Override 
136+     protected  void  saveAdditional (@ NotNull  CompoundTag  compound ) {
123137        super .saveAdditional (compound );
138+         this .saveShared (compound );
124139        ContainerHelper .saveAllItems (compound , items );
125140        if  (!peripheralSettings .isEmpty ()) compound .put (PERIPHERAL_SETTINGS_KEY , peripheralSettings );
126141    }
127142
143+     /** 
144+      * will automatically adds shared data at the end 
145+      * 
146+      * @return combined update tag and shared data 
147+      * @see saveShared 
148+      */ 
128149    @ Override 
129-     public  void  load (@ NotNull  CompoundTag  compound ) {
130-         ContainerHelper .loadAllItems (compound , items );
131-         peripheralSettings  = compound .getCompound (PERIPHERAL_SETTINGS_KEY );
132-         super .load (compound );
150+     public  CompoundTag  getUpdateTag () {
151+         final  CompoundTag  compound  = super .getUpdateTag ();
152+         this .saveShared (compound );
153+         return  compound ;
154+     }
155+ 
156+     /** 
157+      * define datas that should both be saved on server and sync to client 
158+      * 
159+      * @see saveAdditional 
160+      * @see getUpdateTag 
161+      */ 
162+     protected  void  saveShared (@ NotNull  CompoundTag  compound ) {}
163+ 
164+     @ Override 
165+     public  ClientboundBlockEntityDataPacket  getUpdatePacket () {
166+         return  ClientboundBlockEntityDataPacket .create (this );
133167    }
134168
135169    @ Override 
@@ -171,12 +205,13 @@ public int getContainerSize() {
171205    @ Override 
172206    public  boolean  isEmpty () {
173207        for  (ItemStack  itemStack  : items ) {
174-             if  (itemStack .isEmpty ()) return  true ;
208+             if  (!itemStack .isEmpty ()) {
209+                 return  false ;
210+             }
175211        }
176-         return  false ;
212+         return  true ;
177213    }
178214
179- 
180215    @ NotNull 
181216    @ Override 
182217    public  ItemStack  getItem (int  index ) {
@@ -189,7 +224,11 @@ public ItemStack getItem(int index) {
189224    @ NotNull 
190225    @ Override 
191226    public  ItemStack  removeItem (int  index , int  count ) {
192-         return  ContainerHelper .removeItem (items , index , count );
227+         ItemStack  removed  = ContainerHelper .removeItem (items , index , count );
228+         if  (!removed .isEmpty ()) {
229+             this .setChanged ();
230+         }
231+         return  removed ;
193232    }
194233
195234    @ NotNull 
@@ -204,6 +243,7 @@ public void setItem(int index, @NotNull ItemStack stack) {
204243        if  (stack .getCount () > getMaxStackSize ()) {
205244            stack .setCount (getMaxStackSize ());
206245        }
246+         this .setChanged ();
207247    }
208248
209249    @ Override 
@@ -214,6 +254,7 @@ public boolean stillValid(@NotNull Player player) {
214254    @ Override 
215255    public  void  clearContent () {
216256        items .clear ();
257+         this .setChanged ();
217258    }
218259
219260    public  CompoundTag  getPeripheralSettings () {
@@ -222,7 +263,22 @@ public CompoundTag getPeripheralSettings() {
222263
223264    @ Override 
224265    public  void  markSettingsChanged () {
225-         setChanged ();
266+         this .setChanged ();
267+     }
268+ 
269+     /** 
270+      * set this block entity as {@link setChanged changed}, and sync the change to client 
271+      * 
272+      * @see saveShared 
273+      * @see getUpdateTag 
274+      */ 
275+     public  void  markDataSync () {
276+         Level  level  = this .getLevel ();
277+         if  (level  == null  || level .isClientSide ) {
278+             return ;
279+         }
280+         this .setChanged ();
281+         level .sendBlockUpdated (this .getBlockPos (), this .getBlockState (), this .getBlockState (), 0  /* no use on server-side */ );
226282    }
227283}
228284
0 commit comments