BlockRegistry.cpp 693 B

12345678910111213141516171819202122232425262728293031
  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. return blocks[registryToId.search(registry, 0)];
  20. }
  21. const Block& BlockRegistry::getBlock(BlockId id) const {
  22. if(id < blocks.getLength()) {
  23. return blocks[id];
  24. }
  25. return blocks[0];
  26. }