Search in sources :

Example 1 with DrawItem

use of com.microsoft.Malmo.Schemas.DrawItem 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 DrawItem

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

the class ObservationFromFullInventoryImplementation method getInventoryJSON.

public static void getInventoryJSON(JsonObject json, String prefix, IInventory inventory, int maxSlot) {
    int nSlots = Math.min(inventory.getSizeInventory(), maxSlot);
    for (int i = 0; i < nSlots; i++) {
        ItemStack is = inventory.getStackInSlot(i);
        if (is != null) {
            json.addProperty(prefix + i + "_size", is.getCount());
            DrawItem di = MinecraftTypeHelper.getDrawItemFromItemStack(is);
            String name = di.getType();
            if (di.getColour() != null)
                json.addProperty(prefix + i + "_colour", di.getColour().value());
            if (di.getVariant() != null)
                json.addProperty(prefix + i + "_variant", di.getVariant().getValue());
            json.addProperty(prefix + i + "_item", name);
        }
    }
}
Also used : DrawItem(com.microsoft.Malmo.Schemas.DrawItem) ItemStack(net.minecraft.item.ItemStack)

Example 3 with DrawItem

use of com.microsoft.Malmo.Schemas.DrawItem 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, boolean includeNBTData) {
    // 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().player;
    // Hard-coded for now - in future will be parameterised via the XML.
    float depth = 50;
    Vec3d eyePos = viewer.getPositionEyes(partialTicks);
    Vec3d lookVec = viewer.getLook(partialTicks);
    Vec3d searchVec = eyePos.addVector(lookVec.xCoord * depth, lookVec.yCoord * depth, lookVec.zCoord * depth);
    RayTraceResult mop = Minecraft.getMinecraft().world.rayTraceBlocks(eyePos, searchVec, false, false, false);
    RayTraceResult 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 == RayTraceResult.Type.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().world.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());
            }
        }
        // Add the NBTTagCompound, if this is a tile entity.
        if (includeNBTData) {
            TileEntity tileentity = Minecraft.getMinecraft().world.getTileEntity(mop.getBlockPos());
            if (tileentity != null) {
                NBTTagCompound data = tileentity.getUpdateTag();
                if (data != null) {
                    // Turn data directly into json and add it to what we're already returning.
                    String jsonString = data.toString();
                    try {
                        JsonElement jelement = new JsonParser().parse(jsonString);
                        if (jelement != null)
                            jsonMop.add("NBTTagCompound", jelement);
                    } catch (JsonSyntaxException e) {
                    // Duff NBTTagCompound - ignore it.
                    }
                }
            }
        }
        jsonMop.addProperty("inRange", hitDist <= blockReach);
        jsonMop.addProperty("distance", hitDist);
    } else if (mop.typeOfHit == RayTraceResult.Type.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);
            jsonMop.addProperty("yaw", entity.rotationYaw);
            jsonMop.addProperty("pitch", entity.rotationPitch);
            String name = MinecraftTypeHelper.getUnlocalisedEntityName(entity);
            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.getCount());
                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) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) RayTraceResult(net.minecraft.util.math.RayTraceResult) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) JsonObject(com.google.gson.JsonObject) NBTTagString(net.minecraft.nbt.NBTTagString) Vec3d(net.minecraft.util.math.Vec3d) TileEntity(net.minecraft.tileentity.TileEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) IProperty(net.minecraft.block.properties.IProperty) JsonElement(com.google.gson.JsonElement) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) DrawBlock(com.microsoft.Malmo.Schemas.DrawBlock) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) JsonParser(com.google.gson.JsonParser)

Example 4 with DrawItem

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

the class MinecraftTypeHelper method getDrawItemFromItemStack.

/**
 * Attempt to break the item on this itemstack into a type/variant/colour which we can use for communication with the Malmo platform.
 * @param is the ItemStack containing the item we are attempting to deconstruct.
 * @return an XML DrawItem object containing the item's type, variant, colour etc.
 */
public static DrawItem getDrawItemFromItemStack(ItemStack is) {
    if (is == null)
        return null;
    DrawItem di = new DrawItem();
    // Get unlocalised name from the stack, not the stack's item - this ensures we keep the metadata.
    String name = is.getUnlocalizedName();
    if (is.getHasSubtypes()) {
        // If the item has subtypes, then there are varieties - eg different colours, types, etc.
        // Attempt to map from these subtypes back to variant/colour.
        // Do this by decomposing the unlocalised name:
        List<String> itemParts = new ArrayList<String>(Arrays.asList(name.split("\\.")));
        if (is.getItem() instanceof ItemMonsterPlacer) {
            // Special case for eggs:
            itemParts.add(ItemMonsterPlacer.getNamedIdFrom(is).toString());
        }
        // First part will be "tile" or "item".
        // Second part will be the item itself (eg "dyePowder" or "stainedGlass" etc).
        // Third part will be the variant, colour etc.
        Colour col = null;
        Variation var = null;
        for (int part = 2; part < itemParts.size(); part++) {
            String section = itemParts.get(part);
            // First see if this matches a colour:
            if (col == null) {
                col = attemptToGetAsColour(section);
                if (// If it wasn't a colour, check to see if it was a variant:
                col == null && var == null)
                    var = attemptToGetAsVariant(section, is);
            } else if (var == null)
                var = attemptToGetAsVariant(section, is);
        }
        di.setColour(col);
        di.setVariant(var);
    }
    // Use the item registry name for the item - this is what we use in Types.XSD
    Object obj = Item.REGISTRY.getNameForObject(is.getItem());
    String publicName;
    if (obj instanceof ResourceLocation)
        publicName = ((ResourceLocation) obj).getResourcePath();
    else
        publicName = obj.toString();
    di.setType(publicName);
    return di;
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) ArrayList(java.util.ArrayList) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) Variation(com.microsoft.Malmo.Schemas.Variation) ItemMonsterPlacer(net.minecraft.item.ItemMonsterPlacer) Colour(com.microsoft.Malmo.Schemas.Colour)

Example 5 with DrawItem

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

the class MazeDecoratorImplementation method initBlocksAndHeights.

private void initBlocksAndHeights() {
    this.startBlock = getBlock(this.mazeParams.getStartBlock(), this.blockrand);
    this.endBlock = getBlock(this.mazeParams.getEndBlock(), this.blockrand);
    this.floorBlock = getBlock(this.mazeParams.getFloorBlock(), this.blockrand);
    this.pathBlock = getBlock(this.mazeParams.getPathBlock(), this.blockrand);
    this.optimalPathBlock = this.mazeParams.getOptimalPathBlock() != null ? getBlock(this.mazeParams.getOptimalPathBlock(), this.blockrand) : this.pathBlock;
    this.subgoalPathBlock = this.mazeParams.getSubgoalBlock() != null ? getBlock(this.mazeParams.getSubgoalBlock(), this.blockrand) : this.optimalPathBlock;
    this.gapBlock = getBlock(this.mazeParams.getGapBlock(), this.blockrand);
    if (this.mazeParams.getWaypoints() != null) {
        if (this.mazeParams.getWaypoints().getWaypointBlock() != null)
            this.waypointBlock = getBlock(this.mazeParams.getWaypoints().getWaypointBlock(), this.blockrand);
        else {
            BlockOrItemSpec bis = this.mazeParams.getWaypoints().getWaypointItem();
            DrawItem di = new DrawItem();
            di.setType(bis.getType().get(this.blockrand.nextInt(bis.getType().size())));
            if (bis.getColour() != null && !bis.getColour().isEmpty())
                di.setColour(bis.getColour().get(this.blockrand.nextInt(bis.getColour().size())));
            if (bis.getVariant() != null && !bis.getVariant().isEmpty())
                di.setVariant(bis.getVariant().get(this.blockrand.nextInt(bis.getVariant().size())));
            this.waypointItem = MinecraftTypeHelper.getItemStackFromDrawItem(di);
        }
    }
    this.startHeight = getHeight(this.mazeParams.getStartBlock(), this.pathrand);
    this.endHeight = getHeight(this.mazeParams.getEndBlock(), this.pathrand);
    this.pathHeight = getHeight(this.mazeParams.getPathBlock(), this.pathrand);
    this.optimalPathHeight = this.mazeParams.getOptimalPathBlock() != null ? getHeight(this.mazeParams.getOptimalPathBlock(), this.pathrand) : this.pathHeight;
    this.subgoalHeight = this.mazeParams.getSubgoalBlock() != null ? getHeight(this.mazeParams.getSubgoalBlock(), this.pathrand) : this.optimalPathHeight;
    this.gapHeight = getHeight(this.mazeParams.getGapBlock(), this.pathrand);
}
Also used : BlockOrItemSpec(com.microsoft.Malmo.Schemas.BlockOrItemSpec) DrawItem(com.microsoft.Malmo.Schemas.DrawItem)

Aggregations

DrawItem (com.microsoft.Malmo.Schemas.DrawItem)11 ItemStack (net.minecraft.item.ItemStack)8 ArrayList (java.util.ArrayList)5 JsonObject (com.google.gson.JsonObject)4 Colour (com.microsoft.Malmo.Schemas.Colour)3 Variation (com.microsoft.Malmo.Schemas.Variation)3 IBlockState (net.minecraft.block.state.IBlockState)3 Entity (net.minecraft.entity.Entity)3 EntityItem (net.minecraft.entity.item.EntityItem)3 DrawBlock (com.microsoft.Malmo.Schemas.DrawBlock)2 IProperty (net.minecraft.block.properties.IProperty)2 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)2 ItemMonsterPlacer (net.minecraft.item.ItemMonsterPlacer)2 TileEntity (net.minecraft.tileentity.TileEntity)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BlockOrItemSpec (com.microsoft.Malmo.Schemas.BlockOrItemSpec)1 BlockType (com.microsoft.Malmo.Schemas.BlockType)1