Search in sources :

Example 1 with Entity

use of org.pepsoft.minecraft.Entity in project WorldPainter by Captain-Chaos.

the class WPObjectExporter method renderObject.

/**
 * Export an object to the world, optionally taking into account the blocks
 * that are already there.
 *
 * @param world The Minecraft world to which to export the object.
 * @param dimension The dimension corresponding to the exported world, used
 *     to determine the terrain height.
 * @param object The object to export.
 * @param x The X coordinate at which to export the object.
 * @param y The Y coordinate at which to export the object.
 * @param z The Z coordinate at which to export the object.
 * @param obliterate When <code>true</code>, all blocks of the object are
 *     placed regardless of what is already there. When <code>false</code>,
 *     rules are followed and some or all blocks may not be placed,
 *     depending on what is already there.
 */
public static void renderObject(MinecraftWorld world, Dimension dimension, WPObject object, int x, int y, int z, boolean obliterate) {
    final Point3i dim = object.getDimensions();
    final Point3i offset = object.getOffset();
    final int undergroundMode = object.getAttribute(ATTRIBUTE_UNDERGROUND_MODE);
    final int leafDecayMode = object.getAttribute(ATTRIBUTE_LEAF_DECAY_MODE);
    final boolean bottomless = dimension.isBottomless();
    final int[] replaceBlockIds = object.getAttribute(ATTRIBUTE_REPLACE_WITH_AIR);
    final boolean replaceBlocks = replaceBlockIds != null;
    final boolean extendFoundation = object.getAttribute(ATTRIBUTE_EXTEND_FOUNDATION);
    if ((z + offset.z + dim.z - 1) >= world.getMaxHeight()) {
        // Object doesn't fit in the world vertically
        return;
    }
    // System.out.println("Object dimensions: " + dim + ", origin: " + orig);
    for (int dx = 0; dx < dim.x; dx++) {
        for (int dy = 0; dy < dim.y; dy++) {
            final int worldX = x + dx + offset.x;
            final int worldY = y + dy + offset.y;
            final int terrainHeight = dimension.getIntHeightAt(worldX, worldY);
            for (int dz = 0; dz < dim.z; dz++) {
                if (object.getMask(dx, dy, dz)) {
                    final Material objectMaterial = object.getMaterial(dx, dy, dz);
                    final Material finalMaterial = (replaceBlocks && (objectMaterial.blockType == replaceBlockIds[0]) && (objectMaterial.data == replaceBlockIds[1])) ? Material.AIR : objectMaterial;
                    final int worldZ = z + dz + offset.z;
                    if ((bottomless || obliterate) ? (worldZ < 0) : (worldZ < 1)) {
                        continue;
                    } else if (obliterate) {
                        placeBlock(world, worldX, worldY, worldZ, finalMaterial, leafDecayMode);
                    } else {
                        final int existingBlockType = world.getBlockTypeAt(worldX, worldY, worldZ);
                        if (worldZ <= terrainHeight) {
                            switch(undergroundMode) {
                                case COLLISION_MODE_ALL:
                                    // Replace every block
                                    placeBlock(world, worldX, worldY, worldZ, finalMaterial, leafDecayMode);
                                    break;
                                case COLLISION_MODE_SOLID:
                                    // Only replace if object block is solid
                                    if (!objectMaterial.block.veryInsubstantial) {
                                        placeBlock(world, worldX, worldY, worldZ, finalMaterial, leafDecayMode);
                                    }
                                    break;
                                case COLLISION_MODE_NONE:
                                    // Only replace less solid blocks
                                    if (BLOCKS[existingBlockType].veryInsubstantial) {
                                        placeBlock(world, worldX, worldY, worldZ, finalMaterial, leafDecayMode);
                                    }
                                    break;
                            }
                        } else {
                            // Above ground only replace less solid blocks
                            if (BLOCKS[existingBlockType].veryInsubstantial) {
                                placeBlock(world, worldX, worldY, worldZ, finalMaterial, leafDecayMode);
                            }
                        }
                    }
                    if (extendFoundation && (dz == 0) && (terrainHeight != -1) && (worldZ > terrainHeight) && (!finalMaterial.block.veryInsubstantial)) {
                        int legZ = worldZ - 1;
                        while ((legZ >= 0) && world.getMaterialAt(worldX, worldY, legZ).block.veryInsubstantial) {
                            placeBlock(world, worldX, worldY, legZ, finalMaterial, leafDecayMode);
                            legZ--;
                        }
                    }
                }
            }
        }
    }
    List<Entity> entities = object.getEntities();
    if (entities != null) {
        for (Entity entity : entities) {
            double[] pos = entity.getPos();
            double entityX = x + pos[0] + offset.x, entityY = y + pos[2] + offset.y, entityZ = z + pos[1] + offset.z;
            if ((entityZ < 0) || (entityY > (world.getMaxHeight() - 1))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("NOT adding entity " + entity.getId() + " @ " + entityX + "," + entityY + "," + entityZ + " because z coordinate is out of range!");
                }
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace("Adding entity " + entity.getId() + " @ " + entityX + "," + entityY + "," + entityZ);
                }
                // Make sure each entity has a unique ID, otherwise
                // Minecraft will see them all as duplicates and remove
                // them:
                entity.setUUID(UUID.randomUUID());
                world.addEntity(entityX, entityY, entityZ, entity);
            }
        }
    }
    List<TileEntity> tileEntities = object.getTileEntities();
    if (tileEntities != null) {
        for (TileEntity tileEntity : tileEntities) {
            final int tileEntityX = x + tileEntity.getX() + offset.x, tileEntityY = y + tileEntity.getZ() + offset.y, tileEntityZ = z + tileEntity.getY() + offset.z;
            final String entityId = tileEntity.getId();
            if ((tileEntityZ < 0) || (tileEntityZ >= world.getMaxHeight())) {
                if (logger.isTraceEnabled()) {
                    logger.trace("NOT adding tile entity " + entityId + " @ " + tileEntityX + "," + tileEntityY + "," + tileEntityZ + " because z coordinate is out of range!");
                }
            } else {
                final int existingBlockType = world.getBlockTypeAt(tileEntityX, tileEntityY, tileEntityZ);
                if (!TILE_ENTITY_MAP.containsKey(entityId)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Adding unknown tile entity " + entityId + " @ " + tileEntityX + "," + tileEntityY + "," + tileEntityZ + " (block type: " + BLOCK_TYPE_NAMES[existingBlockType] + "; not able to detect whether the block type is correct; map may cause errors!)");
                    }
                    world.addTileEntity(tileEntityX, tileEntityY, tileEntityZ, tileEntity);
                } else if (TILE_ENTITY_MAP.get(entityId).contains(existingBlockType)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Adding tile entity " + entityId + " @ " + tileEntityX + "," + tileEntityY + "," + tileEntityZ + " (block type: " + BLOCK_TYPE_NAMES[existingBlockType] + ")");
                    }
                    world.addTileEntity(tileEntityX, tileEntityY, tileEntityZ, tileEntity);
                } else {
                    // the world limits)
                    if (logger.isTraceEnabled()) {
                        logger.trace("NOT adding tile entity " + entityId + " @ " + tileEntityX + "," + tileEntityY + "," + tileEntityZ + " because the block there is not a (or not the same) tile entity: " + BLOCK_TYPE_NAMES[existingBlockType] + "!");
                    }
                }
            }
        }
    }
}
Also used : TileEntity(org.pepsoft.minecraft.TileEntity) Point3i(javax.vecmath.Point3i) Entity(org.pepsoft.minecraft.Entity) TileEntity(org.pepsoft.minecraft.TileEntity) Material(org.pepsoft.minecraft.Material)

Example 2 with Entity

use of org.pepsoft.minecraft.Entity in project WorldPainter by Captain-Chaos.

the class MirroredObject method getEntities.

@Override
public List<Entity> getEntities() {
    List<Entity> objectEntities = object.getEntities();
    if (objectEntities != null) {
        List<Entity> entities = new ArrayList<>(objectEntities.size());
        for (Entity objectEntity : objectEntities) {
            Entity entity = (Entity) objectEntity.clone();
            double[] pos = entity.getPos();
            double[] vel = entity.getVel();
            if (mirrorYAxis) {
                pos[2] = dimensions.y - pos[2];
                vel[2] = -vel[2];
            } else {
                pos[0] = dimensions.x - pos[0];
                vel[0] = -vel[0];
            }
            entity.setPos(pos);
            entity.setVel(vel);
            float[] rot = entity.getRot();
            rot[0] = MathUtils.mod(rot[0] + 180.0f, 360.0f);
            entity.setRot(rot);
            entities.add(entity);
        }
        return entities;
    } else {
        return null;
    }
}
Also used : Entity(org.pepsoft.minecraft.Entity) TileEntity(org.pepsoft.minecraft.TileEntity) ArrayList(java.util.ArrayList)

Example 3 with Entity

use of org.pepsoft.minecraft.Entity in project WorldPainter by Captain-Chaos.

the class RotatedObject method getEntities.

@Override
public List<Entity> getEntities() {
    List<Entity> objectEntities = object.getEntities();
    if (objectEntities != null) {
        List<Entity> entities = new ArrayList<>(objectEntities.size());
        for (Entity objectEntity : objectEntities) {
            Entity entity = (Entity) objectEntity.clone();
            if (steps != 0) {
                double[] objectPos = objectEntity.getPos();
                double[] pos = entity.getPos();
                switch(steps) {
                    case 1:
                        pos[0] = dimensions.x - objectPos[2];
                        pos[2] = objectPos[0];
                        break;
                    case 2:
                        pos[0] = dimensions.x - objectPos[0];
                        pos[2] = dimensions.y - objectPos[2];
                        break;
                    case 3:
                        pos[0] = objectPos[2];
                        pos[2] = dimensions.y - objectPos[0];
                        break;
                    default:
                        throw new InternalError();
                }
                entity.setPos(pos);
                float[] rot = objectEntity.getRot();
                rot[0] = MathUtils.mod(rot[0] + steps * 90.0f, 360.0f);
                entity.setRot(rot);
            // TODO: adjust velocity
            }
            entities.add(entity);
        }
        return entities;
    } else {
        return null;
    }
}
Also used : Entity(org.pepsoft.minecraft.Entity) TileEntity(org.pepsoft.minecraft.TileEntity) ArrayList(java.util.ArrayList)

Aggregations

Entity (org.pepsoft.minecraft.Entity)3 TileEntity (org.pepsoft.minecraft.TileEntity)3 ArrayList (java.util.ArrayList)2 Point3i (javax.vecmath.Point3i)1 Material (org.pepsoft.minecraft.Material)1