Changes

13 bytes removed ,  13:44, 12 January 2022
Update to 1.18
Line 29: Line 29:  
In order to save data, override the following two methods  
 
In order to save data, override the following two methods  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
BlockEntity#save(CompoundTag tag)
+
BlockEntity#saveAdditional(CompoundTag tag)
    
BlockEntity#load(CompoundTag tag)
 
BlockEntity#load(CompoundTag tag)
Line 74: Line 74:     
=== Synchronizing on block update  ===
 
=== Synchronizing on block update  ===
This method is a bit more complicated, but again you just need to override 2 methods. Here is a tiny example implementation of it:
+
This method is a bit more complicated, but again you just need to override 2 or 3 methods. Here is a tiny example implementation of it:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
@Override
 
@Override
public ClientboundBlockEntityDataPacket getUpdatePacket(){
+
public CompoundTag getUpdateTag() {
    CompoundTag tag = new CompoundTag();
+
  CompoundTag tag = new CompoundTag();
    //Write your data into the tag
+
  //Write your data into the tag
    return new ClientboundBlockEntityDataPacket(getBlockPos(), -1, tag);
+
  return tag;
 
}
 
}
    
@Override
 
@Override
public void onDataPacket(Connection connection, ClientboundBlockEntityDataPacket pkt){
+
public Packet<ClientGamePacketListener> getUpdatePacket() {
    CompoundTag tag = pkt.getTag();
+
  // Will get tag from #getUpdateTag
    //Handle your Data
+
  return ClientboundBlockEntityDataPacket.create(this);
 
}
 
}
 +
 +
// Can override IForgeBlockEntity#onDataPacket. By default, this will defer to the #load.
 
</syntaxhighlight>
 
</syntaxhighlight>
The Constructor of <code>ClientboundBlockEntityDataPacket</code> takes:
+
The static constructors <code>ClientboundBlockEntityDataPacket#create</code> takes:
* The position of your <code>BlockEntity</code>.
+
* The <code>BlockEntity</code>.
* An ID, though it isn’t really used besides by Vanilla, therefore you can just put a -1 in there.
+
* An optional function to get the <code>CompoundTag</code> from the <code>BlockEntity</code>. By default, this uses <code>BlockEntity#getUpdateTag</code>.
* A <code>CompoundTag</code> which should contain your data.
      
Now, to send the packet, an update notification must be given on the server.  
 
Now, to send the packet, an update notification must be given on the server.  
Line 98: Line 99:  
Level#sendBlockUpdated(BlockPos pos, BlockState oldState, BlockState newState, int flags)
 
Level#sendBlockUpdated(BlockPos pos, BlockState oldState, BlockState newState, int flags)
 
</code>
 
</code>
The <code>pos</code> should be your <code>BlockEntity</code>’s position. For <code>oldState</code> and <code>newState</code> you can pass the current <code>BlockState</code> at that position. The <code>flags</code> are a bitmask and should contain <code>2</code>, which will sync the changes to the client. See <code>Constants$BlockFlags</code> for more info as well as the rest of the flags. The flag <code>2</code> is equivalent to <code><nowiki>Constants$BlockFlags#BLOCK_UPDATE</nowiki></code>.
+
The <code>pos</code> should be your <code>BlockEntity</code>’s position. For <code>oldState</code> and <code>newState</code> you can pass the current <code>BlockState</code> at that position. The <code>flags</code> are a bitmask and should contain <code>2</code>, which will sync the changes to the client. See <code>Block</code> for more info as well as the rest of the flags. The flag <code>2</code> is equivalent to <code><nowiki>Block#UPDATE_CLIENTS</nowiki></code>.
    
=== Synchronizing using a custom network message ===
 
=== Synchronizing using a custom network message ===