BlockInstanceGeneratorRule.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "BlockInstanceGeneratorRule.h"
  2. BlockInstanceGeneratorRule::BlockInstanceGeneratorRule(
  3. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  4. : GeneratorRule(zConfig, zMemory)
  5. {
  6. blockType = BlockType::getTypeId(
  7. zConfig->asObject()->zValue("blockType")->asString()->getString());
  8. }
  9. Framework::Either<Block*, int> BlockInstanceGeneratorRule::createBlock(
  10. int x, int y, int z, int dimensionId)
  11. {
  12. return StaticRegistry<BlockType>::INSTANCE.zElement(blockType)->createBlockAt(Framework::Vec3<int>(x, y, z), dimensionId, 0);
  13. }
  14. BlockInstanceGeneratorRuleFactory::BlockInstanceGeneratorRuleFactory()
  15. : ReferenceCounter()
  16. {}
  17. GeneratorRule* BlockInstanceGeneratorRuleFactory::createRule(
  18. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  19. {
  20. return new BlockInstanceGeneratorRule(zConfig, zMemory);
  21. }
  22. Framework::JSON::Validator::JSONValidator*
  23. BlockInstanceGeneratorRuleFactory::getValidator()
  24. {
  25. Framework::RCArray<Framework::Text> blockTypeNames;
  26. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  27. {
  28. if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
  29. {
  30. blockTypeNames.add(new Framework::Text(
  31. StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
  32. }
  33. }
  34. return GeneratorRule::addToValidator(
  35. Framework::JSON::Validator::JSONValidator::buildForObject()
  36. ->withRequiredString("type")
  37. ->withExactMatch("blockInstance")
  38. ->finishString()
  39. ->withRequiredString("blockType")
  40. ->whichIsOneOf(blockTypeNames)
  41. ->finishString())
  42. ->finishObject();
  43. }