Prechádzať zdrojové kódy

(Backport) Introduce EntitySpawnExtension (#160)

Signed-off-by: Max <maxh2709@gmail.com>
shedaniel 3 rokov pred
rodič
commit
9913b1ea5d

+ 34 - 0
common/src/main/java/me/shedaniel/architectury/extensions/network/EntitySpawnExtension.java

@@ -0,0 +1,34 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 architectury
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.extensions.network;
+
+import me.shedaniel.architectury.networking.NetworkManager;
+import net.minecraft.network.FriendlyByteBuf;
+import net.minecraft.world.entity.Entity;
+
+/**
+ * This interface can be implemented on {@linkplain Entity entities} to attach additional spawn data to packets sent to client.
+ * This is used in conjunction with {@link NetworkManager#createAddEntityPacket(Entity)}
+ */
+public interface EntitySpawnExtension {
+    void saveAdditionalSpawnData(FriendlyByteBuf buf);
+    
+    void loadAdditionalSpawnData(FriendlyByteBuf buf);
+}

+ 4 - 0
common/src/main/java/me/shedaniel/architectury/networking/NetworkManager.java

@@ -20,6 +20,7 @@
 package me.shedaniel.architectury.networking;
 
 import dev.architectury.injectables.annotations.ExpectPlatform;
+import me.shedaniel.architectury.extensions.network.EntitySpawnExtension;
 import me.shedaniel.architectury.networking.transformers.PacketCollector;
 import me.shedaniel.architectury.networking.transformers.PacketSink;
 import me.shedaniel.architectury.networking.transformers.PacketTransformer;
@@ -98,6 +99,9 @@ public final class NetworkManager {
      * Easy to use utility method to create an entity spawn packet.
      * This packet is needed everytime any mod adds a non-living entity.
      * The entity should override {@link Entity#getAddEntityPacket()} to point to this method!
+     * <p>
+     * Additionally, entities may implement {@link EntitySpawnExtension}
+     * to load / save additional data to the client.
      *
      * @param entity The entity which should be spawned.
      * @return The ready to use packet to spawn the entity on the client.

+ 7 - 0
fabric/src/main/java/me/shedaniel/architectury/networking/fabric/SpawnEntityPacket.java

@@ -19,6 +19,7 @@
 
 package me.shedaniel.architectury.networking.fabric;
 
+import me.shedaniel.architectury.extensions.network.EntitySpawnExtension;
 import me.shedaniel.architectury.networking.NetworkManager;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
@@ -59,6 +60,9 @@ public class SpawnEntityPacket {
         buffer.writeDouble(deltaMovement.x);
         buffer.writeDouble(deltaMovement.y);
         buffer.writeDouble(deltaMovement.z);
+        if (entity instanceof EntitySpawnExtension) {
+            ((EntitySpawnExtension) entity).saveAdditionalSpawnData(buffer);
+        }
         return NetworkManager.toPacket(NetworkManager.s2c(), PACKET_ID, buffer);
     }
     
@@ -102,6 +106,9 @@ public class SpawnEntityPacket {
                 entity.absMoveTo(x, y, z, xRot, yRot);
                 entity.setYHeadRot(yHeadRot);
                 entity.setYBodyRot(yHeadRot);
+                if (entity instanceof EntitySpawnExtension) {
+                    ((EntitySpawnExtension) entity).loadAdditionalSpawnData(buf);
+                }
                 Minecraft.getInstance().level.putNonPlayerEntity(id, entity);
                 entity.lerpMotion(deltaX, deltaY, deltaZ);
             });

+ 38 - 0
forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinEntitySpawnExtension.java

@@ -0,0 +1,38 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 architectury
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.mixin.forge;
+
+import me.shedaniel.architectury.extensions.network.EntitySpawnExtension;
+import net.minecraft.network.FriendlyByteBuf;
+import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
+import org.spongepowered.asm.mixin.Mixin;
+
+@Mixin(EntitySpawnExtension.class)
+public interface MixinEntitySpawnExtension extends IEntityAdditionalSpawnData {
+    @Override
+    default void writeSpawnData(FriendlyByteBuf buf) {
+        ((EntitySpawnExtension) this).saveAdditionalSpawnData(buf);
+    }
+    
+    @Override
+    default void readSpawnData(FriendlyByteBuf buf) {
+        ((EntitySpawnExtension) this).loadAdditionalSpawnData(buf);
+    }
+}

+ 1 - 0
forge/src/main/resources/architectury.mixins.json

@@ -19,6 +19,7 @@
     "MixinBlockEntity",
     "MixinBlockEntityExtension",
     "MixinChunkSerializer",
+    "MixinEntitySpawnExtension",
     "MixinItemExtension",
     "MixinRegistryEntry",
     "MixinWorldEvent",