Search in sources :

Example 1 with ItemMonsterPlacer

use of net.minecraft.item.ItemMonsterPlacer 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.getEntityName(is));
        }
        // 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.itemRegistry.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 2 with ItemMonsterPlacer

use of net.minecraft.item.ItemMonsterPlacer 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...
            List<ItemStack> subItems = new ArrayList<ItemStack>();
            item.getSubItems(item, null, subItems);
            for (ItemStack is : subItems) {
                String fullName = is.getUnlocalizedName();
                if (is.getItem() instanceof ItemMonsterPlacer) {
                    // Special handling for eggs
                    fullName += "." + ItemMonsterPlacer.getEntityName(is);
                }
                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;
                    }
                }
                System.out.println(parts);
            }
        }
        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) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Variation(com.microsoft.Malmo.Schemas.Variation) ItemMonsterPlacer(net.minecraft.item.ItemMonsterPlacer) Colour(com.microsoft.Malmo.Schemas.Colour)

Aggregations

Colour (com.microsoft.Malmo.Schemas.Colour)2 DrawItem (com.microsoft.Malmo.Schemas.DrawItem)2 Variation (com.microsoft.Malmo.Schemas.Variation)2 ArrayList (java.util.ArrayList)2 ItemMonsterPlacer (net.minecraft.item.ItemMonsterPlacer)2 IBlockState (net.minecraft.block.state.IBlockState)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 ResourceLocation (net.minecraft.util.ResourceLocation)1