Associative Arrays

An Associative Array (sometimes also called a Map or a Dictionary) is like a normal Array in the way that it is able to store multiple entries. Unlike Arrays however, you can choose what type you want the index, or (as we call it in maps) key, to be!

Declaring an Associative Array

You declare Associative Arrays using curly brackets {} and colons :

val myAssocArray = {
    dirt : <minecraft:dirt>,
    gold : <minecraft:gold_ingot>
} as IItemStack[string];

Let’s break this down, shall we?

Okay, so what do I need to think of when using these?

Refering to Items inside an Associative Array.

You refer to items inside an Associative Array the same way you refer to items inside a normal Array:
Only difference is this time, you don’t necessarily need to use an Integer as index, but whatever type you declared your Array to be!

<br />val dirt = &lt;minecraft:dirt&gt;;
val assocArray = {
    &lt;minecraft:dirt&gt; : "This is me"
} as string[IItemStack];


//You can also use varaibles here, as long as the variable is of the correct type

There is one special case, that is when you use strings as indeces:
In this case you can also use the memberGetter like this:

val assocWithStrings = {
    //you can use "" if you want
    "one" : "1",

    //but you don't have to
    two : "2"
} as string[string];

//You can either use the memberGetter

//Or the standard index Getter

Manipulating items inside an Associative Array

As in Arrays, you can manipulate items inside an Associative Array using array[index] = newValue.
There is one major differenc though:
While Arrays have a fixed size, maps don’t. That means you can always add an entry by setting to an index that has previously not been set!

val changingArray = {
    <minecraft:dirt> : "this is me",
    <minecraft:gold_ingot> : "and I hate it"
} as string[IItemStack];

val gg = <minecraft:gold>;

//Overrides the value of gold_ingot
changingArray[gg] = "and I love it";

//adds a new entry
changingArray[<minecraft:grass>] = "Power!";

Retrieving an Associative Array’s Key and Entrysets

The KeySet is an array containing all the map’s keys.
The valueSet is an array containing all the map’s values.
The entrySet is an array containing all the map’s entries (see below).

myAssocArray.keySet   //keySet
myAssocArray.keys     //keySet
myAssocArray.values   //valueSet
myAssocArray.valueSet //valueSet
myAssocArray.entrySet //entrySet

Iterating over an Associative Array

There are two Iterators that allow you to iterate over an Associative Array:

Let’s add an Associative Array that stores crafting recipes to be iterated over:

import crafttweaker.item.IItemStack;
import crafttweaker.item.IIngredient;

val dirt = <minecraft:dirt>;
val recipeMapShaped = {
    <minecraft:grass> : [[dirt, dirt, dirt],[dirt, dirt, dirt],[dirt, dirt, dirt]],
    <minecraft:gold_ingot> : [[dirt, dirt, dirt],[dirt, <minecraft:gold_ingot>, dirt],[dirt, dirt, dirt]]
} as IIngredient[][][IItemStack];

recipeMapShaped[dirt] = [[dirt, dirt, dirt],[dirt, null, dirt],[dirt, dirt, dirt]];

//key will be grass, goldIngot, dirt
for key in recipeMapShaped {
    recipes.addShaped(key, recipeMapShaped[key]);

//keys will be grass, goldIngot, dirt, values will be the recipes for them
for key, value in recipeMapShaped {
    recipes.addShaped(key, value);

ZenType Entry

A map Entry consists of a key and a value.
Currently the only way to get such an object is via a map’s entrySet method.

You can use the getters to get key and value

//Replace map with a reference to an existing map/associative array
val myEntry = map.entrySet[0];

myEntry.key;    //Returns the entry's key.
myEntry.value;  //Returns the entry's value.