Package commoble.bagofyurting.util

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

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

public class NBTMapHelper<K,​KNBT extends net.minecraft.nbt.Tag,​V,​VNBT extends net.minecraft.nbt.Tag> extends Object
Helper class for writing a Map into a CompoundTag example usage for use in e.g. a LevelSavedData, a BlockEntity, 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, NbtUtils.writeBlockPos(blockPos)),
            		nbt -> NbtUtils.readBlockPos(nbt.getCompound(BLOCKPOS)),
            		(nbt, blockState) -> nbt.put(BLOCKSTATE, NbtUtils.writeBlockState(blockState)),
            		nbt -> NbtUtils.readBlockState(nbt.getCompound(BLOCKSTATE))
                            );

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

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

## Constructor Summary

Constructors
Constructor
Description
`NBTMapHelper​(String name, Function<K,​KNBT> keyWriter, Function<KNBT,​K> keyReader, Function<V,​VNBT> valueWriter, Function<VNBT,​V> valueReader)`

## Method Summary

Modifier and Type
Method
Description
`boolean`
`hasData​(net.minecraft.nbt.CompoundTag nbt)`
`Map<K,​V>`
`read​(net.minecraft.nbt.CompoundTag nbt)`
Reconstructs and returns a `Map<K,V>` from a CompoundTag If the nbt used was given by this.write(map), the map returned will be a reconstruction of the original Map
`net.minecraft.nbt.CompoundTag`
`write​(Map<K,​V> map, net.minecraft.nbt.CompoundTag compound)`
Given a map and a CompoundTag, writes the map into the NBT The same CompoundTag 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 Details

### NBTMapHelper

public NBTMapHelper​(String name, Function<K,​KNBT> keyWriter, Function<KNBT,​K> keyReader, Function<V,​VNBT> valueWriter, Function<VNBT,​V> valueReader)

Parameters:
  `name` - A unique identifier for the hashmap, allowing the map to be written into a CompoundTag 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 Details

### hasData

public boolean hasData​(net.minecraft.nbt.CompoundTag nbt)

### read

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

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

### write

public net.minecraft.nbt.CompoundTag write​(Map<K,​V> map, net.minecraft.nbt.CompoundTag compound)
Given a map and a CompoundTag, writes the map into the NBT The same CompoundTag can be given to this.read to retrieve that map

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