BlockRegistry.cpp 776 B

1234567891011121314151617181920212223242526272829303132333435
  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. if(id == nullptr) {
  21. return blocks[0];
  22. }
  23. return blocks[*id];
  24. }
  25. const Block& BlockRegistry::getBlock(BlockId id) const {
  26. if(id < blocks.getLength()) {
  27. return blocks[id];
  28. }
  29. return blocks[0];
  30. }