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(); }
/** * 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; }