Search in sources :

Example 1 with DrawBlock

use of com.microsoft.Malmo.Schemas.DrawBlock in project malmo by Microsoft.

the class ObservationFromRayImplementation method buildMouseOverData.

/** Build the json object for the object under the cursor, whether it is a block or a creature.<br>
     * If there is any data to be returned, the json will be added in a subnode called "LineOfSight".
     * @param json a JSON object into which the info for the object under the mouse will be added.
     */
public static void buildMouseOverData(JsonObject json) {
    // We could use Minecraft.getMinecraft().objectMouseOver but it's limited to the block reach distance, and
    // doesn't register floating tile items.
    // Ideally use Minecraft.timer.renderPartialTicks - but we don't need sub-tick resolution.
    float partialTicks = 0;
    Entity viewer = Minecraft.getMinecraft().thePlayer;
    // Hard-coded for now - in future will be parameterised via the XML.
    float depth = 50;
    Vec3 eyePos = viewer.getPositionEyes(partialTicks);
    Vec3 lookVec = viewer.getLook(partialTicks);
    Vec3 searchVec = eyePos.addVector(lookVec.xCoord * depth, lookVec.yCoord * depth, lookVec.zCoord * depth);
    MovingObjectPosition mop = Minecraft.getMinecraft().theWorld.rayTraceBlocks(eyePos, searchVec, false, false, false);
    MovingObjectPosition mopEnt = findEntity(eyePos, lookVec, depth, mop, true);
    if (mopEnt != null)
        mop = mopEnt;
    if (mop == null) {
        // Nothing under the mouse.
        return;
    }
    // Calculate ranges for player interaction:
    double hitDist = mop.hitVec.distanceTo(eyePos);
    double blockReach = Minecraft.getMinecraft().playerController.getBlockReachDistance();
    double entityReach = Minecraft.getMinecraft().playerController.extendedReach() ? 6.0 : 3.0;
    JsonObject jsonMop = new JsonObject();
    if (mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
        // We're looking at a block - send block data:
        jsonMop.addProperty("hitType", "block");
        jsonMop.addProperty("x", mop.hitVec.xCoord);
        jsonMop.addProperty("y", mop.hitVec.yCoord);
        jsonMop.addProperty("z", mop.hitVec.zCoord);
        IBlockState state = Minecraft.getMinecraft().theWorld.getBlockState(mop.getBlockPos());
        List<IProperty> extraProperties = new ArrayList<IProperty>();
        DrawBlock db = MinecraftTypeHelper.getDrawBlockFromBlockState(state, extraProperties);
        jsonMop.addProperty("type", db.getType().value());
        if (db.getColour() != null)
            jsonMop.addProperty("colour", db.getColour().value());
        if (db.getVariant() != null)
            jsonMop.addProperty("variant", db.getVariant().getValue());
        if (db.getFace() != null)
            jsonMop.addProperty("facing", db.getFace().value());
        if (extraProperties.size() > 0) {
            // Add the extra properties that aren't covered by colour/variant/facing.
            for (IProperty prop : extraProperties) {
                String key = "prop_" + prop.getName();
                if (prop.getValueClass() == Boolean.class)
                    jsonMop.addProperty(key, Boolean.valueOf(state.getValue(prop).toString()));
                else if (prop.getValueClass() == Integer.class)
                    jsonMop.addProperty(key, Integer.valueOf(state.getValue(prop).toString()));
                else
                    jsonMop.addProperty(key, state.getValue(prop).toString());
            }
        }
        jsonMop.addProperty("inRange", hitDist <= blockReach);
        jsonMop.addProperty("distance", hitDist);
    } else if (mop.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
        // Looking at an entity:
        Entity entity = mop.entityHit;
        if (entity != null) {
            jsonMop.addProperty("x", entity.posX);
            jsonMop.addProperty("y", entity.posY);
            jsonMop.addProperty("z", entity.posZ);
            String name = entity.getName();
            String hitType = "entity";
            if (entity instanceof EntityItem) {
                ItemStack is = ((EntityItem) entity).getEntityItem();
                DrawItem di = MinecraftTypeHelper.getDrawItemFromItemStack(is);
                if (di.getColour() != null)
                    jsonMop.addProperty("colour", di.getColour().value());
                if (di.getVariant() != null)
                    jsonMop.addProperty("variant", di.getVariant().getValue());
                jsonMop.addProperty("stackSize", is.stackSize);
                name = di.getType();
                hitType = "item";
            }
            jsonMop.addProperty("type", name);
            jsonMop.addProperty("hitType", hitType);
        }
        jsonMop.addProperty("inRange", hitDist <= entityReach);
        jsonMop.addProperty("distance", hitDist);
    }
    json.add("LineOfSight", jsonMop);
}
Also used : Entity(net.minecraft.entity.Entity) IBlockState(net.minecraft.block.state.IBlockState) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) MovingObjectPosition(net.minecraft.util.MovingObjectPosition) IProperty(net.minecraft.block.properties.IProperty) Vec3(net.minecraft.util.Vec3) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) DrawBlock(com.microsoft.Malmo.Schemas.DrawBlock) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with DrawBlock

use of com.microsoft.Malmo.Schemas.DrawBlock in project malmo by Microsoft.

the class MinecraftTypeHelper method getDrawBlockFromBlockState.

/** Extract the type, variation and facing attributes of a blockstate and return them in a new DrawBlock object.<br>
     * @param state the IBlockState to be examined
     * @return A DrawBlock object
     */
public static DrawBlock getDrawBlockFromBlockState(IBlockState state, List<IProperty> extraProperties) {
    if (state == null)
        return null;
    DrawBlock block = new DrawBlock();
    Object blockName = Block.blockRegistry.getNameForObject(state.getBlock());
    if (blockName instanceof ResourceLocation) {
        String name = ((ResourceLocation) blockName).getResourcePath();
        BlockType type = BlockType.fromValue(name);
        block.setType(type);
    }
    Colour col = null;
    Variation var = null;
    Facing face = null;
    // Add properties:
    for (IProperty prop : (java.util.Set<IProperty>) state.getProperties().keySet()) {
        String propVal = state.getValue(prop).toString();
        boolean matched = false;
        // Try colour first:
        if (col == null) {
            col = attemptToGetAsColour(propVal);
            if (col != null)
                matched = true;
        }
        // Then variant:
        if (!matched && var == null) {
            var = attemptToGetAsVariant(propVal);
            if (var != null)
                matched = true;
        }
        // Then facing:
        if (!matched && face == null) {
            face = attemptToGetAsFacing(propVal);
            if (face != null)
                matched = true;
        }
        if (!matched) {
            if (extraProperties != null)
                extraProperties.add(prop);
        }
    }
    if (col != null)
        block.setColour(col);
    if (var != null)
        block.setVariant(var);
    if (face != null)
        block.setFace(face);
    return block;
}
Also used : Facing(com.microsoft.Malmo.Schemas.Facing) EnumFacing(net.minecraft.util.EnumFacing) BlockType(com.microsoft.Malmo.Schemas.BlockType) IProperty(net.minecraft.block.properties.IProperty) ResourceLocation(net.minecraft.util.ResourceLocation) DrawBlock(com.microsoft.Malmo.Schemas.DrawBlock) Variation(com.microsoft.Malmo.Schemas.Variation) Colour(com.microsoft.Malmo.Schemas.Colour)

Example 3 with DrawBlock

use of com.microsoft.Malmo.Schemas.DrawBlock in project malmo by Microsoft.

the class MovingTargetDecoratorImplementation method isValid.

private boolean isValid(World world, BlockPos pos) {
    // In bounds?
    if (!blockInBounds(pos, this.arenaBounds))
        return false;
    // Already in path?
    if (this.path.contains(pos))
        return false;
    // Does there need to be air above the target?
    if (this.targetParams.isRequiresAirAbove() && !world.isAirBlock(pos.up()))
        return false;
    // Check the current block is "permeable"...
    IBlockState block = world.getBlockState(pos);
    List<IProperty> extraProperties = new ArrayList<IProperty>();
    DrawBlock db = MinecraftTypeHelper.getDrawBlockFromBlockState(block, extraProperties);
    boolean typesMatch = this.targetParams.getPermeableBlocks().getType().isEmpty();
    for (BlockType bt : this.targetParams.getPermeableBlocks().getType()) {
        if (db.getType() == bt) {
            typesMatch = true;
            break;
        }
    }
    if (!typesMatch)
        return false;
    if (db.getColour() != null) {
        boolean coloursMatch = this.targetParams.getPermeableBlocks().getColour().isEmpty();
        for (Colour col : this.targetParams.getPermeableBlocks().getColour()) {
            if (db.getColour() == col) {
                coloursMatch = true;
                break;
            }
        }
        if (!coloursMatch)
            return false;
    }
    if (db.getVariant() != null) {
        boolean variantsMatch = this.targetParams.getPermeableBlocks().getVariant().isEmpty();
        for (Variation var : this.targetParams.getPermeableBlocks().getVariant()) {
            if (db.getVariant() == var) {
                variantsMatch = true;
                break;
            }
        }
        if (!variantsMatch)
            return false;
    }
    return true;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IProperty(net.minecraft.block.properties.IProperty) BlockType(com.microsoft.Malmo.Schemas.BlockType) ArrayList(java.util.ArrayList) DrawBlock(com.microsoft.Malmo.Schemas.DrawBlock) Variation(com.microsoft.Malmo.Schemas.Variation) Colour(com.microsoft.Malmo.Schemas.Colour)

Aggregations

DrawBlock (com.microsoft.Malmo.Schemas.DrawBlock)3 IProperty (net.minecraft.block.properties.IProperty)3 BlockType (com.microsoft.Malmo.Schemas.BlockType)2 Colour (com.microsoft.Malmo.Schemas.Colour)2 Variation (com.microsoft.Malmo.Schemas.Variation)2 ArrayList (java.util.ArrayList)2 IBlockState (net.minecraft.block.state.IBlockState)2 JsonObject (com.google.gson.JsonObject)1 DrawItem (com.microsoft.Malmo.Schemas.DrawItem)1 Facing (com.microsoft.Malmo.Schemas.Facing)1 Entity (net.minecraft.entity.Entity)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1 EnumFacing (net.minecraft.util.EnumFacing)1 MovingObjectPosition (net.minecraft.util.MovingObjectPosition)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 Vec3 (net.minecraft.util.Vec3)1