mirror of
https://github.com/architectury/architectury-api.git
synced 2026-03-28 03:56:59 -05:00
Move EnvExecutor executions to another class
This commit is contained in:
@@ -27,6 +27,6 @@ public class ArchitecturyClient {
|
||||
public static void init() {
|
||||
ClientLifecycleEvent.CLIENT_SETUP.invoker().stateChanged(Minecraft.getInstance());
|
||||
|
||||
SpawnEntityPacket.register();
|
||||
SpawnEntityPacket.Client.register();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
package dev.architectury.networking.fabric;
|
||||
|
||||
import dev.architectury.networking.NetworkManager;
|
||||
import dev.architectury.utils.Env;
|
||||
import dev.architectury.utils.EnvExecutor;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||
@@ -38,11 +36,6 @@ import net.minecraft.world.entity.Entity;
|
||||
public class SpawnEntityPacket {
|
||||
private static final ResourceLocation PACKET_ID = new ResourceLocation("architectury", "spawn_entity_packet");
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void register() {
|
||||
NetworkManager.registerReceiver(NetworkManager.s2c(), PACKET_ID, SpawnEntityPacket::receive);
|
||||
}
|
||||
|
||||
public static Packet<?> create(Entity entity) {
|
||||
if (entity.level.isClientSide()) {
|
||||
throw new IllegalStateException("SpawnPacketUtil.create called on the logical client!");
|
||||
@@ -65,40 +58,49 @@ public class SpawnEntityPacket {
|
||||
return NetworkManager.toPacket(NetworkManager.s2c(), PACKET_ID, buffer);
|
||||
}
|
||||
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void receive(FriendlyByteBuf buf, NetworkManager.PacketContext context) {
|
||||
var entityTypeId = buf.readVarInt();
|
||||
var uuid = buf.readUUID();
|
||||
var id = buf.readVarInt();
|
||||
var x = buf.readDouble();
|
||||
var y = buf.readDouble();
|
||||
var z = buf.readDouble();
|
||||
var xRot = buf.readFloat();
|
||||
var yRot = buf.readFloat();
|
||||
var yHeadRot = buf.readFloat();
|
||||
var deltaX = buf.readDouble();
|
||||
var deltaY = buf.readDouble();
|
||||
var deltaZ = buf.readDouble();
|
||||
EnvExecutor.runInEnv(Env.CLIENT, () -> () -> context.queue(() -> {
|
||||
var entityType = Registry.ENTITY_TYPE.byId(entityTypeId);
|
||||
if (entityType == null) {
|
||||
throw new IllegalStateException("Entity type (" + entityTypeId + ") is unknown, spawning at (" + x + ", " + y + ", " + z + ")");
|
||||
}
|
||||
if (Minecraft.getInstance().level == null) {
|
||||
throw new IllegalStateException("Client world is null!");
|
||||
}
|
||||
var entity = entityType.create(Minecraft.getInstance().level);
|
||||
if (entity == null) {
|
||||
throw new IllegalStateException("Created entity is null!");
|
||||
}
|
||||
entity.setUUID(uuid);
|
||||
entity.setId(id);
|
||||
entity.setPacketCoordinates(x, y, z);
|
||||
entity.absMoveTo(x, y, z, xRot, yRot);
|
||||
entity.setYHeadRot(yHeadRot);
|
||||
entity.setYBodyRot(yHeadRot);
|
||||
Minecraft.getInstance().level.putNonPlayerEntity(id, entity);
|
||||
entity.lerpMotion(deltaX, deltaY, deltaZ);
|
||||
}));
|
||||
public static class Client {
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void register() {
|
||||
NetworkManager.registerReceiver(NetworkManager.s2c(), PACKET_ID, Client::receive);
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void receive(FriendlyByteBuf buf, NetworkManager.PacketContext context) {
|
||||
var entityTypeId = buf.readVarInt();
|
||||
var uuid = buf.readUUID();
|
||||
var id = buf.readVarInt();
|
||||
var x = buf.readDouble();
|
||||
var y = buf.readDouble();
|
||||
var z = buf.readDouble();
|
||||
var xRot = buf.readFloat();
|
||||
var yRot = buf.readFloat();
|
||||
var yHeadRot = buf.readFloat();
|
||||
var deltaX = buf.readDouble();
|
||||
var deltaY = buf.readDouble();
|
||||
var deltaZ = buf.readDouble();
|
||||
context.queue(() -> {
|
||||
var entityType = Registry.ENTITY_TYPE.byId(entityTypeId);
|
||||
if (entityType == null) {
|
||||
throw new IllegalStateException("Entity type (" + entityTypeId + ") is unknown, spawning at (" + x + ", " + y + ", " + z + ")");
|
||||
}
|
||||
if (Minecraft.getInstance().level == null) {
|
||||
throw new IllegalStateException("Client world is null!");
|
||||
}
|
||||
var entity = entityType.create(Minecraft.getInstance().level);
|
||||
if (entity == null) {
|
||||
throw new IllegalStateException("Created entity is null!");
|
||||
}
|
||||
entity.setUUID(uuid);
|
||||
entity.setId(id);
|
||||
entity.setPacketCoordinates(x, y, z);
|
||||
entity.absMoveTo(x, y, z, xRot, yRot);
|
||||
entity.setYHeadRot(yHeadRot);
|
||||
entity.setYBodyRot(yHeadRot);
|
||||
Minecraft.getInstance().level.putNonPlayerEntity(id, entity);
|
||||
entity.lerpMotion(deltaX, deltaY, deltaZ);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
package dev.architectury.test;
|
||||
|
||||
import dev.architectury.platform.Platform;
|
||||
import dev.architectury.registry.level.entity.EntityRendererRegistry;
|
||||
import dev.architectury.test.debug.ConsoleMessageSink;
|
||||
import dev.architectury.test.debug.MessageSink;
|
||||
@@ -54,16 +53,17 @@ public class TestMod {
|
||||
TestParticles.initialize();
|
||||
TestModNet.initialize();
|
||||
TestBlockInteractions.init();
|
||||
if (Platform.getEnvironment() == Env.CLIENT) {
|
||||
initializeClient();
|
||||
}
|
||||
EnvExecutor.runInEnv(Env.CLIENT, () -> TestMod.Client::initializeClient);
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void initializeClient() {
|
||||
TestKeybinds.initialize();
|
||||
TestModNet.initializeClient();
|
||||
EntityRendererRegistry.register(() -> TestEntity.TYPE, context ->
|
||||
new MinecartRenderer<>(context, ModelLayers.MINECART));
|
||||
public static class Client {
|
||||
@Environment(EnvType.CLIENT)
|
||||
public static void initializeClient() {
|
||||
TestKeybinds.initialize();
|
||||
TestModNet.initializeClient();
|
||||
EntityRendererRegistry.register(() -> TestEntity.TYPE, context ->
|
||||
new MinecartRenderer<>(context, ModelLayers.MINECART));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user