mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-21 20:52:21 +00:00
feat: add client multipart preview API
This commit is contained in:
1 parent
a995189f92
commit
e13389f26f
21 files changed
+316
-33
No files matched your search
@@ -1,14 +1,18 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.multipart.handler.MultipartProxy;
|
||||
import codechicken.multipart.handler.MultipartSPH;
|
||||
import codechicken.multipart.handler.MultipartSaveLoad;
|
||||
import scala.collection.JavaConversions;
|
||||
@@ -46,6 +50,68 @@ public final class MultipartHelper {
|
||||
return TileMultipart.createFromNBT(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a render-ready client multipart tile from saved NBT without installing it into a world. Every registered
|
||||
* part factory must support
|
||||
* {@link MultiPartRegistry.IPartFactory2#createPartForClientPreview(String, NBTTagCompound)}; otherwise the whole
|
||||
* preview is rejected and null is returned. Part order and saved state are preserved.
|
||||
*
|
||||
* <p>
|
||||
* A non-null world must be client-side. The tile receives its saved coordinates, binds every part, then runs
|
||||
* {@link TMultiPart#onWorldJoin()} and local {@link TMultiPart#onPartChanged(TMultiPart)} callbacks before
|
||||
* rebuilding its render state. It is not validated, placed in a chunk, or announced to neighboring blocks. A null
|
||||
* world creates a worldless staged preview; render caches initialize lazily when a renderer uses the tile.
|
||||
*
|
||||
* @param world client preview world, or null for worldless staging
|
||||
* @param tag saved multipart NBT; it is read but not mutated
|
||||
* @return a prepared client tile, or null for a foreign/empty tag, missing factory, or unsupported preview part
|
||||
* @throws IllegalArgumentException if world is server-side
|
||||
*/
|
||||
public static TileMultipart createPreviewTileFromNBT(World world, NBTTagCompound tag) {
|
||||
if (tag == null || !tag.getString("id").equals("savedMultipart")) {
|
||||
return null;
|
||||
}
|
||||
if (world != null && !world.isRemote) {
|
||||
throw new IllegalArgumentException("Multipart previews require a client world");
|
||||
}
|
||||
|
||||
NBTTagList partTags = tag.getTagList("parts", 10);
|
||||
List<TMultiPart> parts = new ArrayList<>(partTags.tagCount());
|
||||
for (int i = 0; i < partTags.tagCount(); i++) {
|
||||
NBTTagCompound partTag = partTags.getCompoundTagAt(i);
|
||||
String partId = partTag.getString("id");
|
||||
MultiPartRegistry.IPartFactory2 factory = MultiPartRegistry.getPartFactory(partId);
|
||||
if (factory == null) {
|
||||
MultipartProxy.logger().error("Missing mapping for preview part with ID: " + partId);
|
||||
return null;
|
||||
}
|
||||
|
||||
TMultiPart part = factory.createPartForClientPreview(partId, partTag);
|
||||
if (part == null) {
|
||||
MultipartProxy.logger().warn("Part factory does not support client previews for ID: " + partId);
|
||||
return null;
|
||||
}
|
||||
part.load(partTag);
|
||||
parts.add(part);
|
||||
}
|
||||
if (parts.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TileMultipart tile = MultipartGenerator.generateCompositeTile(null, parts, true);
|
||||
tile.readFromNBT(tag);
|
||||
tile.loadPartList(parts);
|
||||
tile.setWorldObj(world);
|
||||
if (world != null) {
|
||||
tile.forEachPart(TMultiPart::onWorldJoin);
|
||||
}
|
||||
tile.internalPartChange(null);
|
||||
if (world != null) {
|
||||
tile.markRender();
|
||||
}
|
||||
return tile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note. This method should only be used to send tiles that have been created on the server mid-game via an NBT load
|
||||
* to clients.
|
||||
|
||||
Reference in new issue
Block a user