mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
Readd modded entity spawning/tracking support
This commit is contained in:
1 parent
2e1456517a
commit
1f11ac76fa
8 files changed
+272
-48
No files matched your search
+3
-3
@@ -19,7 +19,7 @@
|
||||
|
||||
package net.minecraftforge.fml.common.registry;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
/**
|
||||
* A interface for Entities that need extra information to be communicated
|
||||
@@ -33,7 +33,7 @@ public interface IEntityAdditionalSpawnData
|
||||
*
|
||||
* @param buffer The packet data stream
|
||||
*/
|
||||
void writeSpawnData(ByteBuf buffer);
|
||||
void writeSpawnData(PacketBuffer buffer);
|
||||
|
||||
/**
|
||||
* Called by the client when it receives a Entity spawn packet.
|
||||
@@ -41,5 +41,5 @@ public interface IEntityAdditionalSpawnData
|
||||
*
|
||||
* @param additionalData The packet data stream
|
||||
*/
|
||||
void readSpawnData(ByteBuf additionalData);
|
||||
void readSpawnData(PacketBuffer additionalData);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library 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 version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library 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 library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fml.common.registry;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
|
||||
/**
|
||||
* This interface should be implemented by an Entity that can be 'thrown', like snowballs.
|
||||
* This was created to mimic ModLoaderMP's 'owner' functionality.
|
||||
*/
|
||||
public interface IThrowableEntity
|
||||
{
|
||||
/**
|
||||
* Gets the entity that threw/created this entity.
|
||||
* @return The owner instance, Null if none.
|
||||
*/
|
||||
Entity getThrower();
|
||||
|
||||
/**
|
||||
* Sets the entity that threw/created this entity.
|
||||
* @param entity The new thrower/creator.
|
||||
*/
|
||||
void setThrower(Entity entity);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.minecraftforge.fml.network;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
||||
|
||||
public class FMLPlayHandler
|
||||
{
|
||||
public static final SimpleChannel channel = NetworkRegistry.ChannelBuilder
|
||||
.named(new ResourceLocation("fml", "play"))
|
||||
.clientAcceptedVersions(a -> true)
|
||||
.serverAcceptedVersions(a -> true)
|
||||
.networkProtocolVersion(() -> NetworkHooks.NETVERSION)
|
||||
.simpleChannel();
|
||||
static
|
||||
{
|
||||
channel.registerMessage(0, FMLPlayMessages.SpawnEntity.class, FMLPlayMessages.SpawnEntity::encode, FMLPlayMessages.SpawnEntity::decode, FMLPlayMessages.SpawnEntity::handle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package net.minecraftforge.fml.network;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityTracker;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class FMLPlayMessages
|
||||
{
|
||||
public static class SpawnEntity
|
||||
{
|
||||
private final Entity entity;
|
||||
private final int typeId;
|
||||
private final int entityId;
|
||||
private final UUID uuid;
|
||||
private final double posX, posY, posZ;
|
||||
private final byte pitch, yaw;
|
||||
private final short velX, velY, velZ;
|
||||
private final PacketBuffer buf;
|
||||
|
||||
SpawnEntity(Entity e)
|
||||
{
|
||||
this.entity = e;
|
||||
this.typeId = EntityType.REGISTRY.getId(e.getType());
|
||||
this.entityId = e.getEntityId();
|
||||
this.uuid = e.getUniqueID();
|
||||
this.posX = e.posX;
|
||||
this.posY = e.posY;
|
||||
this.posZ = e.posZ;
|
||||
this.pitch = (byte) MathHelper.floor(e.rotationPitch * 256.0F / 360.0F);
|
||||
this.yaw = (byte) MathHelper.floor(e.rotationYaw * 256.0F / 360.0F);
|
||||
this.velX = (short)(MathHelper.clamp(e.motionX, -3.9D, 3.9D) * 8000.0D);
|
||||
this.velY = (short)(MathHelper.clamp(e.motionY, -3.9D, 3.9D) * 8000.0D);
|
||||
this.velZ = (short)(MathHelper.clamp(e.motionZ, -3.9D, 3.9D) * 8000.0D);
|
||||
this.buf = null;
|
||||
}
|
||||
|
||||
private SpawnEntity(int typeId, int entityId, UUID uuid, double posX, double posY, double posZ,
|
||||
byte pitch, byte yaw, short velX, short velY, short velZ, PacketBuffer buf)
|
||||
{
|
||||
this.entity = null;
|
||||
this.typeId = typeId;
|
||||
this.entityId = entityId;
|
||||
this.uuid = uuid;
|
||||
this.posX = posX;
|
||||
this.posY = posY;
|
||||
this.posZ = posZ;
|
||||
this.pitch = pitch;
|
||||
this.yaw = yaw;
|
||||
this.velX = velX;
|
||||
this.velY = velY;
|
||||
this.velZ = velZ;
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
public static void encode(SpawnEntity msg, PacketBuffer buf)
|
||||
{
|
||||
buf.writeVarInt(msg.typeId);
|
||||
buf.writeInt(msg.entityId);
|
||||
buf.writeLong(msg.uuid.getMostSignificantBits());
|
||||
buf.writeLong(msg.uuid.getLeastSignificantBits());
|
||||
buf.writeDouble(msg.posX);
|
||||
buf.writeDouble(msg.posY);
|
||||
buf.writeDouble(msg.posZ);
|
||||
buf.writeByte(msg.pitch);
|
||||
buf.writeByte(msg.yaw);
|
||||
buf.writeShort(msg.velX);
|
||||
buf.writeShort(msg.velY);
|
||||
buf.writeShort(msg.velZ);
|
||||
if (msg.entity instanceof IEntityAdditionalSpawnData)
|
||||
{
|
||||
((IEntityAdditionalSpawnData) msg.entity).writeSpawnData(buf);
|
||||
}
|
||||
}
|
||||
|
||||
public static SpawnEntity decode(PacketBuffer buf)
|
||||
{
|
||||
return new SpawnEntity(
|
||||
buf.readVarInt(),
|
||||
buf.readInt(),
|
||||
new UUID(buf.readLong(), buf.readLong()),
|
||||
buf.readDouble(), buf.readDouble(), buf.readDouble(),
|
||||
buf.readByte(), buf.readByte(),
|
||||
buf.readShort(), buf.readShort(), buf.readShort(),
|
||||
buf
|
||||
);
|
||||
}
|
||||
|
||||
public static void handle(SpawnEntity msg, Supplier<NetworkEvent.Context> ctx)
|
||||
{
|
||||
ctx.get().enqueueWork(() -> {
|
||||
EntityType<?> type = EntityType.REGISTRY.get(msg.typeId);
|
||||
if (type == null)
|
||||
{
|
||||
throw new RuntimeException(String.format("Could not spawn entity (id %d) with unknown type at (%f, %f, %f)", msg.entityId, msg.posX, msg.posY, msg.posZ));
|
||||
}
|
||||
|
||||
Entity e = type.handleSpawnMessage(Minecraft.getInstance().world, msg);
|
||||
if (e == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EntityTracker.updateServerPosition(e, msg.posX, msg.posY, msg.posZ);
|
||||
e.rotationPitch = (float)(msg.pitch * 360) / 256.0F;
|
||||
e.rotationYaw = (float)(msg.yaw * 360) / 256.0F;
|
||||
|
||||
Entity[] parts = e.getParts();
|
||||
if (parts != null)
|
||||
{
|
||||
int offset = msg.entityId - e.getEntityId();
|
||||
for (Entity part : parts)
|
||||
{
|
||||
part.setEntityId(part.getEntityId() + offset);
|
||||
}
|
||||
}
|
||||
|
||||
e.setEntityId(msg.entityId);
|
||||
e.setUniqueId(msg.uuid);
|
||||
Minecraft.getInstance().world.addEntityToWorld(msg.entityId, e);
|
||||
e.setVelocity(msg.velX / 8000.0, msg.velY / 8000.0, msg.velZ / 8000.0);
|
||||
if (e instanceof IEntityAdditionalSpawnData)
|
||||
{
|
||||
((IEntityAdditionalSpawnData) e).readSpawnData(msg.buf);
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,10 @@ public class NetworkHooks
|
||||
|
||||
public static Packet<?> getEntitySpawningPacket(Entity entity)
|
||||
{
|
||||
if (!entity.getType().usesVanillaSpawning())
|
||||
{
|
||||
return FMLPlayHandler.channel.toVanillaPacket(new FMLPlayMessages.SpawnEntity(entity), NetworkDirection.PLAY_TO_CLIENT);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,8 +83,12 @@ public class SimpleChannel
|
||||
|
||||
public <MSG> void sendTo(MSG message, NetworkManager manager, NetworkDirection direction)
|
||||
{
|
||||
ICustomPacket<Packet<?>> payload = direction.buildPacket(toBuffer(message), instance.getChannelName());
|
||||
manager.sendPacket(payload.getThis());
|
||||
manager.sendPacket(toVanillaPacket(message, direction));
|
||||
}
|
||||
|
||||
public <MSG> Packet<?> toVanillaPacket(MSG message, NetworkDirection direction)
|
||||
{
|
||||
return direction.buildPacket(toBuffer(message), instance.getChannelName()).getThis();
|
||||
}
|
||||
|
||||
public <MSG> void reply(MSG msgToReply, NetworkEvent.Context context)
|
||||
|
||||
Reference in new issue
Block a user