Skip to content

Commit b16b15e

Browse files
authored
Merge pull request #773 from zyxkad/patch-772
fix memory card will not be saved in inventory manager
2 parents 790c06f + 262242c commit b16b15e

File tree

4 files changed

+86
-77
lines changed

4 files changed

+86
-77
lines changed

src/main/java/de/srendi/advancedperipherals/common/blocks/base/PeripheralBlockEntity.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import net.minecraft.core.NonNullList;
1212
import net.minecraft.nbt.CompoundTag;
1313
import net.minecraft.network.chat.Component;
14+
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
1415
import net.minecraft.world.ContainerHelper;
1516
import net.minecraft.world.MenuProvider;
1617
import net.minecraft.world.WorldlyContainer;
1718
import net.minecraft.world.entity.player.Inventory;
1819
import net.minecraft.world.entity.player.Player;
1920
import net.minecraft.world.inventory.AbstractContainerMenu;
2021
import net.minecraft.world.item.ItemStack;
22+
import net.minecraft.world.level.Level;
2123
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
2224
import net.minecraft.world.level.block.entity.BlockEntityType;
2325
import 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

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/InventoryManagerEntity.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import de.srendi.advancedperipherals.common.container.InventoryManagerContainer;
77
import de.srendi.advancedperipherals.common.items.MemoryCardItem;
88
import de.srendi.advancedperipherals.common.setup.BlockEntityTypes;
9-
import de.srendi.advancedperipherals.network.APNetworking;
10-
import de.srendi.advancedperipherals.network.toclient.InventoryManagerUpdatePacket;
119
import net.minecraft.core.BlockPos;
1210
import net.minecraft.core.Direction;
1311
import net.minecraft.nbt.CompoundTag;
@@ -39,9 +37,6 @@ protected InventoryManagerPeripheral createPeripheral() {
3937

4038
@Override
4139
public InventoryManagerContainer createContainer(int id, Inventory playerInventory, BlockPos pos, Level world) {
42-
// Update the clients instance of the inventory manager so the UI shows the correct owner
43-
updateClient();
44-
4540
return new InventoryManagerContainer(id, playerInventory, pos, world);
4641
}
4742

@@ -58,6 +53,7 @@ public boolean canPlaceItemThroughFace(int index, @NotNull ItemStack itemStackIn
5853

5954
@Override
6055
public void setItem(int index, @NotNull ItemStack stack) {
56+
boolean shouldClearOwner = false;
6157
if (stack.getItem() instanceof MemoryCardItem) {
6258
if (stack.hasTag() && stack.getTag().contains("ownerId")) {
6359
UUID owner = stack.getTag().getUUID("ownerId");
@@ -66,12 +62,14 @@ public void setItem(int index, @NotNull ItemStack stack) {
6662
stack.getTag().remove("owner");
6763
} else if (stack != this.getItem(index)) {
6864
// Only clear owner when the new card item is not the current item
69-
this.owner = null;
65+
shouldClearOwner = true;
7066
}
7167
} else {
72-
owner = null;
68+
shouldClearOwner = true;
69+
}
70+
if (shouldClearOwner && this.getLevel() != null && !this.getLevel().isClientSide) {
71+
this.owner = null;
7372
}
74-
updateClient();
7573
super.setItem(index, stack);
7674
}
7775

@@ -87,22 +85,24 @@ public void load(@NotNull CompoundTag data) {
8785
this.owner = data.getUUID("ownerId");
8886
}
8987
super.load(data);
90-
// Fresh the memory card for backward compatibility
91-
this.setItem(0, this.getItem(0));
9288
}
9389

9490
@Override
95-
public void saveAdditional(@NotNull CompoundTag data) {
96-
super.saveAdditional(data);
91+
protected void saveShared(@NotNull CompoundTag data) {
92+
super.saveShared(data);
9793
if (this.owner != null) {
9894
data.putUUID("ownerId", this.owner);
9995
}
10096
}
10197

102-
public void updateClient() {
103-
if (level.isClientSide())
104-
return;
105-
APNetworking.sendToAllAround(new InventoryManagerUpdatePacket(owner != null, this.owner, this.getBlockPos()), this.getLevel().dimension(), this.getBlockPos(), 10);
98+
@Override
99+
public void onLoad() {
100+
super.onLoad();
101+
// Fresh the memory card for backward compatibility
102+
// TODO: remove in 0.8
103+
if (!this.getLevel().isClientSide) {
104+
this.setItem(0, this.getItem(0));
105+
}
106106
}
107107

108108
public Player getOwnerPlayer() {
@@ -113,11 +113,11 @@ public Player getOwnerPlayer() {
113113
return player;
114114
}
115115

116-
public void setOwner(UUID owner) {
117-
this.owner = owner;
118-
}
119-
120116
public UUID getOwner() {
121117
return owner;
122118
}
119+
120+
public void setOwner(UUID owner) {
121+
this.owner = owner;
122+
}
123123
}

src/main/java/de/srendi/advancedperipherals/network/APNetworking.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.srendi.advancedperipherals.AdvancedPeripherals;
44
import de.srendi.advancedperipherals.network.base.IPacket;
5-
import de.srendi.advancedperipherals.network.toclient.InventoryManagerUpdatePacket;
65
import de.srendi.advancedperipherals.network.toclient.ToastToClientPacket;
76
import de.srendi.advancedperipherals.network.toclient.UsernameToCachePacket;
87
import de.srendi.advancedperipherals.network.toserver.RetrieveUsernamePacket;
@@ -33,7 +32,6 @@ public class APNetworking {
3332
public static void init() {
3433
registerServerToClient(ToastToClientPacket.class, ToastToClientPacket::decode);
3534
registerServerToClient(UsernameToCachePacket.class, UsernameToCachePacket::decode);
36-
registerServerToClient(InventoryManagerUpdatePacket.class, InventoryManagerUpdatePacket::decode);
3735

3836
registerClientToServer(RetrieveUsernamePacket.class, RetrieveUsernamePacket::decode);
3937
}

src/main/java/de/srendi/advancedperipherals/network/toclient/InventoryManagerUpdatePacket.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)