我知道由于Client与Server的关系,所以TileEntity的数值需要更新至Client
可是就算我在每次tick的时候更新一次,还是没有效果。
示例:
(那条进度条不用理 未实作)
从上图可见,Stored: 是直接从TileEntity取出的数值,
红色进度条上显示的则是gui取得的数值
当我把TileEntity中的能量抽走,gui不会更新。
甚么都不做就会出现上图的结果
那么,我尝试在TileEntity(implements ITickable)中的update()加上这行:复制代码还是同样的结果
还是我省略了一些重要的步骤?
我参考这个Furnace源代码的时候发现他也是用这行用作更新。
可是就算我在每次tick的时候更新一次,还是没有效果。
示例:

(那条进度条不用理 未实作)
从上图可见,Stored: 是直接从TileEntity取出的数值,
红色进度条上显示的则是gui取得的数值
当我把TileEntity中的能量抽走,gui不会更新。

甚么都不做就会出现上图的结果
那么,我尝试在TileEntity(implements ITickable)中的update()加上这行:
- if(world.isRemote) world.notifyBlockUpdate(pos, getState(), getState(), 3);
还是我省略了一些重要的步骤?
我参考这个Furnace源代码的时候发现他也是用这行用作更新。
本帖最后由 s091424 于 2019-5-4 22:18 编辑
已经重写了这些方法,但是问题还在
附上write与readNBT的method
复制代码复制代码
bxz2311196368 发表于 2019-5-4 20:50
注意 这一段,这才是与客户端同步TE数据调用的方法。
如果不重写这些方法,那么 world.notifyBlockUpdate ...
已经重写了这些方法,但是问题还在
附上write与readNBT的method
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setTag("inventory", inventory.serializeNBT());
- compound.setInteger("generateTick", generateTick);
- storage.writeToNBT(compound);
- return compound;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- inventory.deserializeNBT(compound.getCompoundTag("inventory"));
- generateTick = compound.getInteger("generateTick");
- storage.readFromNBT(compound);
- }
- @Override
- @Nullable
- public SPacketUpdateTileEntity getUpdatePacket() {
- NBTTagCompound updateTagDescribingTileEntityState = getUpdateTag();
- final int METADATA = 0;
- return new SPacketUpdateTileEntity(this.pos, METADATA, updateTagDescribingTileEntityState);
- }
- @Override
- public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
- NBTTagCompound updateTagDescribingTileEntityState = pkt.getNbtCompound();
- handleUpdateTag(updateTagDescribingTileEntityState);
- }
- @Override
- public NBTTagCompound getUpdateTag() {
- NBTTagCompound nbtTagCompound = new NBTTagCompound();
- return writeToNBT(nbtTagCompound);
- }
- @Override
- public void handleUpdateTag(NBTTagCompound tag) {
- this.readFromNBT(tag);
- }
在GUI中进行同步需要重写 这一部分 (注意看注释)。
bxz2311196368 发表于 2019-5-4 22:35
以上,TE中的4个方法在区块加载阶段触发。GUI的方法只在GUI打开阶段循环触发。
如果需要全程进行同步,那么 ...
补一句,getUpdatePacket() 和 onDataPacket() 只会在 setBlockState()/notifyBlockUpdate 的时候触发,getUpdateTag()/handleUpdateTag() 只在区块加载时触发。