BlockRegistry.cpp 720 B

1234567891011121314151617181920212223242526272829
  1. #include "common/block/BlockRegistry.h"
  2. BlockRegistry::BlockRegistry() {
  3. add("air");
  4. add("stone");
  5. add("dirt");
  6. add("grass");
  7. }
  8. void BlockRegistry::forEach(void (*f)(const Block&)) const {
  9. for(const Block& b : blocks) {
  10. f(b);
  11. }
  12. }
  13. void BlockRegistry::add(const char* registry) {
  14. BlockId id = blocks.getLength();
  15. blocks.add(id, registry);
  16. registryToId.add(registry, id);
  17. }
  18. const Block& BlockRegistry::getBlock(const BlockName& registry) const {
  19. const BlockId* id = registryToId.search(registry);
  20. return id == nullptr ? blocks[0] : blocks[*id];
  21. }
  22. const Block& BlockRegistry::getBlock(BlockId id) const {
  23. return id < blocks.getLength() ? blocks[id] : blocks[0];
  24. }