Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockTypePalette.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include "BlockState.h"
5 
6 
7 
8 
9 
37 {
38 public:
39 
42  public std::runtime_error
43  {
44  using Super = std::runtime_error;
45 
46  public:
48  Super(fmt::format(FMT_STRING("No such palette index: {}"), aIndex))
49  {
50  }
51  };
52 
53 
56  public std::runtime_error
57  {
58  using Super = std::runtime_error;
59 
60  public:
61  LoadFailedException(const AString & aReason):
62  Super(aReason)
63  {
64  }
65  };
66 
67 
68 
71 
74  UInt32 index(const AString & aBlockTypeName, const BlockState & aBlockState);
75 
78  std::pair<UInt32, bool> maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const;
79 
81  UInt32 count() const;
82 
85  const std::pair<AString, BlockState> & entry(UInt32 aIndex) const;
86 
90  std::map<UInt32, UInt32> createTransformMapAddMissing(const BlockTypePalette & aFrom);
91 
95  std::map<UInt32, UInt32> createTransformMapWithFallback(const BlockTypePalette & aFrom, UInt32 aFallbackIndex) const;
96 
103  void loadFromString(const AString & aString);
104 
105 
106 protected:
107 
110  std::map<UInt32, std::pair<AString, BlockState>> mNumberToBlock;
111 
114  std::unordered_map<AString, std::map<BlockState, UInt32>> mBlockToNumber;
115 
119 
120 
125  void loadFromJsonString(const AString & aJsonPalette);
126 
132  void loadFromTsv(const AString & aTsvPalette, bool aIsUpgrade);
133 
137  void addMapping(UInt32 aID, const AString & aBlockTypeName, const BlockState & aBlockState);
138 };
unsigned int UInt32
Definition: Globals.h:157
std::string AString
Definition: StringUtils.h:11
Implements custom fmtlib formatting for cChunkCoords.
Definition: ChunkDef.h:104
Represents the state of a single block (previously known as "block meta").
Definition: BlockState.h:20
Holds a palette that maps between block type + state and numbers.
std::map< UInt32, UInt32 > createTransformMapAddMissing(const BlockTypePalette &aFrom)
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
void loadFromString(const AString &aString)
Loads the palette from the string representation.
void loadFromTsv(const AString &aTsvPalette, bool aIsUpgrade)
Loads the palette from the regular or upgrade TSV representation.
std::pair< UInt32, bool > maybeIndex(const AString &aBlockTypeName, const BlockState &aBlockState) const
Returns the <index, true> of the specified block type name and state, if it exists.
std::unordered_map< AString, std::map< BlockState, UInt32 > > mBlockToNumber
The mapping from stringular to numeric representation.
UInt32 count() const
Returns the total number of entries in the palette.
void loadFromJsonString(const AString &aJsonPalette)
Loads the palette from the JSON representation, https://wiki.vg/Data_Generators Throws a LoadFailedEx...
UInt32 index(const AString &aBlockTypeName, const BlockState &aBlockState)
Returns the index of the specified block type name and state.
std::map< UInt32, UInt32 > createTransformMapWithFallback(const BlockTypePalette &aFrom, UInt32 aFallbackIndex) const
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
const std::pair< AString, BlockState > & entry(UInt32 aIndex) const
Returns the blockspec represented by the specified palette index.
std::map< UInt32, std::pair< AString, BlockState > > mNumberToBlock
The mapping from numeric to stringular representation.
void addMapping(UInt32 aID, const AString &aBlockTypeName, const BlockState &aBlockState)
Adds a mapping between the numeric and stringular representation into both maps, updates the mMaxInde...
UInt32 mMaxIndex
The maximum index ever used in the maps.
BlockTypePalette()
Create a new empty instance.
Exception that is thrown if requiesting an index not present in the palette.
Exception that is thrown when loading the palette fails hard (bad format).
LoadFailedException(const AString &aReason)