Search in sources :

Example 1 with UnknownComponentException

use of io.xol.chunkstories.api.exceptions.UnknownComponentException in project chunkstories by Hugobros3.

the class EntitySerializer method readEntityFromStream.

public static Entity readEntityFromStream(DataInputStream dis, OfflineSerializedData source, World world) {
    try {
        int entityDataLength = dis.readInt();
        if (entityDataLength == -1)
            return null;
        DataInputStream in = LengthAwareBufferedIOHelper.getLengthAwareInput(entityDataLength, dis);
        long entityUUID = in.readLong();
        // Obsolete ?
        // When we reach id -1 in a stream of entities, it means we reached the end.
        // if(entityUUID == -1)
        // return null;
        short entityTypeID = in.readShort();
        /*System.out.println("world"+world);
			System.out.println("world.getGameContext()"+world.getGameContext());
			System.out.println("world.getGameContext().getContent().entities()"+world.getGameContext().getContent().entities());
			System.out.println("world.getGameContext().getContent().entities().getEntityTypeById(entityTypeID)"+world.getGameContext().getContent().entities().getEntityTypeById(entityTypeID));
			*/
        Entity entity = world.getContentTranslator().getEntityForId(entityTypeID).create(new Location(world, 0d, 0d, 0d));
        entity.setUUID(entityUUID);
        int componentId = in.readInt();
        // Loop throught all components
        while (true) {
            if (// End of components to read
            componentId == 0)
                break;
            else if (componentId == -1) {
                // Read UTF-8 component name
                String componentName = in.readUTF();
                try {
                    entity.getComponents().tryPullComponentInStream(componentName, source, in);
                } catch (UnknownComponentException e) {
                    logger().warn("Failure reading component " + componentName + " from " + source);
                    logger().warn(e.getMessage());
                }
            } else {
                // Read int32 component id
                try {
                    entity.getComponents().tryPullComponentInStream(componentId, source, in);
                } catch (UnknownComponentException e) {
                    logger().warn(e.getMessage());
                }
            }
            componentId = in.readInt();
        }
        return entity;
    } catch (NullPointerException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) UnknownComponentException(io.xol.chunkstories.api.exceptions.UnknownComponentException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Location(io.xol.chunkstories.api.Location)

Example 2 with UnknownComponentException

use of io.xol.chunkstories.api.exceptions.UnknownComponentException in project chunkstories-api by Hugobros3.

the class PacketEntity method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, UnknownComponentException {
    long entityUUID = in.readLong();
    short entityTypeID = in.readShort();
    if (entityTypeID == -1)
        return;
    World world = processor.getWorld();
    if (world == null)
        return;
    Entity entity = world.getEntityByUUID(entityUUID);
    boolean addToWorld = false;
    // Create an entity if the servers tells you to do so
    if (entity == null) {
        if (world instanceof WorldMaster && sender instanceof RemotePlayer) {
            ((Player) sender).sendMessage("You are sending packets to the server about a removed entity. Ignoring those.");
            return;
        } else {
            entity = processor.getWorld().getContentTranslator().getEntityForId(entityTypeID).create(// This is technically wrong
            new Location(world, 0, 0, 0));
            entity.setUUID(entityUUID);
            addToWorld = true;
        }
    }
    int componentId = in.readInt();
    // Loop throught all components
    while (componentId != 0) {
        try {
            entity.getComponents().tryPullComponentInStream(componentId, sender, in);
        } catch (UnknownComponentException e) {
            processor.logger().warn(e.getMessage());
        }
        componentId = in.readInt();
    }
    // Add to world if it was missing and we didn't receive the despawn flag
    if (addToWorld && entity.exists()) {
        // Only the WorldMaster is allowed to spawn new entities in the world
        if (processor instanceof ClientPacketsProcessor)
            processor.getWorld().addEntity(entity);
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) UnknownComponentException(io.xol.chunkstories.api.exceptions.UnknownComponentException) World(io.xol.chunkstories.api.world.World) PacketWorld(io.xol.chunkstories.api.net.PacketWorld) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Aggregations

Location (io.xol.chunkstories.api.Location)2 Entity (io.xol.chunkstories.api.entity.Entity)2 UnknownComponentException (io.xol.chunkstories.api.exceptions.UnknownComponentException)2 ClientPacketsProcessor (io.xol.chunkstories.api.client.net.ClientPacketsProcessor)1 PacketWorld (io.xol.chunkstories.api.net.PacketWorld)1 Player (io.xol.chunkstories.api.player.Player)1 RemotePlayer (io.xol.chunkstories.api.server.RemotePlayer)1 World (io.xol.chunkstories.api.world.World)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1