Search in sources :

Example 11 with DrawItem

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

the class MinecraftTypeHelper method getItemStackFromDrawItem.

public static ItemStack getItemStackFromDrawItem(DrawItem i) {
    ItemStack itemStack = null;
    // First see if this is an item:
    Item item = MinecraftTypeHelper.ParseItemType(i.getType(), false);
    if (item == null) {
        // No, so is it a block type?
        IBlockState block = MinecraftTypeHelper.ParseBlockType(i.getType());
        if (block != null) {
            // It is - apply the modifications:
            block = BlockDrawingHelper.applyModifications(block, i.getColour(), i.getFace(), i.getVariant());
            // And try to return as an item:
            if (block != null && block.getBlock() != null && Item.getItemFromBlock(block.getBlock()) != null) {
                itemStack = new ItemStack(block.getBlock(), 1, block.getBlock().getMetaFromState(block));
            }
        }
    } else {
        if (item.getHasSubtypes() && (i.getColour() != null || i.getVariant() != null)) {
            // Attempt to find the subtype for this colour/variant - made tricky
            // because Items don't provide any nice property stuff like Blocks do...
            NonNullList<ItemStack> subItems = NonNullList.create();
            item.getSubItems(item, null, subItems);
            for (ItemStack is : subItems) {
                String fullName = is.getUnlocalizedName();
                if (is.getItem() instanceof ItemMonsterPlacer) {
                    // Special handling for eggs
                    fullName += "." + ItemMonsterPlacer.getNamedIdFrom(is).toString();
                }
                String[] parts = fullName.split("\\.");
                for (int p = 0; p < parts.length; p++) {
                    Variation v = attemptToGetAsVariant(parts[p], is);
                    Colour c = attemptToGetAsColour(parts[p]);
                    if ((v != null && i.getVariant() != null && v.getValue().equals(i.getVariant().getValue())) || (c != null && i.getColour() != null && c == i.getColour())) {
                        // This is it??
                        return is;
                    }
                }
            }
        }
        itemStack = new ItemStack(item);
    }
    return itemStack;
}
Also used : DrawItem(com.microsoft.Malmo.Schemas.DrawItem) Item(net.minecraft.item.Item) IBlockState(net.minecraft.block.state.IBlockState) ItemStack(net.minecraft.item.ItemStack) Variation(com.microsoft.Malmo.Schemas.Variation) ItemMonsterPlacer(net.minecraft.item.ItemMonsterPlacer) Colour(com.microsoft.Malmo.Schemas.Colour)

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