commoble.bagofyurting.util

## Class NBTMapHelper<K,KNBT extends net.minecraft.nbt.INBT,V,VNBT extends net.minecraft.nbt.INBT>

- java.lang.Object
- - commoble.bagofyurting.util.NBTMapHelper<K,KNBT,V,VNBT>

    public class NBTMapHelper<K,KNBT extends net.minecraft.nbt.INBT,V,VNBT extends net.minecraft.nbt.INBT>
    extends java.lang.Object

Helper class for writing a Map into a CompoundNBT example usage for use in e.g. a WorldSavedData, a TileEntity, etc:

            private static final String BLOCKS = "blocks";
            private static final String BLOCKPOS = "blockpos";
            private static final String BLOCKSTATE = "blockstate";

             private Map<BlockPos, BlockState> map = new HashMap<>();

             private static final NBTMapHelper<BlockPos, BlockState> BLOCKS_MAPPER = new NBTMapHelper<>(
                            BLOCKS,
             		(nbt, blockPos) -> nbt.put(BLOCKPOS, NBTUtil.writeBlockPos(blockPos)),
             		nbt -> NBTUtil.readBlockPos(nbt.getCompound(BLOCKPOS)),
             		(nbt, blockState) -> nbt.put(BLOCKSTATE, NBTUtil.writeBlockState(blockState)),
             		nbt -> NBTUtil.readBlockState(nbt.getCompound(BLOCKSTATE))
                            );

             @Override
            public void read(CompoundNBT nbt)
            {
                    this.map = BLOCKS_MAPPER.read(nbt);
            }

             @Override
            public CompoundNBT write(CompoundNBT compound)
            {
                    BLOCKS_MAPPER.write(this.map, compound);
                    return compound;
            }

### Constructor Summary

Constructors
Constructor and Description
`NBTMapHelper(java.lang.String name, java.util.function.Function<K,KNBT> keyWriter, java.util.function.Function<KNBT,K> keyReader, java.util.function.Function<V,VNBT> valueWriter, java.util.function.Function<VNBT,V> valueReader)`

### Method Summary

All Methods Instance Methods Concrete Methods
`boolean` `hasData(net.minecraft.nbt.CompoundNBT nbt)`
`java.util.Map<K,V>` `read(net.minecraft.nbt.CompoundNBT nbt)` Reconstructs and returns a `Map<K,V>` from a CompoundNBT If the nbt used was given by this.write(map), the map returned will be a reconstruction of the original Map
`net.minecraft.nbt.CompoundNBT` `write(java.util.Map<K,V> map, net.minecraft.nbt.CompoundNBT compound)` Given a map and a CompoundNBT, writes the map into the NBT The same compoundNBT can be given to this.read to retrieve that map

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### NBTMapHelper

    public NBTMapHelper(java.lang.String name,
                        java.util.function.Function<K,KNBT> keyWriter,
                        java.util.function.Function<KNBT,K> keyReader,
                        java.util.function.Function<V,VNBT> valueWriter,
                        java.util.function.Function<VNBT,V> valueReader)

Parameters:
  `name` - A unique identifier for the hashmap, allowing the map to be written into a CompoundNBT alongside other data
  `keyWriter` - A function that, given a Key, returns an nbt object representing that key
  `keyReader` - A function that, given an nbt object, returns the Key represented by that nbt
  `valueWriter` - A function that, given a Value, returns an nbt object representing that value
  `valueReader` - A Function that, given an nbt object, returns the Value represented by that nbt

### Method Detail

#### hasData

    public boolean hasData(net.minecraft.nbt.CompoundNBT nbt)

#### read

    public java.util.Map<K,V> read(net.minecraft.nbt.CompoundNBT nbt)

Reconstructs and returns a `Map<K,V>` from a CompoundNBT If the nbt used was given by this.write(map), the map returned will be a reconstruction of the original Map

Parameters:
  `nbt` - The CompoundNBT to read and construct the Map from
Returns:
  A Map that the data contained in the CompoundNBT represents

#### write

    public net.minecraft.nbt.CompoundNBT write(java.util.Map<K,V> map,
                                               net.minecraft.nbt.CompoundNBT compound)

Given a map and a CompoundNBT, writes the map into the NBT The same compoundNBT can be given to this.read to retrieve that map

Parameters:
  `map` - A `Map<K,V>`
  `compound` - A CompoundNBT to write the map into
Returns:
  a CompoundNBT that, when used as the argument to this.read(nbt), will cause that function to reconstruct and return a copy of the original map