Exemplo n.º 1
0
  /**
   * Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like
   * the persistence flag before it gets created.
   *
   * @param blockFamily must not be null
   */
  public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
      builder.addComponent(new LightComponent());
    }

    // Copy the components from block prefab into the block item
    Prefab prefab = Assets.getPrefab(blockFamily.getArchetypeBlock().getPrefab());
    if (prefab != null) {
      for (Component component : prefab.iterateComponents()) {
        if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
          builder.addComponent(entityManager.getComponentLibrary().copy(component));
        }
      }
    }

    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    displayNameComponent.name = blockFamily.getDisplayName();

    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
      item.stackId = "block:" + blockFamily.getURI().toString();
      item.stackCount = (byte) quantity;
    }

    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;

    return builder;
  }
Exemplo n.º 2
0
  public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
    if (blockFamily == null) {
      return EntityRef.NULL;
    }

    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
      builder.addComponent(new LightComponent());
    }

    // Copy the components from block prefab into the block item
    for (Component component : blockEntity.iterateComponents()) {
      if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
        builder.addComponent(entityManager.getComponentLibrary().copy(component));
      }
    }

    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
      item.stackId = "block:" + blockFamily.getURI().toString();
      item.stackCount = (byte) 1;
    }

    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;

    return builder.build();
  }
  private void addGameManifestToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder) {
    BlockManager blockManager = CoreRegistry.get(BlockManager.class);
    BiomeManager biomeManager = CoreRegistry.get(BiomeManager.class);
    WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class);
    Time time = CoreRegistry.get(Time.class);
    Game game = CoreRegistry.get(Game.class);

    GameManifest gameManifest =
        new GameManifest(game.getName(), game.getSeed(), time.getGameTimeInMs());
    for (Module module : CoreRegistry.get(ModuleManager.class).getEnvironment()) {
      gameManifest.addModule(module.getId(), module.getVersion());
    }

    List<String> registeredBlockFamilies = Lists.newArrayList();
    for (BlockFamily family : blockManager.listRegisteredBlockFamilies()) {
      registeredBlockFamilies.add(family.getURI().toString());
    }
    gameManifest.setRegisteredBlockFamilies(registeredBlockFamilies);
    gameManifest.setBlockIdMap(blockManager.getBlockIdMap());
    List<Biome> biomes = biomeManager.getBiomes();
    Map<String, Short> biomeIdMap = new HashMap<>(biomes.size());
    for (Biome biome : biomes) {
      short shortId = biomeManager.getBiomeShortId(biome);
      String id = biomeManager.getBiomeId(biome);
      biomeIdMap.put(id, shortId);
    }
    gameManifest.setBiomeIdMap(biomeIdMap);
    gameManifest.addWorld(worldProvider.getWorldInfo());
    saveTransactionBuilder.setGameManifest(gameManifest);
  }
Exemplo n.º 4
0
 @Override
 public BlockFamily getBlockFamily(BlockUri uri) {
   BlockFamily family = registeredBlockInfo.get().registeredFamilyByUri.get(uri);
   if (family == null && generateNewIds) {
     if (isFreeformFamily(uri.getRootFamilyUri())) {
       family = blockLoader.loadWithShape(uri);
     } else {
       family = getAvailableBlockFamily(uri);
     }
     if (family != null) {
       lock.lock();
       try {
         for (Block block : family.getBlocks()) {
           block.setId(getNextId());
         }
         registerFamily(family);
       } finally {
         lock.unlock();
       }
     } else {
       logger.warn("Unable to resolve block family {}", uri);
     }
   }
   return family;
 }
 @Override
 public EntityData.Value serializeCollection(Iterable<BlockFamily> value) {
   EntityData.Value.Builder result = EntityData.Value.newBuilder();
   for (BlockFamily item : value) {
     result.addString(item.getURI().toString());
   }
   return result.build();
 }
Exemplo n.º 6
0
 /**
  * @param family
  * @param andRegister Immediately registers the family - it is expected that the blocks have been
  *     given ids.
  */
 @VisibleForTesting
 public void addBlockFamily(BlockFamily family, boolean andRegister) {
   for (String category : family.getCategories()) {
     categoryLookup.put(category, family.getURI());
   }
   availableFamilies.put(family.getURI(), family);
   if (andRegister) {
     registerFamily(family);
   }
 }
Exemplo n.º 7
0
  public BlockManagerImpl(
      WorldAtlas atlas,
      List<String> registeredBlockFamilies,
      Map<String, Short> knownBlockMappings,
      boolean generateNewIds,
      BlockFamilyFactoryRegistry blockFamilyFactoryRegistry) {
    this.generateNewIds = generateNewIds;
    this.moduleManager = CoreRegistry.get(ModuleManager.class);
    blockLoader =
        new BlockLoader(CoreRegistry.get(AssetManager.class), blockFamilyFactoryRegistry, atlas);
    BlockLoader.LoadBlockDefinitionResults blockDefinitions = blockLoader.loadBlockDefinitions();
    addBlockFamily(getAirFamily(), true);
    for (BlockFamily family : blockDefinitions.families) {
      addBlockFamily(family, false);
    }
    for (FreeformFamily freeformFamily : blockDefinitions.shapelessDefinitions) {
      addFreeformBlockFamily(freeformFamily.uri, freeformFamily.categories);
    }
    if (knownBlockMappings.size() >= MAX_ID) {
      nextId = UNKNOWN_ID;
    } else if (knownBlockMappings.size() > 0) {
      nextId = (short) knownBlockMappings.size();
    }

    for (String rawFamilyUri : registeredBlockFamilies) {
      BlockUri familyUri = new BlockUri(rawFamilyUri);
      BlockFamily family;
      if (isFreeformFamily(familyUri)) {
        family = blockLoader.loadWithShape(familyUri);
      } else {
        family = getAvailableBlockFamily(familyUri);
      }
      if (family != null) {
        for (Block block : family.getBlocks()) {
          Short id = knownBlockMappings.get(block.getURI().toString());
          if (id != null) {
            block.setId(id);
          } else {
            logger.error(
                "Missing id for block {} in provided family {}", block.getURI(), family.getURI());
            if (generateNewIds) {
              block.setId(getNextId());
            } else {
              block.setId(UNKNOWN_ID);
            }
          }
        }
        registerFamily(family);
      } else {
        logger.error("Family not available: {}", rawFamilyUri);
      }
    }
  }
Exemplo n.º 8
0
 @Override
 public Block getBlock(BlockUri uri) {
   Block block = registeredBlockInfo.get().blocksByUri.get(uri);
   if (block == null) {
     // Check if partially registered by getting the block family
     BlockFamily family = getBlockFamily(uri.getFamilyUri());
     if (family != null) {
       block = family.getBlockFor(uri);
     }
     if (block == null) {
       return getAir();
     }
   }
   return block;
 }
Exemplo n.º 9
0
 @VisibleForTesting
 protected void registerFamily(BlockFamily family) {
   logger.info("Registered {}", family);
   lock.lock();
   try {
     RegisteredState newState = new RegisteredState(registeredBlockInfo.get());
     newState.registeredFamilyByUri.put(family.getURI(), family);
     for (Block block : family.getBlocks()) {
       registerBlock(block, newState);
     }
     registeredBlockInfo.set(newState);
   } finally {
     lock.unlock();
   }
   for (BlockRegistrationListener listener : listeners) {
     listener.onBlockFamilyRegistered(family);
   }
 }
Exemplo n.º 10
0
 public void receiveFamilyRegistration(BlockUri familyUri, Map<String, Integer> registration) {
   BlockFamily family;
   if (isFreeformFamily(familyUri)) {
     family = blockLoader.loadWithShape(familyUri);
   } else {
     family = getAvailableBlockFamily(familyUri);
   }
   if (family != null) {
     for (Block block : family.getBlocks()) {
       Integer id = registration.get(block.getURI().toString());
       if (id != null) {
         block.setId((short) id.intValue());
       } else {
         logger.error(
             "Missing id for block {} in registered family {}", block.getURI(), familyUri);
         block.setId(UNKNOWN_ID);
       }
       registerFamily(family);
     }
   } else {
     logger.error("Block family not available: {}", familyUri);
   }
 }
Exemplo n.º 11
0
 @Override
 public EntityData.Value serialize(BlockFamily value) {
   return EntityData.Value.newBuilder().addString(value.getURI().toString()).build();
 }