Changes

537 bytes removed ,  18:01, 20 September 2022
no edit summary
Line 4: Line 4:     
== Creating a <code>BlockEntity</code> ==
 
== Creating a <code>BlockEntity</code> ==
  −
In order to create a <code>BlockEntity</code> you need to extend the <code>BlockEntity</code> class. To register it, listen for the appropriate registry event and create a <code>BlockEntityType</code>:
  −
<syntaxhighlight lang="java">
  −
@SubscribeEvent
  −
public static void registerTE(RegistryEvent.Register<BlockEntityType<?>> evt) {
  −
  BlockEntityType<?> type = BlockEntityType.Builder.of(supplier, validBlocks).build(null);
  −
  type.setRegistryName("examplemod", "mybe");
  −
  evt.getRegistry().register(type);
  −
}
  −
</syntaxhighlight>
   
You can also use a <code>DeferredRegister</code> instead.
 
You can also use a <code>DeferredRegister</code> instead.
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
Line 24: Line 14:     
== Attaching a <code>BlockEntity</code> to a <code>Block</code> ==
 
== Attaching a <code>BlockEntity</code> to a <code>Block</code> ==
To attach your new <code>BlockEntity</code> to a <code>Block</code>, the <code>EntityBlock</code> interface must be implemented on your <code>Block</code> subclass. The method <code>EntityBlock#newBlockEntity(BlockPos, BlockState)</code> must be implemented and return a new isntance of your <code>BlockEntity</code>.
+
To attach your new <code>BlockEntity</code> to a <code>Block</code>, the <code>EntityBlock</code> interface must be implemented on your <code>Block</code> subclass. The method <code>EntityBlock#newBlockEntity(BlockPos, BlockState)</code> must be implemented and return a new instance of your <code>BlockEntity</code>.
    
== Storing Data within your <code>BlockEntity</code> ==
 
== Storing Data within your <code>BlockEntity</code> ==
 
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 64:     
=== 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 89:  
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 ===
11

edits